DDL command not created by Liquibase when trying to run it from Java code

Update:

I think the problem is in this line, because it returns an empty list when I called changelog.getChangeSets() , so what is wrong, this code?

  static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog( "src/main/resources/changelog.xml"); 

Original post:

My goal is to run DDL database commands with an existing list of changes in the Java class. Currently my setup is this:

 public class LiquibaseWithJavaCode { //find the location of the existing changelog.xml file static liquibase.changelog.DatabaseChangeLog changelog = new DatabaseChangeLog( "src/main/resources/changelog.xml"); static void executeUpdate1() throws SQLException, LiquibaseException { java.sql.Connection connection = getConnection(); Database database = DatabaseFactory.getInstance() .findCorrectDatabaseImplementation( new JdbcConnection(connection)); Liquibase liquibase = new liquibase.Liquibase(changelog, new ClassLoaderResourceAccessor(), database); liquibase.update(new Contexts(), new LabelExpression()); } public static Connection getConnection() throws SQLException { Connection conn = null; Properties connectionProps = new Properties(); connectionProps.put("user", "liquibase"); connectionProps.put("password", "password"); conn = DriverManager .getConnection( "jdbc:sqlserver://111.11.1.1;databaseName=liquibase;SelectMethod=cursor", connectionProps); return conn; } } public static void main(String[] args) throws SQLException, LiquibaseException { executeUpdate1(); } 

When I run it, it displays here: enter image description here

I created changelog.xml and contains many changes that process many DDL commands. But it seems that changeLogFile contains no changes, it is empty. I double-checked with the database, and in fact this table was not actually created, only two tables DATABASECHANGELOG and DATABASECHANGELOGLOCK .

Did I do something wrong? How to fix it? Honestly, I am not familiar with this, so please advice.

0
source share
1 answer

new DatabaseChangeLog creates a new empty change file. To analyze an existing file, use:

 ChangeLogParser parser = ChangeLogParserFactory.getInstance().getParser("src/main/resources/changelog.xml", resourceAccessor); DatabaseChangeLog databaseChangeLog = parser.parse("src/main/resources/changelog.xml", new ChangeLogParameters(), resourceAccessor); 

For a resource resource, you probably want new ClassLoaderResourceAccessor()

+3
source

Source: https://habr.com/ru/post/1234293/


All Articles