@@ -19,6 +19,7 @@ Squeal data definition language.
20 , LambdaCase
20 , LambdaCase
21 , MultiParamTypeClasses
21 , MultiParamTypeClasses
22 , OverloadedStrings
23 , OverloadedStrings
23 , RankNTypes
24 , RankNTypes
24 , ScopedTypeVariables
25 , ScopedTypeVariables
@@ -26,6 +27,7 @@ Squeal data definition language.
26 , TypeInType
27 , TypeInType
27 , TypeOperators
28 , TypeOperators
28 , UndecidableSuperClasses
29 , UndecidableSuperClasses
30+ , UndecidableInstances
31module Squeal.PostgreSQL.Definition
33module Squeal.PostgreSQL.Definition
@@ -33,85 +35,16 @@ module Squeal.PostgreSQL.Definition
33 Definition (..)
35 Definition (..)
35 , manipDefinition
37 , manipDefinition
39− , createSchemaIfNotExists
41− , createTableIfNotExists
45− , createTypeComposite
46− , createTypeCompositeFrom
52− , createOrReplaceFunction
53− , TableConstraintExpression (..)
59− , OnDeleteClause (..)
60− , OnUpdateClause (..)
85− , ColumnTypeExpression (..)
97import Control.Category
40import Control.Category
98import Control.DeepSeq
41import Control.DeepSeq
99import Data.ByteString
42import Data.ByteString
100import Data.Monoid
43import Data.Monoid
102import Prelude hiding ((.), id)
44import Prelude hiding ((.), id)
104−import qualified Generics.SOP as SOP
105import qualified GHC.Generics as GHC
46import qualified GHC.Generics as GHC
107−import Squeal.PostgreSQL.Alias
108−import Squeal.PostgreSQL.Expression
109−import Squeal.PostgreSQL.Expression.Logic
110−import Squeal.PostgreSQL.Expression.Sort
111−import Squeal.PostgreSQL.Expression.Type
112−import Squeal.PostgreSQL.List
113−import Squeal.PostgreSQL.PG
114−import Squeal.PostgreSQL.Query
115import Squeal.PostgreSQL.Manipulation
48import Squeal.PostgreSQL.Manipulation
116import Squeal.PostgreSQL.Render
49import Squeal.PostgreSQL.Render
117import Squeal.PostgreSQL.Schema
50import Squeal.PostgreSQL.Schema
@@ -147,1085 +80,3 @@ manipDefinition
147 -- ^ no input or output
80 -- ^ no input or output
148 -> Definition schemas schemas
81 -> Definition schemas schemas
149manipDefinition = UnsafeDefinition . (<> ";") . renderSQL
82manipDefinition = UnsafeDefinition . (<> ";") . renderSQL
151−{-----------------------------------------
153−-----------------------------------------}
156−`createSchema` enters a new schema into the current database.
157−The schema name must be distinct from the name of any existing schema
158−in the current database.
160−A schema is essentially a namespace: it contains named objects
161−(tables, data types, functions, and operators) whose names
162−can duplicate those of other objects existing in other schemas.
163−Named objects are accessed by `QualifiedAlias`es with the schema
169− -> Definition schemas (Create sch '[] schemas)
170−createSchema sch = UnsafeDefinition $
171− "CREATE" <+> "SCHEMA" <+> renderSQL sch <> ";"
173−{- | Idempotent version of `createSchema`. -}
174−createSchemaIfNotExists
175− :: (KnownSymbol sch, Has sch schemas schema)
177− -> Definition schemas schemas
178−createSchemaIfNotExists sch = UnsafeDefinition $
179− "CREATE" <+> "SCHEMA" <+> "IF" <+> "NOT" <+> "EXISTS"
180− <+> renderSQL sch <> ";"
182−{- | `createTable` adds a table to the schema.
184−>>> :set -XOverloadedLabels
187− '[ "a" ::: 'NoDef :=> 'Null 'PGint4
188− , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
193− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table])
194− setup = createTable #tab
195− (nullable int `as` #a :* nullable real `as` #b) Nil
198−CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
201− :: ( KnownSymbol sch
203− , columns ~ (col ': cols)
204− , SOP.SListI columns
205− , SOP.SListI constraints
206− , Has sch schemas0 schema0
207− , schemas1 ~ Alter sch (Create tab ('Table (constraints :=> columns)) schema0) schemas0 )
208− => QualifiedAlias sch tab -- ^ the name of the table to add
209− -> NP (Aliased (ColumnTypeExpression schemas0)) columns
210− -- ^ the names and datatype of each column
211− -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
212− -- ^ constraints that must hold for the table
213− -> Definition schemas0 schemas1
214−createTable tab columns constraints = UnsafeDefinition $
215− "CREATE TABLE" <+> renderCreation tab columns constraints
217−{-| `createTableIfNotExists` creates a table if it doesn't exist, but does not add it to the schema.
218−Instead, the schema already has the table so if the table did not yet exist, the schema was wrong.
219−`createTableIfNotExists` fixes this. Interestingly, this property makes it an idempotent in
220−the `Category` of `Definition`s.
222−>>> :set -XOverloadedLabels -XTypeApplications
225− '[ "a" ::: 'NoDef :=> 'Null 'PGint4
226− , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
229−>>> type Schemas = Public '["tab" ::: 'Table Table]
233− setup :: Definition Schemas Schemas
234− setup = createTableIfNotExists #tab
235− (nullable int `as` #a :* nullable real `as` #b) Nil
238−CREATE TABLE IF NOT EXISTS "tab" ("a" int NULL, "b" real NULL);
240−createTableIfNotExists
241− :: ( Has sch schemas schema
242− , Has tab schema ('Table (constraints :=> columns))
243− , SOP.SListI columns
244− , SOP.SListI constraints )
245− => QualifiedAlias sch tab -- ^ the name of the table to add
246− -> NP (Aliased (ColumnTypeExpression schemas)) columns
247− -- ^ the names and datatype of each column
248− -> NP (Aliased (TableConstraintExpression sch tab schemas)) constraints
249− -- ^ constraints that must hold for the table
250− -> Definition schemas schemas
251−createTableIfNotExists tab columns constraints = UnsafeDefinition $
252− "CREATE TABLE IF NOT EXISTS"
253− <+> renderCreation tab columns constraints
255−-- helper function for `createTable` and `createTableIfNotExists`
257− :: ( KnownSymbol sch
259− , SOP.SListI columns
260− , SOP.SListI constraints )
261− => QualifiedAlias sch tab -- ^ the name of the table to add
262− -> NP (Aliased (ColumnTypeExpression schemas0)) columns
263− -- ^ the names and datatype of each column
264− -> NP (Aliased (TableConstraintExpression sch tab schemas1)) constraints
265− -- ^ constraints that must hold for the table
267−renderCreation tab columns constraints = renderSQL tab
269− ( renderCommaSeparated renderColumnDef columns
270− <> ( case constraints of
273− renderCommaSeparated renderConstraint constraints ) )
276− renderColumnDef :: Aliased (ColumnTypeExpression schemas) x -> ByteString
277− renderColumnDef (ty `As` column) =
278− renderSQL column <+> renderColumnTypeExpression ty
280− :: Aliased (TableConstraintExpression sch tab schemas) constraint
282− renderConstraint (constraint `As` alias) =
283− "CONSTRAINT" <+> renderSQL alias <+> renderSQL constraint
285−-- | Data types are a way to limit the kind of data that can be stored in a
286−-- table. For many applications, however, the constraint they provide is
287−-- too coarse. For example, a column containing a product price should
288−-- probably only accept positive values. But there is no standard data type
289−-- that accepts only positive numbers. Another issue is that you might want
290−-- to constrain column data with respect to other columns or rows.
291−-- For example, in a table containing product information,
292−-- there should be only one row for each product number.
293−-- `TableConstraint`s give you as much control over the data in your tables
294−-- as you wish. If a user attempts to store data in a column that would
295−-- violate a constraint, an error is raised. This applies
296−-- even if the value came from the default value definition.
297−newtype TableConstraintExpression
300− (schemas :: SchemasType)
301− (constraint :: TableConstraint)
302− = UnsafeTableConstraintExpression
303− { renderTableConstraintExpression :: ByteString }
304− deriving (GHC.Generic,Show,Eq,Ord,NFData)
306− (TableConstraintExpression sch tab schemas constraint) where
307− renderSQL = renderTableConstraintExpression
309−{-| A `check` constraint is the most generic `TableConstraint` type.
310−It allows you to specify that the value in a certain column must satisfy
311−a Boolean (truth-value) expression.
315− "tab" ::: 'Table ('[ "inequality" ::: 'Check '["a","b"]] :=> '[
316− "a" ::: 'NoDef :=> 'NotNull 'PGint4,
317− "b" ::: 'NoDef :=> 'NotNull 'PGint4
323− definition :: Definition (Public '[]) (Public Schema)
324− definition = createTable #tab
325− ( (int & notNullable) `as` #a :*
326− (int & notNullable) `as` #b )
327− ( check (#a :* #b) (#a .> #b) `as` #inequality )
330−>>> printSQL definition
331−CREATE TABLE "tab" ("a" int NOT NULL, "b" int NOT NULL, CONSTRAINT "inequality" CHECK (("a" > "b")));
334− :: ( Has sch schemas schema
335− , Has tab schema ('Table table)
336− , HasAll aliases (TableToRow table) subcolumns )
338− -- ^ specify the subcolumns which are getting checked
339− -> (forall t. Condition '[] '[] 'Ungrouped schemas '[] '[t ::: subcolumns])
340− -- ^ a closed `Condition` on those subcolumns
341− -> TableConstraintExpression sch tab schemas ('Check aliases)
342−check _cols condition = UnsafeTableConstraintExpression $
343− "CHECK" <+> parenthesized (renderSQL condition)
345−{-| A `unique` constraint ensure that the data contained in a column,
346−or a group of columns, is unique among all the rows in the table.
350− "tab" ::: 'Table( '[ "uq_a_b" ::: 'Unique '["a","b"]] :=> '[
351− "a" ::: 'NoDef :=> 'Null 'PGint4,
352− "b" ::: 'NoDef :=> 'Null 'PGint4
358− definition :: Definition (Public '[]) (Public Schema)
359− definition = createTable #tab
360− ( (int & nullable) `as` #a :*
361− (int & nullable) `as` #b )
362− ( unique (#a :* #b) `as` #uq_a_b )
365−>>> printSQL definition
366−CREATE TABLE "tab" ("a" int NULL, "b" int NULL, CONSTRAINT "uq_a_b" UNIQUE ("a", "b"));
369− :: ( Has sch schemas schema
370− , Has tab schema('Table table)
371− , HasAll aliases (TableToRow table) subcolumns )
373− -- ^ specify subcolumns which together are unique for each row
374− -> TableConstraintExpression sch tab schemas ('Unique aliases)
375−unique columns = UnsafeTableConstraintExpression $
376− "UNIQUE" <+> parenthesized (renderSQL columns)
378−{-| A `primaryKey` constraint indicates that a column, or group of columns,
379−can be used as a unique identifier for rows in the table.
380−This requires that the values be both unique and not null.
384− "tab" ::: 'Table ('[ "pk_id" ::: 'PrimaryKey '["id"]] :=> '[
385− "id" ::: 'Def :=> 'NotNull 'PGint4,
386− "name" ::: 'NoDef :=> 'NotNull 'PGtext
392− definition :: Definition (Public '[]) (Public Schema)
393− definition = createTable #tab
394− ( serial `as` #id :*
395− (text & notNullable) `as` #name )
396− ( primaryKey #id `as` #pk_id )
399−>>> printSQL definition
400−CREATE TABLE "tab" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_id" PRIMARY KEY ("id"));
403− :: ( Has sch schemas schema
404− , Has tab schema ('Table table)
405− , HasAll aliases (TableToColumns table) subcolumns
406− , AllNotNull subcolumns )
408− -- ^ specify the subcolumns which together form a primary key.
409− -> TableConstraintExpression sch tab schemas ('PrimaryKey aliases)
410−primaryKey columns = UnsafeTableConstraintExpression $
411− "PRIMARY KEY" <+> parenthesized (renderSQL columns)
413−{-| A `foreignKey` specifies that the values in a column
414−(or a group of columns) must match the values appearing in some row of
415−another table. We say this maintains the referential integrity
416−between two related tables.
420− '[ "users" ::: 'Table (
421− '[ "pk_users" ::: 'PrimaryKey '["id"] ] :=>
422− '[ "id" ::: 'Def :=> 'NotNull 'PGint4
423− , "name" ::: 'NoDef :=> 'NotNull 'PGtext
425− , "emails" ::: 'Table (
426− '[ "pk_emails" ::: 'PrimaryKey '["id"]
427− , "fk_user_id" ::: 'ForeignKey '["user_id"] "users" '["id"]
429− '[ "id" ::: 'Def :=> 'NotNull 'PGint4
430− , "user_id" ::: 'NoDef :=> 'NotNull 'PGint4
431− , "email" ::: 'NoDef :=> 'Null 'PGtext
438− setup :: Definition (Public '[]) (Public Schema)
441− ( serial `as` #id :*
442− (text & notNullable) `as` #name )
443− ( primaryKey #id `as` #pk_users ) >>>
445− ( serial `as` #id :*
446− (int & notNullable) `as` #user_id :*
447− (text & nullable) `as` #email )
448− ( primaryKey #id `as` #pk_emails :*
449− foreignKey #user_id #users #id
450− OnDeleteCascade OnUpdateCascade `as` #fk_user_id )
453−CREATE TABLE "users" ("id" serial, "name" text NOT NULL, CONSTRAINT "pk_users" PRIMARY KEY ("id"));
454−CREATE TABLE "emails" ("id" serial, "user_id" int NOT NULL, "email" text NULL, CONSTRAINT "pk_emails" PRIMARY KEY ("id"), CONSTRAINT "fk_user_id" FOREIGN KEY ("user_id") REFERENCES "users" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
456−A `foreignKey` can even be a table self-reference.
460− '[ "employees" ::: 'Table (
461− '[ "employees_pk" ::: 'PrimaryKey '["id"]
462− , "employees_employer_fk" ::: 'ForeignKey '["employer_id"] "employees" '["id"]
464− '[ "id" ::: 'Def :=> 'NotNull 'PGint4
465− , "name" ::: 'NoDef :=> 'NotNull 'PGtext
466− , "employer_id" ::: 'NoDef :=> 'Null 'PGint4
473− setup :: Definition (Public '[]) (Public Schema)
475− createTable #employees
476− ( serial `as` #id :*
477− (text & notNullable) `as` #name :*
478− (integer & nullable) `as` #employer_id )
479− ( primaryKey #id `as` #employees_pk :*
480− foreignKey #employer_id #employees #id
481− OnDeleteCascade OnUpdateCascade `as` #employees_employer_fk )
484−CREATE TABLE "employees" ("id" serial, "name" text NOT NULL, "employer_id" integer NULL, CONSTRAINT "employees_pk" PRIMARY KEY ("id"), CONSTRAINT "employees_employer_fk" FOREIGN KEY ("employer_id") REFERENCES "employees" ("id") ON DELETE CASCADE ON UPDATE CASCADE);
487− :: (ForeignKeyed schemas sch schema child parent
493− -- ^ column or columns in the table
495− -- ^ reference table
496− -> NP Alias refcolumns
497− -- ^ reference column or columns in the reference table
499− -- ^ what to do when reference is deleted
501− -- ^ what to do when reference is updated
502− -> TableConstraintExpression sch child schemas
503− ('ForeignKey columns parent refcolumns)
504−foreignKey keys parent refs ondel onupd = UnsafeTableConstraintExpression $
505− "FOREIGN KEY" <+> parenthesized (renderSQL keys)
506− <+> "REFERENCES" <+> renderSQL parent
507− <+> parenthesized (renderSQL refs)
511−-- | A constraint synonym between types involved in a foreign key constraint.
512−type ForeignKeyed schemas
520− ( Has sch schemas schema
521− , Has child schema ('Table table)
522− , Has parent schema ('Table reftable)
523− , HasAll columns (TableToColumns table) tys
524− , reftable ~ (constraints :=> cols)
525− , HasAll refcolumns cols reftys
526− , SOP.AllZip SamePGType tys reftys
527− , Uniquely refcolumns constraints )
529−-- | `OnDeleteClause` indicates what to do with rows that reference a deleted row.
532− -- ^ if any referencing rows still exist when the constraint is checked,
533− -- an error is raised
534− | OnDeleteRestrict -- ^ prevents deletion of a referenced row
536− -- ^ specifies that when a referenced row is deleted,
537− -- row(s) referencing it should be automatically deleted as well
538− deriving (GHC.Generic,Show,Eq,Ord)
539−instance NFData OnDeleteClause
540−-- | Render `OnDeleteClause`.
541−instance RenderSQL OnDeleteClause where
543− OnDeleteNoAction -> "ON DELETE NO ACTION"
544− OnDeleteRestrict -> "ON DELETE RESTRICT"
545− OnDeleteCascade -> "ON DELETE CASCADE"
547−-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
548−-- when a referenced column is changed (updated).
551− -- ^ if any referencing rows has not changed when the constraint is checked,
552− -- an error is raised
553− | OnUpdateRestrict -- ^ prevents update of a referenced row
555− -- ^ the updated values of the referenced column(s) should be copied
556− -- into the referencing row(s)
557− deriving (GHC.Generic,Show,Eq,Ord)
558−instance NFData OnUpdateClause
560−-- | Render `OnUpdateClause`.
561−instance RenderSQL OnUpdateClause where
563− OnUpdateNoAction -> "ON UPDATE NO ACTION"
564− OnUpdateRestrict -> "ON UPDATE RESTRICT"
565− OnUpdateCascade -> "ON UPDATE CASCADE"
567−{-----------------------------------------
569−-----------------------------------------}
573−-- definition :: Definition '["muh_schema" ::: schema, "public" ::: public] '["public" ::: public]
574−-- definition = dropSchema #muh_schema
577−-- >>> printSQL definition
578−-- DROP SCHEMA "muh_schema";
580− :: Has sch schemas schema
582− -> Definition schemas (Drop sch schemas)
583−dropSchema sch = UnsafeDefinition $ "DROP SCHEMA" <+> renderSQL sch <> ";"
585−-- | `dropTable` removes a table from the schema.
589−-- definition :: Definition '["public" ::: '["muh_table" ::: 'Table t]] (Public '[])
590−-- definition = dropTable #muh_table
593−-- >>> printSQL definition
594−-- DROP TABLE "muh_table";
596− :: ( Has sch schemas schema
597− , Has tab schema ('Table table))
598− => QualifiedAlias sch tab -- ^ table to remove
599− -> Definition schemas (Alter sch (Drop tab schema) schemas)
600−dropTable tab = UnsafeDefinition $ "DROP TABLE" <+> renderSQL tab <> ";"
602−{-----------------------------------------
604−-----------------------------------------}
606−-- | `alterTable` changes the definition of a table from the schema.
608− :: (Has sch schemas schema, Has tab schema ('Table table0))
609− => QualifiedAlias sch tab -- ^ table to alter
610− -> AlterTable sch tab schemas table1 -- ^ alteration to perform
611− -> Definition schemas (Alter sch (Alter tab ('Table table1) schema) schemas)
612−alterTable tab alteration = UnsafeDefinition $
615− <+> renderAlterTable alteration
618−-- | `alterTableRename` changes the name of a table from the schema.
620−-- >>> printSQL $ alterTableRename #foo #bar
621−-- ALTER TABLE "foo" RENAME TO "bar";
623− :: (KnownSymbol table0, KnownSymbol table1)
624− => Alias table0 -- ^ table to rename
625− -> Alias table1 -- ^ what to rename it
626− -> Definition schema (Rename table0 table1 schema)
627−alterTableRename table0 table1 = UnsafeDefinition $
628− "ALTER TABLE" <+> renderSQL table0
629− <+> "RENAME TO" <+> renderSQL table1 <> ";"
631−-- | An `AlterTable` describes the alteration to perform on the columns
636− (schemas :: SchemasType)
637− (table :: TableType) =
638− UnsafeAlterTable {renderAlterTable :: ByteString}
639− deriving (GHC.Generic,Show,Eq,Ord,NFData)
641−-- | An `addConstraint` adds a table constraint.
645−-- definition :: Definition
646−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
647−-- '["public" ::: '["tab" ::: 'Table ('["positive" ::: 'Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
648−-- definition = alterTable #tab (addConstraint #positive (check #col (#col .> 0)))
649−-- in printSQL definition
651−-- ALTER TABLE "tab" ADD CONSTRAINT "positive" CHECK (("col" > 0));
653− :: ( KnownSymbol alias
654− , Has sch schemas schema
655− , Has tab schema ('Table table0)
656− , table0 ~ (constraints :=> columns)
657− , table1 ~ (Create alias constraint constraints :=> columns) )
659− -> TableConstraintExpression sch tab schemas constraint
660− -- ^ constraint to add
661− -> AlterTable sch tab schemas table1
662−addConstraint alias constraint = UnsafeAlterTable $
663− "ADD" <+> "CONSTRAINT" <+> renderSQL alias
664− <+> renderSQL constraint
666−-- | A `dropConstraint` drops a table constraint.
670−-- definition :: Definition
671−-- '["public" ::: '["tab" ::: 'Table ('["positive" ::: Check '["col"]] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
672−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
673−-- definition = alterTable #tab (dropConstraint #positive)
674−-- in printSQL definition
676−-- ALTER TABLE "tab" DROP CONSTRAINT "positive";
678− :: ( KnownSymbol constraint
679− , Has sch schemas schema
680− , Has tab schema ('Table table0)
681− , table0 ~ (constraints :=> columns)
682− , table1 ~ (Drop constraint constraints :=> columns) )
684− -- ^ constraint to drop
685− -> AlterTable sch tab schemas table1
686−dropConstraint constraint = UnsafeAlterTable $
687− "DROP" <+> "CONSTRAINT" <+> renderSQL constraint
689−-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
690−class AddColumn ty where
691− -- | `addColumn` adds a new column, initially filled with whatever
692− -- default value is given or with @NULL@.
696− -- definition :: Definition
697− -- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
698− -- '["public" ::: '["tab" ::: 'Table ('[] :=>
699− -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
700− -- , "col2" ::: 'Def :=> 'Null 'PGtext ])]]
701− -- definition = alterTable #tab (addColumn #col2 (text & nullable & default_ "foo"))
702− -- in printSQL definition
704− -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL DEFAULT E'foo';
708− -- definition :: Definition
709− -- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
710− -- '["public" ::: '["tab" ::: 'Table ('[] :=>
711− -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
712− -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
713− -- definition = alterTable #tab (addColumn #col2 (text & nullable))
714− -- in printSQL definition
716− -- ALTER TABLE "tab" ADD COLUMN "col2" text NULL;
718− :: ( KnownSymbol column
719− , Has sch schemas schema
720− , Has tab schema ('Table table0)
721− , table0 ~ (constraints :=> columns) )
722− => Alias column -- ^ column to add
723− -> ColumnTypeExpression schemas ty -- ^ type of the new column
724− -> AlterTable sch tab schemas (constraints :=> Create column ty columns)
725− addColumn column ty = UnsafeAlterTable $
726− "ADD COLUMN" <+> renderSQL column <+> renderColumnTypeExpression ty
727−instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
728−instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
730−-- | A `dropColumn` removes a column. Whatever data was in the column
731−-- disappears. Table constraints involving the column are dropped, too.
732−-- However, if the column is referenced by a foreign key constraint of
733−-- another table, PostgreSQL will not silently drop that constraint.
737−-- definition :: Definition
738−-- '["public" ::: '["tab" ::: 'Table ('[] :=>
739−-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
740−-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ])]]
741−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4])]]
742−-- definition = alterTable #tab (dropColumn #col2)
743−-- in printSQL definition
745−-- ALTER TABLE "tab" DROP COLUMN "col2";
747− :: ( KnownSymbol column
748− , Has sch schemas schema
749− , Has tab schema ('Table table0)
750− , table0 ~ (constraints :=> columns)
751− , table1 ~ (constraints :=> Drop column columns) )
752− => Alias column -- ^ column to remove
753− -> AlterTable sch tab schemas table1
754−dropColumn column = UnsafeAlterTable $
755− "DROP COLUMN" <+> renderSQL column
757−-- | A `renameColumn` renames a column.
761−-- definition :: Definition
762−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["foo" ::: 'NoDef :=> 'Null 'PGint4])]]
763−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["bar" ::: 'NoDef :=> 'Null 'PGint4])]]
764−-- definition = alterTable #tab (renameColumn #foo #bar)
765−-- in printSQL definition
767−-- ALTER TABLE "tab" RENAME COLUMN "foo" TO "bar";
769− :: ( KnownSymbol column0
770− , KnownSymbol column1
771− , Has sch schemas schema
772− , Has tab schema ('Table table0)
773− , table0 ~ (constraints :=> columns)
774− , table1 ~ (constraints :=> Rename column0 column1 columns) )
775− => Alias column0 -- ^ column to rename
776− -> Alias column1 -- ^ what to rename the column
777− -> AlterTable sch tab schemas table1
778−renameColumn column0 column1 = UnsafeAlterTable $
779− "RENAME COLUMN" <+> renderSQL column0 <+> "TO" <+> renderSQL column1
781−-- | An `alterColumn` alters a single column.
783− :: ( KnownSymbol column
784− , Has sch schemas schema
785− , Has tab schema ('Table table0)
786− , table0 ~ (constraints :=> columns)
787− , Has column columns ty0
788− , table1 ~ (constraints :=> Alter column ty1 columns))
789− => Alias column -- ^ column to alter
790− -> AlterColumn schemas ty0 ty1 -- ^ alteration to perform
791− -> AlterTable sch tab schemas table1
792−alterColumn column alteration = UnsafeAlterTable $
793− "ALTER COLUMN" <+> renderSQL column <+> renderAlterColumn alteration
795−-- | An `AlterColumn` describes the alteration to perform on a single column.
796−newtype AlterColumn (schemas :: SchemasType) (ty0 :: ColumnType) (ty1 :: ColumnType) =
797− UnsafeAlterColumn {renderAlterColumn :: ByteString}
798− deriving (GHC.Generic,Show,Eq,Ord,NFData)
800−-- | A `setDefault` sets a new default for a column. Note that this doesn't
801−-- affect any existing rows in the table, it just changes the default for
802−-- future insert and update commands.
806−-- definition :: Definition
807−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
808−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
809−-- definition = alterTable #tab (alterColumn #col (setDefault 5))
810−-- in printSQL definition
812−-- ALTER TABLE "tab" ALTER COLUMN "col" SET DEFAULT 5;
814− :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty -- ^ default value to set
815− -> AlterColumn schemas (constraint :=> ty) ('Def :=> ty)
816−setDefault expression = UnsafeAlterColumn $
817− "SET DEFAULT" <+> renderExpression expression
819−-- | A `dropDefault` removes any default value for a column.
823−-- definition :: Definition
824−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'Def :=> 'Null 'PGint4])]]
825−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
826−-- definition = alterTable #tab (alterColumn #col dropDefault)
827−-- in printSQL definition
829−-- ALTER TABLE "tab" ALTER COLUMN "col" DROP DEFAULT;
830−dropDefault :: AlterColumn schemas ('Def :=> ty) ('NoDef :=> ty)
831−dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
833−-- | A `setNotNull` adds a @NOT NULL@ constraint to a column.
834−-- The constraint will be checked immediately, so the table data must satisfy
835−-- the constraint before it can be added.
839−-- definition :: Definition
840−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
841−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
842−-- definition = alterTable #tab (alterColumn #col setNotNull)
843−-- in printSQL definition
845−-- ALTER TABLE "tab" ALTER COLUMN "col" SET NOT NULL;
847− :: AlterColumn schemas (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
848−setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
850−-- | A `dropNotNull` drops a @NOT NULL@ constraint from a column.
854−-- definition :: Definition
855−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
856−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'Null 'PGint4])]]
857−-- definition = alterTable #tab (alterColumn #col dropNotNull)
858−-- in printSQL definition
860−-- ALTER TABLE "tab" ALTER COLUMN "col" DROP NOT NULL;
862− :: AlterColumn schemas (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
863−dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
865−-- | An `alterType` converts a column to a different data type.
866−-- This will succeed only if each existing entry in the column can be
867−-- converted to the new type by an implicit cast.
871−-- definition :: Definition
872−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGint4])]]
873−-- '["public" ::: '["tab" ::: 'Table ('[] :=> '["col" ::: 'NoDef :=> 'NotNull 'PGnumeric])]]
875−-- alterTable #tab (alterColumn #col (alterType (numeric & notNullable)))
876−-- in printSQL definition
878−-- ALTER TABLE "tab" ALTER COLUMN "col" TYPE numeric NOT NULL;
879−alterType :: ColumnTypeExpression schemas ty -> AlterColumn schemas ty0 ty
880−alterType ty = UnsafeAlterColumn $ "TYPE" <+> renderColumnTypeExpression ty
883−>>> type ABC = '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4]
884−>>> type BC = '["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4]
887− definition :: Definition
888− '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC)]]
889− '[ "public" ::: '["abc" ::: 'Table ('[] :=> ABC), "bc" ::: 'View BC]]
891− createView #bc (select_ (#b :* #c) (from (table #abc)))
892−in printSQL definition
894−CREATE VIEW "bc" AS SELECT "b" AS "b", "c" AS "c" FROM "abc" AS "abc";
897− :: (KnownSymbol sch, KnownSymbol vw, Has sch schemas schema)
898− => QualifiedAlias sch vw -- ^ the name of the view to add
899− -> Query '[] '[] schemas '[] view -- ^ query
900− -> Definition schemas (Alter sch (Create vw ('View view) schema) schemas)
901−createView alias query = UnsafeDefinition $
902− "CREATE" <+> "VIEW" <+> renderSQL alias <+> "AS"
903− <+> renderQuery query <> ";"
909−-- definition :: Definition
910−-- '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])
911−-- , "bc" ::: 'View ('["b" ::: 'Null 'PGint4, "c" ::: 'Null 'PGint4])]]
912−-- '[ "public" ::: '["abc" ::: 'Table ('[] :=> '["a" ::: 'NoDef :=> 'Null 'PGint4, "b" ::: 'NoDef :=> 'Null 'PGint4, "c" ::: 'NoDef :=> 'Null 'PGint4])]]
913−-- definition = dropView #bc
914−-- in printSQL definition
918− :: (Has sch schemas schema, Has vw schema ('View view))
919− => QualifiedAlias sch vw -- ^ view to remove
920− -> Definition schemas (Alter sch (Drop vw schema) schemas)
921−dropView vw = UnsafeDefinition $ "DROP VIEW" <+> renderSQL vw <> ";"
923−-- | Enumerated types are created using the `createTypeEnum` command, for example
925−-- >>> printSQL $ (createTypeEnum #mood (label @"sad" :* label @"ok" :* label @"happy") :: Definition (Public '[]) '["public" ::: '["mood" ::: 'Typedef ('PGenum '["sad","ok","happy"])]])
926−-- CREATE TYPE "mood" AS ENUM ('sad', 'ok', 'happy');
928− :: (KnownSymbol enum, Has sch schemas schema, SOP.All KnownSymbol labels)
929− => QualifiedAlias sch enum
930− -- ^ name of the user defined enumerated type
931− -> NP PGlabel labels
932− -- ^ labels of the enumerated type
933− -> Definition schemas (Alter sch (Create enum ('Typedef ('PGenum labels)) schema) schemas)
934−createTypeEnum enum labels = UnsafeDefinition $
935− "CREATE" <+> "TYPE" <+> renderSQL enum <+> "AS" <+> "ENUM" <+>
936− parenthesized (renderSQL labels) <> ";"
938−-- | Enumerated types can also be generated from a Haskell type, for example
940−-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
941−-- >>> instance SOP.Generic Schwarma
942−-- >>> instance SOP.HasDatatypeInfo Schwarma
945−-- createSchwarma :: Definition (Public '[]) '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]]
946−-- createSchwarma = createTypeEnumFrom @Schwarma #schwarma
948−-- printSQL createSchwarma
950−-- CREATE TYPE "schwarma" AS ENUM ('Beef', 'Lamb', 'Chicken');
952− :: forall hask sch enum schemas schema.
954− , SOP.All KnownSymbol (LabelsPG hask)
956− , Has sch schemas schema )
957− => QualifiedAlias sch enum
958− -- ^ name of the user defined enumerated type
959− -> Definition schemas (Alter sch (Create enum ('Typedef (PG (Enumerated hask))) schema) schemas)
960−createTypeEnumFrom enum = createTypeEnum enum
961− (SOP.hpure label :: NP PGlabel (LabelsPG hask))
963−{- | `createTypeComposite` creates a composite type. The composite type is
964−specified by a list of attribute names and data types.
967−type PGcomplex = 'PGcomposite
968− '[ "real" ::: 'NotNull 'PGfloat8
969− , "imaginary" ::: 'NotNull 'PGfloat8 ]
974− setup :: Definition (Public '[]) '["public" ::: '["complex" ::: 'Typedef PGcomplex]]
975− setup = createTypeComposite #complex
976− (float8 `as` #real :* float8 `as` #imaginary)
979−CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
982− :: (KnownSymbol ty, Has sch schemas schema, SOP.SListI fields)
983− => QualifiedAlias sch ty
984− -- ^ name of the user defined composite type
985− -> NP (Aliased (TypeExpression schemas)) fields
986− -- ^ list of attribute names and data types
987− -> Definition schemas (Alter sch (Create ty ('Typedef ('PGcomposite fields)) schema) schemas)
988−createTypeComposite ty fields = UnsafeDefinition $
989− "CREATE" <+> "TYPE" <+> renderSQL ty <+> "AS" <+> parenthesized
990− (renderCommaSeparated renderField fields) <> ";"
992− renderField :: Aliased (TypeExpression schemas) x -> ByteString
993− renderField (typ `As` alias) =
994− renderSQL alias <+> renderSQL typ
996−-- | Composite types can also be generated from a Haskell type, for example
998−-- >>> data Complex = Complex {real :: Double, imaginary :: Double} deriving GHC.Generic
999−-- >>> instance SOP.Generic Complex
1000−-- >>> instance SOP.HasDatatypeInfo Complex
1001−-- >>> type Schema = '["complex" ::: 'Typedef (PG (Composite Complex))]
1004−-- createComplex :: Definition (Public '[]) (Public Schema)
1005−-- createComplex = createTypeCompositeFrom @Complex #complex
1007−-- printSQL createComplex
1009−-- CREATE TYPE "complex" AS ("real" float8, "imaginary" float8);
1010−createTypeCompositeFrom
1011− :: forall hask sch ty schemas schema.
1012− ( SOP.All (FieldTyped schemas) (RowPG hask)
1014− , Has sch schemas schema )
1015− => QualifiedAlias sch ty
1016− -- ^ name of the user defined composite type
1017− -> Definition schemas (Alter sch (Create ty ( 'Typedef (PG (Composite hask))) schema) schemas)
1018−createTypeCompositeFrom ty = createTypeComposite ty
1019− (SOP.hcpure (SOP.Proxy :: SOP.Proxy (FieldTyped schemas)) fieldtype
1020− :: NP (Aliased (TypeExpression schemas)) (RowPG hask))
1023−`createDomain` creates a new domain. A domain is essentially a data type
1024−with constraints (restrictions on the allowed set of values).
1026−Domains are useful for abstracting common constraints on fields
1027−into a single location for maintenance. For example, several tables might
1028−contain email address columns, all requiring the same `check` constraint
1029−to verify the address syntax. Define a domain rather than setting up
1030−each table's constraint individually.
1034− createPositive :: Definition (Public '[]) (Public '["positive" ::: 'Typedef 'PGfloat4])
1035− createPositive = createDomain #positive real (#value .> 0 .&& (#value & isNotNull))
1036−in printSQL createPositive
1038−CREATE DOMAIN "positive" AS real CHECK ((("value" > 0) AND "value" IS NOT NULL));
1041− :: (Has sch schemas schema, KnownSymbol dom)
1042− => QualifiedAlias sch dom
1043− -> (forall null. TypeExpression schemas (null ty))
1044− -> (forall tab. Condition '[] '[] 'Ungrouped schemas '[] '[tab ::: '["value" ::: 'Null ty]])
1045− -> Definition schemas (Alter sch (Create dom ('Typedef ty) schema) schemas)
1046−createDomain dom ty condition =
1047− UnsafeDefinition $ "CREATE DOMAIN" <+> renderSQL dom
1048− <+> "AS" <+> renderTypeExpression ty
1049− <+> "CHECK" <+> parenthesized (renderSQL condition) <> ";"
1054− createSmallIntRange :: Definition (Public '[]) (Public '["int2range" ::: 'Typedef ('PGrange 'PGint2)])
1055− createSmallIntRange = createTypeRange #int2range int2
1056−in printSQL createSmallIntRange
1058−CREATE TYPE "int2range" AS RANGE (subtype = int2);
1061− :: (Has sch schemas schema, KnownSymbol range)
1062− => QualifiedAlias sch range
1063− -> (forall null. TypeExpression schemas (null ty))
1064− -> Definition schemas (Alter sch (Create range ('Typedef ('PGrange ty)) schema) schemas)
1065−createTypeRange range ty = UnsafeDefinition $
1066− "CREATE" <+> "TYPE" <+> renderSQL range <+> "AS" <+> "RANGE" <+>
1067− parenthesized ("subtype" <+> "=" <+> renderTypeExpression ty) <> ";"
1071−type Table = '[] :=>
1072− '[ "a" ::: 'NoDef :=> 'Null 'PGint4
1073− , "b" ::: 'NoDef :=> 'Null 'PGfloat4 ]
1078− setup :: Definition (Public '[]) (Public '["tab" ::: 'Table Table, "ix" ::: 'Index])
1080− createTable #tab (nullable int `as` #a :* nullable real `as` #b) Nil >>>
1081− createIndex #ix #tab Gin [#a & AscNullsFirst, #b & AscNullsLast]
1084−CREATE TABLE "tab" ("a" int NULL, "b" real NULL);
1085−CREATE INDEX "ix" ON "tab" USING gin (("a" ASC NULLS FIRST), ("b" ASC NULLS LAST));
1088− :: (Has sch schemas schema, Has tab schema ('Table table), KnownSymbol ix)
1090− -> QualifiedAlias sch tab
1092− -> [SortExpression '[] '[] 'Ungrouped schemas '[] '[tab ::: TableToRow table]]
1093− -> Definition schemas (Alter sch (Create ix 'Index schema) schemas)
1094−createIndex ix tab method cols = UnsafeDefinition $
1095− "CREATE" <+> "INDEX" <+> renderSQL ix <+> "ON" <+> renderSQL tab
1096− <+> "USING" <+> renderSQL method
1097− <+> parenthesized (commaSeparated (parenthesized . renderSQL <$> cols))
1107− deriving stock (Eq, Ord, Show, GHC.Generic)
1108− deriving anyclass (SOP.HasDatatypeInfo, SOP.Generic)
1109−instance RenderSQL IndexMethod where
1119− :: (Has sch schemas schema, KnownSymbol fun)
1120− => QualifiedAlias sch fun
1121− -> TypeExpression schemas arg
1122− -> TypeExpression schemas ret
1123− -> FunctionDefinition schemas arg ret
1124− -> Definition schemas (Alter sch (Create fun ('Function ('UnaryFun arg ret)) schema) schemas)
1125−createFunction fun arg ret fundef = UnsafeDefinition $
1126− "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
1127− <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1129−createOrReplaceFunction
1130− :: (Has sch schemas schema, Has fun schema ('Function ('UnaryFun arg ret)))
1131− => QualifiedAlias sch fun
1132− -> TypeExpression schemas arg
1133− -> TypeExpression schemas ret
1134− -> FunctionDefinition schemas arg ret
1135− -> Definition schemas (Alter sch (Alter fun ('Function ('UnaryFun arg ret)) schema) schemas)
1136−createOrReplaceFunction fun arg ret fundef = UnsafeDefinition $
1137− "CREATE" <+> "FUNCTION" <+> renderSQL fun <+> parenthesized (renderSQL arg)
1138− <+> "RETURNS" <+> renderSQL ret <+> renderSQL fundef <> ";"
1141− :: (Has sch schemas schema, Has fun schema ('Function funty))
1142− => QualifiedAlias sch fun
1143− -- ^ name of the user defined function
1144− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1145−dropFunction fun = UnsafeDefinition $
1146− "DROP" <+> "Function" <+> renderSQL fun <> ";"
1148−newtype FunctionDefinition schemas arg ret = UnsafeFunctionDefinition
1149− { renderFunctionDefinition :: ByteString }
1150− deriving (Eq,Show,GHC.Generic,NFData)
1151−instance RenderSQL (FunctionDefinition schemas arg ret) where
1152− renderSQL = renderFunctionDefinition
1155−-- >>> printSQL (dropIndex #ix :: Definition (Public '["ix" ::: 'Index]) (Public '[]))
1158− :: (Has sch schemas schema, Has ix schema 'Index)
1159− => QualifiedAlias sch ix
1160− -- ^ name of the user defined index
1161− -> Definition schemas (Alter sch (Drop ix schema) schemas)
1162−dropIndex ix = UnsafeDefinition $ "DROP" <+> "INDEX" <+> renderSQL ix <> ";"
1164−-- | Lift `PGTyped` to a field
1165−class FieldTyped schemas ty where
1166− fieldtype :: Aliased (TypeExpression schemas) ty
1167−instance (KnownSymbol alias, PGTyped schemas ty)
1168− => FieldTyped schemas (alias ::: ty) where
1169− fieldtype = pgtype `As` Alias
1173−-- >>> data Schwarma = Beef | Lamb | Chicken deriving GHC.Generic
1174−-- >>> instance SOP.Generic Schwarma
1175−-- >>> instance SOP.HasDatatypeInfo Schwarma
1176−-- >>> printSQL (dropType #schwarma :: Definition '["public" ::: '["schwarma" ::: 'Typedef (PG (Enumerated Schwarma))]] (Public '[]))
1177−-- DROP TYPE "schwarma";
1179− :: (Has sch schemas schema, Has td schema ('Typedef ty))
1180− => QualifiedAlias sch td
1181− -- ^ name of the user defined type
1182− -> Definition schemas (Alter sch (Drop td schema) schemas)
1183−dropType tydef = UnsafeDefinition $ "DROP" <+> "TYPE" <+> renderSQL tydef <> ";"
1185−-- | `ColumnTypeExpression`s are used in `createTable` commands.
1186−newtype ColumnTypeExpression (schemas :: SchemasType) (ty :: ColumnType)
1187− = UnsafeColumnTypeExpression { renderColumnTypeExpression :: ByteString }
1188− deriving (GHC.Generic,Show,Eq,Ord,NFData)
1189−instance RenderSQL (ColumnTypeExpression schemas ty) where
1190− renderSQL = renderColumnTypeExpression
1192−-- | used in `createTable` commands as a column constraint to note that
1193−-- @NULL@ may be present in a column
1195− :: TypeExpression schemas (nullity ty)
1196− -> ColumnTypeExpression schemas ('NoDef :=> 'Null ty)
1197−nullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NULL"
1199−-- | used in `createTable` commands as a column constraint to ensure
1200−-- @NULL@ is not present in a column
1202− :: TypeExpression schemas (nullity ty)
1203− -> ColumnTypeExpression schemas ('NoDef :=> 'NotNull ty)
1204−notNullable ty = UnsafeColumnTypeExpression $ renderSQL ty <+> "NOT NULL"
1206−-- | used in `createTable` commands as a column constraint to give a default
1208− :: Expression '[] '[] 'Ungrouped schemas '[] '[] ty
1209− -> ColumnTypeExpression schemas ('NoDef :=> ty)
1210− -> ColumnTypeExpression schemas ('Def :=> ty)
1211−default_ x ty = UnsafeColumnTypeExpression $
1212− renderSQL ty <+> "DEFAULT" <+> renderExpression x
1214−-- | not a true type, but merely a notational convenience for creating
1215−-- unique identifier columns with type `PGint2`
1216−serial2, smallserial
1217− :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint2)
1218−serial2 = UnsafeColumnTypeExpression "serial2"
1219−smallserial = UnsafeColumnTypeExpression "smallserial"
1220−-- | not a true type, but merely a notational convenience for creating
1221−-- unique identifier columns with type `PGint4`
1223− :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint4)
1224−serial4 = UnsafeColumnTypeExpression "serial4"
1225−serial = UnsafeColumnTypeExpression "serial"
1226−-- | not a true type, but merely a notational convenience for creating
1227−-- unique identifier columns with type `PGint8`
1229− :: ColumnTypeExpression schemas ('Def :=> 'NotNull 'PGint8)
1230−serial8 = UnsafeColumnTypeExpression "serial8"
1231−bigserial = UnsafeColumnTypeExpression "bigserial"