Skip to content

Commit 218b44c

Browse files
committed
Prohibit dropping the last column in a table.
1 parent edd9a30 commit 218b44c

3 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/Rezoom.SQL.Compiler/Error.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ let onlyIntPrimaryKeyAutoincrement =
143143
"SQ060: AUTOINCREMENT can only be specified for an INT or INT64 column"
144144
let tableAlreadyHasPrimaryKey table =
145145
sprintf "SQ061: ``%O`` already has ``%O`` as its primary key constraint" table
146+
let cannotDropLastColumn table columnName =
147+
sprintf "SQ062: ``%O`` can't be dropped because it is the last column remaining in table ``%O``" columnName table
146148

147149

148150
let tableNameNotSuitableForPG =

src/Rezoom.SQL.Compiler/ModelOps.fs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,8 @@ let dropColumn tableName (column : Name) =
303303
failAt tableName.Source <| Error.columnIsReferencedByConstraints column [refName]
304304
| _ -> ()
305305
let table = { table with Columns = table.Columns |> Map.remove column }
306+
if table.Columns |> Map.isEmpty then
307+
failAt tableName.Source <| Error.cannotDropLastColumn tableName.Value column
306308
return! putObject tableName (SchemaTable table)
307309
else
308310
failAt tableName.Source <| Error.columnIsReferencedByConstraints column coveredByConstraints

src/Rezoom.SQL.Test/TestAlterTable.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,11 @@ let ``string can't be autoincrement`` () =
7676
{ defaultTest with
7777
Migration = "create table Foo(x string(10) primary key autoincrement)"
7878
Expect = BadMigration <| Error.onlyIntPrimaryKeyAutoincrement
79+
} |> assertSimple
80+
81+
[<Test>]
82+
let ``can't drop last column`` () =
83+
{ defaultTest with
84+
Migration = "create table Foo(x int); alter table Foo drop column x"
85+
Expect = BadMigration <| Error.cannotDropLastColumn "main.Foo" "x"
7986
} |> assertSimple

0 commit comments

Comments
 (0)