@Test public void testExpandForRemovingDefaultValue() { testExpandForSettingDefaultValue(); AlterColumn operation = SchemaOperations.alterColumn("users", "name").dropDefaultExpression(); changelog.addChangeSet("Michael de Jong", "Set default of 'name' column to 'John Smith'.", operation); migrator.migrate(catalog, refLog, changelog.getLastAdded(), operation); Table originalTable = catalog.getTable("users"); Table ghostTable = getGhostTable(originalTable); Table expectedGhostTable = new Table(ghostTable.getName()) .addColumn(new Column("id", integer(), IDENTITY, NOT_NULL, AUTO_INCREMENT)) .addColumn(new Column("name", varchar(255), NOT_NULL)); assertEquals(expectedGhostTable, ghostTable); }
@Override public void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); Column column = table.getColumn(operation.getColumnName()); operation.getNewColumnName().ifPresent(columnName -> { tableRef.renameColumn(operation.getColumnName(), columnName); column.rename(columnName); List<Index> indexes = Lists.newArrayList(table.getIndexes()); for (Index index : indexes) { table.removeIndex(index.getColumns()); List<String> columns = index.getColumns().stream() .map(this::normalize) .map(refColumn -> { if (refColumn.equals(operation.getColumnName())) { return columnName; } return refColumn; }) .collect(Collectors.toList()); table.addIndex(new Index(columns, index.isUnique())); } }); operation.getHintsToDrop().stream().forEach(column::dropHint); operation.getHintsToAdd().stream().forEach(column::addHint); operation.getNewColumnType().ifPresent(column::modifyType); Optional<String> newDefaultValueExpression = operation.getNewDefaultValueExpression(); if (newDefaultValueExpression.isPresent()) { String defaultValue = newDefaultValueExpression.get(); if (Strings.isNullOrEmpty(defaultValue)) { column.dropDefaultValue(); } else { column.modifyDefaultValue(defaultValue); } } }
AlterColumnMigrator implements SchemaOperationMigrator<AlterColumn> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); Column column = table.getColumn(operation.getColumnName()); operation.getNewColumnName().ifPresent(columnName -> { tableRef.renameColumn(operation.getColumnName(), columnName); column.rename(columnName); List<Index> indexes = Lists.newArrayList(table.getIndexes()); for (Index index : indexes) { table.removeIndex(index.getColumns()); List<String> columns = index.getColumns().stream() .map(this::normalize) .map(refColumn -> { if (refColumn.equals(operation.getColumnName())) { return columnName; } return refColumn; }) .collect(Collectors.toList()); table.addIndex(new Index(columns, index.isUnique())); } }); operation.getHintsToDrop().stream().forEach(column::dropHint); operation.getHintsToAdd().stream().forEach(column::addHint); operation.getNewColumnType().ifPresent(column::modifyType); Optional<String> newDefaultValueExpression = operation.getNewDefaultValueExpression(); if (newDefaultValueExpression.isPresent()) { String defaultValue = newDefaultValueExpression.get(); if (Strings.isNullOrEmpty(defaultValue)) { column.dropDefaultValue(); } else { column.modifyDefaultValue(defaultValue); } } } }
AlterColumnMigrator implements SchemaOperationMigrator<AlterColumn> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); Column column = table.getColumn(operation.getColumnName()); operation.getNewColumnName().ifPresent(columnName -> { tableRef.renameColumn(operation.getColumnName(), columnName); column.rename(columnName); List<Index> indexes = Lists.newArrayList(table.getIndexes()); for (Index index : indexes) { table.removeIndex(index.getColumns()); List<String> columns = index.getColumns().stream() .map(this::normalize) .map(refColumn -> { if (refColumn.equals(operation.getColumnName())) { return columnName; } return refColumn; }) .collect(Collectors.toList()); table.addIndex(new Index(columns, index.isUnique())); } }); operation.getHintsToDrop().stream().forEach(column::dropHint); operation.getHintsToAdd().stream().forEach(column::addHint); operation.getNewColumnType().ifPresent(column::modifyType); Optional<String> newDefaultValueExpression = operation.getNewDefaultValueExpression(); if (newDefaultValueExpression.isPresent()) { String defaultValue = newDefaultValueExpression.get(); if (Strings.isNullOrEmpty(defaultValue)) { column.dropDefaultValue(); } else { column.modifyDefaultValue(defaultValue); } } }  }
AlterColumnMigrator implements SchemaOperationMigrator<AlterColumn> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); Column column = table.getColumn(operation.getColumnName()); operation.getNewColumnName().ifPresent(columnName -> { tableRef.renameColumn(operation.getColumnName(), columnName); column.rename(columnName); List<Index> indexes = Lists.newArrayList(table.getIndexes()); for (Index index : indexes) { table.removeIndex(index.getColumns()); List<String> columns = index.getColumns().stream() .map(this::normalize) .map(refColumn -> { if (refColumn.equals(operation.getColumnName())) { return columnName; } return refColumn; }) .collect(Collectors.toList()); table.addIndex(new Index(columns, index.isUnique())); } }); operation.getHintsToDrop().stream().forEach(column::dropHint); operation.getHintsToAdd().stream().forEach(column::addHint); operation.getNewColumnType().ifPresent(column::modifyType); Optional<String> newDefaultValueExpression = operation.getNewDefaultValueExpression(); if (newDefaultValueExpression.isPresent()) { String defaultValue = newDefaultValueExpression.get(); if (Strings.isNullOrEmpty(defaultValue)) { column.dropDefaultValue(); } else { column.modifyDefaultValue(defaultValue); } } }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation); }
AlterColumnMigrator implements SchemaOperationMigrator<AlterColumn> { @Override public void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation) { String tableName = operation.getTableName(); TransitiveTableMirrorer.mirror(catalog, refLog, version, tableName); TableRef tableRef = refLog.getTableRef(version, tableName); Table table = catalog.getTable(tableRef.getRefId()); Column column = table.getColumn(operation.getColumnName()); operation.getNewColumnName().ifPresent(columnName -> { tableRef.renameColumn(operation.getColumnName(), columnName); column.rename(columnName); List<Index> indexes = Lists.newArrayList(table.getIndexes()); for (Index index : indexes) { table.removeIndex(index.getColumns()); List<String> columns = index.getColumns().stream() .map(this::normalize) .map(refColumn -> { if (refColumn.equals(operation.getColumnName())) { return columnName; } return refColumn; }) .collect(Collectors.toList()); table.addIndex(new Index(columns, index.isUnique())); } }); operation.getHintsToDrop().stream().forEach(column::dropHint); operation.getHintsToAdd().stream().forEach(column::addHint); operation.getNewColumnType().ifPresent(column::modifyType); Optional<String> newDefaultValueExpression = operation.getNewDefaultValueExpression(); if (newDefaultValueExpression.isPresent()) { String defaultValue = newDefaultValueExpression.get(); if (Strings.isNullOrEmpty(defaultValue)) { column.dropDefaultValue(); } else { column.modifyDefaultValue(defaultValue); } } }  @Override void migrate(Catalog catalog, RefLog refLog, Version version, AlterColumn operation);  }