mirror of
https://code.sup39.dev/repos/Wqawg
synced 2024-12-28 07:24:53 +09:00
Migration: Specify default value when adding non-null column
This commit is contained in:
parent
7218262223
commit
bff41673c4
3 changed files with 30 additions and 6 deletions
|
@ -79,7 +79,8 @@ class PersistSchema backend where
|
||||||
removeEntity
|
removeEntity
|
||||||
:: MonadIO m => EntityName -> SchemaT backend m ()
|
:: MonadIO m => EntityName -> SchemaT backend m ()
|
||||||
addField
|
addField
|
||||||
:: MonadIO m => EntityName -> Field -> SchemaT backend m ()
|
:: MonadIO m
|
||||||
|
=> EntityName -> Field -> Maybe Text -> SchemaT backend m ()
|
||||||
renameField
|
renameField
|
||||||
:: MonadIO m
|
:: MonadIO m
|
||||||
=> EntityName -> FieldName -> FieldName -> SchemaT backend m ()
|
=> EntityName -> FieldName -> FieldName -> SchemaT backend m ()
|
||||||
|
|
|
@ -20,6 +20,7 @@ where
|
||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
import Data.Monoid ((<>))
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Database.Persist.Sql (SqlBackend)
|
import Database.Persist.Sql (SqlBackend)
|
||||||
import Database.Persist.Types (SqlType (..))
|
import Database.Persist.Types (SqlType (..))
|
||||||
|
@ -92,9 +93,12 @@ schemaBackend = SqlSchemaBackend
|
||||||
, ssbDropTable = \ table -> mconcat
|
, ssbDropTable = \ table -> mconcat
|
||||||
[ "DROP TABLE ", table2sql table
|
[ "DROP TABLE ", table2sql table
|
||||||
]
|
]
|
||||||
, ssbAddColumn = \ table column -> mconcat
|
, ssbAddColumn = \ table column mdef -> mconcat
|
||||||
[ "ALTER TABLE ", table2sql table
|
[ "ALTER TABLE ", table2sql table
|
||||||
, " ADD COLUMN ", columnSql column
|
, " ADD COLUMN ", columnSql column
|
||||||
|
, case mdef of
|
||||||
|
Nothing -> T.empty
|
||||||
|
Just t -> " DEFAULT " <> t
|
||||||
]
|
]
|
||||||
, ssbRenameColumn = \ table old new -> mconcat
|
, ssbRenameColumn = \ table old new -> mconcat
|
||||||
[ "ALTER TABLE ", table2sql table
|
[ "ALTER TABLE ", table2sql table
|
||||||
|
@ -118,6 +122,16 @@ schemaBackend = SqlSchemaBackend
|
||||||
, " SET ", column2sql column, " = ", val
|
, " SET ", column2sql column, " = ", val
|
||||||
, " WHERE ", column2sql column, " IS NULL"
|
, " WHERE ", column2sql column, " IS NULL"
|
||||||
]
|
]
|
||||||
|
, ssbDefColumn = \ table column val -> mconcat
|
||||||
|
[ "ALTER TABLE ", table2sql table
|
||||||
|
, " ALTER COLUMN ", column2sql column
|
||||||
|
, " SET DEFAULT ", val
|
||||||
|
]
|
||||||
|
, ssbUndefColumn = \ table column -> mconcat
|
||||||
|
[ "ALTER TABLE ", table2sql table
|
||||||
|
, " ALTER COLUMN ", column2sql column
|
||||||
|
, " DROP DEFAULT"
|
||||||
|
]
|
||||||
, ssbDropColumn = \ table column -> mconcat
|
, ssbDropColumn = \ table column -> mconcat
|
||||||
[ "ALTER TABLE ", table2sql table
|
[ "ALTER TABLE ", table2sql table
|
||||||
, " DROP COLUMN ", column2sql column
|
, " DROP COLUMN ", column2sql column
|
||||||
|
|
|
@ -26,11 +26,13 @@ where
|
||||||
|
|
||||||
import Prelude
|
import Prelude
|
||||||
|
|
||||||
|
import Control.Monad (when)
|
||||||
import Control.Monad.IO.Class (MonadIO)
|
import Control.Monad.IO.Class (MonadIO)
|
||||||
import Control.Monad.Trans.Class (lift)
|
import Control.Monad.Trans.Class (lift)
|
||||||
import Control.Monad.Trans.Reader (ask)
|
import Control.Monad.Trans.Reader (ask)
|
||||||
import Data.Char (isUpper, toLower)
|
import Data.Char (isUpper, toLower)
|
||||||
import Data.Foldable (traverse_)
|
import Data.Foldable (traverse_)
|
||||||
|
import Data.Maybe (isJust)
|
||||||
import Data.Text (Text)
|
import Data.Text (Text)
|
||||||
import Database.Persist.Sql (Sql, SqlBackend, rawExecute)
|
import Database.Persist.Sql (Sql, SqlBackend, rawExecute)
|
||||||
import Database.Persist.Types (SqlType)
|
import Database.Persist.Types (SqlType)
|
||||||
|
@ -101,11 +103,13 @@ instance PersistSchema SqlBackend where
|
||||||
, ssbCreateTable :: TableName -> [Column] -> Sql
|
, ssbCreateTable :: TableName -> [Column] -> Sql
|
||||||
, ssbRenameTable :: TableName -> TableName -> Sql
|
, ssbRenameTable :: TableName -> TableName -> Sql
|
||||||
, ssbDropTable :: TableName -> Sql
|
, ssbDropTable :: TableName -> Sql
|
||||||
, ssbAddColumn :: TableName -> Column -> Sql
|
, ssbAddColumn :: TableName -> Column -> Maybe Text -> Sql
|
||||||
, ssbRenameColumn :: TableName -> ColumnName -> ColumnName -> Sql
|
, ssbRenameColumn :: TableName -> ColumnName -> ColumnName -> Sql
|
||||||
, ssbRetypeColumn :: TableName -> ColumnName -> SqlType -> Sql
|
, ssbRetypeColumn :: TableName -> ColumnName -> SqlType -> Sql
|
||||||
, ssbRenullColumn :: TableName -> ColumnName -> MaybeNull -> Sql
|
, ssbRenullColumn :: TableName -> ColumnName -> MaybeNull -> Sql
|
||||||
, ssbUnnullColumn :: TableName -> ColumnName -> Text -> Sql
|
, ssbUnnullColumn :: TableName -> ColumnName -> Text -> Sql
|
||||||
|
, ssbDefColumn :: TableName -> ColumnName -> Text -> Sql
|
||||||
|
, ssbUndefColumn :: TableName -> ColumnName -> Sql
|
||||||
, ssbDropColumn :: TableName -> ColumnName -> Sql
|
, ssbDropColumn :: TableName -> ColumnName -> Sql
|
||||||
, ssbAddUnique
|
, ssbAddUnique
|
||||||
:: TableName -> ConstraintName -> [ColumnName] -> Sql
|
:: TableName -> ConstraintName -> [ColumnName] -> Sql
|
||||||
|
@ -123,11 +127,16 @@ instance PersistSchema SqlBackend where
|
||||||
removeEntity name = do
|
removeEntity name = do
|
||||||
ssb <- ask
|
ssb <- ask
|
||||||
exec $ ssbDropTable ssb $ entity2table name
|
exec $ ssbDropTable ssb $ entity2table name
|
||||||
addField ent (Field name typ mnull) = do
|
addField ent (Field name typ mnull) mdef = do
|
||||||
ssb <- ask
|
ssb <- ask
|
||||||
exec $
|
exec $
|
||||||
ssbAddColumn ssb (entity2table ent) $
|
ssbAddColumn ssb
|
||||||
Column (field2column name) (type2sql ssb typ) mnull
|
(entity2table ent)
|
||||||
|
(Column (field2column name) (type2sql ssb typ) mnull)
|
||||||
|
mdef
|
||||||
|
when (isJust mdef) $
|
||||||
|
exec $
|
||||||
|
ssbUndefColumn ssb (entity2table ent) (field2column name)
|
||||||
renameField entity old new = do
|
renameField entity old new = do
|
||||||
ssb <- ask
|
ssb <- ask
|
||||||
exec $
|
exec $
|
||||||
|
|
Loading…
Reference in a new issue