77import Prelude
88
99import Control.Exception (catch )
10+ import Control.Monad (void )
1011import Data.String.Conversions (cs )
1112import Data.Text (Text )
1213import Data.Time.Clock (getCurrentTime )
@@ -25,60 +26,52 @@ import Database.Schema.Migrations.Backend (Backend (..), rootMigrationName)
2526import Database.Schema.Migrations.Migration (Migration (.. ), newMigration )
2627import Database.Schema.Migrations.Test.BackendTest qualified as BackendTest
2728
28- migrationTableName :: Text
29- migrationTableName = " installed_migrations"
30-
31- createSql :: Text
32- createSql = " CREATE TABLE " <> migrationTableName <> " (migration_id TEXT)"
33-
34- revertSql :: Text
35- revertSql = " DROP TABLE " <> migrationTableName
29+ installedMigrations :: Text
30+ installedMigrations = " installed_migrations"
3631
3732-- | General Backend constructor for all HDBC connection implementations.
3833hdbcBackend :: IConnection conn => conn -> Backend
3934hdbcBackend conn =
4035 Backend
41- { isBootstrapped = elem (cs migrationTableName ) <$> getTables conn
36+ { isBootstrapped = elem (cs installedMigrations ) <$> getTables conn
4237 , getBootstrapMigration =
4338 do
4439 ts <- getCurrentTime
4540 pure $
4641 (newMigration rootMigrationName)
47- { mApply = createSql
48- , mRevert = Just revertSql
42+ { mApply = " CREATE TABLE " <> installedMigrations <> " (migration_id TEXT) "
43+ , mRevert = Just $ " DROP TABLE " <> installedMigrations
4944 , mDesc = Just " Migration table installation"
5045 , mTimestamp = Just ts
5146 }
5247 , applyMigration = \ m -> do
5348 runRaw conn (cs $ mApply m)
54- _ <-
49+ void $
5550 run
5651 conn
5752 ( cs $
5853 " INSERT INTO "
59- <> migrationTableName
54+ <> installedMigrations
6055 <> " (migration_id) VALUES (?)"
6156 )
6257 [toSql $ mId m]
63- pure ()
6458 , revertMigration = \ m -> do
6559 case mRevert m of
6660 Nothing -> pure ()
6761 Just query -> runRaw conn (cs query)
6862 -- Remove migration from installed_migrations in either case.
69- _ <-
63+ void $
7064 run
7165 conn
7266 ( cs $
7367 " DELETE FROM "
74- <> migrationTableName
68+ <> installedMigrations
7569 <> " WHERE migration_id = ?"
7670 )
7771 [toSql $ mId m]
78- pure ()
7972 , getMigrations = do
8073 results <-
81- quickQuery' conn (cs $ " SELECT migration_id FROM " <> migrationTableName ) []
74+ quickQuery' conn (cs $ " SELECT migration_id FROM " <> installedMigrations ) []
8275 pure $ map (fromSql . head ) results
8376 , commitBackend = commit conn
8477 , rollbackBackend = rollback conn
0 commit comments