create if not exists

This commit is contained in:
Eitan Chatav 2019-09-23 19:41:28 -07:00
parent ba71252348
commit efdac7e41a
2 changed files with 16 additions and 7 deletions

View File

@ -184,7 +184,7 @@ createSchema sch = UnsafeDefinition $
createSchemaIfNotExists
:: (KnownSymbol sch, Has sch schemas schema)
=> Alias sch
-> Definition schemas schemas
-> Definition schemas (CreateIfNotExists sch '[] schemas)
createSchemaIfNotExists sch = UnsafeDefinition $
"CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
<+> renderSQL sch <> ";"
@ -248,16 +248,19 @@ in printSQL setup
CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
-}
createTableIfNotExists
:: ( Has sch schemas schema
, Has tab schema ('Table (constraints :=> columns))
:: ( KnownSymbol sch
, KnownSymbol tab
, columns ~ (col ': cols)
, SOP.SListI columns
, SOP.SListI constraints )
, SOP.SListI constraints
, Has sch schemas0 schema0
, schemas1 ~ Alter sch (CreateIfNotExists tab ('Table (constraints :=> columns)) schema0) schemas0 )
=> QualifiedAlias sch tab -- ^ the name of the table to add
-> NP (Aliased (ColumnTypeExpression schemas)) columns
-> NP (Aliased (ColumnTypeExpression schemas0)) columns
-- ^ the names and datatype of each column
-> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints
-> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
-- ^ constraints that must hold for the table
-> Definition schemas schemas
-> Definition schemas0 schemas1
createTableIfNotExists tab columns constraints = UnsafeDefinition $
"CREATE TABLE IF NOT EXISTS"
<+> renderCreation tab columns constraints

View File

@ -60,6 +60,7 @@ module Squeal.PostgreSQL.Schema
, PGlabel (..)
-- * Data Definitions
, Create
, CreateIfNotExists
, Drop
, Alter
, Rename
@ -325,6 +326,11 @@ type family Create alias x xs where
':<>: 'Text "already in use")
Create alias y (x ': xs) = x ': Create alias y xs
type family CreateIfNotExists alias x xs where
CreateIfNotExists alias x '[] = '[alias ::: x]
CreateIfNotExists alias x (alias ::: y ': xs) = (alias ::: y ': xs)
CreateIfNotExists alias y (x ': xs) = x ': Create alias y xs
-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.