Skip to content

Commit

Permalink
landing_job: add landed revisions receipt (bug 1834885)
Browse files Browse the repository at this point in the history
- add diff_id to revision_landing_job table
- add method to set diff IDs and call it at landing time
- add landed_revisions helper method to fetch revision_to_diff_ids
  • Loading branch information
zzzeid committed May 25, 2023
1 parent c728f84 commit 45ab553
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 6 deletions.
32 changes: 29 additions & 3 deletions landoapi/models/landing_job.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,20 @@ class LandingJob(Base):
)

@property
def landing_path(self) -> list[tuple[int, int]]:
return [(revision.revision_id, revision.diff_id) for revision in self.revisions]
def landed_revisions(self):
revision_ids = [revision.id for revision in self.revisions]
revision_to_diff_ids_query = (
revision_landing_job.select()
.where(
revision_landing_job.c.revision_id.in_(revision_ids),
revision_landing_job.c.landing_job_id == self.id,
)
.with_only_columns(
revision_landing_job.c.revision_id, revision_landing_job.c.diff_id
)
.order_by(revision_landing_job.c.index)
)
return dict(list(db.session.execute(revision_to_diff_ids_query)))

@property
def serialized_landing_path(self):
Expand All @@ -147,7 +159,7 @@ def serialized_landing_path(self):
"revision_id": "D{}".format(revision_id),
"diff_id": diff_id,
}
for revision_id, diff_id in self.landing_path
for revision_id, diff_id in self.landed_revisions.items()
]
else:
return [
Expand Down Expand Up @@ -249,6 +261,19 @@ def sort_revisions(self, revisions: list[Revision]):
.values(index=index)
)

def set_landed_revision_diffs(self):
"""Assign diff_ids, if available, to each association row."""
# Update association table records with current diff_id values.
for revision in self.revisions:
db.session.execute(
revision_landing_job.update()
.where(revision_landing_job.c.landing_job_id == self.id)
.where(
revision_landing_job.c.revision_id == revision.id,
)
.values(diff_id=revision.diff_id)
)

def transition_status(
self,
action: LandingJobAction,
Expand Down Expand Up @@ -303,6 +328,7 @@ def transition_status(

if action == LandingJobAction.LAND:
self.landed_commit_id = kwargs["commit_id"]
self.set_landed_revision_diffs()

if commit:
db.session.commit()
Expand Down
2 changes: 2 additions & 0 deletions landoapi/models/revisions.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ class DiffWarningGroup(enum.Enum):


# Association table with custom "index" column to guarantee sorting of revisions.
# The diff_id column is used as a transaction record of the diff_id at landing time.
revision_landing_job = db.Table(
"revision_landing_job",
db.Column("landing_job_id", db.ForeignKey("landing_job.id")),
db.Column("revision_id", db.ForeignKey("revision.id")),
db.Column("index", db.Integer),
db.Column("diff_id", db.Integer, nullable=True),
)


Expand Down
4 changes: 1 addition & 3 deletions landoapi/transplants.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,9 +224,7 @@ def warning_previously_landed(*, revision, diff, **kwargs):
if job is None:
return None

revision_to_diff_id = {
revision.revision_id: revision.diff_id for revision in job.revisions
}
revision_to_diff_id = job.landed_revisions
if job.revision_to_diff_id:
legacy_data = {
int(revision_id): int(diff_id)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""add revision_landing_job.diff_id
Revision ID: 50ffadceca83
Revises: a8cd4d43c4c3
Create Date: 2023-05-25 01:50:36.755884
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = "50ffadceca83"
down_revision = "a8cd4d43c4c3"
branch_labels = None
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"revision_landing_job", sa.Column("diff_id", sa.Integer(), nullable=True)
)
# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("revision_landing_job", "diff_id")
# ### end Alembic commands ###

0 comments on commit 45ab553

Please sign in to comment.