Skip to content

Commit

Permalink
Shut down datasources in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rPraml committed Jan 2, 2024
1 parent 3203ba4 commit 38d5a22
Show file tree
Hide file tree
Showing 7 changed files with 222 additions and 194 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,20 @@ void run() throws SQLException {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);
try {
Properties properties = new Properties();
properties.setProperty("dbmigration.migrationPath", "dbmig_autorun");

Properties properties = new Properties();
properties.setProperty("dbmigration.migrationPath","dbmig_autorun");
AutoRunner autoRunner = new AutoRunner();
autoRunner.setDefaultDbSchema("other");
autoRunner.loadProperties(properties);
autoRunner.run(dataSource);

AutoRunner autoRunner = new AutoRunner();
autoRunner.setDefaultDbSchema("other");
autoRunner.loadProperties(properties);
autoRunner.run(dataSource);


assertTrue(executeQuery(dataSource));
assertTrue(executeQuery(dataSource));
} finally {
dataSource.shutdown();
}
}

private boolean executeQuery(DataSourcePool dataSource) throws SQLException {
Expand All @@ -46,4 +49,4 @@ private boolean executeQuery(DataSourcePool dataSource) throws SQLException {
}
}
}
}
}
226 changes: 123 additions & 103 deletions ebean-migration/src/test/java/io/ebean/migration/MigrationRunnerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ public void run_when_error() throws SQLException {
fail(ex);
}
}
} finally {
dataSource.shutdown();
}
}

Expand All @@ -108,39 +110,41 @@ public void run_when_suppliedDataSource() {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);
try {
MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig");

MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig");

MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);
System.out.println("-- run second time --");
runner.run(dataSource);

// simulate change to repeatable migration
config.setMigrationPath("dbmig3");
System.out.println("-- run third time --");
runner.run(dataSource);
MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);
System.out.println("-- run second time --");
runner.run(dataSource);

config.setMigrationPath("dbmig4");
// simulate change to repeatable migration
config.setMigrationPath("dbmig3");
System.out.println("-- run third time --");
runner.run(dataSource);

config.setPatchResetChecksumOn("m2_view,1.2");
List<MigrationResource> checkState = runner.checkState(dataSource);
assertThat(checkState).hasSize(1);
assertThat(checkState.get(0).version().asString()).isEqualTo("1.3");
config.setMigrationPath("dbmig4");

config.setPatchInsertOn("1.3");
checkState = runner.checkState(dataSource);
assertThat(checkState).isEmpty();
config.setPatchResetChecksumOn("m2_view,1.2");
List<MigrationResource> checkState = runner.checkState(dataSource);
assertThat(checkState).hasSize(1);
assertThat(checkState.get(0).version().asString()).isEqualTo("1.3");

System.out.println("-- run forth time --");
runner.run(dataSource);
config.setPatchInsertOn("1.3");
checkState = runner.checkState(dataSource);
assertThat(checkState).isEmpty();

System.out.println("-- run fifth time --");
checkState = runner.checkState(dataSource);
assertThat(checkState).isEmpty();
runner.run(dataSource);
System.out.println("-- run forth time --");
runner.run(dataSource);

System.out.println("-- run fifth time --");
checkState = runner.checkState(dataSource);
assertThat(checkState).isEmpty();
runner.run(dataSource);
} finally {
dataSource.shutdown();
}
}

@Test
Expand All @@ -153,21 +157,24 @@ public void run_with_dbinit() throws SQLException {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);
try {
MigrationConfig config = createMigrationConfig();
config.setDbUrl("jdbc:h2:mem:testsDbInit");
config.setMigrationPath("dbmig5_base");
config.setMigrationInitPath("dbmig5_init");

MigrationConfig config = createMigrationConfig();
config.setDbUrl("jdbc:h2:mem:testsDbInit");
config.setMigrationPath("dbmig5_base");
config.setMigrationInitPath("dbmig5_init");

MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);
MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);

MigrationRunner runner2 = new MigrationRunner(config);
runner2.run(dataSource);
MigrationRunner runner2 = new MigrationRunner(config);
runner2.run(dataSource);

try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).containsExactly("<init>", "some_i", "m4", "some_r");
try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).containsExactly("<init>", "some_i", "m4", "some_r");
}
} finally {
dataSource.shutdown();
}
}

Expand All @@ -180,21 +187,24 @@ public void run_only_dbinit_available() throws SQLException {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);
try {
MigrationConfig config = createMigrationConfig();
config.setDbUrl("jdbc:h2:mem:testsDbInit2");
config.setMigrationPath("dbmig6_base");
config.setMigrationInitPath("dbmig6_init");

MigrationConfig config = createMigrationConfig();
config.setDbUrl("jdbc:h2:mem:testsDbInit2");
config.setMigrationPath("dbmig6_base");
config.setMigrationInitPath("dbmig6_init");

MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);
MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);

MigrationRunner runner2 = new MigrationRunner(config);
runner2.run(dataSource);
MigrationRunner runner2 = new MigrationRunner(config);
runner2.run(dataSource);

try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).containsExactly("<init>", "m4");
try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).containsExactly("<init>", "m4");
}
} finally {
dataSource.shutdown();
}
}

Expand All @@ -208,30 +218,33 @@ public void run_with_min_version() {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);

MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig");
config.setMinVersion("1.3"); // dbmig must run, if DB is empty!
new MigrationRunner(config).run(dataSource);


config = createMigrationConfig();
config.setMigrationPath("dbmig3");
config.setMinVersion("1.3");
config.setMinVersionFailMessage("Must run dbmig2 first.");

MigrationRunner runner3 = new MigrationRunner(config);
assertThatThrownBy(() -> runner3.run(dataSource))
.isInstanceOf(MigrationException.class)
.hasMessageContaining("Must run dbmig2 first. MigrationVersion mismatch: v1.2.1 < v1.3");

