liquibase无法在数据库中执行变更集

时间:2019-04-27 10:09:10

标签: java liquibase changeset database-versioning

我不知道为什么所有变更集都无法执行并反映在数据库中。

我也提到了这个link,不幸的是,它也有相同的问题,对此post没有答案。

总而言之,尚未执行任何变更集,并且我的数据库中没有表(DATABASCHANGELOG除外)。

enter image description here

请参考以下代码。

类“ DataVersioningController.java ”具有main方法,并调用了“ liquiebaseRun()”方法。

public static void main(String args[]) throws LiquibaseException, SQLException {
       new DataVersioningController().liquiebaseRun();

    }

    public void  liquiebaseRun() throws SQLException, LiquibaseException {

       JdbcConnection jdbcConnection = new JdbcConnection(DataVersioningController.getConnection());
       DatabaseChangeLog dbChangeLog = new DatabaseChangeLog("D:\\ChangeSet.xml");
       Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(jdbcConnection);

       ClassLoaderResourceAccessor classLoaderAccessor = new ClassLoaderResourceAccessor();

       CommandLineResourceAccessor clAccessor = new CommandLineResourceAccessor(getClass().getClassLoader());

        CompositeResourceAccessor ref = new CompositeResourceAccessor(new ResourceAccessor[] { clAccessor, classLoaderAccessor });
       database.setDefaultSchemaName("test");
       database.setLiquibaseSchemaName("test");
       Liquibase liquibase = new Liquibase(dbChangeLog, ref, database);
       liquibase.update(new Contexts("test"), new LabelExpression());

       System.out.println("Completed ");
  }

ChangeSet.xml

<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
        xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
        xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
    http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">

    <changeSet author="admin" id="1" context="test">
        <createTable tableName="person">
            <column autoIncrement="true" name="id" type="INT">
                <constraints primaryKey="true"/>
            </column>
            <column name="name" type="VARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="address" type="VARCHAR(255)"/>
        </createTable>

    </changeSet>
</databaseChangeLog>

1 个答案:

答案 0 :(得分:0)

您不需要手动调用liquibase。 如果您使用的是SpringBoot和Maven,则添加该依赖项:

<!-- https://mvnrepository.com/artifact/org.liquibase/liquibase-core -->
        <dependency>
            <groupId>org.liquibase</groupId>
            <artifactId>liquibase-core</artifactId>
            <version>3.6.2</version>
        </dependency>

,然后将该属性添加到您的application.properties:

spring.liquibase.change-log=//path to your changeset