Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Flyway]:Supported Clean and Baseline command #114

Merged
merged 3 commits into from
Apr 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions flyway-dialect/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,48 @@
<scope>test</scope>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.5.0</version>
<configuration>
<source>1.8</source>
</configuration>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<id>attach-sources</id>
<goals>
<goal>jar-no-fork</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<environmentVariables>
<TESTCONTAINERS_REUSE_ENABLE>true</TESTCONTAINERS_REUSE_ENABLE>
</environmentVariables>
</configuration>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
*/
public class YdbConnection extends Connection<YdbDatabase> {

private static final String DUMMY_SCHEMA_NAME = "";
private static final String YDB_SCHEMA_NAME = "";

protected YdbConnection(YdbDatabase database, java.sql.Connection connection) {
super(database, connection);
Expand All @@ -17,11 +17,11 @@ protected YdbConnection(YdbDatabase database, java.sql.Connection connection) {

@Override
protected String getCurrentSchemaNameOrSearchPath() {
return null; // schema isn't supported
return YDB_SCHEMA_NAME; // schema isn't supported
}

@Override
public YdbSchema getSchema(String name) {
return new YdbSchema(jdbcTemplate, database, DUMMY_SCHEMA_NAME);
return new YdbSchema(jdbcTemplate, database, YDB_SCHEMA_NAME);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public String getRawCreateScript(Table table, boolean baseline) {
" execution_time INT32,\n" +
" success BOOL,\n" +
" PRIMARY KEY (installed_rank)" +
")";
");\n" +
(baseline ? getBaselineStatement(table) : "");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
package tech.ydb.flywaydb.database;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.flywaydb.core.internal.database.base.Schema;
import org.flywaydb.core.internal.jdbc.JdbcTemplate;

Expand All @@ -23,7 +28,7 @@ protected boolean doExists() {
}

@Override
protected boolean doEmpty() {
protected boolean doEmpty() throws SQLException {
return doAllTables().length == 0;
}

Expand All @@ -38,12 +43,25 @@ protected void doDrop() {
}

@Override
protected void doClean() {
protected void doClean() throws SQLException {
List<String> schemaTables = schemaTables();

if (schemaTables.isEmpty()) {
return;
}

jdbcTemplate.executeStatement(
schemaTables.stream()
.map(table -> "DROP TABLE " + table)
.collect(Collectors.joining("; "))
);
}

@Override
protected YdbTable[] doAllTables() {
return new YdbTable[0];
protected YdbTable[] doAllTables() throws SQLException {
return schemaTables().stream()
.map(table -> new YdbTable(jdbcTemplate, database, this, name))
.toArray(YdbTable[]::new);
}

@Override
Expand All @@ -55,4 +73,17 @@ public YdbTable getTable(String tableName) {
public String toString() {
return "ydb_schema";
}

private List<String> schemaTables() throws SQLException {
ResultSet rs = jdbcTemplate.getConnection().getMetaData()
.getTables(null, name, null, new String[]{"TABLE"});

List<String> tables = new ArrayList<>();

while (rs.next()) {
tables.add(database.quote(rs.getString("TABLE_NAME")));
}

return tables;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package tech.ydb.flyway.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;
import org.flywaydb.core.Flyway;
import org.flywaydb.core.api.configuration.FluentConfiguration;
import org.junit.jupiter.api.AfterEach;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.extension.RegisterExtension;
import tech.ydb.test.junit5.YdbHelperExtension;

/**
* @author Kirill Kurdyukov
*/
abstract class YdbFlywayBaseTest {

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

protected static void assertCountTable(int expectedSize, String sql, Statement statement) throws SQLException {
ResultSet rs = statement.executeQuery(sql);
rs.next();

assertEquals(expectedSize, rs.getLong(1));
}

protected static FluentConfiguration createFlyway(String migrationsDir) {
return Flyway.configure()
.locations(migrationsDir)
.dataSource(jdbcUrl(), "", "");
}

protected static String jdbcUrl() {
StringBuilder jdbc = new StringBuilder("jdbc:ydb:")
.append(ydb.useTls() ? "grpcs://" : "grpc://")
.append(ydb.endpoint())
.append(ydb.database());

if (ydb.authToken() != null) {
jdbc.append("?").append("token=").append(ydb.authToken());
}

return jdbc.toString();
}

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);

ResultSet rs = statement.executeQuery("SELECT script FROM flyway_schema_history;");
HashSet<String> scripts = new HashSet<>();

while (rs.next()) {
scripts.add(rs.getString(1));
}

assertEquals(expectedScripts(), scripts);
}
}
}

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

@AfterEach
void checkAfterTest() throws SQLException {
verifyTest();

try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
try (Statement statement = connection.createStatement()) {
statement.execute("DROP TABLE series; DROP TABLE seasons; " +
"DROP TABLE episodes; DROP TABLE flyway_schema_history;");
}
}
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package tech.ydb.flyway.database;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Set;
import org.flywaydb.core.Flyway;
import org.junit.jupiter.api.Test;

/**
* <a href="https://documentation.red-gate.com/flyway/flyway-cli-and-api/commands/baseline">Baseline command</a>
*
* @author Kirill Kurdyukov
*/
public class YdbFlywayBaselineTest extends YdbFlywayBaseTest {

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

@Test
void baselineTest() throws SQLException {
try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
try (Statement statement = connection.createStatement()) {
statement.execute(
"CREATE TABLE series " +
"(" +
" series_id Uint64," +
" title Utf8," +
" series_info Utf8," +
" release_date Uint64," +
" PRIMARY KEY (series_id) " +
");" +
"CREATE TABLE seasons" +
"(" +
" series_id Uint64," +
" season_id Uint64," +
" title Utf8," +
" first_aired Uint64," +
" last_aired Uint64," +
" PRIMARY KEY (series_id, season_id)" +
");" +
"CREATE TABLE episodes" +
"(" +
" series_id Uint64," +
" season_id Uint64," +
" episode_id Uint64," +
" title Utf8," +
" air_date Uint64," +
" PRIMARY KEY (series_id, season_id, episode_id)" +
");"
);
}
}

Flyway flyway = createFlyway("classpath:db/migration").baselineVersion("3").load();

flyway.baseline(); // fixed 4 version

flyway.migrate();
}

@Override
protected Set<String> expectedScripts() {
return EXPECTED_SCRIPTS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package tech.ydb.flyway.database;

import java.sql.Connection;
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;
import org.junit.jupiter.api.Test;

/**
* <a href="https://documentation.red-gate.com/fd/clean-184127458.html">Clean command</a>
*
* @author Kirill Kurdyukov
*/
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();

assertTrue(flyway.migrate().success);

verifyTest();

flyway.clean();

try (Connection connection = DriverManager.getConnection(jdbcUrl())) {
assertDeletedTable(connection, "SELECT * FROM episodes");
assertDeletedTable(connection, "SELECT * FROM series");
assertDeletedTable(connection, "SELECT * FROM seasons");
assertDeletedTable(connection, "SELECT * FROM flyway_schema_history");
}
}

private static void assertDeletedTable(Connection connection, String sql) {
assertThrows(SQLException.class, () -> {
try (Statement statement = connection.createStatement()) {
statement.execute(sql);
}
});
}

@Override
void checkAfterTest() {
}

@Override
protected Set<String> expectedScripts() {
return EXPECTED_SCRIPTS;
}
}
Loading
Loading