Skip to content

Commit

Permalink
Cases migration
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Apr 9, 2021
1 parent 188aeef commit a624b15
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 26 deletions.
7 changes: 7 additions & 0 deletions conbench/tests/migrations/test_854c3ba5abd6_migrate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from alembic import command
from alembic.config import Config

from ...db import Session
from ...entities.summary import Summary


Expand Down Expand Up @@ -465,6 +466,12 @@ def test_upgrade():
command.stamp(alembic_config, "991493b6406a")
command.upgrade(alembic_config, "854c3ba5abd6")

Session.refresh(summary_1)
Session.refresh(summary_2)
Session.refresh(summary_3)
Session.refresh(summary_4)
Session.refresh(summary_5)

# assert after migration
assert summary_1.case.name == "file-write"
assert summary_1.case.tags == expected
Expand Down
14 changes: 1 addition & 13 deletions migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,7 @@
from alembic import context

from conbench.config import Config
from conbench.db import engine, session_maker
from conbench.entities import (
case,
commit,
context as _,
data,
machine,
run,
summary,
time,
user,
)
from conbench.db import engine
from conbench.entities._entity import Base

# this is the Alembic Config object, which provides
Expand Down Expand Up @@ -79,7 +68,6 @@ def run_migrations_online():
prefix="sqlalchemy.",
poolclass=pool.NullPool,
)
session_maker.configure(bind=connectable)

with connectable.connect() as connection:
context.configure(connection=connection, target_metadata=target_metadata)
Expand Down
33 changes: 20 additions & 13 deletions migrations/versions/854c3ba5abd6_migrate_cases.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@
"""
from alembic import op
from sqlalchemy import update

from conbench.entities.case import Case
from conbench.entities.summary import Summary
from conbench.db import Session


# revision identifiers, used by Alembic.
Expand All @@ -21,26 +19,35 @@


def upgrade():
cases = Case.all()
case_table = Case.__table__
summary_table = Summary.__table__
connection = op.get_bind()

cases = connection.execute(case_table.select())
for case in cases:
if "gc_collect" in case.tags or "gc_disable" in case.tags:
new_tags = dict(case.tags)
new_tags.pop("gc_collect")
new_tags.pop("gc_disable")
c = {"name": case.name, "tags": new_tags}
other = Case.first(**c)
other = connection.execute(
case_table.select().where(
case_table.c.name == case.name,
case_table.c.tags == new_tags,
)
).fetchone()
if other:
stmt = (
update(Summary)
.where(Summary.case_id == case.id)
connection.execute(
summary_table.update()
.where(summary_table.c.case_id == case.id)
.values(case_id=other.id)
.execution_options(synchronize_session="fetch")
)
Session.execute(stmt)
case.delete()
case_table.delete().where(case_table.c.id == case.id)
else:
case.tags = new_tags
case.save()
connection.execute(
case_table.update()
.where(case_table.c.id == case.id)
.values(tags=new_tags)
)


def downgrade():
Expand Down

0 comments on commit a624b15

Please sign in to comment.