on dev help sign in
some changes to Alter
commit7d644fe1557118ac7faa95b81b96e6bb907d7194 authorEitan Chatav <eitan.chatav@gmail.com> dateSun 25 Mar 2018, 10:55 (9 years ago) parentbf8b907
2 files changed+65−78
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+65−66 squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs−12
squeal-postgresql/src/Squeal/PostgreSQL/Definition.hs+65−66
@@ -52,11 +52,10 @@ module Squeal.PostgreSQL.Definition
5252 -- * Alter
5353 , alterTable
5454 , alterTableRename
55+ , AlterTable (UnsafeAlterTable, renderAlterTable)
5556 , addConstraint
5657 , dropConstraint
57− , AlterTable (UnsafeAlterTable, renderAlterTable)
58− , addColumnDefault
59− , addColumnNull
58+ , AddColumn (addColumn)
6059 , dropColumn
6160 , renameColumn
6261 , alterColumn
@@ -344,7 +343,7 @@ type ForeignKeyed schema table reftable subcolumns refsubcolumns
344343 , SOP.SListI subcolumns
345344 , SOP.SListI refsubcolumns )
346345
347−-- | `OnDelete` indicates what to do with rows that reference a deleted row.
346+-- | `OnDeleteClause` indicates what to do with rows that reference a deleted row.
348347data OnDeleteClause
349348 = OnDeleteNoAction
350349 -- ^ if any referencing rows still exist when the constraint is checked,
@@ -363,7 +362,7 @@ renderOnDeleteClause = \case
363362 OnDeleteRestrict -> "ON DELETE RESTRICT"
364363 OnDeleteCascade -> "ON DELETE CASCADE"
365364
366−-- | Analagous to `OnDelete` there is also `OnUpdate` which is invoked
365+-- | Analagous to `OnDeleteClause` there is also `OnUpdateClause` which is invoked
367366-- when a referenced column is changed (updated).
368367data OnUpdateClause
369368 = OnUpdateNoAction
@@ -426,6 +425,15 @@ alterTableRename table0 table1 = UnsafeDefinition $
426425 "ALTER TABLE" <+> renderAlias table0
427426 <+> "RENAME TO" <+> renderAlias table1 <> ";"
428427
428+-- | An `AlterTable` describes the alteration to perform on the columns
429+-- of a table.
430+newtype AlterTable
431+ (schema :: TablesType)
432+ (table0 :: TableType)
433+ (table1 :: TableType) =
434+ UnsafeAlterTable {renderAlterTable :: ByteString}
435+ deriving (GHC.Generic,Show,Eq,Ord,NFData)
436+
429437-- | An `addConstraint` adds a table constraint.
430438--
431439-- >>> :{
@@ -438,10 +446,12 @@ alterTableRename table0 table1 = UnsafeDefinition $
438446-- :}
439447-- "ALTER TABLE tab ADD CONSTRAINT positive CHECK ((col > 0));"
440448addConstraint
441− :: KnownSymbol constraintName
442− => Alias constraintName
443− -> TableConstraintExpression schema (TableToColumns table) constraint
444− -> AlterTable schema table (AddConstraint constraintName constraint table)
449+ :: KnownSymbol alias
450+ => Alias alias
451+ -> TableConstraintExpression schema columns constraint
452+ -- ^ constraint to add
453+ -> AlterTable schema (constraints :=> columns)
454+ ((Create alias constraint constraints) :=> columns)
445455addConstraint constraintName constraint = UnsafeAlterTable $
446456 "ADD" <+> "CONSTRAINT" <+> renderAlias constraintName
447457 <+> renderTableConstraintExpression constraint
@@ -460,65 +470,51 @@ addConstraint constraintName constraint = UnsafeAlterTable $
460470dropConstraint
461471 :: KnownSymbol constraintName
462472 => Alias constraintName
473+ -- ^ constraint to drop
463474 -> AlterTable schema
464475 (constraints :=> columns)
465476 (Drop constraintName constraints :=> columns)
466477dropConstraint constraint = UnsafeAlterTable $
467478 "DROP" <+> "CONSTRAINT" <+> renderAlias constraint
468479
469−-- | An `AlterTable` describes the alteration to perform on the columns
470−-- of a table.
471−newtype AlterTable
472− (schema :: TablesType)
473− (table0 :: TableType)
474− (table1 :: TableType) =
475− UnsafeAlterTable {renderAlterTable :: ByteString}
476− deriving (GHC.Generic,Show,Eq,Ord,NFData)
477−
478−-- | An `addColumnDefault` adds a new `Optional` column. The new column is
479−-- initially filled with whatever default value is given.
480−--
481−-- >>> :{
482−-- let
483−-- definition :: Definition
484−-- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
485−-- '["tab" ::: '[] :=>
486−-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
487−-- , "col2" ::: 'Def :=> 'Null 'PGtext ]]
488−-- definition = alterTable #tab
489−-- (addColumnDefault #col2 (text & default_ "foo"))
490−-- in renderDefinition definition
491−-- :}
492−-- "ALTER TABLE tab ADD COLUMN col2 text DEFAULT E'foo';"
493−addColumnDefault
494− :: KnownSymbol column
495− => Alias column -- ^ column to add
496− -> TypeExpression ('Def :=> ty) -- ^ type of the new column
497− -> AlterTable schema table (AddColumn column ('Def :=> ty) table)
498−addColumnDefault column ty = UnsafeAlterTable $
499− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
500−
501−-- | An `addColumnNull` adds a new `Null` column. The new column is initially
502−-- filled with @NULL@s.
503−--
504−-- >>> :{
505−-- let
506−-- definition :: Definition
507−-- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
508−-- '["tab" ::: '[] :=>
509−-- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
510−-- , "col2" ::: 'NoDef :=> 'Null 'PGtext ]]
511−-- definition = alterTable #tab (addColumnNull #col2 text)
512−-- in renderDefinition definition
513−-- :}
514−-- "ALTER TABLE tab ADD COLUMN col2 text;"
515−addColumnNull
516− :: KnownSymbol column
517− => Alias column -- ^ column to add
518− -> TypeExpression ('NoDef :=> 'Null ty) -- ^ type of the new column
519− -> AlterTable schema table (AddColumn column ('NoDef :=> 'Null ty) table)
520−addColumnNull column ty = UnsafeAlterTable $
521− "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
480+-- | An `AddColumn` is either @NULL@ or has @DEFAULT@.
481+class AddColumn ty where
482+ -- | `addColumn` adds a new column, initially filled with whatever
483+ -- default value is given or with @NULL@.
484+ --
485+ -- >>> :{
486+ -- let
487+ -- definition :: Definition
488+ -- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
489+ -- '["tab" ::: '[] :=>
490+ -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
491+ -- , "col2" ::: 'Def :=> 'Null 'PGtext ]]
492+ -- definition = alterTable #tab (addColumn #col2 (text & default_ "foo"))
493+ -- in renderDefinition definition
494+ -- :}
495+ -- "ALTER TABLE tab ADD COLUMN col2 text DEFAULT E'foo';"
496+ --
497+ -- >>> :{
498+ -- let
499+ -- definition :: Definition
500+ -- '["tab" ::: '[] :=> '["col1" ::: 'NoDef :=> 'Null 'PGint4]]
501+ -- '["tab" ::: '[] :=>
502+ -- '[ "col1" ::: 'NoDef :=> 'Null 'PGint4
503+ -- , "col2" ::: 'NoDef :=> 'Null 'PGtext ]]
504+ -- definition = alterTable #tab (addColumn #col2 text)
505+ -- in renderDefinition definition
506+ -- :}
507+ -- "ALTER TABLE tab ADD COLUMN col2 text;"
508+ addColumn
509+ :: KnownSymbol column
510+ => Alias column -- ^ column to add
511+ -> TypeExpression ty -- ^ type of the new column
512+ -> AlterTable schema (constraints :=> columns)
513+ (constraints :=> Create column ty columns)
514+ addColumn column ty = UnsafeAlterTable $
515+ "ADD COLUMN" <+> renderAlias column <+> renderTypeExpression ty
516+instance {-# OVERLAPPING #-} AddColumn ('Def :=> ty)
517+instance {-# OVERLAPPABLE #-} AddColumn ('NoDef :=> 'Null ty)
522518
523519-- | A `dropColumn` removes a column. Whatever data was in the column
524520-- disappears. Table constraints involving the column are dropped, too.
@@ -570,7 +566,8 @@ alterColumn
570566 :: (KnownSymbol column, Has column columns ty0)
571567 => Alias column -- ^ column to alter
572568 -> AlterColumn ty0 ty1 -- ^ alteration to perform
573− -> AlterTable schema ('[] :=> columns) ('[] :=> Alter column columns ty1)
569+ -> AlterTable schema (constraints :=> columns)
570+ (constraints :=> Alter column columns ty1)
574571alterColumn column alteration = UnsafeAlterTable $
575572 "ALTER COLUMN" <+> renderAlias column <+> renderAlterColumn alteration
576573
@@ -594,7 +591,7 @@ newtype AlterColumn (ty0 :: ColumnType) (ty1 :: ColumnType) =
594591-- "ALTER TABLE tab ALTER COLUMN col SET DEFAULT 5;"
595592setDefault
596593 :: Expression '[] 'Ungrouped '[] ty -- ^ default value to set
597− -> AlterColumn (constraints :=> ty) ('Def :=> ty)
594+ -> AlterColumn (constraint :=> ty) ('Def :=> ty)
598595setDefault expression = UnsafeAlterColumn $
599596 "SET DEFAULT" <+> renderExpression expression
600597
@@ -625,7 +622,8 @@ dropDefault = UnsafeAlterColumn $ "DROP DEFAULT"
625622-- in renderDefinition definition
626623-- :}
627624-- "ALTER TABLE tab ALTER COLUMN col SET NOT NULL;"
628−setNotNull :: AlterColumn (optionality ('Null ty)) (optionality ('NotNull ty))
625+setNotNull
626+ :: AlterColumn (constraint :=> 'Null ty) (constraint :=> 'NotNull ty)
629627setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
630628
631629-- | A `dropNotNull` drops a @NOT NULL@ constraint from a column.
@@ -639,7 +637,8 @@ setNotNull = UnsafeAlterColumn $ "SET NOT NULL"
639637-- in renderDefinition definition
640638-- :}
641639-- "ALTER TABLE tab ALTER COLUMN col DROP NOT NULL;"
642−dropNotNull :: AlterColumn (optionality ('NotNull ty)) (optionality ('Null ty))
640+dropNotNull
641+ :: AlterColumn (constraint :=> 'NotNull ty) (constraint :=> 'Null ty)
643642dropNotNull = UnsafeAlterColumn $ "DROP NOT NULL"
644643
645644-- | An `alterType` converts a column to a different data type.
squeal-postgresql/src/Squeal/PostgreSQL/Schema.hs−12
@@ -79,8 +79,6 @@ module Squeal.PostgreSQL.Schema
7979 , NullifyRelations
8080 , Join
8181 , Create
82− , AddColumn
83− , AddConstraint
8482 , Drop
8583 , Alter
8684 , Rename
@@ -462,16 +460,6 @@ type family Create alias x xs where
462460 Create alias x '[] = '[alias ::: x]
463461 Create alias y (x ': xs) = x ': Create alias y xs
464462
465−-- | Add a column to a `TableType` with
466−-- `Squeal.PostgreSQL.Definition.addColumn`.
467−type family AddColumn alias x y where
468− AddColumn alias x (constraints :=> xs) = constraints :=> Create alias x xs
469−
470−-- | Add a constraint to a `TableType` with
471−-- `Squeal.PostgreSQL.Definition.addConstraint`.
472−type family AddConstraint alias x y where
473− AddConstraint alias x (constraints :=> xs) = Create alias x constraints :=> xs
474−
475463-- | @Drop alias xs@ removes the type associated with @alias@ in @xs@
476464-- and is used in `Squeal.PostgreSQL.Definition.dropTable` statements
477465-- and in @ALTER TABLE@ `Squeal.PostgreSQL.Definition.dropColumn` statements.