Skip to content

Commit 894d9e4

Browse files
authored
Initialized SchemaVersion table (#1616)
1 parent a62fb69 commit 894d9e4

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
- Added documentation for running a subset of the backend tests
3838
- Deleted `app/Response/Image` file and refactored `app/Util/Helpers` to include `returnImageData`
3939
- Added test cases for the retrieveProgram function in `Controllers/Program`
40+
- Initialized a SchemaVersion table for the purposes of running robust database migrations
4041

4142
## [0.7.1] - 2025-06-16
4243

app/Database/Database.hs

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,12 @@ module Database.Database
1111

1212
import Config (databasePath, runDb)
1313
import Control.Monad (void)
14-
import Control.Monad.IO.Class (liftIO)
14+
import Control.Monad.IO.Class (MonadIO, liftIO)
1515
import Data.Maybe (fromMaybe)
1616
import Data.Text as T (findIndex, length, reverse, take, unpack)
1717
import Database.CourseVideoSeed (seedVideos)
18-
import Database.Persist.Sqlite (insert_, runMigration, runMigrationQuiet)
18+
import Database.Persist.Sqlite (SqlPersistT, entityVal, insert_, runMigration, runMigrationQuiet,
19+
selectFirst)
1920
import Database.Tables
2021
import System.Directory (createDirectoryIfMissing)
2122
import WebParsing.ArtSciParser (parseCalendar)
@@ -35,11 +36,23 @@ setupDatabase quiet = do
3536
let ind = (T.length dbPath -) . fromMaybe 0 . T.findIndex (=='/') . T.reverse $ dbPath
3637
db = T.unpack $ T.take ind dbPath
3738
createDirectoryIfMissing True db
38-
runDb (
39-
if quiet
40-
then void $ runMigrationQuiet migrateAll
41-
else runMigration migrateAll
42-
)
39+
40+
-- Match SQL database with ORM, then initialize schema version table
41+
let migrateFunction = if quiet then void . runMigrationQuiet else runMigration
42+
runDb $ void $ migrateFunction migrateAll >> getDatabaseVersion
43+
44+
-- | Gets the current version of the database.
45+
-- If no version is defined, initialize the
46+
-- version to 1 and return that.
47+
getDatabaseVersion :: MonadIO m => SqlPersistT m Int
48+
getDatabaseVersion = do
49+
result <- selectFirst [] []
50+
case result of
51+
Just entity -> pure $ schemaVersionVersion $ entityVal entity
52+
Nothing -> do
53+
let initialVersion = 1
54+
insert_ $ SchemaVersion initialVersion
55+
pure initialVersion
4356

4457
-- | Sets up the course information from Artsci Calendar
4558
populateCalendar :: IO ()

app/Database/Tables.hs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ Building
153153
lat Double
154154
lng Double
155155
deriving Generic Show
156+
157+
SchemaVersion
158+
version Int
159+
deriving Show Eq
156160
|]
157161

158162
-- ** TODO: Remove these extra types and class instances

0 commit comments

Comments
 (0)