{-|
Module: Squeal.PostgreSQL.Definition.Table
Description: create, drop and alter tables
Copyright: (c) Eitan Chatav, 2019
Maintainer: eitan@morphism.tech
Stability: experimental

create, drop and alter tables
-}

{-# LANGUAGE
    AllowAmbiguousTypes
  , ConstraintKinds
  , DeriveAnyClass
  , DeriveGeneric
  , DerivingStrategies
  , FlexibleContexts
  , FlexibleInstances
  , GADTs
  , LambdaCase
  , MultiParamTypeClasses
  , OverloadedLabels
  , OverloadedStrings
  , RankNTypes
  , ScopedTypeVariables
  , TypeApplications
  , DataKinds
  , PolyKinds
  , TypeOperators
  , UndecidableSuperClasses
#-}

module Squeal.PostgreSQL.Definition.Table
  ( -- * Create
    createTable
  , createTableIfNotExists
    -- * Drop
  , dropTable
  , dropTableIfExists
    -- * Alter
  , alterTable
  , alterTableIfExists
  , alterTableRename
  , alterTableIfExistsRename
  , alterTableSetSchema
  , AlterTable (..)
    -- ** Constraints
  , addConstraint
  , dropConstraint
    -- ** Columns
  , AddColumn (..)
  , dropColumn
  , renameColumn
  , alterColumn
  , AlterColumn (..)
  , setDefault
  , dropDefault
  , setNotNull
  , dropNotNull
  , alterType
  ) where

import Control.DeepSeq
import Data.ByteString
import GHC.TypeLits

import qualified Generics.SOP as SOP
import qualified GHC.Generics as GHC

import Squeal.PostgreSQL.Type.Alias
import Squeal.PostgreSQL.Definition
import Squeal.PostgreSQL.Definition.Constraint
import Squeal.PostgreSQL.Expression
import Squeal.PostgreSQL.Expression.Type
import Squeal.PostgreSQL.Type.List
import Squeal.PostgreSQL.Render
import Squeal.PostgreSQL.Type.Schema

-- $setup
-- >>> import Squeal.PostgreSQL

{- | `createTable` adds a table to the schema.

>>> :set -XOverloadedLabels
>>> :{
type Table = '[] :=>
  '[ "a" ::: 'NoDef :=> 'Null 'PGint4
   , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
:}

>>> :{
let
  setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table])
  setup = createTable #tab
    (nullable int `as` #a :* nullable real `as` #b) Nil
in printSQL setup
:}
CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
-}
createTable
  :: ( KnownSymbol sch
     , KnownSymbol tab
     , columns ~ (col ': cols)
     , SOP.SListI columns
     , SOP.SListI constraints
     , Has sch db0 schema0
     , db1 ~ Alter sch (Create tab ('Table (constraints :=> columns)) schema0) db0 )
  => QualifiedAlias sch tab -- ^ the name of the table to add
  -> NP (Aliased (ColumnTypeExpression db0)) columns
    -- ^ the names and datatype of each column
  -> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
    -- ^ constraints that must hold for the table
  -> Definition db0 db1
createTable :: forall (sch :: Symbol) (tab :: Symbol) (columns :: ColumnsType)
       (col :: (Symbol, ColumnType)) (cols :: ColumnsType)
       (constraints :: TableConstraints)
       (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (schema0 :: [(Symbol, SchemumType)])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
(KnownSymbol sch, KnownSymbol tab, columns ~ (col : cols),
 SListI columns, SListI constraints, Has sch db0 schema0,
 db1
 ~ Alter
     sch (Create tab ('Table (constraints :=> columns)) schema0) db0) =>
QualifiedAlias sch tab
-> NP (Aliased (ColumnTypeExpression db0)) columns
-> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
-> Definition db0 db1
createTable QualifiedAlias sch tab
tab NP (Aliased (ColumnTypeExpression db0)) columns
columns NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"CREATE TABLE" ByteString -> ByteString -> ByteString
<+> forall (sch :: Symbol) (tab :: Symbol) (columns :: ColumnsType)
       (constraints :: TableConstraints)
       (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
(KnownSymbol sch, KnownSymbol tab, SListI columns,
 SListI constraints) =>
QualifiedAlias sch tab
-> NP (Aliased (ColumnTypeExpression db0)) columns
-> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
-> ByteString
renderCreation QualifiedAlias sch tab
tab NP (Aliased (ColumnTypeExpression db0)) columns
columns NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints

{-| `createTableIfNotExists` creates a table if it doesn't exist, but does not add it to the schema.
Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
`createTableIfNotExists` fixes this. Interestingly, this property makes it an idempotent in
the `Control.Category.Category` of `Definition`s.

>>> :set -XOverloadedLabels -XTypeApplications
>>> :{
type Table = '[] :=>
  '[ "a" ::: 'NoDef :=> 'Null 'PGint4
   , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
:}

>>> type Schemas = Public '["tab" ::: 'Table Table]

>>> :{
let
  setup :: Definition Schemas Schemas
  setup = createTableIfNotExists #tab
    (nullable int `as` #a :* nullable real `as` #b) Nil
in printSQL setup
:}
CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
-}
createTableIfNotExists
  :: ( KnownSymbol sch
     , KnownSymbol tab
     , columns ~ (col ': cols)
     , SOP.SListI columns
     , SOP.SListI constraints
     , Has sch db0 schema0
     , db1 ~ Alter sch (CreateIfNotExists tab ('Table (constraints :=> columns)) schema0) db0 )
  => QualifiedAlias sch tab -- ^ the name of the table to add
  -> NP (Aliased (ColumnTypeExpression db0)) columns
    -- ^ the names and datatype of each column
  -> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
    -- ^ constraints that must hold for the table
  -> Definition db0 db1
createTableIfNotExists :: forall (sch :: Symbol) (tab :: Symbol) (columns :: ColumnsType)
       (col :: (Symbol, ColumnType)) (cols :: ColumnsType)
       (constraints :: TableConstraints)
       (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (schema0 :: [(Symbol, SchemumType)])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
(KnownSymbol sch, KnownSymbol tab, columns ~ (col : cols),
 SListI columns, SListI constraints, Has sch db0 schema0,
 db1
 ~ Alter
     sch
     (CreateIfNotExists tab ('Table (constraints :=> columns)) schema0)
     db0) =>
QualifiedAlias sch tab
-> NP (Aliased (ColumnTypeExpression db0)) columns
-> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
-> Definition db0 db1
createTableIfNotExists QualifiedAlias sch tab
tab NP (Aliased (ColumnTypeExpression db0)) columns
columns NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"CREATE TABLE IF NOT EXISTS"
  ByteString -> ByteString -> ByteString
<+> forall (sch :: Symbol) (tab :: Symbol) (columns :: ColumnsType)
       (constraints :: TableConstraints)
       (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
(KnownSymbol sch, KnownSymbol tab, SListI columns,
 SListI constraints) =>
QualifiedAlias sch tab
-> NP (Aliased (ColumnTypeExpression db0)) columns
-> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
-> ByteString
renderCreation QualifiedAlias sch tab
tab NP (Aliased (ColumnTypeExpression db0)) columns
columns NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints

-- helper function for `createTable` and `createTableIfNotExists`
renderCreation
  :: ( KnownSymbol sch
     , KnownSymbol tab
     , SOP.SListI columns
     , SOP.SListI constraints )
  => QualifiedAlias sch tab -- ^ the name of the table to add
  -> NP (Aliased (ColumnTypeExpression db0)) columns
    -- ^ the names and datatype of each column
  -> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
    -- ^ constraints that must hold for the table
  -> ByteString
renderCreation :: forall (sch :: Symbol) (tab :: Symbol) (columns :: ColumnsType)
       (constraints :: TableConstraints)
       (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
(KnownSymbol sch, KnownSymbol tab, SListI columns,
 SListI constraints) =>
QualifiedAlias sch tab
-> NP (Aliased (ColumnTypeExpression db0)) columns
-> NP (Aliased (TableConstraintExpression sch tab db1)) constraints
-> ByteString
renderCreation QualifiedAlias sch tab
tab NP (Aliased (ColumnTypeExpression db0)) columns
columns NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints = forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab
  ByteString -> ByteString -> ByteString
<+> ByteString -> ByteString
parenthesized
    ( forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (x :: (Symbol, ColumnType)).
Aliased (ColumnTypeExpression db) x -> ByteString
renderColumnDef NP (Aliased (ColumnTypeExpression db0)) columns
columns
      forall a. Semigroup a => a -> a -> a
<> ( case NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints of
             NP (Aliased (TableConstraintExpression sch tab db1)) constraints
Nil -> ByteString
""
             NP (Aliased (TableConstraintExpression sch tab db1)) constraints
_ -> ByteString
", " forall a. Semigroup a => a -> a -> a
<>
               forall {k} (xs :: [k]) (expression :: k -> *).
SListI xs =>
(forall (x :: k). expression x -> ByteString)
-> NP expression xs -> ByteString
renderCommaSeparated forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (constraint :: (Symbol, TableConstraint)).
Aliased (TableConstraintExpression sch tab db) constraint
-> ByteString
renderConstraint NP (Aliased (TableConstraintExpression sch tab db1)) constraints
constraints ) )
  forall a. Semigroup a => a -> a -> a
<> ByteString
";"
  where
    renderColumnDef :: Aliased (ColumnTypeExpression db) x -> ByteString
    renderColumnDef :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (x :: (Symbol, ColumnType)).
Aliased (ColumnTypeExpression db) x -> ByteString
renderColumnDef (ColumnTypeExpression db ty
ty `As` Alias alias
column) =
      forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias alias
column ByteString -> ByteString -> ByteString
<+> forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty :: ColumnType).
ColumnTypeExpression db ty -> ByteString
renderColumnTypeExpression ColumnTypeExpression db ty
ty
    renderConstraint
      :: Aliased (TableConstraintExpression sch tab db) constraint
      -> ByteString
    renderConstraint :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (constraint :: (Symbol, TableConstraint)).
Aliased (TableConstraintExpression sch tab db) constraint
-> ByteString
renderConstraint (TableConstraintExpression sch tab db ty
constraint `As` Alias alias
alias) =
      ByteString
"CONSTRAINT" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias alias
alias ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL TableConstraintExpression sch tab db ty
constraint

-- | `dropTable` removes a table from the schema.
--
-- >>> :{
-- let
--   definition :: Definition '["public" ::: '["muh_table" ::: 'Table t]] (Public '[])
--   definition = dropTable #muh_table
-- :}
--
-- >>> printSQL definition
-- DROP TABLE "muh_table";
dropTable
  :: ( Has sch db schema
     , KnownSymbol tab )
  => QualifiedAlias sch tab -- ^ table to remove
  -> Definition db (Alter sch (DropSchemum tab 'Table schema) db)
dropTable :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol).
(Has sch db schema, KnownSymbol tab) =>
QualifiedAlias sch tab
-> Definition db (Alter sch (DropSchemum tab 'Table schema) db)
dropTable QualifiedAlias sch tab
tab = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$ ByteString
"DROP TABLE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | Drop a table if it exists.
dropTableIfExists
  :: ( Has sch db schema
     , KnownSymbol tab)
  => QualifiedAlias sch tab -- ^ table to remove
  -> Definition db (Alter sch (DropSchemumIfExists tab 'Table schema) db)
dropTableIfExists :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol).
(Has sch db schema, KnownSymbol tab) =>
QualifiedAlias sch tab
-> Definition
     db (Alter sch (DropSchemumIfExists tab 'Table schema) db)
dropTableIfExists QualifiedAlias sch tab
tab = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"DROP TABLE IF EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | `alterTable` changes the definition of a table from the schema.
alterTable
  :: (Has sch db schema, KnownSymbol tab)
  => QualifiedAlias sch tab -- ^ table to alter
  -> AlterTable sch tab db table -- ^ alteration to perform
  -> Definition db (Alter sch (Alter tab ('Table table) schema) db)
alterTable :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (table :: TableType).
(Has sch db schema, KnownSymbol tab) =>
QualifiedAlias sch tab
-> AlterTable sch tab db table
-> Definition db (Alter sch (Alter tab ('Table table) schema) db)
alterTable QualifiedAlias sch tab
tab AlterTable sch tab db table
alteration = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER TABLE"
  ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab
  ByteString -> ByteString -> ByteString
<+> forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> ByteString
renderAlterTable AlterTable sch tab db table
alteration
  forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | `alterTable` changes the definition of a table from the schema.
alterTableIfExists
  :: (Has sch db schema, KnownSymbol tab)
  => QualifiedAlias sch tab -- ^ table to alter
  -> AlterTable sch tab db table -- ^ alteration to perform
  -> Definition db (Alter sch (AlterIfExists tab ('Table table) schema) db)
alterTableIfExists :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (table :: TableType).
(Has sch db schema, KnownSymbol tab) =>
QualifiedAlias sch tab
-> AlterTable sch tab db table
-> Definition
     db (Alter sch (AlterIfExists tab ('Table table) schema) db)
alterTableIfExists QualifiedAlias sch tab
tab AlterTable sch tab db table
alteration = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER TABLE IF EXISTS"
  ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab
tab
  ByteString -> ByteString -> ByteString
<+> forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> ByteString
renderAlterTable AlterTable sch tab db table
alteration
  forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | `alterTableRename` changes the name of a table from the schema.
--
-- >>> type Schemas = '[ "public" ::: '[ "foo" ::: 'Table ('[] :=> '[]) ] ]
-- >>> :{
--  let migration :: Definition Schemas '["public" ::: '["bar" ::: 'Table ('[] :=> '[]) ] ]
--      migration = alterTableRename #foo #bar
--  in printSQL migration
-- :}
-- ALTER TABLE "foo" RENAME TO "bar";
alterTableRename
  :: ( Has sch db schema
     , KnownSymbol tab1
     , Has tab0 schema ('Table table))
  => QualifiedAlias sch tab0 -- ^ table to rename
  -> Alias tab1 -- ^ what to rename it
  -> Definition db (Alter sch (Rename tab0 tab1 schema) db )
alterTableRename :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab1 :: Symbol)
       (tab0 :: Symbol) (table :: TableType).
(Has sch db schema, KnownSymbol tab1,
 Has tab0 schema ('Table table)) =>
QualifiedAlias sch tab0
-> Alias tab1
-> Definition db (Alter sch (Rename tab0 tab1 schema) db)
alterTableRename QualifiedAlias sch tab0
tab0 Alias tab1
tab1 = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER TABLE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab0
tab0
  ByteString -> ByteString -> ByteString
<+> ByteString
"RENAME TO" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias tab1
tab1 forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | `alterTableIfExistsRename` changes the name of a table from the schema if it exists.
--
-- >>> type Schemas = '[ "public" ::: '[ "foo" ::: 'Table ('[] :=> '[]) ] ]
-- >>> :{
--  let migration :: Definition Schemas Schemas
--      migration = alterTableIfExistsRename #goo #gar
--  in printSQL migration
-- :}
-- ALTER TABLE IF EXISTS "goo" RENAME TO "gar";
alterTableIfExistsRename
  :: ( Has sch db schema
     , KnownSymbol tab0
     , KnownSymbol tab1 )
  => QualifiedAlias sch tab0 -- ^ table to rename
  -> Alias tab1 -- ^ what to rename it
  -> Definition db (Alter sch (RenameIfExists tab0 tab1 schema) db )
alterTableIfExistsRename :: forall (sch :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab0 :: Symbol)
       (tab1 :: Symbol).
(Has sch db schema, KnownSymbol tab0, KnownSymbol tab1) =>
QualifiedAlias sch tab0
-> Alias tab1
-> Definition db (Alter sch (RenameIfExists tab0 tab1 schema) db)
alterTableIfExistsRename QualifiedAlias sch tab0
tab0 Alias tab1
tab1 = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER TABLE IF EXISTS" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch tab0
tab0
  ByteString -> ByteString -> ByteString
<+> ByteString
"RENAME TO" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias tab1
tab1 forall a. Semigroup a => a -> a -> a
<> ByteString
";"

{- | This form moves the table into another schema.

>>> type DB0 = '[ "sch0" ::: '[ "tab" ::: 'Table ('[] :=> '[]) ], "sch1" ::: '[] ]
>>> type DB1 = '[ "sch0" ::: '[], "sch1" ::: '[ "tab" ::: 'Table ('[] :=> '[]) ] ]
>>> :{
let def :: Definition DB0 DB1
    def = alterTableSetSchema (#sch0 ! #tab) #sch1
in printSQL def
:}
ALTER TABLE "sch0"."tab" SET SCHEMA "sch1";
-}
alterTableSetSchema
  :: ( Has sch0 db schema0
     , Has tab schema0 ('Table table)
     , Has sch1 db schema1 )
  => QualifiedAlias sch0 tab -- ^ table to move
  -> Alias sch1 -- ^ where to move it
  -> Definition db (SetSchema sch0 sch1 schema0 schema1 tab 'Table table db)
alterTableSetSchema :: forall (sch0 :: Symbol) (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema0 :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (table :: TableType) (sch1 :: Symbol)
       (schema1 :: [(Symbol, SchemumType)]).
(Has sch0 db schema0, Has tab schema0 ('Table table),
 Has sch1 db schema1) =>
QualifiedAlias sch0 tab
-> Alias sch1
-> Definition
     db (SetSchema sch0 sch1 schema0 schema1 tab 'Table table db)
alterTableSetSchema QualifiedAlias sch0 tab
tab Alias sch1
sch = forall (db0 :: [(Symbol, [(Symbol, SchemumType)])])
       (db1 :: [(Symbol, [(Symbol, SchemumType)])]).
ByteString -> Definition db0 db1
UnsafeDefinition forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER TABLE" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL QualifiedAlias sch0 tab
tab ByteString -> ByteString -> ByteString
<+> ByteString
"SET SCHEMA" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias sch1
sch forall a. Semigroup a => a -> a -> a
<> ByteString
";"

-- | An `AlterTable` describes the alteration to perform on the columns
-- of a table.
newtype AlterTable
  (sch :: Symbol)
  (tab :: Symbol)
  (db :: SchemasType)
  (table :: TableType) =
    UnsafeAlterTable {forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> ByteString
renderAlterTable :: ByteString}
  deriving (forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType) x.
Rep (AlterTable sch tab db table) x -> AlterTable sch tab db table
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType) x.
AlterTable sch tab db table -> Rep (AlterTable sch tab db table) x
$cto :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType) x.
Rep (AlterTable sch tab db table) x -> AlterTable sch tab db table
$cfrom :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType) x.
AlterTable sch tab db table -> Rep (AlterTable sch tab db table) x
GHC.Generic,Int -> AlterTable sch tab db table -> ShowS
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
Int -> AlterTable sch tab db table -> ShowS
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
[AlterTable sch tab db table] -> ShowS
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> String
showList :: [AlterTable sch tab db table] -> ShowS
$cshowList :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
[AlterTable sch tab db table] -> ShowS
show :: AlterTable sch tab db table -> String
$cshow :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> String
showsPrec :: Int -> AlterTable sch tab db table -> ShowS
$cshowsPrec :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
Int -> AlterTable sch tab db table -> ShowS
Show,AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
/= :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c/= :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
== :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c== :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
Eq,AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
AlterTable sch tab db table
-> AlterTable sch tab db table -> Ordering
AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
Eq (AlterTable sch tab db table)
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table
-> AlterTable sch tab db table -> Ordering
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
min :: AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
$cmin :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
max :: AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
$cmax :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table
-> AlterTable sch tab db table -> AlterTable sch tab db table
>= :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c>= :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
> :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c> :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
<= :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c<= :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
< :: AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
$c< :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> AlterTable sch tab db table -> Bool
compare :: AlterTable sch tab db table
-> AlterTable sch tab db table -> Ordering
$ccompare :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table
-> AlterTable sch tab db table -> Ordering
Ord,forall a. (a -> ()) -> NFData a
forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> ()
rnf :: AlterTable sch tab db table -> ()
$crnf :: forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
AlterTable sch tab db table -> ()
NFData)

-- | An `addConstraint` adds a table constraint.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('["positive" ::: 'Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--   definition = alterTable #tab (addConstraint #positive (check #col (#col .> 0)))
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > (0 :: int4)));
addConstraint
  :: ( KnownSymbol alias
     , Has sch db schema
     , Has tab schema ('Table (constraints :=> columns)) )
  => Alias alias
  -> TableConstraintExpression sch tab db constraint
  -- ^ constraint to add
  -> AlterTable sch tab db (Create alias constraint constraints :=> columns)
addConstraint :: forall (alias :: Symbol) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (constraints :: TableConstraints) (columns :: ColumnsType)
       (constraint :: TableConstraint).
(KnownSymbol alias, Has sch db schema,
 Has tab schema ('Table (constraints :=> columns))) =>
Alias alias
-> TableConstraintExpression sch tab db constraint
-> AlterTable
     sch tab db (Create alias constraint constraints :=> columns)
addConstraint Alias alias
alias TableConstraintExpression sch tab db constraint
constraint = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
  ByteString
"ADD" ByteString -> ByteString -> ByteString
<+> ByteString
"CONSTRAINT" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias alias
alias
    ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL TableConstraintExpression sch tab db constraint
constraint

-- | A `dropConstraint` drops a table constraint.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--   definition = alterTable #tab (dropConstraint #positive)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" DROP CONSTRAINT "positive";
dropConstraint
  :: ( KnownSymbol constraint
     , Has sch db schema
     , Has tab schema ('Table (constraints :=> columns)) )
  => Alias constraint
  -- ^ constraint to drop
  -> AlterTable sch tab db (Drop constraint constraints :=> columns)
dropConstraint :: forall (constraint :: Symbol) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (constraints :: TableConstraints) (columns :: ColumnsType).
(KnownSymbol constraint, Has sch db schema,
 Has tab schema ('Table (constraints :=> columns))) =>
Alias constraint
-> AlterTable sch tab db (Drop constraint constraints :=> columns)
dropConstraint Alias constraint
constraint = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
  ByteString
"DROP" ByteString -> ByteString -> ByteString
<+> ByteString
"CONSTRAINT" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias constraint
constraint

-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
class AddColumn ty where
  -- | `addColumn` adds a new column, initially filled with whatever
  -- default value is given or with @NULL@.
  --
  -- >>> :{
  -- let
  --   definition :: Definition
  --     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
  --     '["public" ::: '["tab" ::: 'Table ('[] :=>
  --        '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
  --         , "col2" ::: 'Def :=> 'Null 'PGtext ])]]
  --   definition = alterTable #tab (addColumn #col2 (text & nullable & default_ "foo"))
  -- in printSQL definition
  -- :}
  -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL DEFAULT (E'foo' :: text);
  --
  -- >>> :{
  -- let
  --   definition :: Definition
  --     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
  --     '["public" ::: '["tab" ::: 'Table ('[] :=>
  --        '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
  --         , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
  --   definition = alterTable #tab (addColumn #col2 (text & nullable))
  -- in printSQL definition
  -- :}
  -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL;
  addColumn
    :: ( KnownSymbol column
       , Has sch db schema
       , Has tab schema ('Table (constraints :=> columns)) )
    => Alias column -- ^ column to add
    -> ColumnTypeExpression db ty -- ^ type of the new column
    -> AlterTable sch tab db (constraints :=> Create column ty columns)
  addColumn Alias column
column ColumnTypeExpression db ty
ty = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
    ByteString
"ADD COLUMN" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias column
column ByteString -> ByteString -> ByteString
<+> forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty :: ColumnType).
ColumnTypeExpression db ty -> ByteString
renderColumnTypeExpression ColumnTypeExpression db ty
ty
instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)

-- | A `dropColumn` removes a column. Whatever data was in the column
-- disappears. Table constraints involving the column are dropped, too.
-- However, if the column is referenced by a foreign key constraint of
-- another table, PostgreSQL will not silently drop that constraint.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=>
--        '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
--         , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
--   definition = alterTable #tab (dropColumn #col2)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" DROP COLUMN "col2";
dropColumn
  :: ( KnownSymbol column
     , Has sch db schema
     , Has tab schema ('Table (constraints :=> columns)) )
  => Alias column -- ^ column to remove
  -> AlterTable sch tab db (constraints :=> Drop column columns)
dropColumn :: forall (column :: Symbol) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (constraints :: TableConstraints) (columns :: ColumnsType).
(KnownSymbol column, Has sch db schema,
 Has tab schema ('Table (constraints :=> columns))) =>
Alias column
-> AlterTable sch tab db (constraints :=> Drop column columns)
dropColumn Alias column
column = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
  ByteString
"DROP COLUMN" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias column
column

-- | A `renameColumn` renames a column.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4])]]
--   definition = alterTable #tab (renameColumn #foo #bar)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" RENAME COLUMN "foo" TO "bar";
renameColumn
  :: ( KnownSymbol column0
     , KnownSymbol column1
     , Has sch db schema
     , Has tab schema ('Table (constraints :=> columns)) )
  => Alias column0 -- ^ column to rename
  -> Alias column1 -- ^ what to rename the column
  -> AlterTable sch tab db (constraints :=> Rename column0 column1 columns)
renameColumn :: forall (column0 :: Symbol) (column1 :: Symbol) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (constraints :: TableConstraints) (columns :: ColumnsType).
(KnownSymbol column0, KnownSymbol column1, Has sch db schema,
 Has tab schema ('Table (constraints :=> columns))) =>
Alias column0
-> Alias column1
-> AlterTable
     sch tab db (constraints :=> Rename column0 column1 columns)
renameColumn Alias column0
column0 Alias column1
column1 = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
  ByteString
"RENAME COLUMN" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias column0
column0  ByteString -> ByteString -> ByteString
<+> ByteString
"TO" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias column1
column1

-- | An `alterColumn` alters a single column.
alterColumn
  :: ( KnownSymbol column
     , Has sch db schema
     , Has tab schema ('Table (constraints :=> columns))
     , Has column columns ty0 )
  => Alias column -- ^ column to alter
  -> AlterColumn db ty0 ty1 -- ^ alteration to perform
  -> AlterTable sch tab db (constraints :=> Alter column ty1 columns)
alterColumn :: forall (column :: Symbol) (sch :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])])
       (schema :: [(Symbol, SchemumType)]) (tab :: Symbol)
       (constraints :: TableConstraints) (columns :: ColumnsType)
       (ty0 :: ColumnType) (ty1 :: ColumnType).
(KnownSymbol column, Has sch db schema,
 Has tab schema ('Table (constraints :=> columns)),
 Has column columns ty0) =>
Alias column
-> AlterColumn db ty0 ty1
-> AlterTable sch tab db (constraints :=> Alter column ty1 columns)
alterColumn Alias column
column AlterColumn db ty0 ty1
alteration = forall (sch :: Symbol) (tab :: Symbol)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (table :: TableType).
ByteString -> AlterTable sch tab db table
UnsafeAlterTable forall a b. (a -> b) -> a -> b
$
  ByteString
"ALTER COLUMN" ByteString -> ByteString -> ByteString
<+> forall sql. RenderSQL sql => sql -> ByteString
renderSQL Alias column
column ByteString -> ByteString -> ByteString
<+> forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> ByteString
renderAlterColumn AlterColumn db ty0 ty1
alteration

-- | An `AlterColumn` describes the alteration to perform on a single column.
newtype AlterColumn (db :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
  UnsafeAlterColumn {forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> ByteString
renderAlterColumn :: ByteString}
  deriving (forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType) x.
Rep (AlterColumn db ty0 ty1) x -> AlterColumn db ty0 ty1
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType) x.
AlterColumn db ty0 ty1 -> Rep (AlterColumn db ty0 ty1) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
$cto :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType) x.
Rep (AlterColumn db ty0 ty1) x -> AlterColumn db ty0 ty1
$cfrom :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType) x.
AlterColumn db ty0 ty1 -> Rep (AlterColumn db ty0 ty1) x
GHC.Generic,Int -> AlterColumn db ty0 ty1 -> ShowS
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
Int -> AlterColumn db ty0 ty1 -> ShowS
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
[AlterColumn db ty0 ty1] -> ShowS
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AlterColumn db ty0 ty1] -> ShowS
$cshowList :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
[AlterColumn db ty0 ty1] -> ShowS
show :: AlterColumn db ty0 ty1 -> String
$cshow :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> String
showsPrec :: Int -> AlterColumn db ty0 ty1 -> ShowS
$cshowsPrec :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
Int -> AlterColumn db ty0 ty1 -> ShowS
Show,AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c/= :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
== :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c== :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
Eq,AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Ordering
AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
Eq (AlterColumn db ty0 ty1)
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Ordering
forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
$cmin :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
max :: AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
$cmax :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1
-> AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1
>= :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c>= :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
> :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c> :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
<= :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c<= :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
< :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
$c< :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Bool
compare :: AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Ordering
$ccompare :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> AlterColumn db ty0 ty1 -> Ordering
Ord,forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> ()
forall a. (a -> ()) -> NFData a
rnf :: AlterColumn db ty0 ty1 -> ()
$crnf :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
AlterColumn db ty0 ty1 -> ()
NFData)

-- | A `setDefault` sets a new default for a column. Note that this doesn't
-- affect any existing rows in the table, it just changes the default for
-- future insert and update commands.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
--   definition = alterTable #tab (alterColumn #col (setDefault 5))
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT (5 :: int4);
setDefault
  :: Expression 'Ungrouped '[] '[] db '[] '[] ty -- ^ default value to set
  -> AlterColumn db (constraint :=> ty) ('Def :=> ty)
setDefault :: forall (db :: [(Symbol, [(Symbol, SchemumType)])]) (ty :: NullType)
       (constraint :: Optionality).
Expression 'Ungrouped '[] '[] db '[] '[] ty
-> AlterColumn db (constraint :=> ty) ('Def :=> ty)
setDefault Expression 'Ungrouped '[] '[] db '[] '[] ty
expression = forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
ByteString -> AlterColumn db ty0 ty1
UnsafeAlterColumn forall a b. (a -> b) -> a -> b
$
  ByteString
"SET DEFAULT" ByteString -> ByteString -> ByteString
<+> forall (grp :: Grouping) (lat :: FromType) (with :: FromType)
       (db :: [(Symbol, [(Symbol, SchemumType)])]) (params :: [NullType])
       (from :: FromType) (ty :: NullType).
Expression grp lat with db params from ty -> ByteString
renderExpression Expression 'Ungrouped '[] '[] db '[] '[] ty
expression

-- | A `dropDefault` removes any default value for a column.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
--   definition = alterTable #tab (alterColumn #col dropDefault)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ALTER COLUMN "col" DROP DEFAULT;
dropDefault :: AlterColumn db ('Def :=> ty) ('NoDef :=> ty)
dropDefault :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty :: NullType).
AlterColumn db ('Def :=> ty) ('NoDef :=> ty)
dropDefault = forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
ByteString -> AlterColumn db ty0 ty1
UnsafeAlterColumn forall a b. (a -> b) -> a -> b
$ ByteString
"DROP DEFAULT"

-- | A `setNotNull` adds a @NOT NULL@ constraint to a column.
-- The constraint will be checked immediately, so the table data must satisfy
-- the constraint before it can be added.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--   definition = alterTable #tab (alterColumn #col setNotNull)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ALTER COLUMN "col" SET NOT NULL;
setNotNull
  :: AlterColumn db (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
setNotNull :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (constraint :: Optionality) (ty :: PGType).
AlterColumn
  db (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
setNotNull = forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
ByteString -> AlterColumn db ty0 ty1
UnsafeAlterColumn forall a b. (a -> b) -> a -> b
$ ByteString
"SET NOT NULL"

-- | A `dropNotNull` drops a @NOT NULL@ constraint from a column.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
--   definition = alterTable #tab (alterColumn #col dropNotNull)
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ALTER COLUMN "col" DROP NOT NULL;
dropNotNull
  :: AlterColumn db (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
dropNotNull :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (constraint :: Optionality) (ty :: PGType).
AlterColumn
  db (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
dropNotNull = forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
ByteString -> AlterColumn db ty0 ty1
UnsafeAlterColumn forall a b. (a -> b) -> a -> b
$ ByteString
"DROP NOT NULL"

-- | An `alterType` converts a column to a different data type.
-- This will succeed only if each existing entry in the column can be
-- converted to the new type by an implicit cast.
--
-- >>> :{
-- let
--   definition :: Definition
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
--     '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]]
--   definition =
--     alterTable #tab (alterColumn #col (alterType (numeric & notNullable)))
-- in printSQL definition
-- :}
-- ALTER TABLE "tab" ALTER COLUMN "col" TYPE numeric NOT NULL;
alterType :: ColumnTypeExpression db ty -> AlterColumn db ty0 ty
alterType :: forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty :: ColumnType) (ty0 :: ColumnType).
ColumnTypeExpression db ty -> AlterColumn db ty0 ty
alterType ColumnTypeExpression db ty
ty = forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty0 :: ColumnType) (ty1 :: ColumnType).
ByteString -> AlterColumn db ty0 ty1
UnsafeAlterColumn forall a b. (a -> b) -> a -> b
$ ByteString
"TYPE" ByteString -> ByteString -> ByteString
<+> forall (db :: [(Symbol, [(Symbol, SchemumType)])])
       (ty :: ColumnType).
ColumnTypeExpression db ty -> ByteString
renderColumnTypeExpression ColumnTypeExpression db ty
ty