Instead of directly going with customChange
, let’s first target normal SQL script.
In my case, when I tried this:
<changeSet author="rakhi" id="test" dbms="${dbType}">
<sql endDelimiter=";" splitStatements="true" stripComments="true">
CREATE TABLE message
(
id1 INT NOT NULL,
message1 text NOT NULL
)
;
</sql>
</changeSet>
(note the contexts
attribute is not set)
and tried with the similar Java code you provided:
java.sql.Connection connection = null; //your openConnection logic here
try {
connection = openConnection(dbUrl,dbUsername,dbPassword);
Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
Liquibase liquibase = new liquibase.Liquibase(changelog, new ClassLoaderResourceAccessor(), database);
liquibase.update("");
} catch (SQLException | IOException | LiquibaseException e) {
logger.error(e.getMessage());
}
Liquibase executed the changeset and created a table in DB for me. Pasting partial console statements below:
[DEBUG] 2021-06-18 16:34:30.487 [main] ChangeSet - Reading ChangeSet: com/db/liquibase/changelog/prd.db.changelog-master.xml::test::auth
[DEBUG] 2021-06-18 16:34:30.488 [main] PostgresDatabase - Executing Statement: CREATE TABLE message
(
id1 INT NOT NULL,
message1 text NOT NULL
)
[INFO ] 2021-06-18 16:34:30.488 [main] JdbcExecutor - Executing with the 'jdbc' executor
[INFO ] 2021-06-18 16:34:30.488 [main] JdbcExecutor - CREATE TABLE message
(
id1 INT NOT NULL,
message1 text NOT NULL
)
[DEBUG] 2021-06-18 16:34:30.519 [main] JdbcExecutor - 0 row(s) affected
[INFO ] 2021-06-18 16:34:30.519 [main] ChangeSet - Custom SQL executed
[INFO ] 2021-06-18 16:34:30.520 [main] ChangeSet - ChangeSet com/db/liquibase/changelog/prd.db.changelog-master.xml::test::auth ran successfully in 33ms
[INFO ] 2021-06-18 16:34:30.520 [main] JdbcExecutor - Executing with the 'jdbc' executor
[INFO ] 2021-06-18 16:34:30.521 [main] JdbcExecutor - Executing with the 'jdbc' executor
[INFO ] 2021-06-18 16:34:30.521 [main] JdbcExecutor - SELECT MAX(ORDEREXECUTED) FROM public.databasechangelog
[INFO ] 2021-06-18 16:34:30.522 [main] JdbcExecutor - INSERT INTO public.databasechangelog (ID, AUTHOR, FILENAME, DATEEXECUTED, ORDEREXECUTED, MD5SUM, DESCRIPTION, COMMENTS, EXECTYPE, CONTEXTS, LABELS, LIQUIBASE, DEPLOYMENT_ID) VALUES ('test', 'auth', 'com/db/liquibase/changelog/prd.db.changelog-master.xml', NOW(), 1, '8:90ae87c0dddab31a70358badc27f0ae9', 'sql', '', 'EXECUTED', NULL, NULL, '3.10.0', '4014270479')
[DEBUG] 2021-06-18 16:34:30.525 [main] JdbcExecutor - 1 row(s) affected
[DEBUG] 2021-06-18 16:34:30.526 [main] JdbcExecutor - Release Database Lock
[INFO ] 2021-06-18 16:34:30.526 [main] JdbcExecutor - Executing with the 'jdbc' executor
[DEBUG] 2021-06-18 16:34:30.526 [main] JdbcExecutor - UPDATE public.databasechangeloglock SET LOCKED = FALSE, LOCKEDBY = NULL, LOCKGRANTED = NULL WHERE ID = 1
[INFO ] 2021-06-18 16:34:30.527 [main] StandardLockService - Successfully released change log lock
[WARN ] 2021-06-18 16:34:30.528 [main] PostgresDatabase - Failed to restore the auto commit to true
[WARN ] 2021-06-18 16:34:30.528 [main] DatabaseGenerator - Unable to close the liquibase/database/connection resource.
[INFO ] 2021-06-18 16:34:30.528 [main] DatabaseGenerator - [DatabaseGenerator][main] end