Skip to content

Commit

Permalink
Add debugging log for DB migration
Browse files Browse the repository at this point in the history
  • Loading branch information
schustmi committed Oct 21, 2024
1 parent f809f02 commit abd6e50
Showing 1 changed file with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
"""

from alembic import op
import sqlalchemy as sa
import sqlmodel

# revision identifiers, used by Alembic.
revision = "c22561cbb3a9"
Expand All @@ -18,6 +20,37 @@
def upgrade() -> None:
"""Upgrade database schema and/or data, creating a new revision."""
# ### commands auto generated by Alembic - please adjust! ###
bind = op.get_bind()
session = sqlmodel.Session(bind=bind)

avs = session.exec(
sa.text(
"""
SELECT artifact_id, version
FROM artifact_version
"""
)
).all()

print(avs)
from collections import defaultdict
mapping = defaultdict(set)

for artifact_id, version in avs:
if version in mapping[artifact_id]:
artifact_name = session.exec(
sa.text(
"""
SELECT name
FROM artifact
WHERE id = :id_
"""
), params={"id_": artifact_id}
).one()
print(f"Found duplicate for artifact version {artifact_name} (version {version})")

mapping[artifact_id].add(version)

with op.batch_alter_table("artifact", schema=None) as batch_op:
batch_op.create_unique_constraint("unique_artifact_name", ["name"])

Expand Down

0 comments on commit abd6e50

Please sign in to comment.