Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for CORE-955 #5

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,6 @@

public class ModifyDataTypeGenerator extends AbstractSqlGenerator<ModifyDataTypeStatement> {

@Override
public Warnings warn(ModifyDataTypeStatement modifyDataTypeStatement, Database database, SqlGeneratorChain sqlGeneratorChain) {
Warnings warnings = super.warn(modifyDataTypeStatement, database, sqlGeneratorChain);

if (database instanceof MySQLDatabase && !modifyDataTypeStatement.getNewDataType().toLowerCase().contains("varchar")) {
warnings.addWarning("modifyDataType will lose primary key/autoincrement/not null settings for mysql. Use <sql> and re-specify all configuration if this is the case");
}

return warnings;
}

public ValidationErrors validate(ModifyDataTypeStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {
ValidationErrors validationErrors = new ValidationErrors();
validationErrors.checkRequiredField("tableName", statement.getTableName());
Expand Down Expand Up @@ -56,7 +45,6 @@ public Sql[] generateSql(ModifyDataTypeStatement statement, Database database, S
private String getModifyString(Database database) {
if (database instanceof SybaseASADatabase
|| database instanceof SybaseDatabase
|| database instanceof MySQLDatabase
|| database instanceof OracleDatabase
|| database instanceof MaxDBDatabase
|| database instanceof InformixDatabase
Expand All @@ -78,7 +66,6 @@ private String getPreDataTypeString(Database database) {
} else if (database instanceof SybaseASADatabase
|| database instanceof SybaseDatabase
|| database instanceof MSSQLDatabase
|| database instanceof MySQLDatabase
|| database instanceof HsqlDatabase
|| database instanceof H2Database
|| database instanceof CacheDatabase
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package liquibase.sqlgenerator.core;

import java.util.HashSet;

import liquibase.database.Database;
import liquibase.database.structure.Column;
import liquibase.database.typeconversion.TypeConverterFactory;
import liquibase.diff.DiffStatusListener;
import liquibase.exception.DatabaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.snapshot.DatabaseSnapshot;
import liquibase.snapshot.DatabaseSnapshotGenerator;
import liquibase.snapshot.DatabaseSnapshotGeneratorFactory;
import liquibase.sql.Sql;
import liquibase.sql.UnparsedSql;
import liquibase.sqlgenerator.SqlGeneratorChain;
import liquibase.statement.core.ModifyDataTypeStatement;

public class ModifyDataTypeGeneratorMySQL extends ModifyDataTypeGenerator {

@Override
public int getPriority() {
return PRIORITY_DATABASE;
}

/**
* Generate MySQL-specific modify statement.
*
* @param statement Statement instance
* @param database Database instance
* @param sqlGeneratorChain SqlGeneratorChain instance
* @return SQL statement array
*/
public Sql[] generateSql(ModifyDataTypeStatement statement, Database database, SqlGeneratorChain sqlGeneratorChain) {

// Main alter statement
String alterTable = "ALTER TABLE " + database.escapeTableName(statement.getSchemaName(), statement.getTableName()) + " MODIFY ";

// Add column name
alterTable += database.escapeColumnName(statement.getSchemaName(), statement.getTableName(), statement.getColumnName()) + " ";

// Add column type
alterTable += TypeConverterFactory.getInstance().findTypeConverter(database).getDataType(statement.getNewDataType(), false);

// Get column information to avoid inadvertently removing default value, null-ability, uniqueness, etc.
DatabaseSnapshotGeneratorFactory factory = DatabaseSnapshotGeneratorFactory.getInstance();
DatabaseSnapshotGenerator generator = factory.getGenerator(database);
DatabaseSnapshot snapshot = null;
try {
snapshot = generator.createSnapshot(database, statement.getSchemaName(), new HashSet<DiffStatusListener>());
} catch (DatabaseException e) {
throw new UnexpectedLiquibaseException("Error retrieving database snapshot", e);
}

Column col = null;
try {
col = snapshot.getColumn(statement.getTableName(), statement.getColumnName());
} catch (NullPointerException e) {
throw new UnexpectedLiquibaseException("Error retrieving database snapshot", e);
}

if (!col.isNullable()) {
alterTable += " NOT NULL";
}

if (col.getDefaultValue() != null) {
alterTable += " DEFAULT " + col.getDefaultValue();
}

if (col.isAutoIncrement()) {
alterTable += " AUTO INCREMENT";
}

if (col.isUnique()) {
alterTable += " UNIQUE";
}

if (col.isPrimaryKey()) {
alterTable += " PRIMARY KEY";
}

return new Sql[]{new UnparsedSql(alterTable)};
}
}