|
| 1 | +package io.ebean.migration.runner; |
| 2 | + |
| 3 | +import io.ebean.migration.MigrationConfig; |
| 4 | +import io.ebean.migration.MigrationRunner; |
| 5 | +import io.ebean.test.containers.PostgresContainer; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import java.sql.Connection; |
| 9 | +import java.sql.PreparedStatement; |
| 10 | +import java.sql.ResultSet; |
| 11 | +import java.sql.SQLException; |
| 12 | + |
| 13 | +class MigrationPostgresSchemaTest { |
| 14 | + |
| 15 | + private static PostgresContainer createPostgres() { |
| 16 | + return PostgresContainer.builder("17") |
| 17 | + .port(0) // random port |
| 18 | + .containerName("pg17_temp") |
| 19 | + .user("mig_exp_schema") |
| 20 | + .password("mig_exp_schema") |
| 21 | + .dbName("mig_exp_schema") |
| 22 | + .build(); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + void runTwice_expect_setSchemaCalled() throws SQLException { |
| 27 | + PostgresContainer postgresContainer = createPostgres(); |
| 28 | + postgresContainer.stopRemove(); |
| 29 | + postgresContainer.start(); |
| 30 | + |
| 31 | + |
| 32 | + MigrationConfig config = new MigrationConfig(); |
| 33 | + config.setDbUrl(postgresContainer.jdbcUrl()); |
| 34 | + config.setDbUsername("mig_exp_schema"); |
| 35 | + config.setDbPassword("mig_exp_schema"); |
| 36 | + config.setDbSchema("bar"); |
| 37 | + |
| 38 | + // first run, creates and sets the schema correctly (no issue here) |
| 39 | + config.setMigrationPath("dbmig"); |
| 40 | + MigrationRunner runner = new MigrationRunner(config); |
| 41 | + runner.run(); |
| 42 | + |
| 43 | + // run again, SHOULD set the schema (this is where the bug is) |
| 44 | + config.setMigrationPath("dbmig2"); |
| 45 | + MigrationRunner runner2 = new MigrationRunner(config); |
| 46 | + runner2.run(); |
| 47 | + |
| 48 | + // make sure the m4 table was created in the bar schema |
| 49 | + try (Connection connection = config.createConnection()) { |
| 50 | + try (PreparedStatement stmt = connection.prepareStatement("select * from bar.m4")) { |
| 51 | + try (ResultSet resultSet = stmt.executeQuery()) { |
| 52 | + resultSet.next(); |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | +} |
0 commit comments