|
| 1 | +"""Add sync_release_pull_request table |
| 2 | +
|
| 3 | +Revision ID: 7d2b9fb924fa |
| 4 | +Revises: 31111f804dec |
| 5 | +Create Date: 2024-02-07 09:53:51.363885 |
| 6 | +
|
| 7 | +""" |
| 8 | +import sqlalchemy as sa |
| 9 | + |
| 10 | +from alembic import op |
| 11 | +from typing import TYPE_CHECKING |
| 12 | + |
| 13 | +from sqlalchemy import ( |
| 14 | + Column, |
| 15 | + ForeignKey, |
| 16 | + Integer, |
| 17 | + String, |
| 18 | +) |
| 19 | +from sqlalchemy.ext.declarative import declarative_base |
| 20 | +from sqlalchemy.orm import relationship |
| 21 | + |
| 22 | +if TYPE_CHECKING: |
| 23 | + Base = object |
| 24 | +else: |
| 25 | + Base = declarative_base() |
| 26 | + |
| 27 | + |
| 28 | +# revision identifiers, used by Alembic. |
| 29 | +revision = "7d2b9fb924fa" |
| 30 | +down_revision = "31111f804dec" |
| 31 | +branch_labels = None |
| 32 | +depends_on = None |
| 33 | + |
| 34 | + |
| 35 | +class SyncReleaseTargetModel(Base): |
| 36 | + __tablename__ = "sync_release_run_targets" |
| 37 | + id = Column(Integer, primary_key=True) |
| 38 | + downstream_pr_url = Column(String) |
| 39 | + downstream_pr_id = Column(Integer, ForeignKey("sync_release_pull_request.id")) |
| 40 | + |
| 41 | + pull_request = relationship( |
| 42 | + "SyncReleasePullRequestModel", back_populates="sync_release_targets" |
| 43 | + ) |
| 44 | + |
| 45 | + |
| 46 | +class GitProjectModel(Base): |
| 47 | + __tablename__ = "git_projects" |
| 48 | + id = Column(Integer, primary_key=True) |
| 49 | + namespace = Column(String, index=True) |
| 50 | + repo_name = Column(String, index=True) |
| 51 | + sync_release_pull_requests = relationship( |
| 52 | + "SyncReleasePullRequestModel", back_populates="project" |
| 53 | + ) |
| 54 | + project_url = Column(String) |
| 55 | + instance_url = Column(String, nullable=False) |
| 56 | + |
| 57 | + @classmethod |
| 58 | + def get_or_create( |
| 59 | + cls, namespace: str, repo_name: str, project_url: str, session: sa.orm.Session |
| 60 | + ) -> "GitProjectModel": |
| 61 | + project = ( |
| 62 | + session.query(GitProjectModel) |
| 63 | + .filter_by( |
| 64 | + namespace=namespace, repo_name=repo_name, project_url=project_url |
| 65 | + ) |
| 66 | + .first() |
| 67 | + ) |
| 68 | + if not project: |
| 69 | + project = cls() |
| 70 | + project.repo_name = repo_name |
| 71 | + project.namespace = namespace |
| 72 | + project.project_url = project_url |
| 73 | + project.instance_url = "https://src.fedoraproject.org/" |
| 74 | + session.add(project) |
| 75 | + return project |
| 76 | + |
| 77 | + |
| 78 | +class SyncReleasePullRequestModel(Base): |
| 79 | + __tablename__ = "sync_release_pull_request" |
| 80 | + |
| 81 | + id = Column(Integer, primary_key=True) |
| 82 | + pr_id = Column(Integer, index=True) |
| 83 | + project_id = Column(Integer, ForeignKey("git_projects.id"), index=True) |
| 84 | + project = relationship( |
| 85 | + "GitProjectModel", back_populates="sync_release_pull_requests" |
| 86 | + ) |
| 87 | + sync_release_targets = relationship( |
| 88 | + "SyncReleaseTargetModel", back_populates="pull_request" |
| 89 | + ) |
| 90 | + |
| 91 | + @classmethod |
| 92 | + def get_or_create( |
| 93 | + cls, |
| 94 | + pr_id: int, |
| 95 | + namespace: str, |
| 96 | + repo_name: str, |
| 97 | + project_url: str, |
| 98 | + session: sa.orm.Session, |
| 99 | + ) -> "SyncReleasePullRequestModel": |
| 100 | + project = GitProjectModel.get_or_create( |
| 101 | + namespace=namespace, |
| 102 | + repo_name=repo_name, |
| 103 | + project_url=project_url, |
| 104 | + session=session, |
| 105 | + ) |
| 106 | + pr = ( |
| 107 | + session.query(SyncReleasePullRequestModel) |
| 108 | + .filter_by(pr_id=pr_id, project_id=project.id) |
| 109 | + .first() |
| 110 | + ) |
| 111 | + if not pr: |
| 112 | + pr = SyncReleasePullRequestModel() |
| 113 | + pr.pr_id = pr_id |
| 114 | + pr.project_id = project.id |
| 115 | + session.add(pr) |
| 116 | + return pr |
| 117 | + |
| 118 | + |
| 119 | +def upgrade(): |
| 120 | + # ### commands auto generated by Alembic - please adjust! ### |
| 121 | + op.create_table( |
| 122 | + "sync_release_pull_request", |
| 123 | + sa.Column("id", sa.Integer(), nullable=False), |
| 124 | + sa.Column("pr_id", sa.Integer(), nullable=True), |
| 125 | + sa.Column("project_id", sa.Integer(), nullable=True), |
| 126 | + sa.ForeignKeyConstraint( |
| 127 | + ["project_id"], |
| 128 | + ["git_projects.id"], |
| 129 | + ), |
| 130 | + sa.PrimaryKeyConstraint("id"), |
| 131 | + ) |
| 132 | + op.create_index( |
| 133 | + op.f("ix_sync_release_pull_request_pr_id"), |
| 134 | + "sync_release_pull_request", |
| 135 | + ["pr_id"], |
| 136 | + unique=False, |
| 137 | + ) |
| 138 | + op.create_index( |
| 139 | + op.f("ix_sync_release_pull_request_project_id"), |
| 140 | + "sync_release_pull_request", |
| 141 | + ["project_id"], |
| 142 | + unique=False, |
| 143 | + ) |
| 144 | + op.add_column( |
| 145 | + "sync_release_run_targets", |
| 146 | + sa.Column("downstream_pr_id", sa.Integer(), nullable=True), |
| 147 | + ) |
| 148 | + op.create_foreign_key( |
| 149 | + None, |
| 150 | + "sync_release_run_targets", |
| 151 | + "sync_release_pull_request", |
| 152 | + ["downstream_pr_id"], |
| 153 | + ["id"], |
| 154 | + ) |
| 155 | + |
| 156 | + # ### end Alembic commands ### |
| 157 | + bind = op.get_bind() |
| 158 | + session = sa.orm.Session(bind=bind) |
| 159 | + |
| 160 | + # Create Packit downstream pull request models |
| 161 | + # and missing git projects. |
| 162 | + |
| 163 | + # Split the groups back, this may not fully produce the same thing. |
| 164 | + for sync_release in session.query(SyncReleaseTargetModel): |
| 165 | + if sync_release.downstream_pr_url and not sync_release.pull_request: |
| 166 | + try: |
| 167 | + url = sync_release.downstream_pr_url |
| 168 | + # noqa[203]: prettier like it this way |
| 169 | + project_url = url[0 : (url.rfind("/pull-request/"))] # noqa[203] |
| 170 | + pr_id = int(url[(url.rfind("/pull-request/") + 14) :]) # noqa[203] |
| 171 | + namespace = "rpms" |
| 172 | + repo = url[ |
| 173 | + (url.rfind("rpms/") + 5) : url.rfind("/pull-request") # noqa[203] |
| 174 | + ] |
| 175 | + pull_request = SyncReleasePullRequestModel.get_or_create( |
| 176 | + pr_id, namespace, repo, project_url, session |
| 177 | + ) |
| 178 | + sync_release.pull_request = pull_request |
| 179 | + session.add(pull_request) |
| 180 | + except Exception as e: |
| 181 | + print( |
| 182 | + f"Error creating SyncReleasePullRequestModel during migration {e}" |
| 183 | + ) |
| 184 | + |
| 185 | + |
| 186 | +def downgrade(): |
| 187 | + # ### commands auto generated by Alembic - please adjust! ### |
| 188 | + op.drop_constraint(None, "sync_release_run_targets", type_="foreignkey") |
| 189 | + op.drop_column("sync_release_run_targets", "downstream_pr_id") |
| 190 | + op.drop_index( |
| 191 | + op.f("ix_sync_release_pull_request_project_id"), |
| 192 | + table_name="sync_release_pull_request", |
| 193 | + ) |
| 194 | + op.drop_index( |
| 195 | + op.f("ix_sync_release_pull_request_pr_id"), |
| 196 | + table_name="sync_release_pull_request", |
| 197 | + ) |
| 198 | + op.drop_table("sync_release_pull_request") |
| 199 | + # ### end Alembic commands ### |
0 commit comments