如何在jhipster-spring项目中应用liquibase变更日志

tnkciper  于 2021-07-24  发布在  Java
关注(0)|答案(0)|浏览(252)

我有一个jhipsterspring启动应用程序,其中我有2个实体,而生成的实体db不是我喜欢的。例如,我的实体有一个通过id连接的关系,但我想通过另一个字段连接它们。
为此,我做的第一件事就是注解掉master.xml中的所有变更日志,我想一步一步地更改数据库。我从我的第一个changelog开始,我生成了另一个changelog并从原始的changesetid和所需的值复制了数据。但当我试着运行mvn时liquibase:update or 运行应用程序时出现以下错误:

liquibase.exception.MigrationFailedException: Migration failed for change set config/liquibase/changelog/20210126_removed_id_from_price.xml::20210126-1::jhipster:
     Reason: liquibase.exception.DatabaseException: Table "MATERIAL_PRICE" already exists; SQL statement:
CREATE TABLE PUBLIC.material_price (amount DECIMAL(21, 2) NOT NULL, CONSTRAINT PK_MATERIAL_PRICE PRIMARY KEY (amount)) [42101-200] [Failed SQL: (42101) CREATE TABLE PUBLIC.material_price (amount DECIMAL(21, 2) NOT NULL, CONSTRAINT PK_MATERIAL_PRICE PRIMARY KEY (amount))]
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:646)
    at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:53)
    at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:83)
    at liquibase.Liquibase.update(Liquibase.java:202)
    at liquibase.Liquibase.update(Liquibase.java:179)
    at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:366)
    at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:314)
    at org.springframework.boot.autoconfigure.liquibase.DataSourceClosingSpringLiquibase.afterPropertiesSet(DataSourceClosingSpringLiquibase.java:46)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.initDb(AsyncSpringLiquibase.java:118)
    at io.github.jhipster.config.liquibase.AsyncSpringLiquibase.lambda$afterPropertiesSet$0(AsyncSpringLiquibase.java:93)
    at io.github.jhipster.async.ExceptionHandlingAsyncTaskExecutor.lambda$createWrappedRunnable$1(ExceptionHandlingAsyncTaskExecutor.java:78)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: liquibase.exception.DatabaseException: Table "MATERIAL_PRICE" already exists; SQL statement:
CREATE TABLE PUBLIC.material_price (amount DECIMAL(21, 2) NOT NULL, CONSTRAINT PK_MATERIAL_PRICE PRIMARY KEY (amount)) [42101-200] [Failed SQL: (42101) CREATE TABLE PUBLIC.material_price (amount DECIMAL(21, 2) NOT NULL, CONSTRAINT PK_MATERIAL_PRICE PRIMARY KEY (amount))]
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:402)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:59)
    at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:131)
    at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1276)
    at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1258)
    at liquibase.changelog.ChangeSet.execute(ChangeSet.java:609)
    ... 13 common frames omitted
Caused by: org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "MATERIAL_PRICE" already exists; SQL statement:
CREATE TABLE PUBLIC.material_price (amount DECIMAL(21, 2) NOT NULL, CONSTRAINT PK_MATERIAL_PRICE PRIMARY KEY (amount)) [42101-200]
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:453)
    at org.h2.message.DbException.getJdbcSQLException(DbException.java:429)
    at org.h2.message.DbException.get(DbException.java:205)
    at org.h2.message.DbException.get(DbException.java:181)
    at org.h2.command.ddl.CreateTable.update(CreateTable.java:89)
    at org.h2.command.CommandContainer.update(CommandContainer.java:198)
    at org.h2.command.Command.executeUpdate(Command.java:251)
    at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:228)
    at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:201)
    at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95)
    at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java)
    at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:398)
    ... 18 common frames omitted

那么,如何生成一个变更日志,从中获取变更呢?我是个刚开始喝酒的人。
以下是我的更改日志:
原件:

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

    <!--
        Added the entity MaterialPrice.
    -->
    <changeSet id="20210124151850-1" author="jhipster">
        <createTable tableName="material_price">
            <column name="id" type="bigint">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <column name="amount" type="decimal(21,2)">
                <constraints nullable="true" />
            </column>
            <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here -->
        </createTable>
    </changeSet>

    <changeSet id="20210124151850-1-relations" author="jhipster">

    </changeSet>
    <!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

    <!--
        Load sample data generated with Faker.js
        - This data can be easily edited using a CSV editor (or even MS Excel) and
          is located in the 'src/main/resources/config/liquibase/fake-data' directory
        - By default this data is applied when running with the JHipster 'dev' profile.
          This can be customized by adding or removing 'faker' in the 'spring.liquibase.contexts'
          Spring Boot configuration key.
    -->
    <changeSet id="20210124151850-1-data" author="jhipster" context="faker">
        <loadData
                  file="config/liquibase/fake-data/material_price.csv"
                  separator=";"
                  tableName="material_price">
            <column name="id" type="numeric"/>
            <column name="amount" type="numeric"/>
            <!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
        </loadData>
    </changeSet>

</databaseChangeLog>

我自己生成的:

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

    <!--
        Added the entity MaterialPrice.
    -->
    <changeSet id="20210126-1" author="jhipster">
        <createTable tableName="material_price">
            <column name="amount" type="decimal(21,2)">
                <constraints primaryKey="true" nullable="false"/>
            </column>
            <!-- jhipster-needle-liquibase-add-column - JHipster will add columns here -->
        </createTable>
    </changeSet>

    <changeSet id="20210124151850-1-relations" author="jhipster">

    </changeSet>
    <!-- jhipster-needle-liquibase-add-changeset - JHipster will add changesets here -->

    <!--
        Load sample data generated with Faker.js
        - This data can be easily edited using a CSV editor (or even MS Excel) and
          is located in the 'src/main/resources/config/liquibase/fake-data' directory
        - By default this data is applied when running with the JHipster 'dev' profile.
          This can be customized by adding or removing 'faker' in the 'spring.liquibase.contexts'
          Spring Boot configuration key.
    -->
    <changeSet id="20210124151850-1-data" author="jhipster" context="faker">
        <loadData
            file="config/liquibase/fake-data/material_price.csv"
            separator=";"
            tableName="material_price">
            <column name="id" type="numeric"/>
            <column name="amount" type="numeric"/>
            <!-- jhipster-needle-liquibase-add-loadcolumn - JHipster (and/or extensions) can add load columns here -->
        </loadData>
    </changeSet>

</databaseChangeLog>

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题