-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Flyway]: Supported info & repair & validate & delete scan table flyw…
…ay_schema_history (#116)
- Loading branch information
1 parent
c46b63b
commit be065dd
Showing
9 changed files
with
162 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 24 additions & 0 deletions
24
flyway-dialect/src/test/java/tech/ydb/flyway/database/YdbFlywayInfoTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package tech.ydb.flyway.database; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author Kirill Kurdyukov | ||
*/ | ||
public class YdbFlywayInfoTest extends YdbFlywayBaseTest { | ||
|
||
@Test | ||
void simpleTest() { | ||
createFlyway("classpath:db/migration-step-3").load().migrate(); | ||
|
||
var flyway = createFlyway("classpath:db/migration").load(); | ||
|
||
var info = flyway.info(); | ||
|
||
assertEquals(3, info.applied().length); | ||
assertEquals(6, info.all().length); | ||
|
||
flyway.migrate(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
flyway-dialect/src/test/java/tech/ydb/flyway/database/YdbFlywayRepairAndValidateTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package tech.ydb.flyway.database; | ||
|
||
import java.sql.DriverManager; | ||
import java.sql.SQLException; | ||
import org.flywaydb.core.api.ErrorCode; | ||
import org.flywaydb.core.internal.command.DbMigrate; | ||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import org.junit.jupiter.api.Test; | ||
|
||
/** | ||
* @author Kirill Kurdyukov | ||
*/ | ||
public class YdbFlywayRepairAndValidateTest extends YdbFlywayBaseTest { | ||
|
||
@Test | ||
void markUnSuccessMigrationThenRepairDeletedTest() throws SQLException { | ||
assertThrows(DbMigrate.FlywayMigrateException.class, | ||
() -> createFlyway("classpath:db/migration-with-failed").load().migrate()); | ||
|
||
try (var connection = DriverManager.getConnection(jdbcUrl())) { | ||
try (var st = connection.createStatement()) { | ||
var rs = st.executeQuery("SELECT script FROM flyway_schema_history WHERE success = false"); | ||
|
||
rs.next(); | ||
|
||
assertEquals("V3__create_episodes.sql", rs.getString(1)); | ||
|
||
assertFalse(rs.next()); | ||
} | ||
} | ||
|
||
createFlyway("classpath:db/migration-step-3").load().repair(); | ||
|
||
try (var connection = DriverManager.getConnection(jdbcUrl())) { | ||
try (var st = connection.createStatement()) { | ||
var rs = st.executeQuery("SELECT COUNT(*) FROM flyway_schema_history"); | ||
|
||
rs.next(); | ||
|
||
assertEquals(2, rs.getLong(1)); | ||
} | ||
} | ||
|
||
assertTrue(createFlyway("classpath:db/migration").load().migrate().success); | ||
} | ||
|
||
@Test | ||
void updateChecksumMigrationTest() { | ||
assertTrue(createFlyway("classpath:db/migration-step-3").load().migrate().success); | ||
|
||
String checksumPrev = checksum(); | ||
|
||
var flyway = createFlyway("classpath:db/migration-with-failed").load(); | ||
|
||
var validateResult = flyway.validateWithResult(); | ||
|
||
assertFalse(validateResult.validationSuccessful); | ||
assertEquals("create episodes", validateResult.invalidMigrations.get(0).description); | ||
assertEquals(ErrorCode.CHECKSUM_MISMATCH, validateResult.invalidMigrations.get(0).errorDetails.errorCode); | ||
|
||
flyway.repair(); | ||
|
||
assertNotEquals(checksumPrev, checksum()); | ||
|
||
flyway = createFlyway("classpath:db/migration").load(); | ||
|
||
flyway.repair(); | ||
|
||
assertEquals(checksumPrev, checksum()); | ||
|
||
assertTrue(flyway.migrate().success); | ||
} | ||
|
||
private static String checksum() { | ||
try (var connection = DriverManager.getConnection(jdbcUrl())) { | ||
var rs = connection.createStatement().executeQuery("SELECT checksum FROM flyway_schema_history WHERE version = '3'"); | ||
|
||
rs.next(); | ||
return rs.getString(1); | ||
} catch (SQLException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
flyway-dialect/src/test/resources/db/migration-with-failed/V1__create_series.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
CREATE TABLE series -- series is the table name. | ||
( -- Must be unique within the folder. | ||
series_id Int64, | ||
title Text, | ||
series_info Text, | ||
release_date Int64, | ||
PRIMARY KEY (series_id) -- The primary key is a column or | ||
-- combination of columns that uniquely identifies | ||
-- each table row (contains only | ||
-- non-repeating values). A table can have | ||
-- only one primary key. For every table | ||
-- in YDB, the primary key is required. | ||
); |
10 changes: 10 additions & 0 deletions
10
flyway-dialect/src/test/resources/db/migration-with-failed/V2__create_seasons.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
|
||
CREATE TABLE seasons | ||
( | ||
series_id Uint64, | ||
season_id Uint64, | ||
title Utf8, | ||
first_aired Uint64, | ||
last_aired Uint64, | ||
PRIMARY KEY (series_id, season_id) | ||
); |
9 changes: 9 additions & 0 deletions
9
flyway-dialect/src/test/resources/db/migration-with-failed/V3__create_episodes.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CREATE TABLE episodes | ||
( | ||
series_id STRANGE, | ||
season_id Uint64, | ||
episode_id Uint64, | ||
title Utf8, | ||
air_date Uint64, | ||
PRIMARY KEY (series_id, season_id, episode_id) | ||
); |