Validates and alters a column in a table in schema.
Alterations are made by setting properties of the column passed to modify
. If the column's nullability
changes from nullable to not nullable, all previously null values for that column
are set to the value of unencodedInitialValue
.
Example:
database.alterColumn("table", "column", (c) {
c.isIndexed = true;
c.isNullable = false;
}), unencodedInitialValue: "0");
Source
void alterColumn(String tableName, String columnName, void modify(SchemaColumn targetColumn), {String unencodedInitialValue}) { var table = schema.tableForName(tableName); if (table == null) { throw new SchemaException("Table $tableName does not exist."); } var existingColumn = table[columnName]; if (existingColumn == null) { throw new SchemaException("Column $columnName does not exist."); } var newColumn = new SchemaColumn.from(existingColumn); modify(newColumn); if (existingColumn.type != newColumn.type) { throw new SchemaException( "May not change column type for '${existingColumn.name}' in '$tableName' (${existingColumn.typeString} -> ${newColumn.typeString})"); } if (existingColumn.autoincrement != newColumn.autoincrement) { throw new SchemaException( "May not change column autoincrementing behavior for '${existingColumn.name}' in '$tableName'"); } if (existingColumn.isPrimaryKey != newColumn.isPrimaryKey) { throw new SchemaException( "May not change column primary key status for '${existingColumn.name}' in '$tableName'"); } if (existingColumn.relatedTableName != newColumn.relatedTableName) { throw new SchemaException( "May not change reference table for foreign key column '${existingColumn.name}' in '$tableName' (${existingColumn.relatedTableName} -> ${newColumn.relatedTableName})"); } if (existingColumn.relatedColumnName != newColumn.relatedColumnName) { throw new SchemaException( "May not change reference column for foreign key column '${existingColumn.name}' in '$tableName' (${existingColumn.relatedColumnName} -> ${newColumn.relatedColumnName})"); } if (existingColumn.name != newColumn.name) { renameColumn(tableName, existingColumn.name, newColumn.name); } if (existingColumn.isNullable == true && newColumn.isNullable == false && unencodedInitialValue == null && newColumn.defaultValue == null) { throw new SchemaException( "May not change column '${existingColumn.name}' in '$tableName' to be nullable without defaultValue or unencodedInitialValue."); } table.replaceColumn(existingColumn, newColumn); if (store != null) { if (existingColumn.isIndexed != newColumn.isIndexed) { if (newColumn.isIndexed) { commands.addAll(store.addIndexToColumn(table, newColumn)); } else { commands.addAll(store.deleteIndexFromColumn(table, newColumn)); } } if (existingColumn.isUnique != newColumn.isUnique) { commands.addAll(store.alterColumnUniqueness(table, newColumn)); } if (existingColumn.defaultValue != newColumn.defaultValue) { commands.addAll(store.alterColumnDefaultValue(table, newColumn)); } if (existingColumn.isNullable != newColumn.isNullable) { commands.addAll(store.alterColumnNullability( table, newColumn, unencodedInitialValue)); } if (existingColumn.deleteRule != newColumn.deleteRule) { commands.addAll(store.alterColumnDeleteRule(table, newColumn)); } } }