(out *strings.Builder, schemaName, tableName string, columnDiff *schema.ColumnDiff)
| 1190 | } |
| 1191 | |
| 1192 | func writeAlterColumnDiff(out *strings.Builder, schemaName, tableName string, columnDiff *schema.ColumnDiff) error { |
| 1193 | oldColumn := columnDiff.OldColumn |
| 1194 | newColumn := columnDiff.NewColumn |
| 1195 | if oldColumn.Type != newColumn.Type { |
| 1196 | if _, err := fmt.Fprintf(out, "ALTER TABLE \"%s\".\"%s\" ALTER COLUMN \"%s\" TYPE %s", schemaName, tableName, newColumn.Name, newColumn.Type); err != nil { |
| 1197 | return err |
| 1198 | } |
| 1199 | if requiresExplicitCasting(oldColumn.Type, newColumn.Type) { |
| 1200 | if _, err := fmt.Fprintf(out, " USING \"%s\"::%s", newColumn.Name, newColumn.Type); err != nil { |
| 1201 | return err |
| 1202 | } |
| 1203 | } |
| 1204 | if _, err := out.WriteString(";\n\n"); err != nil { |
| 1205 | return err |
| 1206 | } |
| 1207 | } |
| 1208 | if oldColumn.Default != newColumn.Default { |
| 1209 | if newColumn.Default == "" { |
| 1210 | if _, err := fmt.Fprintf(out, "ALTER TABLE \"%s\".\"%s\" ALTER COLUMN \"%s\" DROP DEFAULT;\n\n", schemaName, tableName, newColumn.Name); err != nil { |
| 1211 | return err |
| 1212 | } |
| 1213 | } else if _, err := fmt.Fprintf(out, "ALTER TABLE \"%s\".\"%s\" ALTER COLUMN \"%s\" SET DEFAULT %s;\n\n", schemaName, tableName, newColumn.Name, newColumn.Default); err != nil { |
| 1214 | return err |
| 1215 | } |
| 1216 | } |
| 1217 | if oldColumn.Nullable != newColumn.Nullable { |
| 1218 | nullability := "DROP NOT NULL" |
| 1219 | if !newColumn.Nullable { |
| 1220 | nullability = "SET NOT NULL" |
| 1221 | } |
| 1222 | if _, err := fmt.Fprintf(out, "ALTER TABLE \"%s\".\"%s\" ALTER COLUMN \"%s\" %s;\n\n", schemaName, tableName, newColumn.Name, nullability); err != nil { |
| 1223 | return err |
| 1224 | } |
| 1225 | } |
| 1226 | if oldColumn.GetComment() != newColumn.GetComment() { |
| 1227 | if err := writeColumnCommentDiff(out, schemaName, tableName, newColumn.Name, newColumn.GetComment()); err != nil { |
| 1228 | return err |
| 1229 | } |
| 1230 | } |
| 1231 | return nil |
| 1232 | } |
| 1233 | |
| 1234 | func requiresExplicitCasting(oldType, newType string) bool { |
| 1235 | oldBaseType := extractBaseType(oldType) |
no test coverage detected