1
0
Fork 0
mirror of https://code.sup39.dev/repos/Wqawg synced 2024-12-27 15:24:51 +09:00

Migration: Specify default value when adding non-null column

This commit is contained in:
fr33domlover 2016-09-01 16:30:39 +00:00
parent 7218262223
commit bff41673c4
3 changed files with 30 additions and 6 deletions

View file

@ -79,7 +79,8 @@ class PersistSchema backend where
removeEntity
:: MonadIO m => EntityName -> SchemaT backend m ()
addField
:: MonadIO m => EntityName -> Field -> SchemaT backend m ()
:: MonadIO m
=> EntityName -> Field -> Maybe Text -> SchemaT backend m ()
renameField
:: MonadIO m
=> EntityName -> FieldName -> FieldName -> SchemaT backend m ()

View file

@ -20,6 +20,7 @@ where
import Prelude
import Data.Monoid ((<>))
import Data.Text (Text)
import Database.Persist.Sql (SqlBackend)
import Database.Persist.Types (SqlType (..))
@ -92,9 +93,12 @@ schemaBackend = SqlSchemaBackend
, ssbDropTable = \ table -> mconcat
[ "DROP TABLE ", table2sql table
]
, ssbAddColumn = \ table column -> mconcat
, ssbAddColumn = \ table column mdef -> mconcat
[ "ALTER TABLE ", table2sql table
, " ADD COLUMN ", columnSql column
, case mdef of
Nothing -> T.empty
Just t -> " DEFAULT " <> t
]
, ssbRenameColumn = \ table old new -> mconcat
[ "ALTER TABLE ", table2sql table
@ -118,6 +122,16 @@ schemaBackend = SqlSchemaBackend
, " SET ", column2sql column, " = ", val
, " 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
[ "ALTER TABLE ", table2sql table
, " DROP COLUMN ", column2sql column

View file

@ -26,11 +26,13 @@ where
import Prelude
import Control.Monad (when)
import Control.Monad.IO.Class (MonadIO)
import Control.Monad.Trans.Class (lift)
import Control.Monad.Trans.Reader (ask)
import Data.Char (isUpper, toLower)
import Data.Foldable (traverse_)
import Data.Maybe (isJust)
import Data.Text (Text)
import Database.Persist.Sql (Sql, SqlBackend, rawExecute)
import Database.Persist.Types (SqlType)
@ -101,11 +103,13 @@ instance PersistSchema SqlBackend where
, ssbCreateTable :: TableName -> [Column] -> Sql
, ssbRenameTable :: TableName -> TableName -> Sql
, ssbDropTable :: TableName -> Sql
, ssbAddColumn :: TableName -> Column -> Sql
, ssbAddColumn :: TableName -> Column -> Maybe Text -> Sql
, ssbRenameColumn :: TableName -> ColumnName -> ColumnName -> Sql
, ssbRetypeColumn :: TableName -> ColumnName -> SqlType -> Sql
, ssbRenullColumn :: TableName -> ColumnName -> MaybeNull -> Sql
, ssbUnnullColumn :: TableName -> ColumnName -> Text -> Sql
, ssbDefColumn :: TableName -> ColumnName -> Text -> Sql
, ssbUndefColumn :: TableName -> ColumnName -> Sql
, ssbDropColumn :: TableName -> ColumnName -> Sql
, ssbAddUnique
:: TableName -> ConstraintName -> [ColumnName] -> Sql
@ -123,11 +127,16 @@ instance PersistSchema SqlBackend where
removeEntity name = do
ssb <- ask
exec $ ssbDropTable ssb $ entity2table name
addField ent (Field name typ mnull) = do
addField ent (Field name typ mnull) mdef = do
ssb <- ask
exec $
ssbAddColumn ssb (entity2table ent) $
Column (field2column name) (type2sql ssb typ) mnull
ssbAddColumn ssb
(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
ssb <- ask
exec $