Skip to content

Commit c886adb

Browse files
committed
Use USING CAST(...) clause when editing Postgres column type.
1 parent 218b44c commit c886adb

3 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/Rezoom.SQL.Compiler/AST.fs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ type TypeName =
4343
| BooleanTypeName
4444
| DateTimeTypeName
4545
| DateTimeOffsetTypeName
46+
member this.SupportsCollation =
47+
match this with
48+
| StringTypeName _ -> true
49+
| _ -> false
4650
override this.ToString() =
4751
match this with
4852
| GuidTypeName -> "GUID"

src/Rezoom.SQL.Compiler/Postgres.fs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,19 +108,30 @@ type private PostgresStatement(indexer : IParameterIndexer) as this =
108108
this.Expr.Name(col)
109109
ws
110110
|]
111-
let inline changeType (col : Name) (ty : TypeName) (collation : Name option) =
111+
let inline changeType (col : Name) (ty : TypeName) (collation : Name option) changingType =
112112
seq {
113113
yield! alterColumn col
114114
yield text "TYPE"
115115
yield ws
116116
yield! this.Expr.TypeName(ty)
117117
match collation with
118-
| None -> ()
119-
| Some collation ->
118+
| Some collation when ty.SupportsCollation ->
120119
yield ws
121120
yield text "COLLATE"
122121
yield ws
123122
yield this.Expr.CollationName(collation)
123+
| _ -> ()
124+
if changingType then
125+
yield ws
126+
yield text "USING"
127+
yield ws
128+
yield text "CAST("
129+
yield this.Expr.Name(col)
130+
yield ws
131+
yield text "AS"
132+
yield ws
133+
yield! this.Expr.TypeName(ty)
134+
yield text ")"
124135
}
125136
seq {
126137
yield text "ALTER TABLE"
@@ -165,10 +176,10 @@ type private PostgresStatement(indexer : IParameterIndexer) as this =
165176
yield text (if change.NewNullable then "DROP NOT NULL" else "SET NOT NULL")
166177
| ChangeType change ->
167178
let schemaColumn = change.ExistingInfo.Column |> Option.get
168-
yield! changeType change.Column change.NewType schemaColumn.Collation
179+
yield! changeType change.Column change.NewType schemaColumn.Collation true
169180
| ChangeCollation change ->
170181
let schemaColumn = change.ExistingInfo.Column |> Option.get
171-
yield! changeType change.Column schemaColumn.ColumnTypeName (Some change.NewCollation)
182+
yield! changeType change.Column schemaColumn.ColumnTypeName (Some change.NewCollation) false
172183
}
173184
override this.PrimaryKeyClause(pk) =
174185
seq {

src/Rezoom.SQL.Test/TestPostgresSmoke.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ ALTER TABLE "public"."smoketable1" ALTER COLUMN "float64column" DROP DEFAULT;
125125
ALTER TABLE "smoketable1" ALTER COLUMN "stringncolumn" TYPE VARCHAR(80) COLLATE "C";
126126
ALTER TABLE "smoketable1" ALTER COLUMN "float64column" DROP NOT NULL;
127127
ALTER TABLE "smoketable1" ALTER COLUMN "float64column" SET NOT NULL;
128-
ALTER TABLE "smoketable1" ALTER COLUMN "int16column" TYPE INT;
128+
ALTER TABLE "smoketable1" ALTER COLUMN "int16column" TYPE INT USING CAST("int16column" AS INT);
129129
DROP INDEX "ix_smoketable2_parentid";
130130
ALTER TABLE "smoketable2" DROP CONSTRAINT "smoketable2_friendid_fk_smoketablefriend_autocolumn" RESTRICT;
131131
ALTER TABLE "smoketable2" DROP COLUMN "friendid" RESTRICT;

0 commit comments

Comments
 (0)