mirror of
https://github.com/ilyakooo0/squeal.git
synced 2024-10-26 15:09:10 +03:00
create if not exists
This commit is contained in:
parent
ba71252348
commit
efdac7e41a
@ -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
|
||||
|
@ -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.
|
||||
|
Loading…
Reference in New Issue
Block a user