mirror of
https://github.com/ilyakooo0/squeal.git
synced 2024-09-11 13:57:29 +03:00
alter if exists
This commit is contained in:
parent
0c1d0356f0
commit
f136497ef5
@ -35,7 +35,9 @@ module Squeal.PostgreSQL.Definition.Table
|
||||
, dropTable
|
||||
, dropTableIfExists
|
||||
, alterTable
|
||||
, alterTableIfExists
|
||||
, alterTableRename
|
||||
, alterTableIfExistsRename
|
||||
, AlterTable (..)
|
||||
, addConstraint
|
||||
, dropConstraint
|
||||
@ -202,16 +204,28 @@ dropTableIfExists tab = UnsafeDefinition $ "DROP TABLE IF EXISTS" <+> renderSQL
|
||||
|
||||
-- | `alterTable` changes the definition of a table from the schema.
|
||||
alterTable
|
||||
:: (Has sch schemas schema, Has tab schema ('Table table0))
|
||||
:: (Has sch schemas schema, KnownSymbol tab)
|
||||
=> QualifiedAlias sch tab -- ^ table to alter
|
||||
-> AlterTable sch tab schemas table1 -- ^ alteration to perform
|
||||
-> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
|
||||
-> AlterTable sch tab schemas table -- ^ alteration to perform
|
||||
-> Definition schemas (Alter sch (Alter tab ('Table table) schema) schemas)
|
||||
alterTable tab alteration = UnsafeDefinition $
|
||||
"ALTER TABLE"
|
||||
<+> renderSQL tab
|
||||
<+> renderAlterTable alteration
|
||||
<> ";"
|
||||
|
||||
-- | `alterTable` changes the definition of a table from the schema.
|
||||
alterTableIfExists
|
||||
:: (Has sch schemas schema, KnownSymbol tab)
|
||||
=> QualifiedAlias sch tab -- ^ table to alter
|
||||
-> AlterTable sch tab schemas table -- ^ alteration to perform
|
||||
-> Definition schemas (Alter sch (AlterIfExists tab ('Table table) schema) schemas)
|
||||
alterTableIfExists tab alteration = UnsafeDefinition $
|
||||
"ALTER TABLE IF EXISTS"
|
||||
<+> renderSQL tab
|
||||
<+> renderAlterTable alteration
|
||||
<> ";"
|
||||
|
||||
-- | `alterTableRename` changes the name of a table from the schema.
|
||||
--
|
||||
-- >>> printSQL $ alterTableRename #foo #bar
|
||||
@ -225,6 +239,15 @@ alterTableRename tab0 tab1 = UnsafeDefinition $
|
||||
"ALTER TABLE" <+> renderSQL tab0
|
||||
<+> "RENAME TO" <+> renderSQL tab1 <> ";"
|
||||
|
||||
alterTableIfExistsRename
|
||||
:: (KnownSymbol tab0, KnownSymbol tab1)
|
||||
=> Alias tab0 -- ^ table to rename
|
||||
-> Alias tab1 -- ^ what to rename it
|
||||
-> Definition schema (RenameIfExists tab0 tab1 schema)
|
||||
alterTableIfExistsRename tab0 tab1 = UnsafeDefinition $
|
||||
"ALTER TABLE IF EXISTS" <+> renderSQL tab0
|
||||
<+> "RENAME TO" <+> renderSQL tab1 <> ";"
|
||||
|
||||
-- | An `AlterTable` describes the alteration to perform on the columns
|
||||
-- of a table.
|
||||
newtype AlterTable
|
||||
|
@ -69,7 +69,9 @@ module Squeal.PostgreSQL.Schema
|
||||
, DropIfExists
|
||||
, DropSchemumIfExists
|
||||
, Alter
|
||||
, AlterIfExists
|
||||
, Rename
|
||||
, RenameIfExists
|
||||
, ConstraintInvolves
|
||||
, DropIfConstraintsInvolve
|
||||
, IsNotElem
|
||||
@ -396,16 +398,34 @@ type family DropSchemumIfExists alias sch xs where
|
||||
-- with the type @x@ and is used in `Squeal.PostgreSQL.Definition.alterTable`
|
||||
-- and `Squeal.PostgreSQL.Definition.alterColumn`.
|
||||
type family Alter alias x xs where
|
||||
Alter alias x '[] = TypeError
|
||||
('Text "Alter: alias "
|
||||
':<>: 'ShowType alias
|
||||
':<>: 'Text " does not exist" )
|
||||
Alter alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
|
||||
Alter alias x1 (x0 ': xs) = x0 ': Alter alias x1 xs
|
||||
|
||||
type family AlterIfExists alias x xs where
|
||||
AlterIfExists alias x '[] = '[]
|
||||
AlterIfExists alias x1 (alias ::: x0 ': xs) = alias ::: x1 ': xs
|
||||
AlterIfExists alias x1 (x0 ': xs) = x0 ': AlterIfExists alias x1 xs
|
||||
|
||||
-- | @Rename alias0 alias1 xs@ replaces the alias @alias0@ by @alias1@ in @xs@
|
||||
-- and is used in `Squeal.PostgreSQL.Definition.alterTableRename` and
|
||||
-- `Squeal.PostgreSQL.Definition.renameColumn`.
|
||||
type family Rename alias0 alias1 xs where
|
||||
Rename alias0 alias1 '[] = TypeError
|
||||
('Text "Rename: alias "
|
||||
':<>: 'ShowType alias0
|
||||
':<>: 'Text " does not exist" )
|
||||
Rename alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
|
||||
Rename alias0 alias1 (x ': xs) = x ': Rename alias0 alias1 xs
|
||||
|
||||
type family RenameIfExists alias0 alias1 xs where
|
||||
RenameIfExists alias x '[] = '[]
|
||||
RenameIfExists alias0 alias1 ((alias0 ::: x0) ': xs) = (alias1 ::: x0) ': xs
|
||||
RenameIfExists alias0 alias1 (x ': xs) = x ': RenameIfExists alias0 alias1 xs
|
||||
|
||||
-- | Check if a `TableConstraint` involves a column
|
||||
type family ConstraintInvolves column constraint where
|
||||
ConstraintInvolves column ('Check columns) = column `Elem` columns
|
||||
|
Loading…
Reference in New Issue
Block a user