Skip to content

Commit

Permalink
DbMigration update - constraints up front
Browse files Browse the repository at this point in the history
  • Loading branch information
rbygrave committed Aug 10, 2015
1 parent 845faf7 commit 851f40e
Show file tree
Hide file tree
Showing 72 changed files with 4,015 additions and 3,317 deletions.
1,014 changes: 507 additions & 507 deletions dbmigration-test/resources/dbmigration/myapp/migration-current.xml

Large diffs are not rendered by default.

694 changes: 347 additions & 347 deletions h2-migration.xml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion h2autocommit-migration.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet>
<createTable name="ut_detail">
<createTable name="ut_detail" pkName="pk_ut_detail">
<column name="id" type="integer" primaryKey="true" identity="true"/>
<column name="name" type="varchar(255)"/>
<column name="qty" type="integer"/>
Expand Down
2 changes: 1 addition & 1 deletion h2ebasicver-migration.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet>
<createTable name="e_basicver">
<createTable name="e_basicver" pkName="pk_e_basicver">
<column name="id" type="integer" primaryKey="true" identity="true"/>
<column name="name" type="varchar(255)"/>
<column name="description" type="varchar(255)"/>
Expand Down
4 changes: 2 additions & 2 deletions h2other-migration.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<migration xmlns="http://ebean-orm.github.io/xml/ns/dbmigration">
<changeSet>
<createTable name="e_basic">
<createTable name="e_basic" pkName="pk_e_basic">
<column name="id" type="integer" primaryKey="true" identity="true"/>
<column name="status" type="varchar(1)" checkConstraint="check (status in ('N','A','I'))"/>
<column name="status" type="varchar(1)" checkConstraint="check (status in ('N','A','I'))" checkConstraintName="ck_e_basic_status"/>
<column name="name" type="varchar(255)"/>
<column name="description" type="varchar(255)"/>
<column name="some_date" type="timestamp"/>
Expand Down
1,596 changes: 798 additions & 798 deletions mysql-migration.xml

Large diffs are not rendered by default.

1,596 changes: 798 additions & 798 deletions pg-migration.xml

Large diffs are not rendered by default.

42 changes: 25 additions & 17 deletions src/main/java/com/avaje/ebean/config/AbstractNamingConvention.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,50 @@
package com.avaje.ebean.config;

import com.avaje.ebean.config.dbplatform.DatabasePlatform;

import javax.persistence.Inheritance;
import javax.persistence.Table;

