on dev help sign in
alter if exists
f136497Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition/Table.hs+26−3
@@ -35,7 +35,9 @@ module Squeal.PostgreSQL.Definition.Table
35 , dropTable
35 , dropTable
36 , dropTableIfExists
36 , dropTableIfExists
37 , alterTable
37 , alterTable
38+ , alterTableIfExists
38 , alterTableRename
39 , alterTableRename
40+ , alterTableIfExistsRename
39 , AlterTable (..)
41 , AlterTable (..)
40 , addConstraint
42 , addConstraint
41 , dropConstraint
43 , dropConstraint
@@ -202,16 +204,28 @@ dropTableIfExists tab = UnsafeDefinition $ "DROP TABLE IF EXISTS" <+> renderSQL
202
204
203-- | `alterTable` changes the definition of a table from the schema.
205-- | `alterTable` changes the definition of a table from the schema.
204alterTable
206alterTable
205− :: (Has sch schemas schema, Has tab schema ('Table table0))
207+ :: (Has sch schemas schema, KnownSymbol tab)
206 => QualifiedAlias sch tab -- ^ table to alter
208 => QualifiedAlias sch tab -- ^ table to alter
207− -> AlterTable sch tab schemas table1 -- ^ alteration to perform
209+ -> AlterTable sch tab schemas table -- ^ alteration to perform
208− -> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
210+ -> Definition schemas (Alter sch (Alter tab ('Table table) schema) schemas)
209alterTable tab alteration = UnsafeDefinition $
211alterTable tab alteration = UnsafeDefinition $
210 "ALTER TABLE"
212 "ALTER TABLE"
211 <+> renderSQL tab
213 <+> renderSQL tab
212 <+> renderAlterTable alteration
214 <+> renderAlterTable alteration
213 <> ";"
215 <> ";"
214
216
217+-- | `alterTable` changes the definition of a table from the schema.
218+alterTableIfExists
219+ :: (Has sch schemas schema, KnownSymbol tab)
220+ => QualifiedAlias sch tab -- ^ table to alter
221+ -> AlterTable sch tab schemas table -- ^ alteration to perform
222+ -> Definition schemas (Alter sch (AlterIfExists tab ('Table table) schema) schemas)
223+alterTableIfExists tab alteration = UnsafeDefinition $
224+ "ALTER TABLE IF EXISTS"
225+ <+> renderSQL tab
226+ <+> renderAlterTable alteration
227+ <> ";"
228+
215-- | `alterTableRename` changes the name of a table from the schema.
229-- | `alterTableRename` changes the name of a table from the schema.
216--
230--
217-- >>> printSQL $ alterTableRename #foo #bar
231-- >>> printSQL $ alterTableRename #foo #bar
@@ -225,6 +239,15 @@ alterTableRename tab0 tab1 = UnsafeDefinition $
225 "ALTER TABLE" <+> renderSQL tab0
239 "ALTER TABLE" <+> renderSQL tab0
226 <+> "RENAME TO" <+> renderSQL tab1 <> ";"
240 <+> "RENAME TO" <+> renderSQL tab1 <> ";"
227
241
242+alterTableIfExistsRename
243+ :: (KnownSymbol tab0, KnownSymbol tab1)
244+ => Alias tab0 -- ^ table to rename
245+ -> Alias tab1 -- ^ what to rename it
246+ -> Definition schema (RenameIfExists tab0 tab1 schema)
247+alterTableIfExistsRename tab0 tab1 = UnsafeDefinition $
248+ "ALTER TABLE IF EXISTS" <+> renderSQL tab0
249+ <+> "RENAME TO" <+> renderSQL tab1 <> ";"
250+
228-- | An `AlterTable` describes the alteration to perform on the columns
251-- | An `AlterTable` describes the alteration to perform on the columns
229-- of a table.
252-- of a table.
230newtype AlterTable
253newtype AlterTable
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20
@@ -69,7 +69,9 @@ module Squeal.PostgreSQL.Schema
69 , DropIfExists
69 , DropIfExists
70 , DropSchemumIfExists
70 , DropSchemumIfExists
71 , Alter
71 , Alter
72+ , AlterIfExists
72 , Rename
73 , Rename
74+ , RenameIfExists
73 , ConstraintInvolves
75 , ConstraintInvolves
74 , DropIfConstraintsInvolve
76 , DropIfConstraintsInvolve
75 , IsNotElem
77 , IsNotElem
@@ -396,16 +398,34 @@ type family DropSchemumIfExists alias sch xs where
396-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
398-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
397-- and `Squeal.PostgreSQL.Definition.alterColumn`.
399-- and `Squeal.PostgreSQL.Definition.alterColumn`.
398type family Alter alias x xs where
400type family Alter alias x xs where
401+ Alter alias x '[] = TypeError
402+ ('Text "Alter: alias "
403+ ':<>: 'ShowType alias
404+ ':<>: 'Text " does not exist" )
399 Alter alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
405 Alter alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
400 Alter alias x1 (x0 ': xs) = x0 ': Alter alias x1 xs
406 Alter alias x1 (x0 ': xs) = x0 ': Alter alias x1 xs
401
407
408+type family AlterIfExists alias x xs where
409+ AlterIfExists alias x '[] = '[]
410+ AlterIfExists alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
411+ AlterIfExists alias x1 (x0 ': xs) = x0 ': AlterIfExists alias x1 xs
412+
402-- | @Rename alias0 alias1 xs@ replaces the alias @alias0@ by @alias1@ in @xs@
413-- | @Rename alias0 alias1 xs@ replaces the alias @alias0@ by @alias1@ in @xs@
403-- and is used in `Squeal.PostgreSQL.Definition.alterTableRename` and
414-- and is used in `Squeal.PostgreSQL.Definition.alterTableRename` and
404-- `Squeal.PostgreSQL.Definition.renameColumn`.
415-- `Squeal.PostgreSQL.Definition.renameColumn`.
405type family Rename alias0 alias1 xs where
416type family Rename alias0 alias1 xs where
417+ Rename alias0 alias1 '[] = TypeError
418+ ('Text "Rename: alias "
419+ ':<>: 'ShowType alias0
420+ ':<>: 'Text " does not exist" )
406 Rename alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
421 Rename alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
407 Rename alias0 alias1 (x ': xs) = x ': Rename alias0 alias1 xs
422 Rename alias0 alias1 (x ': xs) = x ': Rename alias0 alias1 xs
408
423
424+type family RenameIfExists alias0 alias1 xs where
425+ RenameIfExists alias x '[] = '[]
426+ RenameIfExists alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
427+ RenameIfExists alias0 alias1 (x ': xs) = x ': RenameIfExists alias0 alias1 xs
428+
409-- | Check if a `TableConstraint` involves a column
429-- | Check if a `TableConstraint` involves a column
410type family ConstraintInvolves column constraint where
430type family ConstraintInvolves column constraint where
411 ConstraintInvolves column ('Check columns) = column `Elem` columns
431 ConstraintInvolves column ('Check columns) = column `Elem` columns