Skip to content

Commit dc577ed

Browse files
committed
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.
1 parent a0e1b50 commit dc577ed

File tree

7 files changed

+95
-13
lines changed

7 files changed

+95
-13
lines changed

ebean-migration/pom.xml

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<parent>
55
<groupId>org.avaje</groupId>
66
<artifactId>java11-oss</artifactId>
7-
<version>4.0</version>
7+
<version>5.1</version>
88
<relativePath />
99
</parent>
1010

@@ -22,6 +22,12 @@
2222
</properties>
2323

2424
<dependencies>
25+
<dependency>
26+
<groupId>org.slf4j</groupId>
27+
<artifactId>slf4j-api</artifactId>
28+
<version>2.0.17</version>
29+
<scope>test</scope>
30+
</dependency>
2531
<dependency>
2632
<groupId>io.avaje</groupId>
2733
<artifactId>avaje-applog</artifactId>
@@ -134,23 +140,23 @@ mvn install:install-file -Dfile=/some/path/to/ojdbc7.jar -DgroupId=oracle \
134140
<!-- </dependency>-->
135141

136142
<dependency>
137-
<groupId>ch.qos.logback</groupId>
138-
<artifactId>logback-classic</artifactId>
139-
<version>1.5.2</version>
143+
<groupId>io.avaje</groupId>
144+
<artifactId>avaje-simple-logger</artifactId>
145+
<version>0.5</version>
140146
<scope>test</scope>
141147
</dependency>
142148

143149
<dependency>
144150
<groupId>io.ebean</groupId>
145151
<artifactId>ebean-test-containers</artifactId>
146-
<version>7.3</version>
152+
<version>7.14</version>
147153
<scope>test</scope>
148154
</dependency>
149155

150156
<dependency>
151157
<groupId>io.avaje</groupId>
152158
<artifactId>junit</artifactId>
153-
<version>1.4</version>
159+
<version>1.6</version>
154160
<scope>test</scope>
155161
</dependency>
156162

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationSchema.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@ static void createIfNeeded(MigrationConfig config, Connection connection) throws
3737
new MigrationSchema(config, connection).createAndSetIfNeeded();
3838
}
3939

40+
static void setIfNeeded(MigrationConfig config, Connection connection) throws SQLException {
41+
new MigrationSchema(config, connection).setSchemaIfNeeded();
42+
}
43+
44+
private void setSchemaIfNeeded() throws SQLException {
45+
if (dbSchema != null && setCurrentSchema) {
46+
setSchema();
47+
}
48+
}
49+
4050
private String trim(String dbSchema) {
4151
return (dbSchema == null) ? null : dbSchema.trim();
4252
}

ebean-migration/src/main/java/io/ebean/migration/runner/MigrationTable.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,9 @@ private ScriptTransform createScriptTransform(MigrationConfig config) {
141141
*/
142142
void createIfNeededAndLock() throws SQLException, IOException {
143143
SQLException suppressedException = null;
144-
if (!tableKnownToExist) {
144+
if (tableKnownToExist) {
145+
MigrationSchema.setIfNeeded(config, context.connection());
146+
} else {
145147
MigrationSchema.createIfNeeded(config, context.connection());
146148
if (!tableExists()) {
147149
try {
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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+
}

ebean-migration/src/test/java/io/ebean/migration/runner/MigrationSchemaTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import java.sql.Connection;
77

8-
public class MigrationSchemaTest {
8+
class MigrationSchemaTest {
99

1010
@Test
1111
void testCreateAndSetIfNeeded() throws Exception {
@@ -14,13 +14,12 @@ void testCreateAndSetIfNeeded() throws Exception {
1414
config.setDbSchema("SOME_NEW_SCHEMA");
1515
config.setCreateSchemaIfNotExists(true);
1616

17-
Connection connection = config.createConnection();
18-
19-
MigrationSchema.createIfNeeded(config, connection);
17+
try (Connection connection = config.createConnection()) {
18+
MigrationSchema.createIfNeeded(config, connection);
19+
}
2020
}
2121

2222
private MigrationConfig createMigrationConfig() {
23-
2423
MigrationConfig config = new MigrationConfig();
2524
config.setDbUsername("sa");
2625
config.setDbPassword("");
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
logger.format=plain
2+
3+
## default log level to use when running tests
4+
logger.defaultLogLevel=INFO
5+
6+
## some test specific log levels
7+
log.level.io.ebean=TRACE

test-native-image/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>io.ebean</groupId>
4444
<artifactId>ebean-test-containers</artifactId>
45-
<version>7.3</version>
45+
<version>7.14</version>
4646
<scope>test</scope>
4747
</dependency>
4848

0 commit comments

Comments
 (0)