on dev help sign in
drop if exists
c824222Eitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+69−30
@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
50 , createDomain
50 , createDomain
51 , createTypeRange
51 , createTypeRange
52 , createIndex
52 , createIndex
53+ , IndexMethod (..)
54+ , btree, hash, gist, spgist, gin, brin
53 , createFunction
55 , createFunction
54 , createOrReplaceFunction
56 , createOrReplaceFunction
55 , createSetFunction
57 , createSetFunction
@@ -70,10 +72,15 @@ module Squeal.PostgreSQL.Definition
70 , languageSqlQuery
72 , languageSqlQuery
71 -- ** Drop
73 -- ** Drop
72 , dropSchema
74 , dropSchema
75+ , dropSchemaIfExists
73 , dropTable
76 , dropTable
77+ , dropTableIfExists
74 , dropView
78 , dropView
79+ , dropViewIfExists
75 , dropType
80 , dropType
81+ , dropTypeIfExists
76 , dropIndex
82 , dropIndex
83+ , dropIndexIfExists
77 , dropFunction
84 , dropFunction
78 , dropOperator
85 , dropOperator
79 -- ** Alter
86 -- ** Alter
@@ -591,12 +598,19 @@ DROP statements
591-- >>> printSQL definition
598-- >>> printSQL definition
592-- DROP SCHEMA "muh_schema";
599-- DROP SCHEMA "muh_schema";
593dropSchema
600dropSchema
594− :: Has sch schemas schema
601+ :: KnownSymbol sch
595 => Alias sch
602 => Alias sch
596 -- ^ user defined schema
603 -- ^ user defined schema
597 -> Definition schemas (Drop sch schemas)
604 -> Definition schemas (Drop sch schemas)
598dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
605dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
599
606
607+dropSchemaIfExists
608+ :: KnownSymbol sch
609+ => Alias sch
610+ -- ^ user defined schema
611+ -> Definition schemas (DropIfExists sch schemas)
612+dropSchemaIfExists sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
613+
600-- | `dropTable` removes a table from the schema.
614-- | `dropTable` removes a table from the schema.
601--
615--
602-- >>> :{
616-- >>> :{
@@ -609,11 +623,18 @@ dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
609-- DROP TABLE "muh_table";
623-- DROP TABLE "muh_table";
610dropTable
624dropTable
611 :: ( Has sch schemas schema
625 :: ( Has sch schemas schema
612− , Has tab schema ('Table table))
626+ , KnownSymbol tab )
613 => QualifiedAlias sch tab -- ^ table to remove
627 => QualifiedAlias sch tab -- ^ table to remove
614− -> Definition schemas (Alter sch (Drop tab schema) schemas)
628+ -> Definition schemas (Alter sch (DropSchemum tab 'Table schema) schemas)
615dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
629dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
616
630
631+dropTableIfExists
632+ :: ( Has sch schemas schema
633+ , Has tab schema ('Table table))
634+ => QualifiedAlias sch tab -- ^ table to remove
635+ -> Definition schemas (Alter sch (DropIfExists tab schema) schemas)
636+dropTableIfExists tab = UnsafeDefinition $ "DROP TABLE IF EXISTS" <+> renderSQL tab <> ";"
637+
617{-----------------------------------------
638{-----------------------------------------
618ALTER statements
639ALTER statements
619-----------------------------------------}
640-----------------------------------------}
@@ -940,11 +961,17 @@ createOrReplaceView alias query = UnsafeDefinition $
940-- :}
961-- :}
941-- DROP VIEW "bc";
962-- DROP VIEW "bc";
942dropView
963dropView
943− :: (Has sch schemas schema, Has vw schema ('View view))
964+ :: (Has sch schemas schema, KnownSymbol vw)
944 => QualifiedAlias sch vw -- ^ view to remove
965 => QualifiedAlias sch vw -- ^ view to remove
945− -> Definition schemas (Alter sch (Drop vw schema) schemas)
966+ -> Definition schemas (Alter sch (DropSchemum vw 'View schema) schemas)
946dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
967dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
947
968
969+dropViewIfExists
970+ :: (Has sch schemas schema, KnownSymbol vw)
971+ => QualifiedAlias sch vw -- ^ view to remove
972+ -> Definition schemas (Alter sch (DropIfExists vw schema) schemas)
973+dropViewIfExists vw = UnsafeDefinition $ "DROP VIEW IF EXISTS" <+> renderSQL vw <> ";"
974+
948-- | Enumerated types are created using the `createTypeEnum` command, for example
975-- | Enumerated types are created using the `createTypeEnum` command, for example
949--
976--
950-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
977-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
@@ -1100,10 +1127,10 @@ type Table = '[] :=>
1100
1127
1101>>> :{
1128>>> :{
1102let
1129let
1103− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1130+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index 'Btree])
1104 setup =
1131 setup =
1105 createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1132 createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1106− createIndex #ix #tab Btree [#a & AscNullsFirst, #b & AscNullsLast]
1133+ createIndex #ix #tab btree [#a & AscNullsFirst, #b & AscNullsLast]
1107in printSQL setup
1134in printSQL setup
1108:}
1135:}
1109CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
1136CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -1113,9 +1140,9 @@ createIndex
1113 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1140 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1114 => Alias ix
1141 => Alias ix
1115 -> QualifiedAlias sch tab
1142 -> QualifiedAlias sch tab
1116− -> IndexMethod
1143+ -> IndexMethod method
1117 -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1144 -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1118− -> Definition schemas (Alter sch (Create ix 'Index schema) schemas)
1145+ -> Definition schemas (Alter sch (Create ix ('Index method) schema) schemas)
1119createIndex ix tab method cols = UnsafeDefinition $
1146createIndex ix tab method cols = UnsafeDefinition $
1120 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1147 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1121 <+> "USING" <+> renderSQL method
1148 <+> "USING" <+> renderSQL method
@@ -1134,23 +1161,21 @@ createIndex ix tab method cols = UnsafeDefinition $
1134 DescNullsLast expression -> parenthesized (renderSQL expression)
1161 DescNullsLast expression -> parenthesized (renderSQL expression)
1135 <+> "DESC NULLS LAST"
1162 <+> "DESC NULLS LAST"
1136
1163
1137−data IndexMethod
1164+newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
1138− = Btree
1139− | Hash
1140− | Gist
1141− | Spgist
1142− | Gin
1143− | Brin
1144 deriving stock (Eq, Ord, Show, GHC.Generic)
1165 deriving stock (Eq, Ord, Show, GHC.Generic)
1145− deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1146−instance RenderSQL IndexMethod where
1166+instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
1147− renderSQL = \case
1167+btree :: IndexMethod 'Btree
1148− Btree -> "btree"
1168+btree = UnsafeIndexMethod "btree"
1149− Hash -> "hash"
1169+hash :: IndexMethod 'Hash
1150− Gist -> "gist"
1170+hash = UnsafeIndexMethod "hash"
1151− Spgist -> "spgist"
1171+gist :: IndexMethod 'Gist
1152− Gin -> "gin"
1172+gist = UnsafeIndexMethod "gist"
1153− Brin -> "brin"
1173+spgist :: IndexMethod 'Spgist
1174+spgist = UnsafeIndexMethod "spgist"
1175+gin :: IndexMethod 'Gin
1176+gin = UnsafeIndexMethod "gin"
1177+brin :: IndexMethod 'Brin
1178+brin = UnsafeIndexMethod "brin"
1154
1179
1155createFunction
1180createFunction
1156 :: ( Has sch schemas schema
1181 :: ( Has sch schemas schema
@@ -1309,15 +1334,22 @@ instance RenderSQL (FunctionDefinition schemas args ret) where
1309 renderSQL = renderFunctionDefinition
1334 renderSQL = renderFunctionDefinition
1310
1335
1311-- |
1336-- |
1312−-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1337+-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index 'Btree]) (Public '[]))
1313-- DROP INDEX "ix";
1338-- DROP INDEX "ix";
1314dropIndex
1339dropIndex
1315− :: (Has sch schemas schema, Has ix schema 'Index)
1340+ :: (Has sch schemas schema, KnownSymbol ix)
1316 => QualifiedAlias sch ix
1341 => QualifiedAlias sch ix
1317 -- ^ name of the user defined index
1342 -- ^ name of the user defined index
1318− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1343+ -> Definition schemas (Alter sch (DropSchemum ix 'Index schema) schemas)
1319dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1344dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1320
1345
1346+dropIndexIfExists
1347+ :: (Has sch schemas schema, KnownSymbol ix)
1348+ => QualifiedAlias sch ix
1349+ -- ^ name of the user defined index
1350+ -> Definition schemas (Alter sch (DropSchemumIfExists ix 'Index schema) schemas)
1351+dropIndexIfExists ix = UnsafeDefinition $ "DROP INDEX IF EXISTS" <+> renderSQL ix <> ";"
1352+
1321-- | Lift `PGTyped` to a field
1353-- | Lift `PGTyped` to a field
1322class FieldTyped schemas ty where
1354class FieldTyped schemas ty where
1323 fieldtype :: Aliased (TypeExpression schemas) ty
1355 fieldtype :: Aliased (TypeExpression schemas) ty
@@ -1333,12 +1365,19 @@ instance (KnownSymbol alias, PGTyped schemas ty)
1333-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1365-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1334-- DROP TYPE "schwarma";
1366-- DROP TYPE "schwarma";
1335dropType
1367dropType
1336− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1368+ :: (Has sch schemas schema, KnownSymbol td)
1337 => QualifiedAlias sch td
1369 => QualifiedAlias sch td
1338 -- ^ name of the user defined type
1370 -- ^ name of the user defined type
1339− -> Definition schemas (Alter sch (Drop td schema) schemas)
1371+ -> Definition schemas (Alter sch (DropSchemum td 'Typedef schema) schemas)
1340dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1372dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1341
1373
1374+dropTypeIfExists
1375+ :: (Has sch schemas schema, KnownSymbol td)
1376+ => QualifiedAlias sch td
1377+ -- ^ name of the user defined type
1378+ -> Definition schemas (Alter sch (DropSchemumIfExists td 'Typedef schema) schemas)
1379+dropTypeIfExists tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1380+
1342-- | `ColumnTypeExpression`s are used in `createTable` commands.
1381-- | `ColumnTypeExpression`s are used in `createTable` commands.
1343newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1382newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1344 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
1383 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+56−8
@@ -44,6 +44,7 @@ module Squeal.PostgreSQL.Schema
44 , ColumnsType
44 , ColumnsType
45 , TableType
45 , TableType
46 , SchemumType (..)
46 , SchemumType (..)
47+ , IndexType (..)
47 , OperatorType (..)
48 , OperatorType (..)
48 , ReturnsType (..)
49 , ReturnsType (..)
49 , SchemaType
50 , SchemaType
@@ -63,6 +64,9 @@ module Squeal.PostgreSQL.Schema
63 , CreateIfNotExists
64 , CreateIfNotExists
64 , CreateOrReplace
65 , CreateOrReplace
65 , Drop
66 , Drop
67+ , DropSchemum
68+ , DropIfExists
69+ , DropSchemumIfExists
66 , Alter
70 , Alter
67 , Rename
71 , Rename
68 , ConstraintInvolves
72 , ConstraintInvolves
@@ -324,7 +328,7 @@ type family Create alias x xs where
324 Create alias x (alias ::: y ': xs) = TypeError
328 Create alias x (alias ::: y ': xs) = TypeError
325 ('Text "Create: alias "
329 ('Text "Create: alias "
326 ':<>: 'ShowType alias
330 ':<>: 'ShowType alias
327− ':<>: 'Text "already in use")
331+ ':<>: 'Text "already exists")
328 Create alias y (x ': xs) = x ': Create alias y xs
332 Create alias y (x ': xs) = x ': Create alias y xs
329
333
330type family CreateIfNotExists alias x xs where
334type family CreateIfNotExists alias x xs where
@@ -336,21 +340,57 @@ type family CreateOrReplace alias x xs where
336 CreateOrReplace alias x '[] = '[alias ::: x]
340 CreateOrReplace alias x '[] = '[alias ::: x]
337 CreateOrReplace alias x (alias ::: x ': xs) = alias ::: x ': xs
341 CreateOrReplace alias x (alias ::: x ': xs) = alias ::: x ': xs
338 CreateOrReplace alias x (alias ::: y ': xs) = TypeError
342 CreateOrReplace alias x (alias ::: y ': xs) = TypeError
339− ('Text "CreateOrReplace: alias "
343+ ('Text "CreateOrReplace: expected type "
344+ ':<>: 'ShowType y
345+ ':<>: 'Text " but alias "
340 ':<>: 'ShowType alias
346 ':<>: 'ShowType alias
341− ':<>: 'Text " of type "
347+ ':<>: 'Text " has type "
342− ':<>: 'ShowType x
348+ ':<>: 'ShowType x)
343− ':<>: 'Text " cannot be replaced by unequal type "
344− ':<>: 'ShowType y)
345 CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
349 CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
346
350
347-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
351-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
348-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
352-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
349-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.
353-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.
350type family Drop alias xs where
354type family Drop alias xs where
355+ Drop alias '[] = TypeError
356+ ('Text "Drop: alias "
357+ ':<>: 'ShowType alias
358+ ':<>: 'Text " does not exist" )
351− Drop alias ((alias ::: x) ': xs) = xs
359+ Drop alias (alias ::: x ': xs) = xs
352 Drop alias (x ': xs) = x ': Drop alias xs
360 Drop alias (x ': xs) = x ': Drop alias xs
353
361
362+type family DropSchemum alias sch xs where
363+ DropSchemum alias sch '[] = TypeError
364+ ('Text "DropSchemum: alias "
365+ ':<>: 'ShowType alias
366+ ':<>: 'Text " does not exist" )
367+ DropSchemum alias sch (alias ::: sch x ': xs) = xs
368+ DropSchemum alias sch0 (alias ::: sch1 x ': xs) = TypeError
369+ ('Text "DropSchemum: expected schemum "
370+ ':<>: 'ShowType sch1
371+ ':<>: 'Text " but alias "
372+ ':<>: 'ShowType alias
373+ ':<>: 'Text " has schemum "
374+ ':<>: 'ShowType sch0)
375+ DropSchemum alias sch (x ': xs) = x ': DropSchemum alias sch xs
376+
377+type family DropIfExists alias xs where
378+ DropIfExists alias '[] = '[]
379+ DropIfExists alias (alias ::: x ': xs) = xs
380+ DropIfExists alias (x ': xs) = x ': DropIfExists alias xs
381+
382+type family DropSchemumIfExists alias sch xs where
383+ DropSchemumIfExists alias sch '[] = '[]
384+ DropSchemumIfExists alias sch (alias ::: sch x ': xs) = xs
385+ DropSchemumIfExists alias sch0 (alias ::: sch1 x ': xs) = TypeError
386+ ('Text "DropSchemumIfExists: expected schemum "
387+ ':<>: 'ShowType sch1
388+ ':<>: 'Text " but alias "
389+ ':<>: 'ShowType alias
390+ ':<>: 'Text " has schemum "
391+ ':<>: 'ShowType sch0)
392+ DropSchemumIfExists alias sch (x ': xs) = x ': DropSchemumIfExists alias sch xs
393+
354-- | @Alter alias x xs@ replaces the type associated with an @alias@ in @xs@
394-- | @Alter alias x xs@ replaces the type associated with an @alias@ in @xs@
355-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
395-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
356-- and `Squeal.PostgreSQL.Definition.alterColumn`.
396-- and `Squeal.PostgreSQL.Definition.alterColumn`.
@@ -387,11 +427,19 @@ data SchemumType
387 = Table TableType
427 = Table TableType
388 | View RowType
428 | View RowType
389 | Typedef PGType
429 | Typedef PGType
390− | Index
430+ | Index IndexType
391 | Function [NullityType] ReturnsType
431 | Function [NullityType] ReturnsType
392 | Operator OperatorType
432 | Operator OperatorType
393 | UnsafeSchemum Symbol
433 | UnsafeSchemum Symbol
394
434
435+data IndexType
436+ = Btree
437+ | Hash
438+ | Gist
439+ | Spgist
440+ | Gin
441+ | Brin
442+
395data ReturnsType
443data ReturnsType
396 = Returns NullityType
444 = Returns NullityType
397 | ReturnsTable RowType
445 | ReturnsTable RowType