Table 用 XML 替换 yaml 时已经存在错误
Table already exists error when replacing yaml by XML
我正在尝试 运行 在我的 MySQL database.While 上使用 db.changelog-master 作为 yaml 文件进行一些迁移,一切正常。我打算将 xml 文件用作 db.changelog,因此我将 spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
添加到我的属性中并创建了这两个 xml 文件
db.changelog-master.xml
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
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.8.xsd">
<include file="/db/changelog/db.changelog-1.0.xml"/>
<include file="/db/changelog/db.changelog-2.0.xml"/>
</databaseChangeLog>
db.changelog-1.0.xml
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
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.8.xsd">
<changeSet author="gunter" id="changelog-1.0">
<createTable tableName="employee">
<column name="id" type="serial" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="first_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="last_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
<dropTable tableName="employee"/>
</rollback>
</changeSet>
</databaseChangeLog>
当我尝试 运行 我的项目时,出现以下错误
错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-25 21:56:26.319 ERROR 13920 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.3.jar:2.4.3]
at com.tutorial.demo.TutorialApplication.main(TutorialApplication.java:10) ~[classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.4.jar:5.3.4]
... 21 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.4.jar:5.3.4]
... 35 common frames omitted
Caused by: liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:124) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.lambda$null[=14=](Liquibase.java:273) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.lambda$update(Liquibase.java:272) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.runInScope(Liquibase.java:2322) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.update(Liquibase.java:216) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.update(Liquibase.java:202) ~[liquibase-core-4.3.1.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:322) ~[liquibase-core-4.3.1.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:275) ~[liquibase-core-4.3.1.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.4.jar:5.3.4]
... 47 common frames omitted
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:672) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:49) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.lambda$null[=14=](ChangeLogIterator.java:111) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.lambda$run(ChangeLogIterator.java:110) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:94) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:226) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:66) ~[liquibase-core-4.3.1.jar:na]
... 65 common frames omitted
Caused by: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:393) ~[liquibase-core-4.3.1.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:82) ~[liquibase-core-4.3.1.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:150) ~[liquibase-core-4.3.1.jar:na]
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1275) ~[liquibase-core-4.3.1.jar:na]
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1257) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:637) ~[liquibase-core-4.3.1.jar:na]
... 85 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Table 'employee' already exists
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:762) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:646) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-3.4.5.jar:na]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:389) ~[liquibase-core-4.3.1.jar:na]
... 90 common frames omitted
Process finished with exit code 1
我想是在跟踪已经进行的迁移时出现了某种错误
每个变更集都有其唯一的校验和计算并存储在专用的 liquibase 元数据中 table。由于 yaml 文件产生的校验和与 xml 不同,liquibase 无法将第一个变更集识别为已执行的变更集并尝试再次应用它。您可以尝试使用 clearChecksum 命令删除已计算的状态,并在下次启动时重新计算它。
或者您可以让 liquibase 为您计算它,您可以更新数据库中的元数据 table 以匹配 XML 的一种形式,这样它就不会尝试 运行 再说一遍。 calculateChecksum.
元数据 table 名称默认为 DATABASECHANGELOG
。
虽然@SzaPe 关于校验和的回答绝对正确,但还有另一件事应该完成 - 为您的变更集编写 preConditions
。
preConditions
你的 changeSet 看起来像:
<changeSet author="gunter" id="changelog-1.0">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="employee"/>
</not>
</preConditions>
<createTable tableName="employee">
<column name="id" type="serial" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="first_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="last_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
<dropTable tableName="employee"/>
</rollback>
</changeSet>
因此,当您执行 xml changeSet 时,前提条件将检查 employee
table 是否存在,如果 table 存在,则不会执行 changeSet 本身,但由于 onFail="MARK_RAN"
属性,它在 databasechangelog
中被标记为“运行”。
我正在尝试 运行 在我的 MySQL database.While 上使用 db.changelog-master 作为 yaml 文件进行一些迁移,一切正常。我打算将 xml 文件用作 db.changelog,因此我将 spring.liquibase.change-log=classpath:/db/changelog/db.changelog-master.xml
添加到我的属性中并创建了这两个 xml 文件
db.changelog-master.xml
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
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.8.xsd">
<include file="/db/changelog/db.changelog-1.0.xml"/>
<include file="/db/changelog/db.changelog-2.0.xml"/>
</databaseChangeLog>
db.changelog-1.0.xml
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
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.8.xsd">
<changeSet author="gunter" id="changelog-1.0">
<createTable tableName="employee">
<column name="id" type="serial" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="first_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="last_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
<dropTable tableName="employee"/>
</rollback>
</changeSet>
</databaseChangeLog>
当我尝试 运行 我的项目时,出现以下错误
错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2021-02-25 21:56:26.319 ERROR 13920 --- [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeController': Unsatisfied dependency expressed through field 'employeeDao'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:944) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:917) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:582) ~[spring-context-5.3.4.jar:5.3.4]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:144) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:767) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:759) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:426) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:326) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1311) ~[spring-boot-2.4.3.jar:2.4.3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1300) ~[spring-boot-2.4.3.jar:2.4.3]
at com.tutorial.demo.TutorialApplication.main(TutorialApplication.java:10) ~[classes/:na]
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'employeeDao': Unsatisfied dependency expressed through field 'jdbcTemplate'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1413) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:601) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.4.jar:5.3.4]
... 21 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean[=14=](AbstractBeanFactory.java:335) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1380) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1300) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:657) ~[spring-beans-5.3.4.jar:5.3.4]
... 35 common frames omitted
Caused by: liquibase.exception.LiquibaseException: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:124) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.lambda$null[=14=](Liquibase.java:273) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.lambda$update(Liquibase.java:272) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.runInScope(Liquibase.java:2322) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.update(Liquibase.java:216) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Liquibase.update(Liquibase.java:202) ~[liquibase-core-4.3.1.jar:na]
at liquibase.integration.spring.SpringLiquibase.performUpdate(SpringLiquibase.java:322) ~[liquibase-core-4.3.1.jar:na]
at liquibase.integration.spring.SpringLiquibase.afterPropertiesSet(SpringLiquibase.java:275) ~[liquibase-core-4.3.1.jar:na]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845) ~[spring-beans-5.3.4.jar:5.3.4]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782) ~[spring-beans-5.3.4.jar:5.3.4]
... 47 common frames omitted
Caused by: liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/db.changelog-1.0.xml::changelog-1.0::gunter:
Reason: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:672) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.visitor.UpdateVisitor.visit(UpdateVisitor.java:49) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.lambda$null[=14=](ChangeLogIterator.java:111) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.lambda$run(ChangeLogIterator.java:110) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:94) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child[=14=](Scope.java:160) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:169) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:159) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:138) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:222) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.child(Scope.java:226) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeLogIterator.run(ChangeLogIterator.java:66) ~[liquibase-core-4.3.1.jar:na]
... 65 common frames omitted
Caused by: liquibase.exception.DatabaseException: Table 'employee' already exists [Failed SQL: (1050) CREATE TABLE test.employee (id INT AUTO_INCREMENT NOT NULL, first_name VARCHAR(255) NOT NULL, last_name VARCHAR(255) NOT NULL, CONSTRAINT PK_EMPLOYEE PRIMARY KEY (id))]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:393) ~[liquibase-core-4.3.1.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:82) ~[liquibase-core-4.3.1.jar:na]
at liquibase.executor.jvm.JdbcExecutor.execute(JdbcExecutor.java:150) ~[liquibase-core-4.3.1.jar:na]
at liquibase.database.AbstractJdbcDatabase.execute(AbstractJdbcDatabase.java:1275) ~[liquibase-core-4.3.1.jar:na]
at liquibase.database.AbstractJdbcDatabase.executeStatements(AbstractJdbcDatabase.java:1257) ~[liquibase-core-4.3.1.jar:na]
at liquibase.changelog.ChangeSet.execute(ChangeSet.java:637) ~[liquibase-core-4.3.1.jar:na]
... 85 common frames omitted
Caused by: java.sql.SQLSyntaxErrorException: Table 'employee' already exists
at com.mysql.cj.jdbc.exceptions.SQLError.createSQLException(SQLError.java:120) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:122) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.StatementImpl.executeInternal(StatementImpl.java:762) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.mysql.cj.jdbc.StatementImpl.execute(StatementImpl.java:646) ~[mysql-connector-java-8.0.23.jar:8.0.23]
at com.zaxxer.hikari.pool.ProxyStatement.execute(ProxyStatement.java:95) ~[HikariCP-3.4.5.jar:na]
at com.zaxxer.hikari.pool.HikariProxyStatement.execute(HikariProxyStatement.java) ~[HikariCP-3.4.5.jar:na]
at liquibase.executor.jvm.JdbcExecutor$ExecuteStatementCallback.doInStatement(JdbcExecutor.java:389) ~[liquibase-core-4.3.1.jar:na]
... 90 common frames omitted
Process finished with exit code 1
我想是在跟踪已经进行的迁移时出现了某种错误
每个变更集都有其唯一的校验和计算并存储在专用的 liquibase 元数据中 table。由于 yaml 文件产生的校验和与 xml 不同,liquibase 无法将第一个变更集识别为已执行的变更集并尝试再次应用它。您可以尝试使用 clearChecksum 命令删除已计算的状态,并在下次启动时重新计算它。
或者您可以让 liquibase 为您计算它,您可以更新数据库中的元数据 table 以匹配 XML 的一种形式,这样它就不会尝试 运行 再说一遍。 calculateChecksum.
元数据 table 名称默认为 DATABASECHANGELOG
。
虽然@SzaPe 关于校验和的回答绝对正确,但还有另一件事应该完成 - 为您的变更集编写 preConditions
。
preConditions
你的 changeSet 看起来像:
<changeSet author="gunter" id="changelog-1.0">
<preConditions onFail="MARK_RAN">
<not>
<tableExists tableName="employee"/>
</not>
</preConditions>
<createTable tableName="employee">
<column name="id" type="serial" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="first_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
<column name="last_name" type="varchar(255)">
<constraints nullable="false"/>
</column>
</createTable>
<rollback>
<dropTable tableName="employee"/>
</rollback>
</changeSet>
因此,当您执行 xml changeSet 时,前提条件将检查 employee
table 是否存在,如果 table 存在,则不会执行 changeSet 本身,但由于 onFail="MARK_RAN"
属性,它在 databasechangelog
中被标记为“运行”。