on dev help sign in
create if not exists
commitefdac7e41a00b86c7264f8dfa83b7be1c3793ddb authorEitan Chatav <eitan@EEM109LMBP-1.local> dateMon 23 Sep 2019, 19:41 (7 years ago) parentba71252
2 files changed+16−7
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+10−7 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+10−7
@@ -184,7 +184,7 @@ createSchema sch = UnsafeDefinition $
184184createSchemaIfNotExists
185185 :: (KnownSymbol sch, Has sch schemas schema)
186186 => Alias sch
187− -> Definition schemas schemas
187+ -> Definition schemas (CreateIfNotExists sch '[] schemas)
188188createSchemaIfNotExists sch = UnsafeDefinition $
189189 "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
190190 <+> renderSQL sch <> ";"
@@ -248,16 +248,19 @@ in printSQL setup
248248CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
249249-}
250250createTableIfNotExists
251− :: ( Has sch schemas schema
252− , Has tab schema ('Table (constraints :=> columns))
251+ :: ( KnownSymbol sch
252+ , KnownSymbol tab
253+ , columns ~ (col ': cols)
253254 , 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 )
255258 => QualifiedAlias sch tab -- ^ the name of the table to add
256− -> NP (Aliased (ColumnTypeExpression schemas)) columns
259+ -> NP (Aliased (ColumnTypeExpression schemas0)) columns
257260 -- ^ the names and datatype of each column
258− -> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints
261+ -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
259262 -- ^ constraints that must hold for the table
260− -> Definition schemas schemas
263+ -> Definition schemas0 schemas1
261264createTableIfNotExists tab columns constraints = UnsafeDefinition $
262265 "CREATE TABLE IF NOT EXISTS"
263266 <+> renderCreation tab columns constraints
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs+6
@@ -60,6 +60,7 @@ module Squeal.PostgreSQL.Schema
6060 , PGlabel (..)
6161 -- * Data Definitions
6262 , Create
63+ , CreateIfNotExists
6364 , Drop
6465 , Alter
6566 , Rename
@@ -325,6 +326,11 @@ type family Create alias x xs where
325326 ':<>: 'Text "already in use")
326327 Create alias y (x ': xs) = x ': Create alias y xs
327328
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+
328334-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
329335-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
330336-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.