// now run dbmig2, as intended by error message
config = createMigrationConfig();
config.setMigrationPath("dbmig2");
new MigrationRunner(config).run(dataSource);

// dbmig3 should pass now
runner3.run(dataSource);
try {
MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig");
config.setMinVersion("1.3"); // dbmig must run, if DB is empty!
new MigrationRunner(config).run(dataSource);


config = createMigrationConfig();
config.setMigrationPath("dbmig3");
config.setMinVersion("1.3");
config.setMinVersionFailMessage("Must run dbmig2 first.");

MigrationRunner runner3 = new MigrationRunner(config);
assertThatThrownBy(() -> runner3.run(dataSource))
.isInstanceOf(MigrationException.class)
.hasMessageContaining("Must run dbmig2 first. MigrationVersion mismatch: v1.2.1 < v1.3");

// now run dbmig2, as intended by error message
config = createMigrationConfig();
config.setMigrationPath("dbmig2");
new MigrationRunner(config).run(dataSource);

// dbmig3 should pass now
runner3.run(dataSource);
} finally {
dataSource.shutdown();
}
}

@Test
Expand All @@ -244,23 +257,26 @@ public void run_init_with_min_version() {
dataSourceConfig.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);

// init
MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig5_base");
config.setMigrationInitPath("dbmig5_init");
config.setMinVersion("2.0"); // init must run, although DB is empty!
new MigrationRunner(config).run(dataSource);

// test if migration detects correct init-version (1.3)
config = createMigrationConfig();
config.setMigrationPath("dbmig3");
config.setMinVersion("2.0");

MigrationRunner runner = new MigrationRunner(config);
assertThatThrownBy(() -> runner.run(dataSource))
.isInstanceOf(MigrationException.class)
.hasMessageContaining("MigrationVersion mismatch: v1.3 < v2.0");
try {
// init
MigrationConfig config = createMigrationConfig();
config.setMigrationPath("dbmig5_base");
config.setMigrationInitPath("dbmig5_init");
config.setMinVersion("2.0"); // init must run, although DB is empty!
new MigrationRunner(config).run(dataSource);

// test if migration detects correct init-version (1.3)
config = createMigrationConfig();
config.setMigrationPath("dbmig3");
config.setMinVersion("2.0");

MigrationRunner runner = new MigrationRunner(config);
assertThatThrownBy(() -> runner.run(dataSource))
.isInstanceOf(MigrationException.class)
.hasMessageContaining("MigrationVersion mismatch: v1.3 < v2.0");
} finally {
dataSource.shutdown();
}
}

@Test
Expand All @@ -278,20 +294,24 @@ public void run_with_skipMigration() throws SQLException {
config.setMigrationPath("dbmig");

DataSourcePool dataSource = DataSourceFactory.create("skipMigration", dataSourceConfig);
new MigrationRunner(config).run(dataSource);
try {
new MigrationRunner(config).run(dataSource);

// assert migrations are in the migration table
try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).contains("<init>", "hello", "initial", "add_m3", "test", "m2_view");
}
// assert migrations are in the migration table
try (final Connection connection = dataSource.getConnection()) {
final List<String> names = migrationNames(connection);
assertThat(names).contains("<init>", "hello", "initial", "add_m3", "test", "m2_view");
}

// assert the migrations didn't actually run (create the tables etc)
try (final Connection connection = dataSource.getConnection()) {
singleQueryResult(connection, "select acol from m3");
fail();
} catch (SQLException e) {
assertThat(e.getMessage()).contains("Table \"M3\" not found;");
// assert the migrations didn't actually run (create the tables etc)
try (final Connection connection = dataSource.getConnection()) {
singleQueryResult(connection, "select acol from m3");
fail();
} catch (SQLException e) {
assertThat(e.getMessage()).contains("Table \"M3\" not found;");
}
} finally {
dataSource.shutdown();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,28 +57,29 @@ public void autoEnableEarlyMode_when_indexFileAddedToExistingMigrations() {
.setPassword("");

DataSourcePool dataSource = DataSourceFactory.create("test", dataSourceConfig);

MigrationConfig config = new MigrationConfig();
config.setPlatform("h2");
config.setRunPlaceholderMap(Map.of("my_table_name", "bar"));

// initial traditional migration
config.setMigrationPath("indexB_0");
MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);

assertThat(config.isEarlyChecksumMode()).isFalse();

// add an index file now, expect automatically go to early mode + patch checksums
config.setMigrationPath("indexB_1");
new MigrationRunner(config).run(dataSource);
assertThat(config.isEarlyChecksumMode()).isTrue();

// early mode via <init> row + add an extra migration
config.setMigrationPath("indexB_2");
new MigrationRunner(config).run(dataSource);

dataSource.shutdown();
try {
MigrationConfig config = new MigrationConfig();
config.setPlatform("h2");
config.setRunPlaceholderMap(Map.of("my_table_name", "bar"));

// initial traditional migration
config.setMigrationPath("indexB_0");
MigrationRunner runner = new MigrationRunner(config);
runner.run(dataSource);

assertThat(config.isEarlyChecksumMode()).isFalse();

// add an index file now, expect automatically go to early mode + patch checksums
config.setMigrationPath("indexB_1");
new MigrationRunner(config).run(dataSource);
assertThat(config.isEarlyChecksumMode()).isTrue();

// early mode via <init> row + add an extra migration
config.setMigrationPath("indexB_2");
new MigrationRunner(config).run(dataSource);
} finally {
dataSource.shutdown();
}
}

@Test
Expand Down
Loading

0 comments on commit 38d5a22

Please sign in to comment.