#2283 - ddl-generator - Refactor tidy whitespace etc

This commit is contained in:
rbygrave
2021-08-02 20:38:05 +12:00
parent 928b90122f
commit 71223895ac
15 changed files with 22 additions and 146 deletions
@@ -16,7 +16,6 @@ import java.util.ServiceLoader;
* <p>
* Typically this is run as a main method in src/test once a developer is happy
* with the next set of changes to the model.
* </p>
*
* <h3>Example: Run for a single specific platform</h3>
*
@@ -39,11 +38,9 @@ import java.util.ServiceLoader;
* are no longer being used by the application. These changes are called
* "pending drops" and we must explicitly specify to include these in
* a generated migration.
* </p>
* <p>
* Use <code>setGeneratePendingDrop()</code> to specify a prior migration
* that has drop column changes that we want to generate a migration for.
* </p>
*
* <h3>Example: Generate for pending drops</h3>
*
@@ -67,7 +64,6 @@ public interface DbMigration {
* Create a DbMigration implementation to use.
*/
static DbMigration create() {
Iterator<DbMigration> loader = ServiceLoader.load(DbMigration.class).iterator();
if (loader.hasNext()) {
return loader.next();
@@ -76,7 +72,7 @@ public interface DbMigration {
}
/**
* Set to false to suppress logging to System out.
* Set logging to System out (defaults to true).
*/
void setLogToSystemOut(boolean logToSystemOut);
@@ -105,13 +101,12 @@ public interface DbMigration {
void setMigrationPath(String migrationPath);
/**
* Set the server to use to determine the current model.
* Typically this is not called explicitly.
* Set the server to use to determine the current model. Usually this is not called explicitly.
*/
void setServer(Database database);
/**
* Set the DatabaseConfig to use. Typically this is not called explicitly.
* Set the DatabaseConfig to use. Usually this is not called explicitly.
*/
void setServerConfig(DatabaseConfig config);
@@ -119,7 +114,6 @@ public interface DbMigration {
* Set the specific platform to generate DDL for.
* <p>
* If not set this defaults to the platform of the default database.
* </p>
*/
void setPlatform(Platform platform);
@@ -127,17 +121,15 @@ public interface DbMigration {
* Set the specific platform to generate DDL for.
* <p>
* If not set this defaults to the platform of the default database.
* </p>
*/
void setPlatform(DatabasePlatform databasePlatform);
/**
* Set to false to turn off strict mode.
* Set to false in order to turn off strict mode.
* <p>
* Strict mode checks that a column changed to non-null on an existing table via DB migration has a default
* value specified. Set this to false if that isn't the case but it is known that all the existing rows have
* a value specified (there are no existing null values for the column).
* </p>
*/
void setStrictMode(boolean strictMode);
@@ -182,7 +174,6 @@ public interface DbMigration {
* Set to true if ALTER TABLE ADD FOREIGN KEY should be generated with an option to skip validation.
* <p>
* Currently this is only useful for Postgres DDL adding the <code>NOT VALID</code> option.
* </p>
*/
void setAddForeignKeySkipCheck(boolean addForeignKeySkipCheck);
@@ -191,7 +182,6 @@ public interface DbMigration {
* <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);
@@ -200,7 +190,6 @@ public interface DbMigration {
* <p>
* Use this when you want to generate sql scripts for multiple database platforms
* from the migration (e.g. generate migration sql for MySql, Postgres and Oracle).
* </p>
*/
void addPlatform(Platform platform);
@@ -29,10 +29,9 @@ class LastMigration {
}
/**
* Return the last migation version given the migration directory.
* Return the last migration version given the migration directory.
*/
static String lastVersion(File migDirectory, File modelDir) {
List<MigrationVersion> versions = new ArrayList<>();
File[] sqlFiles = migDirectory.listFiles(pathname -> includeSqlFile(pathname.getName().toLowerCase()));
@@ -4,7 +4,6 @@ package io.ebeaninternal.dbmigration;
* Exception when db migration resource path does not exist.
* <p>
* Typically the working directory or pathToResources is incorrect.
* </p>
*/
public class UnknownResourcePathException extends RuntimeException {
@@ -27,25 +27,17 @@ import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
public class CurrentModel {
private final SpiEbeanServer server;
private final DatabasePlatform databasePlatform;
private final DbConstraintNaming constraintNaming;
private final boolean platformTypes;
private final boolean jaxbPresent;
private final String ddlHeader;
private final DdlOptions ddlOptions = new DdlOptions();
private ModelContainer model;
private ChangeSet changeSet;
private DdlWrite write;
private DdlOptions ddlOptions = new DdlOptions();
/**
* Construct with a given EbeanServer instance for DDL create all generation, not migration.
*/
@@ -20,19 +20,15 @@ import static io.ebeaninternal.dbmigration.ddlgeneration.platform.SplitColumns.s
public class MCompoundUniqueConstraint {
private final String name;
/**
* Flag if true indicates this was specifically created for a OneToOne mapping.
*/
private final boolean oneToOne;
/**
* The columns combined to be unique.
*/
private final String[] columns;
private final String platforms;
private String[] nullableColumns;
/**
@@ -4,7 +4,7 @@ import io.ebeaninternal.dbmigration.migration.Configuration;
import io.ebeaninternal.dbmigration.migration.DefaultTablespace;
/**
* Holds configuration such as the default tablespaces to use for tables,
* Holds configuration such as the default tablespace to use for tables,
* indexes, history tables etc.
*/
public class MConfiguration {
@@ -32,7 +32,6 @@ public class MConfiguration {
* </p>
*/
public void apply(Configuration configuration) {
DefaultTablespace defaultTablespace = configuration.getDefaultTablespace();
if (defaultTablespace != null) {
String tables = defaultTablespace.getTables();
@@ -13,8 +13,8 @@ import java.util.Objects;
*/
public class MIndex {
private String tableName;
private String indexName;
private final String tableName;
private final String indexName;
private String platforms;
private List<String> columns = new ArrayList<>();
private boolean unique;
@@ -48,67 +48,32 @@ public class MTable {
private static final Logger logger = LoggerFactory.getLogger(MTable.class);
/**
* Table name.
*/
private final String name;
/**
* The associated draft table.
*/
private MTable draftTable;
/**
* Marked true for draft tables. These need to have their FK references adjusted
* after all the draft tables have been identified.
*/
private boolean draft;
private PartitionMeta partitionMeta;
/**
* Primary key name.
*/
private String pkName;
/**
* Table comment.
*/
private String comment;
/**
* Tablespace to use.
*/
private String tablespace;
private String storageEngine;
/**
* Tablespace to use for indexes on this table.
*/
private String indexTablespace;
private IdentityMode identityMode;
/**
* If set to true this table should has history support.
*/
private boolean withHistory;
/**
* The columns on the table.
*/
private Map<String, MColumn> columns = new LinkedHashMap<>();
private final Map<String, MColumn> columns = new LinkedHashMap<>();
/**
* Compound unique constraints.
*/
private List<MCompoundUniqueConstraint> uniqueConstraints = new ArrayList<>();
private final List<MCompoundUniqueConstraint> uniqueConstraints = new ArrayList<>();
/**
* Compound foreign keys.
*/
private List<MCompoundForeignKey> compoundKeys = new ArrayList<>();
private final List<MCompoundForeignKey> compoundKeys = new ArrayList<>();
/**
* Column name for the 'When created' column. This can be used for the initial effective start date when adding
@@ -121,7 +86,7 @@ public class MTable {
*/
private AddColumn addColumn;
private List<String> droppedColumns = new ArrayList<>();
private final List<String> droppedColumns = new ArrayList<>();
public MTable(BeanDescriptor<?> descriptor) {
this.name = descriptor.getBaseTable();
@@ -153,18 +118,15 @@ public class MTable {
* later when creating the CreateTable object.
*/
public MTable createDraftTable() {
draftTable = new MTable(name + "_draft");
draftTable.draft = true;
draftTable.whenCreatedColumn = whenCreatedColumn;
// compoundKeys
// compoundUniqueConstraints
draftTable.identityMode = identityMode;
for (MColumn col : allColumns()) {
draftTable.addColumn(col.copyForDraft());
}
return draftTable;
}
@@ -239,7 +201,6 @@ public class MTable {
* Return the CreateTable migration for this table.
*/
public CreateTable createTable() {
CreateTable createTable = new CreateTable();
createTable.setName(name);
createTable.setPkName(pkName);
@@ -258,22 +219,18 @@ public class MTable {
if (draft) {
createTable.setDraft(Boolean.TRUE);
}
for (MColumn column : allColumns()) {
// filter out draftOnly columns from the base table
if (draft || !column.isDraftOnly()) {
createTable.getColumn().add(column.createColumn());
}
}
for (MCompoundForeignKey compoundKey : compoundKeys) {
createTable.getForeignKey().add(compoundKey.createForeignKey());
}
for (MCompoundUniqueConstraint constraint : uniqueConstraints) {
createTable.getUniqueConstraint().add(constraint.getUniqueConstraint());
}
return createTable;
}
@@ -281,7 +238,6 @@ public class MTable {
* Compare to another version of the same table to perform a diff.
*/
public void compare(ModelDiff modelDiff, MTable newTable) {
if (withHistory != newTable.withHistory) {
if (withHistory) {
DropHistoryTable dropHistoryTable = new DropHistoryTable();
@@ -308,14 +264,12 @@ public class MTable {
modelDiff.addTableComment(addTableComment);
}
compareCompoundKeys(modelDiff, newTable);
compareUniqueKeys(modelDiff, newTable);
}
private void compareColumns(ModelDiff modelDiff, MTable newTable) {
addColumn = null;
Map<String, MColumn> newColumnMap = newTable.getColumns();
// compare newColumns to existing columns (look for new and diff columns)
@@ -374,10 +328,10 @@ public class MTable {
currentKeys.removeAll(newTable.getUniqueConstraints());
newKeys.removeAll(getUniqueConstraints());
for (MCompoundUniqueConstraint currentKey: currentKeys) {
for (MCompoundUniqueConstraint currentKey : currentKeys) {
modelDiff.addUniqueConstraint(currentKey.dropUniqueConstraint(name));
}
for (MCompoundUniqueConstraint newKey: newKeys) {
for (MCompoundUniqueConstraint newKey : newKeys) {
modelDiff.addUniqueConstraint(newKey.addUniqueConstraint(name));
}
}
@@ -489,7 +443,6 @@ public class MTable {
}
public List<String> allHistoryColumns(boolean includeDropped) {
List<String> columnNames = new ArrayList<>(columns.size());
for (MColumn column : columns.values()) {
if (column.isIncludeInHistory()) {
@@ -595,7 +548,6 @@ public class MTable {
* Sometimes the case for a primaryKey that is also a foreign key.
*/
public MColumn addColumn(String dbCol, String columnDefn, boolean notnull) {
MColumn existingColumn = getColumn(dbCol);
if (existingColumn != null) {
if (notnull) {
@@ -613,7 +565,6 @@ public class MTable {
* Add a 'new column' to the AddColumn migration object.
*/
private void diffNewColumn(MColumn newColumn) {
if (addColumn == null) {
addColumn = new AddColumn();
addColumn.setTableName(name);
@@ -631,7 +582,6 @@ public class MTable {
* Add a 'drop column' to the diff.
*/
private void diffDropColumn(ModelDiff modelDiff, MColumn existingColumn) {
DropColumn dropColumn = new DropColumn();
dropColumn.setTableName(name);
dropColumn.setColumnName(existingColumn.getName());
@@ -640,7 +590,6 @@ public class MTable {
// table as well as the base table
dropColumn.setWithHistory(Boolean.TRUE);
}
modelDiff.addDropColumn(dropColumn);
}
@@ -661,7 +610,6 @@ public class MTable {
* </p>
*/
public void checkDuplicateForeignKeys() {
if (hasDuplicateForeignKeys()) {
int counter = 1;
for (MCompoundForeignKey fk : compoundKeys) {
@@ -687,7 +635,6 @@ public class MTable {
* Adjust the references (FK) if it should relate to a draft table.
*/
public void adjustReferences(ModelContainer modelContainer) {
Collection<MColumn> cols = allColumns();
for (MColumn col : cols) {
String references = col.getReferences();
@@ -17,7 +17,6 @@ public class MTableIdentity {
* Return the IdentityMode from CreateTable.
*/
public static IdentityMode fromCreateTable(CreateTable createTable) {
IdType type = fromType(createTable.getIdentityType());
IdentityGenerated generated = fromGenerated(createTable.getIdentityGenerated());
int start = toInt(createTable.getIdentityStart(), createTable.getSequenceInitial());
@@ -39,7 +38,6 @@ public class MTableIdentity {
* Set the IdentityMode to the CreateTable model.
*/
public static void toCreateTable(IdentityMode identityMode, CreateTable createTable) {
if (!identityMode.isPlatformDefault()) {
createTable.setIdentityType(toType(identityMode.getIdType()));
}
@@ -18,13 +18,9 @@ public class MigrationModel {
private static final Logger logger = LoggerFactory.getLogger(MigrationModel.class);
private final ModelContainer model = new ModelContainer();
private final File modelDirectory;
private final String modelSuffix;
private MigrationVersion lastVersion;
public MigrationModel(File modelDirectory, String modelSuffix) {
this.modelDirectory = modelDirectory;
this.modelSuffix = modelSuffix;
@@ -58,14 +54,9 @@ public class MigrationModel {
if (!initMigration) {
for (MigrationResource migrationResource : resources) {
logger.debug("read {}", migrationResource);
model.apply(migrationResource.read(), migrationResource.getVersion());
model.apply(migrationResource.read(), migrationResource.version());
}
}
// remember the last version
if (!resources.isEmpty()) {
lastVersion = resources.get(resources.size() - 1).getVersion();
}
}
private MigrationVersion createVersion(File xmlFile) {
@@ -73,9 +64,4 @@ public class MigrationModel {
String versionName = fileName.substring(0, fileName.length() - modelSuffix.length());
return MigrationVersion.parse(versionName);
}
public String getNextVersion(String initialVersion) {
return lastVersion == null ? initialVersion : lastVersion.nextVersion();
}
}
@@ -31,7 +31,7 @@ public class MigrationResource implements Comparable<MigrationResource> {
/**
* Return the version associated with this resource.
*/
public MigrationVersion getVersion() {
public MigrationVersion version() {
return version;
}
@@ -39,7 +39,6 @@ public class MigrationResource implements Comparable<MigrationResource> {
* Read and return the migration from the resource.
*/
public Migration read() {
return MigrationXmlReader.read(migrationFile);
}
@@ -37,19 +37,16 @@ import java.util.TreeSet;
public class ModelContainer {
private final Set<String> schemas = new TreeSet<>();
/**
* All the tables in the model.
*/
private final Map<String, MTable> tables = new LinkedHashMap<>();
/**
* All the non unique non foreign key indexes.
* All the non-unique non-foreign key indexes.
*/
private final Map<String, MIndex> indexes = new LinkedHashMap<>();
private final PendingDrops pendingDrops = new PendingDrops();
private final List<MTable> partitionedTables = new ArrayList<>();
public ModelContainer() {
@@ -53,29 +53,24 @@ public class ModelDiff {
this.baseModel = new ModelContainer();
}
/**
* Return true if the apply and drop changes are both empty.
* This means there are no migration changes.
* Return true if apply and drop changes are both empty. This means there are no migration changes.
*/
public boolean isEmpty() {
return applyChanges.isEmpty() && dropChanges.isEmpty();
}
/**
* Return the diff as a migration potentially containing
* an apply changeSet and a drop changeSet.
* Return the diff as a migration potentially containing an apply changeSet and a drop changeSet.
*/
public Migration getMigration() {
Migration migration = new Migration();
if (!applyChanges.isEmpty()) {
// add a non empty apply changeSet
// add a non-empty apply changeSet
migration.getChangeSet().add(getApplyChangeSet());
}
if (!dropChanges.isEmpty()) {
// add a non empty drop changeSet
// add a non-empty drop changeSet
migration.getChangeSet().add(getDropChangeSet());
}
return migration;
@@ -121,7 +116,6 @@ public class ModelDiff {
* Compare to a 'newer' model and collect the differences.
*/
public void compareTo(ModelContainer newModel) {
Map<String, MTable> newTables = newModel.getTables();
for (MTable newTable : newTables.values()) {
@@ -179,7 +173,6 @@ public class ModelDiff {
* Compare tables looking for add/drop/modify columns etc.
*/
protected void compareTables(MTable currentTable, MTable newTable) {
currentTable.compare(this, newTable);
}
@@ -187,7 +180,6 @@ public class ModelDiff {
* Compare tables looking for add/drop/modify columns etc.
*/
protected void compareIndexes(MIndex currentIndex, MIndex newIndex) {
currentIndex.compare(this, newIndex);
}
@@ -24,7 +24,6 @@ public class PendingDrops {
* Add a 'pending drops' changeSet for the given version.
*/
public void add(MigrationVersion version, ChangeSet changeSet) {
Entry entry = map.computeIfAbsent(version.normalised(), k -> new Entry(version));
entry.add(changeSet);
}
@@ -33,7 +32,6 @@ public class PendingDrops {
* Return the list of versions with pending drops.
*/
public List<String> pendingDrops() {
List<String> versions = new ArrayList<>();
for (Entry value : map.values()) {
if (value.hasPendingDrops()) {
@@ -48,7 +46,6 @@ public class PendingDrops {
* to remove the (unsuppressed) pending drops for this version.
*/
public boolean appliedDropsFor(ChangeSet changeSet) {
MigrationVersion version = MigrationVersion.parse(changeSet.getDropsFor());
Entry entry = map.get(version.normalised());
@@ -68,7 +65,6 @@ public class PendingDrops {
* </p>
*/
public Migration migrationForVersion(String pendingVersion) {
Entry entry = getEntry(pendingVersion);
Migration migration = new Migration();
@@ -96,7 +92,6 @@ public class PendingDrops {
}
private Entry getEntry(String pendingVersion) {
if ("next".equalsIgnoreCase(pendingVersion)) {
Iterator<Entry> it = map.values().iterator();
if (it.hasNext()) {
@@ -115,7 +110,6 @@ public class PendingDrops {
* Register pending drop columns on history tables to the new model.
*/
public void registerPendingHistoryDropColumns(ModelContainer newModel) {
for (Entry entry : map.values()) {
for (ChangeSet changeSet : entry.list) {
newModel.registerPendingHistoryDropColumns(changeSet);
@@ -140,7 +134,6 @@ public class PendingDrops {
static class Entry {
final MigrationVersion version;
final List<ChangeSet> list = new ArrayList<>();
Entry(MigrationVersion version) {
@@ -180,7 +173,6 @@ public class PendingDrops {
* removed all the changeSets (and there are no suppressForever ones).
*/
boolean removeDrops(ChangeSet appliedDrops) {
Iterator<ChangeSet> iterator = list.iterator();
while (iterator.hasNext()) {
ChangeSet next = iterator.next();
@@ -199,20 +191,16 @@ public class PendingDrops {
* Remove the applied drops from the pending ones matching by table name and column name.
*/
private void removeMatchingChanges(ChangeSet pendingDrops, ChangeSet appliedDrops) {
List<Object> pending = pendingDrops.getChangeSetChildren();
Iterator<Object> iterator = pending.iterator();
while (iterator.hasNext()) {
Object pendingDrop = iterator.next();
if (pendingDrop instanceof DropColumn && dropColumnIn((DropColumn) pendingDrop, appliedDrops)) {
iterator.remove();
} else if (pendingDrop instanceof DropTable && dropTableIn((DropTable) pendingDrop, appliedDrops)) {
iterator.remove();
} else if (pendingDrop instanceof DropHistoryTable && dropHistoryTableIn((DropHistoryTable) pendingDrop, appliedDrops)) {
iterator.remove();
}
}
}
@@ -27,9 +27,7 @@ public class PlatformDdlWriter {
private static final Logger logger = LoggerFactory.getLogger(PlatformDdlWriter.class);
private final DatabaseConfig databaseConfig;
private final PlatformDdl platformDdl;
private final int lockTimeoutSeconds;
public PlatformDdlWriter(DatabasePlatform platform, DatabaseConfig dbConfig, int lockTimeoutSeconds) {
@@ -42,7 +40,6 @@ public class PlatformDdlWriter {
* Write the migration as platform specific ddl.
*/
public void processMigration(Migration dbMigration, DdlWrite write, File writePath, String fullVersion) throws IOException {
DdlHandler handler = handler();
handler.generateProlog(write);
if (lockTimeoutSeconds > 0) {
@@ -51,7 +48,6 @@ public class PlatformDdlWriter {
write.apply().append(lockSql).endOfStatement().newLine();
}
}
List<ChangeSet> changeSets = dbMigration.getChangeSet();
for (ChangeSet changeSet : changeSets) {
if (isApply(changeSet)) {
@@ -59,7 +55,6 @@ public class PlatformDdlWriter {
}
}
handler.generateEpilog(write);
writePlatformDdl(write, writePath, fullVersion);
}