Flyway不能正确清理数据库,并且执行迁移文件两次。

huangapple go评论70阅读模式
英文:

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:

这是来自FlywayTestExecutionListener.class的,但它再次迁移数据库。

为什么Flyway会这样做,以及如何停止它。

更多详情

当我在我的表中不使用模式名称时,不会出现问题。一切都正常工作。

英文:

I'm using flyway for migration database, as well I use FlywayTest for my integration tests, but When I create a test using :

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = APPApplication.class)
@Rollback
@TestExecutionListeners({DependencyInjectionTestExecutionListener.class, FlywayTestExecutionListener.class})
@FlywayTest
public class RolesRepositoryTest {
   ...
}

I get this error :

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:

If you not :

---> Start reset database for  'com.name.project.infrastructure.database.jpa.RolesRepositoryTest'.

This comes from FlywayTestExecutionListener.class but it migrate the database again.

Why flyway do this, and how can stop this.

More details

The problem is not happen when I don't use schema names in my tables. every thing work fine.

答案1

得分: 1

如果您在每个测试中都重用FlywayTestExecutionListener,那么即使使用beforeTestClass,每个带有@TestExecutionListeners的类也将执行设置。

在JUnit中,为一组测试设置资源只需定义一个Rule(适用于JUnit 4)和Extensions(适用于JUnit 5)即可执行一次。

英文:

If you're reusing FlywayTestExecutionListener on every test, then the setup will be executed per every class with the @TestExecutionListeners, even if you use beforeTestClass.

In JUnit, a way to setup resources for a set of tests a single time is to define a Rule (for Unit 4) and Extensions (for JUnit 5).

huangapple
  • 本文由 发表于 2020年9月12日 17:47:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/63858997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定