#2206 - Postgres DDL generation - for create index use "if not exists" clause

This commit is contained in:
Robin Bygrave
2021-03-25 16:45:44 +13:00
parent f341ff0167
commit 8992980689
4 changed files with 116 additions and 1 deletions
@@ -3,7 +3,6 @@ package io.ebeaninternal.dbmigration.ddlgeneration.platform;
import io.ebean.annotation.ConstraintMode;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.DbConstraintNaming;
import io.ebean.config.ServerConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.config.dbplatform.DbDefaultValue;
import io.ebean.config.dbplatform.DbIdentity;
@@ -113,6 +112,7 @@ public class PlatformDdl {
protected String uniqueIndex = "unique";
protected String indexConcurrent = "";
protected String createIndexIfNotExists = "";
/**
* Set false for MsSqlServer to allow multiple nulls for OneToOne mapping.
@@ -414,6 +414,9 @@ public class PlatformDdl {
if (create.isConcurrent()) {
buffer.append(indexConcurrent);
}
if (create.isNotExistsCheck()) {
buffer.append(createIndexIfNotExists);
}
buffer.append(maxConstraintName(create.getIndexName())).append(" on ").append(create.getTableName());
appendColumns(create.getColumns(), buffer);
return buffer.toString();
@@ -18,6 +18,7 @@ public class PostgresDdl extends PlatformDdl {
this.dropTableCascade = " cascade";
this.columnSetType = "type ";
this.alterTableIfExists = "if exists ";
this.createIndexIfNotExists = "if not exists ";
this.columnSetNull = "drop not null";
this.addForeignKeySkipCheck = " not valid";
this.indexConcurrent = "concurrently ";
@@ -12,7 +12,11 @@ class WriteCreateIndex {
private final boolean unique;
private final boolean concurrent;
private final String definition;
private final boolean notExistsCheck;
/**
* Create index for foreign key.
*/
WriteCreateIndex(String indexName, String tableName, String[] columns, boolean unique) {
this.indexName = indexName;
this.tableName = tableName;
@@ -20,8 +24,12 @@ class WriteCreateIndex {
this.unique = unique;
this.concurrent = false;
this.definition = null;
this.notExistsCheck = false;
}
/**
* Create non-foreign key index.
*/
public WriteCreateIndex(CreateIndex index) {
this.indexName = index.getIndexName();
this.tableName = index.getTableName();
@@ -29,6 +37,7 @@ class WriteCreateIndex {
this.unique = Boolean.TRUE.equals(index.isUnique());
this.concurrent = Boolean.TRUE.equals(index.isConcurrent());
this.definition = index.getDefinition();
this.notExistsCheck = true;
}
public String getIndexName() {
@@ -58,4 +67,8 @@ class WriteCreateIndex {
public boolean useDefinition() {
return definition != null && !definition.isEmpty();
}
public boolean isNotExistsCheck() {
return notExistsCheck;
}
}