Skip to content

Commit

Permalink
[Flyway]:
Browse files Browse the repository at this point in the history
1) SCAN SELECT -> SELECT FROM flyway schema history table
2) Tested Info command
3) Tested Repair command
4) Tested Validate command
  • Loading branch information
KirillKurdyukov committed Apr 11, 2024
1 parent c46b63b commit c3ec574
Show file tree
Hide file tree
Showing 9 changed files with 159 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public String getRawCreateScript(Table table, boolean baseline) {

@Override
public String getSelectStatement(Table table) {
return "SCAN SELECT " + quote("installed_rank")
return "SELECT " + quote("installed_rank")
+ "," + quote("version")
+ "," + quote("description")
+ "," + quote("type")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@
*/
abstract class YdbFlywayBaseTest {

private static final Set<String> EXPECTED_ALL_SCRIPTS = Set.of(
"V1__create_series.sql", "V2__create_seasons.sql",
"V3__create_episodes.sql", "V4__load_data.sql",
"V5__create_series_title_index.sql", "V6__rename_index_title_index.sql"
);

@RegisterExtension
private static final YdbHelperExtension ydb = new YdbHelperExtension();

Expand Down Expand Up @@ -51,9 +57,7 @@ protected static String jdbcUrl() {
protected void verifyTest() throws SQLException {
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
try (Statement statement = connection.createStatement()) {
assertCountTable(2, "SELECT COUNT(*) FROM series", statement);
assertCountTable(9, "SELECT COUNT(*) FROM seasons", statement);
assertCountTable(70, "SELECT COUNT(*) FROM episodes", statement);
verifyCountTables(statement);

ResultSet rs = statement.executeQuery("SELECT script FROM flyway_schema_history;");
HashSet<String> scripts = new HashSet<>();
Expand All @@ -67,8 +71,14 @@ protected void verifyTest() throws SQLException {
}
}

protected void verifyCountTables(Statement statement) throws SQLException {
assertCountTable(2, "SELECT COUNT(*) FROM series", statement);
assertCountTable(9, "SELECT COUNT(*) FROM seasons", statement);
assertCountTable(70, "SELECT COUNT(*) FROM episodes", statement);
}

protected Set<String> expectedScripts() {
return Set.of();
return EXPECTED_ALL_SCRIPTS;
}

@AfterEach
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import org.flywaydb.core.Flyway;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -17,12 +16,6 @@
*/
public class YdbFlywayCleanTest extends YdbFlywayBaseTest {

private static final Set<String> EXPECTED_SCRIPTS = Set.of(
"V1__create_series.sql", "V2__create_seasons.sql",
"V3__create_episodes.sql", "V4__load_data.sql",
"V5__create_series_title_index.sql", "V6__rename_index_title_index.sql"
);

@Test
void cleanSchemaTest() throws SQLException {
Flyway flyway = createFlyway("classpath:db/migration").cleanDisabled(false).load();
Expand Down Expand Up @@ -52,9 +45,4 @@ private static void assertDeletedTable(Connection connection, String sql) {
@Override
void checkAfterTest() {
}

@Override
protected Set<String> expectedScripts() {
return EXPECTED_SCRIPTS;
}
}
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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
Expand All @@ -21,12 +20,6 @@ public class YdbFlywayMigrationTest extends YdbFlywayBaseTest {
"migration-step-5", "migration",
};

private static final Set<String> EXPECTED_SCRIPTS = Set.of(
"V1__create_series.sql", "V2__create_seasons.sql",
"V3__create_episodes.sql", "V4__load_data.sql",
"V5__create_series_title_index.sql", "V6__rename_index_title_index.sql"
);

@Test
void simpleTest() {
assertTrue(createFlyway("classpath:db/migration").load().migrate().success);
Expand Down Expand Up @@ -64,9 +57,4 @@ void evolutionConcurrencySchemaTest() throws ExecutionException, InterruptedExce
}
}
}

@Override
protected Set<String> expectedScripts() {
return EXPECTED_SCRIPTS;
}
}
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);
}
}
}
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.
);
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)
);
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)
);

0 comments on commit c3ec574

Please sign in to comment.