Skip to content
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
6 changes: 3 additions & 3 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 12 additions & 6 deletions ebean-migration/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<parent>
<groupId>org.avaje</groupId>
<artifactId>java11-oss</artifactId>
<version>4.0</version>
<version>5.1</version>
<relativePath />
</parent>

Expand All @@ -22,6 +22,12 @@
</properties>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>2.0.17</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.avaje</groupId>
<artifactId>avaje-applog</artifactId>
Expand Down Expand Up @@ -134,23 +140,23 @@ mvn install:install-file -Dfile=/some/path/to/ojdbc7.jar -DgroupId=oracle \
<!-- </dependency>-->

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.5.2</version>
<groupId>io.avaje</groupId>
<artifactId>avaje-simple-logger</artifactId>
<version>0.5</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test-containers</artifactId>
<version>7.3</version>
<version>7.14</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>io.avaje</groupId>
<artifactId>junit</artifactId>
<version>1.4</version>
<version>1.6</version>
<scope>test</scope>
</dependency>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

import java.sql.Connection;

public class MigrationSchemaTest {
class MigrationSchemaTest {

@Test
void testCreateAndSetIfNeeded() throws Exception {
Expand All @@ -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("");
Expand Down
7 changes: 7 additions & 0 deletions ebean-migration/src/test/resources/avaje-logger.properties
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test-native-image/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<dependency>
<groupId>io.ebean</groupId>
<artifactId>ebean-test-containers</artifactId>
<version>7.3</version>
<version>7.14</version>
<scope>test</scope>
</dependency>

Expand Down