Skip to content

Commit 631bec8

Browse files
committed
Merge branch 'master' of github.com:ebean-orm/ebean-migration
2 parents 167e58f + f92cc6d commit 631bec8

File tree

5 files changed

+67
-24
lines changed

5 files changed

+67
-24
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,8 @@ void lockMigrationTable(String sqlTable, Connection connection) throws SQLExcept
187187
}
188188

189189
private boolean obtainNamedLock(Connection connection) throws SQLException {
190-
try (PreparedStatement query = connection.prepareStatement("select get_lock('ebean_migration', 10)")) {
190+
String hash = Integer.toHexString(connection.getMetaData().getURL().hashCode());
191+
try (PreparedStatement query = connection.prepareStatement("select get_lock('ebean_migration-" + hash + "', 10)")) {
191192
try (ResultSet resultSet = query.executeQuery()) {
192193
if (resultSet.next()) {
193194
return resultSet.getInt(1) == 1;
@@ -199,7 +200,8 @@ private boolean obtainNamedLock(Connection connection) throws SQLException {
199200

200201
@Override
201202
void unlockMigrationTable(String sqlTable, Connection connection) throws SQLException {
202-
try (PreparedStatement query = connection.prepareStatement("select release_lock('ebean_migration')")) {
203+
String hash = Integer.toHexString(connection.getMetaData().getURL().hashCode());
204+
try (PreparedStatement query = connection.prepareStatement("select release_lock('ebean_migration-" + hash + "')")) {
203205
query.execute();
204206
}
205207
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,18 +150,28 @@ private ScriptTransform createScriptTransform(MigrationConfig config) {
150150
* </p>
151151
*/
152152
public void createIfNeededAndLock() throws SQLException, IOException {
153+
SQLException sqlEx = null;
153154
if (!tableExists()) {
154155
try {
155156
createTable();
156157
} catch (SQLException e) {
157158
if (tableExists()) {
158-
log.info("Ignoring error during table creation, as an other process may have created the table");
159+
sqlEx = e;
160+
log.info("Ignoring error during table creation, as an other process may have created the table", e);
159161
} else {
160162
throw e;
161163
}
162164
}
163165
}
164-
obtainLockWithWait();
166+
try {
167+
obtainLockWithWait();
168+
} catch (RuntimeException re) {
169+
// catch "failed to obtain row locks"
170+
if (sqlEx != null) {
171+
re.addSuppressed(sqlEx);
172+
}
173+
throw re;
174+
}
165175
readExistingMigrations();
166176
}
167177

ebean-migration/src/test/java/io/ebean/migration/MigrationTableAsyncTest.java

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,17 @@
44
import io.ebean.datasource.DataSourcePool;
55
import io.ebean.datasource.DataSourceFactory;
66
import io.ebean.docker.commands.*;
7+
import io.ebean.migration.runner.MigrationPlatform;
8+
import io.ebean.migration.runner.MigrationTable;
9+
710
import org.junit.jupiter.api.AfterEach;
811
import org.junit.jupiter.api.BeforeEach;
912
import org.junit.jupiter.api.Disabled;
1013
import org.junit.jupiter.api.Test;
1114

1215
import static org.assertj.core.api.Assertions.assertThat;
1316

17+
import java.io.IOException;
1418
import java.sql.Connection;
1519
import java.sql.SQLException;
1620
import java.sql.Statement;
@@ -38,15 +42,25 @@ public void shutdown() {
3842
dataSource.shutdown();
3943
}
4044

41-
@Disabled
45+
//@Disabled
4246
@Test
4347
public void testDb2() throws Exception {
4448
// Works
49+
Db2Config conf = new Db2Config("latest");
50+
conf.setPort(50055);
51+
conf.setContainerName("mig_async_db2");
52+
conf.setUser("test_ebean");
53+
conf.setPassword("test");
54+
55+
Db2Container container = new Db2Container(conf);
56+
container.startWithCreate();
57+
4558
config.setMigrationPath("dbmig");
46-
config.setDbUsername("unit");
59+
config.setDbUsername("test_ebean");
4760
config.setDbPassword("test");
48-
config.setDbUrl("jdbc:db2://localhost:50000/unit:currentSchema=ASYNC;");
49-
runTest();
61+
config.setDbUrl(container.jdbcUrl());
62+
runTest(false);
63+
runTest(true);
5064
}
5165

5266
@Disabled
@@ -66,7 +80,8 @@ public void testOracle() throws Exception {
6680
config.setDbUsername("test_ebean");
6781
config.setDbPassword("test");
6882
config.setDbUrl(container.jdbcUrl());
69-
runTest();
83+
runTest(false);
84+
runTest(true);
7085
}
7186

7287
@Test
@@ -85,7 +100,8 @@ public void testMySqlDb() throws Exception {
85100
config.setDbUsername("test_ebean");
86101
config.setDbPassword("test");
87102
config.setDbUrl("jdbc:mysql://localhost:14307/test_ebean");
88-
runTest();
103+
runTest(false);
104+
runTest(true);
89105
}
90106

91107
@Test
@@ -104,7 +120,8 @@ public void testMariaDb() throws Exception {
104120
config.setDbUsername("test_ebean");
105121
config.setDbPassword("test");
106122
config.setDbUrl("jdbc:mariadb://localhost:14308/test_ebean");
107-
runTest();
123+
runTest(false);
124+
runTest(true);
108125
}
109126

110127
//@Disabled
@@ -124,7 +141,8 @@ public void testSqlServer() throws Exception {
124141
config.setDbUsername("test_ebean");
125142
config.setDbPassword("SqlS3rv#r");
126143
config.setDbUrl("jdbc:sqlserver://localhost:9435;databaseName=test_ebean;sendTimeAsDateTime=false");
127-
runTest();
144+
runTest(true);
145+
runTest(false);
128146
}
129147

130148

@@ -138,10 +156,11 @@ public void testH2() throws Exception {
138156
config.setDbUsername("sa");
139157
config.setDbPassword("");
140158
config.setDbUrl("jdbc:h2:mem:dbAsync;LOCK_TIMEOUT=100000");
141-
runTest();
159+
runTest(false);
160+
runTest(true);
142161
}
143162

144-
private void runTest() throws SQLException, InterruptedException, ExecutionException {
163+
private void runTest(boolean withExisting) throws SQLException, InterruptedException, ExecutionException, IOException {
145164
DataSourceConfig dataSourceConfig = new DataSourceConfig();
146165
dataSourceConfig.setUrl(config.getDbUrl());
147166
dataSourceConfig.setUsername(config.getDbUsername());
@@ -151,6 +170,21 @@ private void runTest() throws SQLException, InterruptedException, ExecutionExcep
151170
dropTable("m1");
152171
dropTable("m2");
153172
dropTable("m3");
173+
174+
if (withExisting) {
175+
// create empty migration table
176+
try (Connection conn = dataSource.getConnection()) {
177+
String derivedPlatformName = DbNameUtil.normalise(conn);
178+
179+
config.setPlatform(derivedPlatformName);
180+
MigrationPlatform platform = DbNameUtil.platform(derivedPlatformName);
181+
MigrationTable table = new MigrationTable(config, conn, false, platform);
182+
table.createIfNeededAndLock();
183+
table.unlockMigrationTable();
184+
conn.commit();
185+
}
186+
}
187+
154188
ExecutorService exec = Executors.newFixedThreadPool(8);
155189
List<Future<String>> futures = new ArrayList<>();
156190
for (int i = 0; i < 2; i++) {
@@ -176,6 +210,7 @@ private void dropTable(String tableName) throws SQLException {
176210
} else if (dbProductName.contains("oracle")) {
177211
// do nothing, re-created via container.startWithDropCreate();
178212
} else {
213+
stmt.execute("drop view if exists " + tableName + "_vw");
179214
stmt.execute("drop table if exists " + tableName);
180215
}
181216
conn.commit();

ebean-migration/src/test/java/io/ebean/migration/MigrationTableTest.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
import io.ebean.datasource.DataSourceConfig;
66
import io.ebean.datasource.DataSourcePool;
77
import io.ebean.datasource.DataSourceFactory;
8-
import org.junit.jupiter.api.AfterAll;
98
import org.junit.jupiter.api.AfterEach;
10-
import org.junit.jupiter.api.BeforeAll;
119
import org.junit.jupiter.api.BeforeEach;
1210
import org.junit.jupiter.api.Test;
1311

@@ -65,6 +63,7 @@ public void testMigrationTableBase() throws Exception {
6563
}
6664
}
6765
assertThat(rawVersions).containsExactly("0", "hello", "1.1", "1.2", "1.2.1", "m2_view");
66+
table.unlockMigrationTable();
6867
conn.rollback();
6968
}
7069
}
@@ -81,6 +80,7 @@ public void testMigrationTableRepeatableOk() throws Exception {
8180
MigrationTable table = new MigrationTable(config, conn, false, platform);
8281
table.createIfNeededAndLock();
8382
assertThat(table.getVersions()).containsExactly("1.1");
83+
table.unlockMigrationTable();
8484
conn.rollback();
8585
}
8686

@@ -93,6 +93,7 @@ public void testMigrationTableRepeatableOk() throws Exception {
9393
MigrationTable table = new MigrationTable(config, conn, false, platform);
9494
table.createIfNeededAndLock();
9595
assertThat(table.getVersions()).containsExactly("1.1", "1.2", "m2_view");
96+
table.unlockMigrationTable();
9697
conn.rollback();
9798
}
9899
}
@@ -110,6 +111,7 @@ public void testMigrationTableRepeatableFail() throws Exception {
110111
MigrationTable table = new MigrationTable(config, conn, false, platform);
111112
table.createIfNeededAndLock();
112113
assertThat(table.getVersions()).containsExactly("1.1");
114+
table.unlockMigrationTable();
113115
conn.rollback();
114116
}
115117

ebean-migration/src/test/java/io/ebean/migration/MigrationTableTestDb2.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,12 @@
55
import io.ebean.datasource.DataSourceConfig;
66
import io.ebean.datasource.DataSourcePool;
77
import io.ebean.datasource.DataSourceFactory;
8-
import org.junit.jupiter.api.AfterAll;
98
import org.junit.jupiter.api.AfterEach;
10-
import org.junit.jupiter.api.BeforeAll;
119
import org.junit.jupiter.api.BeforeEach;
1210
import org.junit.jupiter.api.Disabled;
1311
import org.junit.jupiter.api.Test;
1412

1513
import java.sql.Connection;
16-
import java.sql.PreparedStatement;
17-
import java.sql.ResultSet;
18-
import java.util.ArrayList;
19-
import java.util.List;
20-
21-
import static org.assertj.core.api.Assertions.assertThat;
2214

2315
public class MigrationTableTestDb2 {
2416

@@ -55,7 +47,9 @@ public void testMigrationTableBase() throws Exception {
5547
try (Connection conn = dataSource.getConnection()) {
5648
MigrationTable table = new MigrationTable(config, conn, false, platform);
5749
table.createIfNeededAndLock();
50+
table.unlockMigrationTable();
5851
table.createIfNeededAndLock();
52+
table.unlockMigrationTable();
5953
conn.commit();
6054
}
6155
}

0 commit comments

Comments
 (0)