module Database.Peregrin
( migrate
, MigrationError(..)
) where
import Control.Applicative ((<$>))
import Control.Exception (Exception, throwIO)
import Control.Monad (forM_, when, void)
import Database.Peregrin.Metadata
import Data.Text (Text)
import qualified Data.Text as T
import Data.Int (Int32, Int64)
import Data.Maybe (listToMaybe, fromMaybe)
import Data.String (fromString)
import Database.PostgreSQL.Simple (Connection, Only(..), Query)
import qualified Database.PostgreSQL.Simple as P
import Database.PostgreSQL.Simple.ToField (ToField(..))
import Database.PostgreSQL.Simple.FromRow (FromRow(..), field)
import Database.PostgreSQL.Simple.Transaction (withTransactionLevel, IsolationLevel(..))
data Migration = Migration Text Text
instance FromRow Migration where
fromRow = Migration <$> field <*> field
data MigrationError =
MigrationModifiedError Text
deriving (Show, Eq)
instance Exception MigrationError
data MigrationContext = MigrationContext { mcMetaMigrationTable :: Table
, mcMigrationTable :: Table
}
migrate :: Connection -> Schema -> [(Text, Text)] -> IO ()
migrate connection schema =
migrate' tables connection schema
where
tables = MigrationContext { mcMetaMigrationTable = Table schema "__peregrin_migration_meta__"
, mcMigrationTable = Table schema "__peregrin_migration__"
}
migrate' :: MigrationContext -> Connection -> Schema -> [(Text, Text)] -> IO ()
migrate' tables c schema migrations = do
void $ transact $ execute sqlCreateSchema [ schema ]
void $ transact $ execute sqlCreateMetaTbl [ metaTable ]
withLock $
metaMigrate 1 [ (sqlInsertMetaVersion0, [metaTable])
, (sqlCreateMigrationTbl, [migrationTable])
]
forM_ migrations $ \(mid, sql) ->
withLock $ do
existingMigration :: (Maybe Migration) <-
listToMaybe <$> query sqlFindMigration
[ toField migrationTable
, toField mid ]
case existingMigration of
Just (Migration _ sql') | sql == sql' ->
return ()
Just _ ->
throwIO $ MigrationModifiedError mid
Nothing -> do
void $ execute sqlInsertMigration [ toField migrationTable
, toField mid
, toField sql
]
void $ execute_ $ fromString $ T.unpack sql
where
migrationTable = mcMigrationTable tables
metaTable = mcMetaMigrationTable tables
metaMigrate :: ToField a => Int32 -> [(Query, [a])] -> IO ()
metaMigrate metaVersion sqls = do
Only currentMetaVersion <- fromMaybe (Only 0) <$> fmap listToMaybe (query sqlGetMetaVersion [metaTable])
when (currentMetaVersion + 1 == metaVersion) $ do
forM_ sqls $ \(q, ps) -> execute q ps
rowCount <- execute sqlUpdateMetaVersion [ toField metaTable
, toField metaVersion
, toField currentMetaVersion
]
when (rowCount /= 1) $ error $ "Unexpected row count " ++ show rowCount ++ " from update on \"migration_meta\" table!"
transact = withTransactionLevel ReadCommitted c
execute :: ToField a => Query -> [a] -> IO Int64
execute = P.execute c
execute_ :: Query -> IO Int64
execute_ = P.execute_ c
query :: (ToField a, FromRow r) => Query -> [a] -> IO [r]
query = P.query c
withLock txn =
transact $ do
void $ execute sqlLockMetaTbl [metaTable]
txn
sqlCreateSchema =
"CREATE SCHEMA IF NOT EXISTS ?"
sqlCreateMetaTbl =
"CREATE TABLE IF NOT EXISTS ? (\
\ \"meta_version\" INTEGER PRIMARY KEY\
\)"
sqlGetMetaVersion =
"SELECT \"meta_version\" FROM ?"
sqlUpdateMetaVersion =
"UPDATE ? \
\ SET \"meta_version\" = ? \
\ WHERE \"meta_version\" = ?"
sqlLockMetaTbl =
"LOCK TABLE ? IN ACCESS EXCLUSIVE MODE"
sqlInsertMetaVersion0 =
"INSERT INTO ? (\"meta_version\") VALUES (0)"
sqlCreateMigrationTbl =
"CREATE TABLE ? ( \
\ \"id\" TEXT PRIMARY KEY,\
\ \"sql\" TEXT NOT NULL\
\)"
sqlFindMigration =
"SELECT \"id\", \"sql\"\
\ FROM ? \
\ WHERE \"id\" = ?"
sqlInsertMigration =
"INSERT INTO ? \
\ (\"id\", \"sql\") \
\ VALUES (?, ?)"