on dev help sign in
alter if exists
commitf136497ef54c6e3f4f78adeff55303c4e926397e authorEitan Chatav <eitan@EEM109LMBP-1.local> dateFri 27 Sep 2019, 11:32 (7 years ago) parent0c1d035
2 files changed+46−3
squeal-postgresql/src/Squeal/PostgreSQL/Definition/Table.hs+26−3 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20
squeal-postgresql/src/Squeal/PostgreSQL/Definition/Table.hs+26−3
@@ -35,7 +35,9 @@ module Squeal.PostgreSQL.Definition.Table
3535 , dropTable
3636 , dropTableIfExists
3737 , alterTable
38+ , alterTableIfExists
3839 , alterTableRename
40+ , alterTableIfExistsRename
3941 , AlterTable (..)
4042 , addConstraint
4143 , dropConstraint
@@ -202,16 +204,28 @@ dropTableIfExists tab = UnsafeDefinition $ "DROP TABLE IF EXISTS" <+> renderSQL
202204
203205-- | `alterTable` changes the definition of a table from the schema.
204206alterTable
205− :: (Has sch schemas schema, Has tab schema ('Table table0))
207+ :: (Has sch schemas schema, KnownSymbol tab)
206208 => QualifiedAlias sch tab -- ^ table to alter
207− -> AlterTable sch tab schemas table1 -- ^ alteration to perform
208− -> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
209+ -> AlterTable sch tab schemas table -- ^ alteration to perform
210+ -> Definition schemas (Alter sch (Alter tab ('Table table) schema) schemas)
209211alterTable tab alteration = UnsafeDefinition $
210212 "ALTER TABLE"
211213 <+> renderSQL tab
212214 <+> renderAlterTable alteration
213215 <> ";"
214216
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+
215229-- | `alterTableRename` changes the name of a table from the schema.
216230--
217231-- >>> printSQL $ alterTableRename #foo #bar
@@ -225,6 +239,15 @@ alterTableRename tab0 tab1 = UnsafeDefinition $
225239 "ALTER TABLE" <+> renderSQL tab0
226240 <+> "RENAME TO" <+> renderSQL tab1 <> ";"
227241
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+
228251-- | An `AlterTable` describes the alteration to perform on the columns
229252-- of a table.
230253newtype AlterTable
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+20
@@ -69,7 +69,9 @@ module Squeal.PostgreSQL.Schema
6969 , DropIfExists
7070 , DropSchemumIfExists
7171 , Alter
72+ , AlterIfExists
7273 , Rename
74+ , RenameIfExists
7375 , ConstraintInvolves
7476 , DropIfConstraintsInvolve
7577 , IsNotElem
@@ -396,16 +398,34 @@ type family DropSchemumIfExists alias sch xs where
396398-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
397399-- and `Squeal.PostgreSQL.Definition.alterColumn`.
398400type family Alter alias x xs where
401+ Alter alias x '[] = TypeError
402+ ('Text "Alter: alias "
403+ ':<>: 'ShowType alias
404+ ':<>: 'Text " does not exist" )
399405 Alter alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
400406 Alter alias x1 (x0 ': xs) = x0 ': Alter alias x1 xs
401407
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+
402413-- | @Rename alias0 alias1 xs@ replaces the alias @alias0@ by @alias1@ in @xs@
403414-- and is used in `Squeal.PostgreSQL.Definition.alterTableRename` and
404415-- `Squeal.PostgreSQL.Definition.renameColumn`.
405416type family Rename alias0 alias1 xs where
417+ Rename alias0 alias1 '[] = TypeError
418+ ('Text "Rename: alias "
419+ ':<>: 'ShowType alias0
420+ ':<>: 'Text " does not exist" )
406421 Rename alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
407422 Rename alias0 alias1 (x ': xs) = x ': Rename alias0 alias1 xs
408423
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+
409429-- | Check if a `TableConstraint` involves a column
410430type family ConstraintInvolves column constraint where
411431 ConstraintInvolves column ('Check columns) = column `Elem` columns