#1820 - ENH: For DB Migration add setLockTimeout(10); ... to add Postgres set lock_timeout = to generated migrations

This commit is contained in:
rob bygrave
2019-09-12 20:59:52 +12:00
parent 2fb533a606
commit a2b418df8b
6 changed files with 56 additions and 2 deletions
@@ -144,6 +144,15 @@ public interface DbMigration {
*/
void setAddForeignKeySkipCheck(boolean addForeignKeySkipCheck);
/**
* Set the lock timeout to be included with the DDL generation.
* <p>
* Currently this is only useful for Postgres migrations adding a <code>set lock_timeout</code>
* statement to the generated database migration.
* </p>
*/
void setLockTimeout(int seconds);
/**
* Add an additional platform to write the migration DDL.
* <p>
@@ -108,6 +108,7 @@ public class DefaultDbMigration implements DbMigration {
protected String name;
protected String generatePendingDrop;
private boolean addForeignKeySkipCheck;
private int lockTimeoutSeconds;
protected boolean includeBuiltInPartitioning = true;
@@ -187,6 +188,11 @@ public class DefaultDbMigration implements DbMigration {
this.addForeignKeySkipCheck = addForeignKeySkipCheck;
}
@Override
public void setLockTimeout(int seconds) {
this.lockTimeoutSeconds = seconds;
}
@Override
public void setGeneratePendingDrop(String generatePendingDrop) {
this.generatePendingDrop = generatePendingDrop;
@@ -641,7 +647,7 @@ public class DefaultDbMigration implements DbMigration {
}
private PlatformDdlWriter createDdlWriter(DatabasePlatform platform) {
return new PlatformDdlWriter(platform, serverConfig, migrationConfig);
return new PlatformDdlWriter(platform, serverConfig, migrationConfig, lockTimeoutSeconds);
}
/**
@@ -211,6 +211,13 @@ public class PlatformDdl {
return inlineForeignKeys;
}
/**
* By default this does nothing returning null / no lock timeout.
*/
public String setLockTimeout(int lockTimeoutSeconds) {
return null;
}
/**
* Write all the table columns converting to platform types as necessary.
*/
@@ -20,6 +20,10 @@ public class PostgresDdl extends PlatformDdl {
this.addForeignKeySkipCheck = " not valid";
}
public String setLockTimeout(int lockTimeoutSeconds) {
return "set lock_timeout = " + (lockTimeoutSeconds * 1000);
}
@Override
public boolean suppressPrimaryKeyOnPartition() {
return true;
@@ -33,10 +33,13 @@ public class PlatformDdlWriter {
private final PlatformDdl platformDdl;
public PlatformDdlWriter(DatabasePlatform platform, ServerConfig serverConfig, DbMigrationConfig config) {
private final int lockTimeoutSeconds;
public PlatformDdlWriter(DatabasePlatform platform, ServerConfig serverConfig, DbMigrationConfig config, int lockTimeoutSeconds) {
this.platformDdl = PlatformDdlBuilder.create(platform);
this.serverConfig = serverConfig;
this.config = config;
this.lockTimeoutSeconds = lockTimeoutSeconds;
}
/**
@@ -46,6 +49,13 @@ public class PlatformDdlWriter {
DdlHandler handler = handler();
handler.generateProlog(write);
if (lockTimeoutSeconds > 0) {
String lockSql = platformDdl.setLockTimeout(lockTimeoutSeconds);
if (lockSql != null) {
write.apply().append(lockSql).endOfStatement().newLine();
}
}
List<ChangeSet> changeSets = dbMigration.getChangeSet();
for (ChangeSet changeSet : changeSets) {
if (isApply(changeSet)) {
@@ -0,0 +1,18 @@
package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebean.config.dbplatform.postgres.PostgresPlatform;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class PostgresDdlTest {
private PostgresDdl postgresDdl = new PostgresDdl(new PostgresPlatform());
@Test
public void setLockTimeout() {
final String sql = postgresDdl.setLockTimeout(5);
assertThat(sql).isEqualTo("set lock_timeout = 5000");
}
}