• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

Courseography / courseography / c1af5ae6-6501-463e-8e2b-3f62f7a4d663

07 Nov 2025 07:09PM UTC coverage: 55.158% (+0.04%) from 55.118%
c1af5ae6-6501-463e-8e2b-3f62f7a4d663

Pull #1607

circleci

akarki2005
Merge branch 'master' of https://github.com/Courseography/courseography into rename-post-table-to-program
Pull Request #1607: Rename post table to program

487 of 958 branches covered (50.84%)

Branch coverage included in aggregate %.

16 of 32 new or added lines in 6 files covered. (50.0%)

16 existing lines in 1 file now uncovered.

2224 of 3957 relevant lines covered (56.2%)

159.45 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

46.0
/app/Database/Database.hs
1
{-|
2
    Module      : Database.Database
3
    Description : Main module for database course seeding.
4

5
The main module for parsing course information from the web and
6
inserting it into the database. Run when @cabal run database@ is executed.
7
-}
8

9
module Database.Database
10
    (populateCalendar, setupDatabase, getDatabaseVersion, setDatabaseVersion) where
11

12
import Config (databasePath, runDb)
13
import Control.Monad (void)
14
import Control.Monad.IO.Class (MonadIO, liftIO)
15
import Data.Maybe (fromMaybe)
16
import Data.Text as T (findIndex, length, reverse, take, unpack)
17
import Database.CourseVideoSeed (seedVideos)
18
import Database.Persist.Sqlite (Entity (..), SqlPersistT, entityVal, insert_, runMigration,
19
                                runMigrationQuiet, selectFirst, update, (=.))
20
import Database.Tables
21
import System.Directory (createDirectoryIfMissing)
22
import WebParsing.ArtSciParser (parseCalendar)
23

24

25
distTableSetUpStr :: String
26
distTableSetUpStr = "Distribution table set up"
×
27
breathTableSetUpStr :: String
28
breathTableSetUpStr = "breadth table set up"
×
29

30

31
-- | Creates the database if it doesn't exist and runs migrations.
32
setupDatabase :: Bool -> IO ()
33
setupDatabase quiet = do
1✔
34
    -- Create db folder if it doesn't exist
35
    dbPath <- liftIO databasePath
5✔
36
    let ind = (T.length dbPath -) . fromMaybe 0 . T.findIndex (=='/') . T.reverse $ dbPath
5✔
37
        db = T.unpack $ T.take ind dbPath
5✔
38
    createDirectoryIfMissing True db
5✔
39
    runDb (
5✔
40
        if quiet
5!
41
            then void $ runMigrationQuiet migrateAll
1✔
NEW
42
            else runMigration migrateAll
×
43
        )
44

45
    -- Match SQL database with ORM, then initialize schema version table
46
    let migrateFunction = if quiet then void . runMigrationQuiet else runMigration
5!
47
    runDb $ void $ migrateFunction migrateAll >> getDatabaseVersion
5✔
48

49
-- | Gets the current version of the database.
50
-- If no version is defined, initialize the
51
-- version to 1 and return that.
52
getDatabaseVersion :: MonadIO m => SqlPersistT m Int
53
getDatabaseVersion = do
1✔
54
    result <- selectFirst [] []
1✔
55
    case result of
5✔
56
        Just entity -> pure $ schemaVersionVersion $ entityVal entity
×
57
        Nothing -> do
1✔
58
            let initialVersion = 1
1✔
59
            setDatabaseVersion initialVersion
5✔
60
            pure initialVersion
5✔
61

62
-- | Sets the database version number to newVersion
63
setDatabaseVersion :: MonadIO m => Int -> SqlPersistT m ()
64
setDatabaseVersion newVersion = do
5✔
65
    result <- selectFirst [] []
1✔
66
    case result of
5✔
NEW
67
        Just (Entity key _) -> update key [SchemaVersionVersion =. newVersion]
×
68
        Nothing -> insert_ $ SchemaVersion newVersion
5✔
69

70
-- | Sets up the course information from Artsci Calendar
71
populateCalendar :: IO ()
72
populateCalendar = do
×
73
    populateStaticInfo
×
74
    parseCalendar
×
75

76
-- | Sets up the tables and seeds the videos for the database.
77
populateStaticInfo :: IO ()
78
populateStaticInfo = do
×
79
    setupDistributionTable
×
80
    print distTableSetUpStr
×
81
    setupBreadthTable
×
82
    print breathTableSetUpStr
×
83
    seedVideos
×
84

85
-- | Sets up the Distribution table.
86
setupDistributionTable :: IO ()
87
setupDistributionTable = runDb $ do
×
88
    insert_ $ Distribution "Humanities"
×
89
    insert_ $ Distribution "Social Science"
×
90
    insert_ $ Distribution "Science"
×
91

92
-- | Sets up the Breadth table.
93
setupBreadthTable :: IO ()
94
setupBreadthTable = runDb $ do
×
95
    insert_ $ Breadth "Creative and Cultural Representations (1)"
×
96
    insert_ $ Breadth "Thought, Belief, and Behaviour (2)"
×
97
    insert_ $ Breadth "Society and its Institutions (3)"
×
98
    insert_ $ Breadth "Living Things and Their Environment (4)"
×
99
    insert_ $ Breadth "The Physical and Mathematical Universes (5)"
×
100
    insert_ $ Breadth "No Breadth"
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc