on dev help sign in
drop if exists

and index types

commitc824222ae3c540023f1351e66a99e52f6dce5168 authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 23 Sep 2019, 20:55 (7 years ago) parentee96a86
2 files changed+125−38
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+69−30 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+56−8
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+69−30
@@ -50,6 +50,8 @@ module Squeal.PostgreSQL.Definition
5050 , createDomain
5151 , createTypeRange
5252 , createIndex
53+ , IndexMethod (..)
54+ , btree, hash, gist, spgist, gin, brin
5355 , createFunction
5456 , createOrReplaceFunction
5557 , createSetFunction
@@ -70,10 +72,15 @@ module Squeal.PostgreSQL.Definition
7072 , languageSqlQuery
7173 -- ** Drop
7274 , dropSchema
75+ , dropSchemaIfExists
7376 , dropTable
77+ , dropTableIfExists
7478 , dropView
79+ , dropViewIfExists
7580 , dropType
81+ , dropTypeIfExists
7682 , dropIndex
83+ , dropIndexIfExists
7784 , dropFunction
7885 , dropOperator
7986 -- ** Alter
@@ -591,12 +598,19 @@ DROP statements
591598-- >>> printSQL definition
592599-- DROP SCHEMA "muh_schema";
593600dropSchema
594− :: Has sch schemas schema
601+ :: KnownSymbol sch
595602 => Alias sch
596603 -- ^ user defined schema
597604 -> Definition schemas (Drop sch schemas)
598605dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
599606
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+
600614-- | `dropTable` removes a table from the schema.
601615--
602616-- >>> :{
@@ -609,11 +623,18 @@ dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
609623-- DROP TABLE "muh_table";
610624dropTable
611625 :: ( Has sch schemas schema
612− , Has tab schema ('Table table))
626+ , KnownSymbol tab )
613627 => 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)
615629dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
616630
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+
617638{-----------------------------------------
618639ALTER statements
619640-----------------------------------------}
@@ -940,11 +961,17 @@ createOrReplaceView alias query = UnsafeDefinition $
940961-- :}
941962-- DROP VIEW "bc";
942963dropView
943− :: (Has sch schemas schema, Has vw schema ('View view))
964+ :: (Has sch schemas schema, KnownSymbol vw)
944965 => 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)
946967dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
947968
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+
948975-- | Enumerated types are created using the `createTypeEnum` command, for example
949976--
950977-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
@@ -1100,10 +1127,10 @@ type Table = '[] :=>
11001127
11011128>>> :{
11021129let
1103− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1130+ setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index 'Btree])
11041131 setup =
11051132 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]
11071134in printSQL setup
11081135:}
11091136CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
@@ -1113,9 +1140,9 @@ createIndex
11131140 :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
11141141 => Alias ix
11151142 -> QualifiedAlias sch tab
1116− -> IndexMethod
1143+ -> IndexMethod method
11171144 -> [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)
11191146createIndex ix tab method cols = UnsafeDefinition $
11201147 "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
11211148 <+> "USING" <+> renderSQL method
@@ -1134,23 +1161,21 @@ createIndex ix tab method cols = UnsafeDefinition $
11341161 DescNullsLast expression -> parenthesized (renderSQL expression)
11351162 <+> "DESC NULLS LAST"
11361163
1137−data IndexMethod
1138− = Btree
1139− | Hash
1140− | Gist
1141− | Spgist
1142− | Gin
1143− | Brin
1164+newtype IndexMethod ty = UnsafeIndexMethod {renderIndexMethod :: ByteString}
11441165 deriving stock (Eq, Ord, Show, GHC.Generic)
1145− deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1146−instance RenderSQL IndexMethod where
1147− renderSQL = \case
1148− Btree -> "btree"
1149− Hash -> "hash"
1150− Gist -> "gist"
1151− Spgist -> "spgist"
1152− Gin -> "gin"
1153− Brin -> "brin"
1166+instance RenderSQL (IndexMethod ty) where renderSQL = renderIndexMethod
1167+btree :: IndexMethod 'Btree
1168+btree = UnsafeIndexMethod "btree"
1169+hash :: IndexMethod 'Hash
1170+hash = UnsafeIndexMethod "hash"
1171+gist :: IndexMethod 'Gist
1172+gist = UnsafeIndexMethod "gist"
1173+spgist :: IndexMethod 'Spgist
1174+spgist = UnsafeIndexMethod "spgist"
1175+gin :: IndexMethod 'Gin
1176+gin = UnsafeIndexMethod "gin"
1177+brin :: IndexMethod 'Brin
1178+brin = UnsafeIndexMethod "brin"
11541179
11551180createFunction
11561181 :: ( Has sch schemas schema
@@ -1309,15 +1334,22 @@ instance RenderSQL (FunctionDefinition schemas args ret) where
13091334 renderSQL = renderFunctionDefinition
13101335
13111336-- |
1312−-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1337+-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index 'Btree]) (Public '[]))
13131338-- DROP INDEX "ix";
13141339dropIndex
1315− :: (Has sch schemas schema, Has ix schema 'Index)
1340+ :: (Has sch schemas schema, KnownSymbol ix)
13161341 => QualifiedAlias sch ix
13171342 -- ^ 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)
13191344dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
13201345
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+
13211353-- | Lift `PGTyped` to a field
13221354class FieldTyped schemas ty where
13231355 fieldtype :: Aliased (TypeExpression schemas) ty
@@ -1333,12 +1365,19 @@ instance (KnownSymbol alias, PGTyped schemas ty)
13331365-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
13341366-- DROP TYPE "schwarma";
13351367dropType
1336− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1368+ :: (Has sch schemas schema, KnownSymbol td)
13371369 => QualifiedAlias sch td
13381370 -- ^ 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)
13401372dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
13411373
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+
13421381-- | `ColumnTypeExpression`s are used in `createTable` commands.
13431382newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
13441383 = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+56−8
@@ -44,6 +44,7 @@ module Squeal.PostgreSQL.Schema
4444 , ColumnsType
4545 , TableType
4646 , SchemumType (..)
47+ , IndexType (..)
4748 , OperatorType (..)
4849 , ReturnsType (..)
4950 , SchemaType
@@ -63,6 +64,9 @@ module Squeal.PostgreSQL.Schema
6364 , CreateIfNotExists
6465 , CreateOrReplace
6566 , Drop
67+ , DropSchemum
68+ , DropIfExists
69+ , DropSchemumIfExists
6670 , Alter
6771 , Rename
6872 , ConstraintInvolves
@@ -324,7 +328,7 @@ type family Create alias x xs where
324328 Create alias x (alias ::: y ': xs) = TypeError
325329 ('Text "Create: alias "
326330 ':<>: 'ShowType alias
327− ':<>: 'Text "already in use")
331+ ':<>: 'Text "already exists")
328332 Create alias y (x ': xs) = x ': Create alias y xs
329333
330334type family CreateIfNotExists alias x xs where
@@ -336,21 +340,57 @@ type family CreateOrReplace alias x xs where
336340 CreateOrReplace alias x '[] = '[alias ::: x]
337341 CreateOrReplace alias x (alias ::: x ': xs) = alias ::: x ': xs
338342 CreateOrReplace alias x (alias ::: y ': xs) = TypeError
339− ('Text "CreateOrReplace: alias "
343+ ('Text "CreateOrReplace: expected type "
344+ ':<>: 'ShowType y
345+ ':<>: 'Text " but alias "
340346 ':<>: 'ShowType alias
341− ':<>: 'Text " of type "
342− ':<>: 'ShowType x
343− ':<>: 'Text " cannot be replaced by unequal type "
344− ':<>: 'ShowType y)
347+ ':<>: 'Text " has type "
348+ ':<>: 'ShowType x)
345349 CreateOrReplace alias y (x ': xs) = x ': CreateOrReplace alias y xs
346350
347351-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
348352-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
349353-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.
350354type family Drop alias xs where
351− Drop alias ((alias ::: x) ': xs) = xs
355+ Drop alias '[] = TypeError
356+ ('Text "Drop: alias "
357+ ':<>: 'ShowType alias
358+ ':<>: 'Text " does not exist" )
359+ Drop alias (alias ::: x ': xs) = xs
352360 Drop alias (x ': xs) = x ': Drop alias xs
353361
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+
354394-- | @Alter alias x xs@ replaces the type associated with an @alias@ in @xs@
355395-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
356396-- and `Squeal.PostgreSQL.Definition.alterColumn`.
@@ -387,11 +427,19 @@ data SchemumType
387427 = Table TableType
388428 | View RowType
389429 | Typedef PGType
390− | Index
430+ | Index IndexType
391431 | Function [NullityType] ReturnsType
392432 | Operator OperatorType
393433 | UnsafeSchemum Symbol
394434
435+data IndexType
436+ = Btree
437+ | Hash
438+ | Gist
439+ | Spgist
440+ | Gin
441+ | Brin
442+
395443data ReturnsType
396444 = Returns NullityType
397445 | ReturnsTable RowType