Skip to content

Commit

Permalink
Faster backfill commit id
Browse files Browse the repository at this point in the history
  • Loading branch information
dianaclarke committed Jul 29, 2021
1 parent 28f5808 commit da73aaa
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions migrations/versions/c181484ce40f_backfill_commit_id.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""
from alembic import op
from sqlalchemy import MetaData
from sqlalchemy import distinct, select


# revision identifiers, used by Alembic.
Expand All @@ -25,27 +26,26 @@ def upgrade():
distribution_table = meta.tables["distribution"]

commits = connection.execute(commit_table.select())
distributions = connection.execute(distribution_table.select())
shas = connection.execute(select(distinct(distribution_table.c.sha)))
commits_by_sha = {c["sha"]: c for c in commits}

i = 1
i, count = 1, shas.rowcount

for distribution in distributions:
if distribution.commit_id:
continue
for row in shas:
sha = row.sha

commit = commits_by_sha.get(distribution["sha"])
commit = commits_by_sha.get(sha)
if not commit:
print(f"Could not find commit for distribution {distribution.id}")
print(f"Could not find commit sha {sha}")
continue

connection.execute(
distribution_table.update()
.where(distribution_table.c.id == distribution.id)
.where(distribution_table.c.sha == sha)
.values(commit_id=commit.id)
)

print(f"Updated distribution {1}")
print(f"Updated sha {i} of {count}")
i += 1

print("Done with migration")
Expand Down

0 comments on commit da73aaa

Please sign in to comment.