@@ -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