0 | module Oracle.Types.Migration
 1 |
 2 | import Data.List
 3 | import Oracle.Internal.Pointer
 4 | import Oracle.Types.Error
 5 |
 6 | %default total
 7 |
 8 | ||| A database migration.
 9 | |||
10 | ||| Migrations are defined entirely through the Idris API. There are no
11 | ||| migration files or filesystem-based migration discovery mechanisms.
12 | |||
13 | ||| `migrationversion` must uniquely identify the migration within a migration set.
14 | ||| Migrations are normally applied in ascending version order.
15 | |||
16 | ||| `migrationname` provides a human-readable description of the migration.
17 | |||
18 | ||| `migrationup` applies the migration.
19 | |||
20 | ||| `migrationdown` reverses the migration.
21 | |||
22 | public export
23 | record Migration where
24 |   constructor MkMigration
25 |   migrationversion : Int
26 |   migrationname    : String
27 |   migrationup      : Connection -> IO (Either OracleError ())
28 |   migrationdown    : Connection -> IO (Either OracleError ())
29 |
30 | ||| Information about a migration that has been recorded in the database.
31 | |||
32 | ||| This represents the persisted migration history rather than the executable migration definition itself.
33 | |||
34 | public export
35 | record MigrationInfo where
36 |   constructor MkMigrationInfo
37 |   migrationinfoversion   : Int
38 |   migrationinfoname      : String
39 |   migrationinfoappliedAt : String
40 |
41 | ||| The status of a migration relative to the supplied migration definitions and the migration history persisted in Oracle.
42 | |||
43 | public export
44 | data MigrationStatus
45 |   = MigrationPending Migration
46 |   | MigrationApplied Migration MigrationInfo
47 |   | MigrationMissing MigrationInfo
48 |
49 | ||| Determine whether a migration version matches a persisted migration.
50 | |||
51 | export
52 | sameMigrationVersion : Migration -> MigrationInfo -> Bool
53 | sameMigrationVersion migration migrationinfo =
54 |   migrationversion migration == migrationinfoversion migrationinfo
55 |
56 | ||| Determine whether a migration has already been applied.
57 | |||
58 | export
59 | isMigrationApplied : Migration -> List MigrationInfo -> Bool
60 | isMigrationApplied migration migrationinfos =
61 |   any (sameMigrationVersion migration) migrationinfos
62 |