diff options
Diffstat (limited to 'buildbot_gentoo_ci/db/model.py')
-rw-r--r-- | buildbot_gentoo_ci/db/model.py | 55 |
1 files changed, 17 insertions, 38 deletions
diff --git a/buildbot_gentoo_ci/db/model.py b/buildbot_gentoo_ci/db/model.py index 448e67f..d5364c7 100644 --- a/buildbot_gentoo_ci/db/model.py +++ b/buildbot_gentoo_ci/db/model.py @@ -17,8 +17,11 @@ # Modifyed by Gentoo Authors. # Copyright 2024 Gentoo Authors -import uuid +from __future__ import annotations + +from typing import TYPE_CHECKING +import uuid import alembic import alembic.config import sqlalchemy as sa @@ -32,26 +35,9 @@ from buildbot.db.migrate_utils import test_unicode from buildbot.db.types.json import JsonObject from buildbot.util import sautils - -class UpgradeFromBefore0p9Error(Exception): - - def __init__(self): - message = """You are trying to upgrade a buildbot 0.8.x master to buildbot 0.9.x or newer. - This is not supported. Please start from a clean database - http://docs.buildbot.net/latest/manual/upgrading/0.9-upgrade.html""" - # Call the base class constructor with the parameters it needs - super().__init__(message) - - -class UpgradeFromBefore3p0Error(Exception): - - def __init__(self): - message = """You are trying to upgrade to Buildbot 3.0 or newer from Buildbot 2.x or older. - This is only supported via an intermediate upgrade to newest Buildbot 2.10.x that is - available. Please first upgrade to 2.10.x and then try to upgrade to this version. - http://docs.buildbot.net/latest/manual/upgrading/3.0-upgrade.html""" - super().__init__(message) - +if TYPE_CHECKING: + from sqlalchemy.engine.base import Connectable as SQLAConnection + from sqlalchemy.engine.reflection import Inspector class Model(base.DBConnectorComponent): @@ -364,6 +350,7 @@ class Model(base.DBConnectorComponent): sa.Column('change_id', sa.Integer, nullable=True, default=0), sa.Column('deleted', sa.Boolean, default=False), sa.Column('deleted_at', sa.Integer, nullable=True), + sa.Column('created_at', sa.Integer, nullable=True), ) versions_keywords = sautils.Table( @@ -496,13 +483,9 @@ class Model(base.DBConnectorComponent): config_path = util.sibpath(__file__, "migrations/alembic.ini") - def table_exists(self, conn, table): - try: - r = conn.execute(f"select * from {table} limit 1") - r.close() - return True - except Exception: - return False + def table_exists(self, conn: SQLAConnection, table: str): + inspector: Inspector = sa.inspect(conn.engine) + return inspector.has_table(table) def migrate_get_version(self, conn): r = conn.execute("select version from migrate_version limit 1") @@ -550,19 +533,10 @@ class Model(base.DBConnectorComponent): alembic_scripts = self.alembic_get_scripts() current_script_rev_head = alembic_scripts.get_current_head() - #if self.table_exists(conn, 'version'): - # raise UpgradeFromBefore0p9Error() - if self.table_exists(conn, 'migrate_version'): version = self.migrate_get_version(conn) - #if version < 40: - # raise UpgradeFromBefore0p9Error() - - last_sqlalchemy_migrate_version = 0 - if version != last_sqlalchemy_migrate_version: - raise UpgradeFromBefore3p0Error() - + last_sqlalchemy_migrate_version = 58 self.alembic_stamp(conn, alembic_scripts, alembic_scripts.get_base()) conn.execute('drop table migrate_version') @@ -576,6 +550,11 @@ class Model(base.DBConnectorComponent): self.alembic_stamp(conn, alembic_scripts, current_script_rev_head) return + def upgrade(rev, context): + log.msg(f'Upgrading from {rev} to {current_script_rev_head}') + return alembic_scripts._upgrade_revs(current_script_rev_head, rev) + + context = alembic.runtime.migration.MigrationContext.configure(conn) current_rev = context.get_current_revision() |