migrate
migrate
¶
Unified migration runner for all Marianne SQLite databases.
Applies numbered migrations sequentially using PRAGMA user_version for version tracking. Each migration is either a SQL string (split on semicolons) or an async callable for complex data transforms.
Design decisions: - Uses individual execute() calls, NOT executescript() (which implicitly commits and breaks transactional safety). - Each migration runs in its own transaction. On failure, that migration is rolled back and no further migrations are attempted. - Forward-compatible: a database from a newer Marianne version produces a warning, not an error.
Functions¶
get_version
async
¶
Read the current schema version from PRAGMA user_version.
set_version
async
¶
Set the schema version via PRAGMA user_version.
Note: PRAGMA user_version doesn't support parameter binding, so we format the integer directly. This is safe because version is always an int (enforced by the type signature).
Source code in src/marianne/schema/migrate.py
apply_migrations
async
¶
Apply pending migrations sequentially. Returns the final version.
Migrations are indexed starting at 0. Migration[0] brings the DB from version 0 → 1, migration[1] from 1 → 2, and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
db
|
Connection
|
An open aiosqlite connection. |
required |
migrations
|
list[MigrationStep]
|
Ordered list of migration steps. |
required |
db_name
|
str
|
Human-readable name for log messages. |
'unknown'
|
Returns:
| Type | Description |
|---|---|
int
|
The database version after applying migrations. |
Source code in src/marianne/schema/migrate.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 | |