From bff41673c49ae9b108375ba584302f92f453071d Mon Sep 17 00:00:00 2001 From: fr33domlover Date: Thu, 1 Sep 2016 16:30:39 +0000 Subject: [PATCH] Migration: Specify default value when adding non-null column --- src/Database/Persist/Schema.hs | 3 ++- src/Database/Persist/Schema/PostgreSQL.hs | 16 +++++++++++++++- src/Database/Persist/Schema/Sql.hs | 17 +++++++++++++---- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/src/Database/Persist/Schema.hs b/src/Database/Persist/Schema.hs index 88a9b8d..96362d0 100644 --- a/src/Database/Persist/Schema.hs +++ b/src/Database/Persist/Schema.hs @@ -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 () diff --git a/src/Database/Persist/Schema/PostgreSQL.hs b/src/Database/Persist/Schema/PostgreSQL.hs index ca677cd..17d904a 100644 --- a/src/Database/Persist/Schema/PostgreSQL.hs +++ b/src/Database/Persist/Schema/PostgreSQL.hs @@ -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 diff --git a/src/Database/Persist/Schema/Sql.hs b/src/Database/Persist/Schema/Sql.hs index 54309b9..fc0dca1 100644 --- a/src/Database/Persist/Schema/Sql.hs +++ b/src/Database/Persist/Schema/Sql.hs @@ -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 $