|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
5 | 5 | from importlib.resources import as_file, files |
| 6 | +import importlib.metadata |
6 | 7 |
|
7 | 8 | from alembic import command |
8 | 9 | from alembic.config import Config |
| 10 | +from alembic.script import ScriptDirectory |
| 11 | +from alembic.script.revision import ResolutionError |
9 | 12 |
|
10 | 13 | from app.boot.admin_db import get_admin_db_dsn, get_backup_dir, get_backup_mirror_dir, get_instance_mode_default |
11 | 14 | from app.boot.db import get_optional_db_dsn |
|
14 | 17 | from app.periphery.admin.privileges import reconcile_app_role_privileges |
15 | 18 |
|
16 | 19 |
|
| 20 | +class DatabaseRevisionAheadOfInstalledPackageError(RuntimeError): |
| 21 | + """Raised when the target database revision is newer than the installed package knows about.""" |
| 22 | + |
| 23 | + |
17 | 24 | def upgrade_database(revision: str = "head") -> None: |
18 | 25 | """Apply packaged Alembic migrations to the configured database.""" |
19 | 26 |
|
20 | 27 | config = Config() |
21 | 28 | admin_dsn = get_admin_db_dsn() |
22 | | - if _database_has_shellbrain_objects(admin_dsn): |
23 | | - backup_and_verify_before_destructive_action( |
24 | | - admin_dsn=admin_dsn, |
25 | | - backup_root=get_backup_dir(), |
26 | | - mirror_root=get_backup_mirror_dir(), |
27 | | - ) |
28 | | - config.set_main_option("sqlalchemy.url", admin_dsn) |
29 | 29 | with as_file(files("app").joinpath("migrations")) as migrations_path: |
30 | 30 | config.set_main_option("script_location", str(migrations_path)) |
| 31 | + script = ScriptDirectory.from_config(config) |
| 32 | + if _database_has_shellbrain_objects(admin_dsn): |
| 33 | + _assert_database_revision_is_known(admin_dsn=admin_dsn, script=script) |
| 34 | + backup_and_verify_before_destructive_action( |
| 35 | + admin_dsn=admin_dsn, |
| 36 | + backup_root=get_backup_dir(), |
| 37 | + mirror_root=get_backup_mirror_dir(), |
| 38 | + ) |
| 39 | + config.set_main_option("sqlalchemy.url", admin_dsn) |
31 | 40 | command.upgrade(config, revision) |
32 | 41 | if fetch_instance_metadata(admin_dsn) is None: |
33 | 42 | ensure_instance_metadata( |
@@ -59,3 +68,48 @@ def _database_has_shellbrain_objects(admin_dsn: str) -> bool: |
59 | 68 | """ |
60 | 69 | ) |
61 | 70 | return bool(cur.fetchone()[0]) |
| 71 | + |
| 72 | + |
| 73 | +def _assert_database_revision_is_known(*, admin_dsn: str, script: ScriptDirectory) -> None: |
| 74 | + """Fail early with a user-facing error when the database revision is newer than this package.""" |
| 75 | + |
| 76 | + current_revision = _fetch_database_revision(admin_dsn) |
| 77 | + if current_revision is None: |
| 78 | + return |
| 79 | + try: |
| 80 | + script.get_revision(current_revision) |
| 81 | + except ResolutionError as exc: |
| 82 | + installed_version = _installed_shellbrain_version() |
| 83 | + raise DatabaseRevisionAheadOfInstalledPackageError( |
| 84 | + "Installed Shellbrain package " |
| 85 | + f"({installed_version}) cannot manage database revision {current_revision}. " |
| 86 | + "This database was likely migrated by a newer Shellbrain release than the one currently installed. " |
| 87 | + "Upgrade Shellbrain to a build that includes this revision, then rerun `shellbrain init` or " |
| 88 | + "`shellbrain admin migrate`." |
| 89 | + ) from exc |
| 90 | + |
| 91 | + |
| 92 | +def _fetch_database_revision(admin_dsn: str) -> str | None: |
| 93 | + """Return the current alembic revision when present.""" |
| 94 | + |
| 95 | + import psycopg |
| 96 | + |
| 97 | + try: |
| 98 | + with psycopg.connect(admin_dsn.replace("+psycopg", "")) as conn: |
| 99 | + with conn.cursor() as cur: |
| 100 | + cur.execute("SELECT version_num FROM alembic_version") |
| 101 | + row = cur.fetchone() |
| 102 | + except psycopg.Error: |
| 103 | + return None |
| 104 | + if row is None or row[0] is None: |
| 105 | + return None |
| 106 | + return str(row[0]) |
| 107 | + |
| 108 | + |
| 109 | +def _installed_shellbrain_version() -> str: |
| 110 | + """Return the installed Shellbrain package version when available.""" |
| 111 | + |
| 112 | + try: |
| 113 | + return importlib.metadata.version("shellbrain") |
| 114 | + except importlib.metadata.PackageNotFoundError: |
| 115 | + return "dev" |
0 commit comments