From dc577ed8c6b25b5ed0acc28848d5995649b8bd5b Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 2 Sep 2025 08:42:11 +1200 Subject: [PATCH 1/2] Fix bug where db schema isn't set on subsequent migrations The dbSchema was being created and set correctly on the first migration run, but for subsequent migrations the schema was not being set on the connection before running the migration. This fixes that issue. --- ebean-migration/pom.xml | 18 ++++-- .../migration/runner/MigrationSchema.java | 10 ++++ .../migration/runner/MigrationTable.java | 4 +- .../runner/MigrationPostgresSchemaTest.java | 58 +++++++++++++++++++ .../migration/runner/MigrationSchemaTest.java | 9 ++- .../test/resources/avaje-logger.properties | 7 +++ test-native-image/pom.xml | 2 +- 7 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 ebean-migration/src/test/java/io/ebean/migration/runner/MigrationPostgresSchemaTest.java create mode 100644 ebean-migration/src/test/resources/avaje-logger.properties diff --git a/ebean-migration/pom.xml b/ebean-migration/pom.xml index 671234e..d398eab 100644 --- a/ebean-migration/pom.xml +++ b/ebean-migration/pom.xml @@ -4,7 +4,7 @@ org.avaje java11-oss - 4.0 + 5.1 @@ -22,6 +22,12 @@ + + org.slf4j + slf4j-api + 2.0.17 + test + io.avaje avaje-applog @@ -134,23 +140,23 @@ mvn install:install-file -Dfile=/some/path/to/ojdbc7.jar -DgroupId=oracle \ - ch.qos.logback - logback-classic - 1.5.2 + io.avaje + avaje-simple-logger + 0.5 test io.ebean ebean-test-containers - 7.3 + 7.14 test io.avaje junit - 1.4 + 1.6 test diff --git a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java index 7fcc0f4..3efa1a3 100644 --- a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java +++ b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java @@ -37,6 +37,16 @@ static void createIfNeeded(MigrationConfig config, Connection connection) throws new MigrationSchema(config, connection).createAndSetIfNeeded(); } + static void setIfNeeded(MigrationConfig config, Connection connection) throws SQLException { + new MigrationSchema(config, connection).setSchemaIfNeeded(); + } + + private void setSchemaIfNeeded() throws SQLException { + if (dbSchema != null && setCurrentSchema) { + setSchema(); + } + } + private String trim(String dbSchema) { return (dbSchema == null) ? null : dbSchema.trim(); } diff --git a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java index 8dea179..0ab5b13 100644 --- a/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java +++ b/ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java @@ -141,7 +141,9 @@ private ScriptTransform createScriptTransform(MigrationConfig config) { */ void createIfNeededAndLock() throws SQLException, IOException { SQLException suppressedException = null; - if (!tableKnownToExist) { + if (tableKnownToExist) { + MigrationSchema.setIfNeeded(config, context.connection()); + } else { MigrationSchema.createIfNeeded(config, context.connection()); if (!tableExists()) { try { diff --git a/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationPostgresSchemaTest.java b/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationPostgresSchemaTest.java new file mode 100644 index 0000000..2c07020 --- /dev/null +++ b/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationPostgresSchemaTest.java @@ -0,0 +1,58 @@ +package io.ebean.migration.runner; + +import io.ebean.migration.MigrationConfig; +import io.ebean.migration.MigrationRunner; +import io.ebean.test.containers.PostgresContainer; +import org.junit.jupiter.api.Test; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; + +class MigrationPostgresSchemaTest { + + private static PostgresContainer createPostgres() { + return PostgresContainer.builder("17") + .port(0) // random port + .containerName("pg17_temp") + .user("mig_exp_schema") + .password("mig_exp_schema") + .dbName("mig_exp_schema") + .build(); + } + + @Test + void runTwice_expect_setSchemaCalled() throws SQLException { + PostgresContainer postgresContainer = createPostgres(); + postgresContainer.stopRemove(); + postgresContainer.start(); + + + MigrationConfig config = new MigrationConfig(); + config.setDbUrl(postgresContainer.jdbcUrl()); + config.setDbUsername("mig_exp_schema"); + config.setDbPassword("mig_exp_schema"); + config.setDbSchema("bar"); + + // first run, creates and sets the schema correctly (no issue here) + config.setMigrationPath("dbmig"); + MigrationRunner runner = new MigrationRunner(config); + runner.run(); + + // run again, SHOULD set the schema (this is where the bug is) + config.setMigrationPath("dbmig2"); + MigrationRunner runner2 = new MigrationRunner(config); + runner2.run(); + + // make sure the m4 table was created in the bar schema + try (Connection connection = config.createConnection()) { + try (PreparedStatement stmt = connection.prepareStatement("select * from bar.m4")) { + try (ResultSet resultSet = stmt.executeQuery()) { + resultSet.next(); + } + } + } + } + +} diff --git a/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationSchemaTest.java b/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationSchemaTest.java index 82f9cdf..6f6363e 100644 --- a/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationSchemaTest.java +++ b/ebean-migration/src/test/java/io/ebean/migration/runner/MigrationSchemaTest.java @@ -5,7 +5,7 @@ import java.sql.Connection; -public class MigrationSchemaTest { +class MigrationSchemaTest { @Test void testCreateAndSetIfNeeded() throws Exception { @@ -14,13 +14,12 @@ void testCreateAndSetIfNeeded() throws Exception { config.setDbSchema("SOME_NEW_SCHEMA"); config.setCreateSchemaIfNotExists(true); - Connection connection = config.createConnection(); - - MigrationSchema.createIfNeeded(config, connection); + try (Connection connection = config.createConnection()) { + MigrationSchema.createIfNeeded(config, connection); + } } private MigrationConfig createMigrationConfig() { - MigrationConfig config = new MigrationConfig(); config.setDbUsername("sa"); config.setDbPassword(""); diff --git a/ebean-migration/src/test/resources/avaje-logger.properties b/ebean-migration/src/test/resources/avaje-logger.properties new file mode 100644 index 0000000..09ff393 --- /dev/null +++ b/ebean-migration/src/test/resources/avaje-logger.properties @@ -0,0 +1,7 @@ +logger.format=plain + +## default log level to use when running tests +logger.defaultLogLevel=INFO + +## some test specific log levels +log.level.io.ebean=TRACE diff --git a/test-native-image/pom.xml b/test-native-image/pom.xml index 2b2bc82..402a6d8 100644 --- a/test-native-image/pom.xml +++ b/test-native-image/pom.xml @@ -42,7 +42,7 @@ io.ebean ebean-test-containers - 7.3 + 7.14 test From 646e30020e80df284dc9615576f17f24027b5faf Mon Sep 17 00:00:00 2001 From: Rob Bygrave Date: Tue, 2 Sep 2025 08:43:45 +1200 Subject: [PATCH 2/2] Update github workflow actions --- .github/workflows/build.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c1c575f..74595cf 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -16,14 +16,14 @@ jobs: os: [ubuntu-latest] steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 - name: Set up Java - uses: actions/setup-java@v2 + uses: actions/setup-java@v4 with: java-version: ${{ matrix.java_version }} distribution: 'zulu' - name: Maven cache - uses: actions/cache@v2 + uses: actions/cache@v4 env: cache-name: maven-cache with: