java - 用 XML 替换 yaml 时表已存在错误
问题描述
我正在尝试在我的 MySQL 数据库上运行一些迁移。使用 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$0(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$0(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$0(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$0(Liquibase.java:273) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child$0(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$1(Liquibase.java:272) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child$0(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$2.lambda$null$0(ChangeLogIterator.java:111) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child$0(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$2.lambda$run$1(ChangeLogIterator.java:110) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child$0(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$2.run(ChangeLogIterator.java:94) ~[liquibase-core-4.3.1.jar:na]
at liquibase.Scope.lambda$child$0(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 元数据表中。由于 yaml 文件导致的校验和与 xml 不同,liquibase 不会将第一个变更集识别为已执行的变更集,并尝试再次应用它。您可以尝试使用clearChecksum命令删除已计算的状态,并在下次启动时重新计算。
或者,您可以要求 liquibase 为您计算它,您可以更新数据库中的元数据表以匹配 XML 的一种形式,因此它不会尝试再次运行它。计算校验和。
默认情况下,元数据表名称是DATABASECHANGELOG
。
推荐阅读
- powershell - 将结果从自定义 {0}`t{1}`t{2}`t{3}`t{4} 格式输出到 OGV?
- php - 尝试从发布请求访问请求属性以登录数据库 PHP Slim 3
- javascript - 检测反应页面变化的最佳方法
- spring-boot - 如果处理失败,如何将 STOMP 消息重新传递给消费者?
- python - 迭代器相当于python中的切片?
- javascript - Mongoose 聚合:“组”阶段内的查询
- java - 我们可以从 AOP 中的方面实现类调用 rest 端点吗?我还需要在其他项目中使用这个 AOP lib 项目
- javascript - 如何将自定义标记添加到 Google 时间轴图表?
- javascript - 对数组进行排序,嵌套所有非唯一值
- python - 使用 Enterprise Architect Python API 旋转标签