Flyway 没有正确清理数据库,执行两次迁移文件
Flyway don't clean database correctly, and execute migration files two times
我正在使用 flyway 迁移数据库,我也使用 FlywayTest
进行集成测试,但是当我使用 :
创建测试时
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = APPApplication.class)
@Rollback
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, FlywayTestExecutionListener.class})
@FlywayTest
public class RolesRepositoryTest {
...
}
我收到这个错误:
o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
o.f.c.internal.database.DatabaseFactory : Database: jdbc:h2:mem:testdb (H2 1.4)
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.055s)
o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 1 - CREATE FIRST TABLES
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 2 - INSERT ROLES
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 3 - INSERT ADMINS
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 4 - INSERT OTHERS
o.f.core.internal.command.DbMigrate : Successfully applied 4 migrations to schema "PUBLIC" (execution time 00:00.464s)
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
o.f.test.FlywayTestExecutionListener : ---> Start reset database for 'com.name.project.infrastructure.database.jpa.RolesRepositoryTest'.
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbClean : Successfully cleaned schema "PUBLIC" (execution time 00:00.010s)
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.023s)
o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 1 - CREATE FIRST TABLES
o.f.core.internal.command.DbMigrate : Migration of schema "PUBLIC" to version 1 - CREATE FIRST TABLES failed! Please restore backups and roll back database and code!
o.s.test.context.TestContextManager : Caught exception while invoking 'beforeTestClass' callback on TestExecutionListener [org.flywaydb.test.FlywayTestExecutionListener@797b0699] for test class [class com.name.project.infrastructure.database.jpa.RolesRepositoryTest]
org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException:
Migration V1__CREATE_FIRST_TABLES.sql failed
--------------------------------------------
SQL State : 42S01
Error Code : 42101
Message : Table "ROLES" already exists; SQL statement:
如果你没有:
---> Start reset database for 'com.name.project.infrastructure.database.jpa.RolesRepositoryTest'.
这来自FlywayTestExecutionListener.class
但它再次迁移了数据库。
为什么 flyway 这样做,如何阻止它。
更多详情
当我不在我的表中使用架构名称时,问题就不会发生。一切正常。
如果您在每次测试中都重复使用 FlywayTestExecutionListener
,那么即使您使用 beforeTestClass
,也会每隔 class 使用 @TestExecutionListeners
执行设置。
在 JUnit 中,一次为一组测试设置资源的方法是定义一个 Rule (for Unit 4) and Extensions(对于 JUnit 5)。
我正在使用 flyway 迁移数据库,我也使用 FlywayTest
进行集成测试,但是当我使用 :
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = APPApplication.class)
@Rollback
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, FlywayTestExecutionListener.class})
@FlywayTest
public class RolesRepositoryTest {
...
}
我收到这个错误:
o.f.c.internal.license.VersionPrinter : Flyway Community Edition 6.0.8 by Redgate
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Starting...
com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Start completed.
o.f.c.internal.database.DatabaseFactory : Database: jdbc:h2:mem:testdb (H2 1.4)
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.055s)
o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 1 - CREATE FIRST TABLES
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 2 - INSERT ROLES
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 3 - INSERT ADMINS
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 4 - INSERT OTHERS
o.f.core.internal.command.DbMigrate : Successfully applied 4 migrations to schema "PUBLIC" (execution time 00:00.464s)
o.hibernate.jpa.internal.util.LogHelper : HHH000204: Processing PersistenceUnitInfo [name: default]
o.f.test.FlywayTestExecutionListener : ---> Start reset database for 'com.name.project.infrastructure.database.jpa.RolesRepositoryTest'.
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbClean : Successfully cleaned schema "PUBLIC" (execution time 00:00.010s)
o.f.c.internal.database.base.Database : Flyway upgrade recommended: H2 1.4.200 is newer than this version of Flyway and support has not been tested. The latest supported version of H2 is 1.4.199.
o.f.core.internal.command.DbValidate : Successfully validated 4 migrations (execution time 00:00.023s)
o.f.c.i.s.JdbcTableSchemaHistory : Creating Schema History table "PUBLIC"."flyway_schema_history" ...
o.f.core.internal.command.DbMigrate : Current version of schema "PUBLIC": << Empty Schema >>
o.f.core.internal.command.DbMigrate : Migrating schema "PUBLIC" to version 1 - CREATE FIRST TABLES
o.f.core.internal.command.DbMigrate : Migration of schema "PUBLIC" to version 1 - CREATE FIRST TABLES failed! Please restore backups and roll back database and code!
o.s.test.context.TestContextManager : Caught exception while invoking 'beforeTestClass' callback on TestExecutionListener [org.flywaydb.test.FlywayTestExecutionListener@797b0699] for test class [class com.name.project.infrastructure.database.jpa.RolesRepositoryTest]
org.flywaydb.core.internal.command.DbMigrate$FlywayMigrateException:
Migration V1__CREATE_FIRST_TABLES.sql failed
--------------------------------------------
SQL State : 42S01
Error Code : 42101
Message : Table "ROLES" already exists; SQL statement:
如果你没有:
---> Start reset database for 'com.name.project.infrastructure.database.jpa.RolesRepositoryTest'.
这来自FlywayTestExecutionListener.class
但它再次迁移了数据库。
为什么 flyway 这样做,如何阻止它。
更多详情
当我不在我的表中使用架构名称时,问题就不会发生。一切正常。
如果您在每次测试中都重复使用 FlywayTestExecutionListener
,那么即使您使用 beforeTestClass
,也会每隔 class 使用 @TestExecutionListeners
执行设置。
在 JUnit 中,一次为一组测试设置资源的方法是定义一个 Rule (for Unit 4) and Extensions(对于 JUnit 5)。