Skip to content

Commit

Permalink
WIP: alembic
Browse files Browse the repository at this point in the history
  • Loading branch information
maringuu committed Jun 15, 2023
1 parent d76b0ee commit 2430121
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/storage/migration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@


def db_needs_migration():
return False
with OperateInDirectory(get_src_dir()): # alembic must be executed from src for paths to line up
with AdminConnection().engine.connect() as db:
alembic_cfg_path = Path(__file__).parent.parent.parent / 'alembic.ini'
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
"""plugin_device_tree_add_schema
Revision ID: a1c21b3422a7
Revises: 221cfef47173
Create Date: 2023-04-24 11:18:50.037043
"""
from alembic import op
import sqlalchemy as sa
from alembic import context

from sqlalchemy.dialects.postgresql import ARRAY, CHAR, JSONB, VARCHAR
from sqlalchemy.ext.mutable import MutableDict, MutableList


# revision identifiers, used by Alembic.
revision = 'a1c21b3422a7'
down_revision = '221cfef47173'
branch_labels = None
depends_on = None

# The version we downgrade to
PLUGIN_OLD_VERSION = "1.0"
# The version we upgrade to
PLUGIN_NEW_VERSION = "2.0.0"
# All versions (with possibly all different schemas) we can upgrade from
PLUGIN_UPGRADALBE_VERSIONS = [PLUGIN_OLD_VERSION]
PLUGIN = "device_tree"


# References https://stackoverflow.com/questions/43153346/update-column-content-during-alembic-migration
def upgrade() -> None:
connection = context.get_bind()

# Define the table
# XXX Should this maybe be dynamically generated by reflection?
analysis_table = sa.Table(
"analysis",
sa.MetaData(),
sa.Column("uid", VARCHAR(78)),
sa.Column("plugin", VARCHAR(64)),
sa.Column("plugin_version", VARCHAR(16)),
sa.Column("result", MutableDict.as_mutable(JSONB), default={}),
)

a = connection.execute(
sa.select([analysis_table.c.uid, analysis_table.c.result,],).where(
analysis_table.c.plugin == PLUGIN,
analysis_table.c.plugin_version.in_(PLUGIN_UPGRADALBE_VERSIONS),
),
).fetchall()

for uid, old_result in a:
new_result = {"result": old_result}
connection.execute(
analysis_table.update().where(
analysis_table.c.uid == uid,
analysis_table.c.plugin == PLUGIN,
).values(
result=new_result,
plugin_version=PLUGIN_NEW_VERSION,
)
)


def downgrade() -> None:
connection = context.get_bind()

# Define the table
# XXX Should this maybe be dynamically generated by reflection?
analysis_table = sa.Table(
"analysis",
sa.MetaData(),
sa.Column("uid", VARCHAR(78)),
sa.Column("plugin", VARCHAR(64)),
sa.Column("plugin_version", VARCHAR(16)),
sa.Column("result", MutableDict.as_mutable(JSONB), default={}),
)

a = connection.execute(
sa.select([analysis_table.c.uid, analysis_table.c.plugin, analysis_table.c.result,],).where(
analysis_table.c.plugin == "device_tree",
analysis_table.c.plugin_version == PLUGIN_NEW_VERSION,
),
).fetchall()

for uid, plugin, new_result in a:
old_result = new_result.pop("result")
connection.execute(
analysis_table.update().where(
analysis_table.c.uid == uid,
analysis_table.c.plugin == plugin,
).values(
result=old_result,
plugin_version=PLUGIN_OLD_VERSION,
)
)

0 comments on commit 2430121

Please sign in to comment.