List<String> alterColumnNullability(SchemaTable table, SchemaColumn column, String unencodedInitialValue)

Source

List<String> alterColumnNullability(SchemaTable table, SchemaColumn column, String unencodedInitialValue) {
  if (column.isNullable) {
    return ["ALTER TABLE ${table.name} ALTER COLUMN ${_columnNameForColumn(column)} DROP NOT NULL"];
  } else {
    if (unencodedInitialValue == null && column.defaultValue == null) {
      throw new SchemaException("Attempting to change column ${column
          .name} to 'not nullable', but not defaultValue or unencodedInitialValue is set for existing columns.");
    }

    if (column.defaultValue == null) {
      return [
        "UPDATE ${table.name} SET ${_columnNameForColumn(column)}=$unencodedInitialValue WHERE ${_columnNameForColumn(
            column)} IS NULL",
        "ALTER TABLE ${table.name} ALTER COLUMN ${_columnNameForColumn(column)} SET NOT NULL",
      ];
    } else {
      return ["ALTER TABLE ${table.name} ALTER COLUMN ${_columnNameForColumn(column)} SET NOT NULL"];
    }
  }
}