import com.avaje.ebean.config.dbplatform.DatabasePlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Provides some base implementation for NamingConventions.
*
* @author emcgreal
*/
public abstract class AbstractNamingConvention implements NamingConvention {

/** The Constant logger. */
private static final Logger logger = LoggerFactory.getLogger(AbstractNamingConvention.class);

/** The Constant DEFAULT_SEQ_FORMAT. */
/**
* The Constant DEFAULT_SEQ_FORMAT.
*/
public static final String DEFAULT_SEQ_FORMAT = "{table}_seq";

/** Sequence Format that includes the Primary Key column */
/**
* Sequence Format that includes the Primary Key column
*/
public static final String TABLE_PKCOLUMN_SEQ_FORMAT = "{table}_{column}_seq";

/** The catalog. */
/**
* The catalog.
*/
private String catalog;

/** The schema. */
/**
* The schema.
*/
private String schema;

/** The sequence format. */
/**
* The sequence format.
*/
private String sequenceFormat;

/** The database platform. */
/**
* The database platform.
*/
protected DatabasePlatform databasePlatform;

/** Used to trim off extra prefix for M2M. */
/**
* Used to trim off extra prefix for M2M.
*/
protected int rhsPrefixLength = 3;

protected boolean useForeignKeyPrefix;
Expand All @@ -59,8 +68,7 @@ public AbstractNamingConvention(String sequenceFormat) {
}

/**
* Construct with the default sequence format ("{table}_seq") and
* useForeignKeyPrefix as true.
* Construct with the default sequence format ("{table}_seq") and useForeignKeyPrefix as true.
*/
public AbstractNamingConvention() {
this(DEFAULT_SEQ_FORMAT);
Expand Down Expand Up @@ -233,7 +241,7 @@ protected TableName getTableNameFromAnnotation(Class<?> beanClass) {
if (t != null && !isEmpty(t.name())) {
// Note: empty catalog and schema are converted to null
// Only need to convert quoted identifiers from annotations
return new TableName(quoteIdentifiers(t.catalog()), quoteIdentifiers(t.schema()), quoteIdentifiers(t.name()));
return new TableName(quoteIdentifiers(t.catalog()), quoteIdentifiers(t.schema()), quoteIdentifiers(t.name()));
}

// No annotation
Expand Down
136 changes: 136 additions & 0 deletions src/main/java/com/avaje/ebean/config/DbConstraintNaming.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.avaje.ebean.config;

/**
* Naming convention used for constraint names.
* <p>
* Note that these constraint names are trimmed in the PlatformDdl which can be overridden
* but provides a decent default implementation.
* </p>
*/
public class DbConstraintNaming {

protected String pkPrefix = "pk_";
protected String pkSuffix = "";

protected String fkPrefix = "fk_";
protected String fkMiddle = "_";
protected String fkSuffix = "";

protected String fkIndexPrefix = "ix_";
protected String fkIndexMiddle = "_";
protected String fkIndexSuffix = "";

protected String uqPrefix = "uq_";
protected String uqSuffix = "";

protected String ckPrefix = "ck_";
protected String ckSuffix = "";

protected boolean lowerCaseNames = true;

protected DbConstraintNormalise normalise = new DbConstraintNormalise();

public DbConstraintNaming() {
}

/**
* Return the primary key constraint name.
*/
public String primaryKeyName(String tableName) {

return pkPrefix + normaliseTable(tableName) + pkSuffix;
}

/**
* Return the foreign key constraint name given a single column foreign key.
*/
public String foreignKeyConstraintName(String tableName, String columnName) {
return fkPrefix + normaliseTable(tableName) + fkMiddle + normaliseColumn(columnName) + fkSuffix;
}

/**
* Return the index name associated with a foreign key constraint given a single column foreign key.
*/
public String foreignKeyIndexName(String tableName, String[] columns) {

String colPart = joinColumnNames(columns);
return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix;
}

public String foreignKeyIndexName(String tableName, String column) {

String colPart = normaliseTable(column);
return fkIndexPrefix + normaliseTable(tableName) + fkIndexMiddle + colPart + fkIndexSuffix;
}

/**
* Join the column names together with underscores.
*/
protected String joinColumnNames(String[] columns) {

if (columns.length == 1) {
return normaliseColumn(columns[0]);
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < columns.length; i++) {
if (i > 0) {
sb.append("_");
}
sb.append(normaliseColumn(columns[i]));
}
return sb.toString();
}

/**
* Return the unique constraint name.
*/
public String uniqueConstraintName(String tableName, String columnName) {

return uqPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + uqSuffix;
}

/**
* Return the unique constraint name.
*/
public String uniqueConstraintName(String tableName, String[] columns) {

String colPart = joinColumnNames(columns);
return uqPrefix + normaliseTable(tableName) + "_" + colPart + uqSuffix;
}

/**
* Return the check constraint name.
*/
public String checkConstraintName(String tableName, String columnName) {

return ckPrefix + normaliseTable(tableName) + "_" + normaliseColumn(columnName) + ckSuffix;
}

/**
* Normalise the table name by trimming catalog and schema and removing any
* quoted identifier characters (",',[,] etc).
*/
public String normaliseTable(String tableName) {

return normalise.normaliseTable(tableName);
}

/**
* Normalise the column name by removing any quoted identifier characters (",',[,] etc).
*/
public String normaliseColumn(String tableName) {

return normalise.normaliseColumn(tableName);
}

/**
* Lower case the table or column name checking for quoted identifiers.
*/
public String lowerName(String tableName) {
if (lowerCaseNames && normalise.notQuoted(tableName)) {
return tableName.toLowerCase();
}
return tableName;
}

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,19 @@
package com.avaje.ebean.dbmigration.ddlgeneration.platform;

import com.avaje.ebean.dbmigration.ddlgeneration.platform.util.DbQuotes;
package com.avaje.ebean.config;

/**
* Used to normalise table and column names which means stripping out
* quoted identifier characters and any catalog or schema prefix.
*/
public class DdlNameNormalise {
public class DbConstraintNormalise {

protected final String[] quotedIdentifiers;

protected boolean lowerCaseTables = true;

protected boolean lowerCaseColumns = true;

protected DbQuotes quotes = new DbQuotes();

public DdlNameNormalise() {
}

public boolean notQuoted(String tableName) {
return quotes.notQuoted(tableName);
public DbConstraintNormalise() {
this.quotedIdentifiers = new String[]{"\"", "'", "[", "]", "`"};
}

/**
Expand Down Expand Up @@ -54,9 +49,31 @@ public String normaliseColumn(String columnName) {
/**
* Trim off the platform quoted identifier quotes like [ ' and ".
*/
protected String trimQuotes(String tableName) {
public boolean notQuoted(String tableName) {

// remove quoted identifier characters
for (int i = 0; i < quotedIdentifiers.length; i++) {
if (tableName.contains(quotedIdentifiers[i])) {
return false;
}
}
return true;
}

/**
* Trim off the platform quoted identifier quotes like [ ' and ".
*/
public String trimQuotes(String tableName) {

return quotes.trimQuotes(tableName);
if (tableName == null) {
return "";
}
// remove quoted identifier characters
for (int i = 0; i < quotedIdentifiers.length; i++) {
tableName = tableName.replace(quotedIdentifiers[i], "");
}
return tableName;
}


}
11 changes: 5 additions & 6 deletions src/main/java/com/avaje/ebean/config/NamingConvention.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ public interface NamingConvention {
String getPropertyFromColumn(Class<?> beanClass, String dbColumnName);

/**
* Return the sequence name given the table name (for DB's that use
* sequences).
* Return the sequence name given the table name (for DB's that use sequences).
* <p>
* Typically you might append "_seq" to the table name as an example.
* </p>
Expand All @@ -99,14 +98,14 @@ public interface NamingConvention {
* Return true if a prefix should be used building a foreign key name.
* <p>
* This by default is true and this works well when the primary key column
* names are simply "ID". In this case a prefix (such as "ORDER" and
* "CUSTOMER" etc) is added to the foreign key column producing "ORDER_ID" and
* "CUSTOMER_ID".
* names are simply "id". In this case a prefix (such as "order" and
* "customer" etc) is added to the foreign key column producing "order_id" and
* "customer_id".
* </p>
* <p>
* This should return false when your primary key columns are the same as the
* foreign key columns. For example, when the primary key columns are
* "ORDER_ID", "CUST_ID" etc ... and they are the same as the foreign key
* "order_id", "cust_id" etc ... and they are the same as the foreign key
* column names.
* </p>
*/
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/avaje/ebean/config/ServerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,11 @@ public class ServerConfig {
*/
private NamingConvention namingConvention = new UnderscoreNamingConvention();

/**
* Naming convention used in DDL generation for primary keys, foreign keys etc.
*/
private DbConstraintNaming constraintNaming = new DbConstraintNaming();

/**
* Behaviour of update to include on the change properties.
*/
Expand Down Expand Up @@ -975,6 +980,20 @@ public void setNamingConvention(NamingConvention namingConvention) {
this.namingConvention = namingConvention;
}

/**
* Return the constraint naming convention used in DDL generation.
*/
public DbConstraintNaming getConstraintNaming() {
return constraintNaming;
}

/**
* Set the constraint naming convention used in DDL generation.
*/
public void setConstraintNaming(DbConstraintNaming constraintNaming) {
this.constraintNaming = constraintNaming;
}

/**
* Return the configuration for the Autofetch feature.
*/
Expand Down
Loading

0 comments on commit 851f40e

Please sign in to comment.