Update:
I think the problem is 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 database DDL commands with existing changelog in Java class. Currently my set up is like 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 here is the output:
I created the changelog.xml
and it contains a lot of changeset that handles a lot of DDL command. But it seems the changeLogFile
does not contain any changeset, it is empty. I double checked with the database, and it was indeed no actually table created, only two tables DATABASECHANGELOG
and DATABASECHANGELOGLOCK
being created.
Did I do anything wrong? How do I correct it? To be honest I am not familiar about it so please advice.
new DatabaseChangeLog just creates an new empty changelog file. To parse an existing file, use:
For a resourceAccessor, you probably want
new ClassLoaderResourceAccessor()