on dev help sign in
create if not exists
efdac7eEitan Chatav
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+10−7
@@ -184,7 +184,7 @@ createSchema sch = UnsafeDefinition $
184createSchemaIfNotExists
184createSchemaIfNotExists
185 :: (KnownSymbol sch, Has sch schemas schema)
185 :: (KnownSymbol sch, Has sch schemas schema)
186 => Alias sch
186 => Alias sch
187− -> Definition schemas schemas
187+ -> Definition schemas (CreateIfNotExists sch '[] schemas)
188createSchemaIfNotExists sch = UnsafeDefinition $
188createSchemaIfNotExists sch = UnsafeDefinition $
189 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
189 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
190 <+> renderSQL sch <> ";"
190 <+> renderSQL sch <> ";"
@@ -248,16 +248,19 @@ in printSQL setup
248CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
248CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
249-}
249-}
250createTableIfNotExists
250createTableIfNotExists
251− :: ( Has sch schemas schema
251+ :: ( KnownSymbol sch
252− , Has tab schema ('Table (constraints :=> columns))
252+ , KnownSymbol tab
253+ , columns ~ (col ': cols)
253 , SOP.SListI columns
254 , SOP.SListI columns
254− , SOP.SListI constraints )
255+ , SOP.SListI constraints
256+ , Has sch schemas0 schema0
257+ , schemas1 ~ Alter sch (CreateIfNotExists tab ('Table (constraints :=> columns)) schema0) schemas0 )
255 => QualifiedAlias sch tab -- ^ the name of the table to add
258 => QualifiedAlias sch tab -- ^ the name of the table to add
256− -> NP (Aliased (ColumnTypeExpression schemas)) columns
259+ -> NP (Aliased (ColumnTypeExpression schemas0)) columns
257 -- ^ the names and datatype of each column
260 -- ^ the names and datatype of each column
258− -> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints
261+ -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
259 -- ^ constraints that must hold for the table
262 -- ^ constraints that must hold for the table
260− -> Definition schemas schemas
263+ -> Definition schemas0 schemas1
261createTableIfNotExists tab columns constraints = UnsafeDefinition $
264createTableIfNotExists tab columns constraints = UnsafeDefinition $
262 "CREATE TABLE IF NOT EXISTS"
265 "CREATE TABLE IF NOT EXISTS"
263 <+> renderCreation tab columns constraints
266 <+> renderCreation tab columns constraints
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
@@ -60,6 +60,7 @@ module Squeal.PostgreSQL.Schema
60 , PGlabel (..)
60 , PGlabel (..)
61 -- * Data Definitions
61 -- * Data Definitions
62 , Create
62 , Create
63+ , CreateIfNotExists
63 , Drop
64 , Drop
64 , Alter
65 , Alter
65 , Rename
66 , Rename
@@ -325,6 +326,11 @@ type family Create alias x xs where
325 ':<>: 'Text "already in use")
326 ':<>: 'Text "already in use")
326 Create alias y (x ': xs) = x ': Create alias y xs
327 Create alias y (x ': xs) = x ': Create alias y xs
327
328
329+type family CreateIfNotExists alias x xs where
330+ CreateIfNotExists alias x '[] = '[alias ::: x]
331+ CreateIfNotExists alias x (alias ::: y ': xs) = (alias ::: y ': xs)
332+ CreateIfNotExists alias y (x ': xs) = x ': Create alias y xs
333+
328-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
334-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
329-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
335-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
330-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.
336-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.