mirror of https://git.sr.ht/~sircmpwn/hub.sr.ht
Make source repo unique by project
We add a unique constraint on source_repo table to prevent multiple
links of the same remote repository to a given project.
The UI already prevents duplicates (from ef585b6e
) but it seems better
to enforce this in the database model.
This commit is contained in:
parent
84e0598f4d
commit
4e5be22783
|
@ -0,0 +1,32 @@
|
|||
"""Make source repo unique by project
|
||||
|
||||
Revision ID: 9deca12b2917
|
||||
Revises: 4da86bb54214
|
||||
Create Date: 2020-12-27 15:10:57.823055
|
||||
|
||||
"""
|
||||
|
||||
# revision identifiers, used by Alembic.
|
||||
revision = '9deca12b2917'
|
||||
down_revision = '4da86bb54214'
|
||||
|
||||
from alembic import op
|
||||
|
||||
|
||||
def upgrade():
|
||||
# Drop duplicate source_repo keeping the latest.
|
||||
op.execute(
|
||||
"DELETE FROM source_repo A"
|
||||
" USING source_repo B"
|
||||
" WHERE A.project_id = B.project_id AND A.remote_id = B.remote_id"
|
||||
" AND A.repo_type = B.repo_type AND A.id < B.id;"
|
||||
)
|
||||
op.create_unique_constraint(
|
||||
"project_source_repo_unique",
|
||||
"source_repo",
|
||||
["project_id", "remote_id", "repo_type"],
|
||||
)
|
||||
|
||||
|
||||
def downgrade():
|
||||
op.drop_constraint("project_source_repo_unique", "source_repo", type_="unique")
|
|
@ -14,6 +14,12 @@ class RepoType(Enum):
|
|||
|
||||
class SourceRepo(Base):
|
||||
__tablename__ = "source_repo"
|
||||
__table_args__ = (
|
||||
sa.UniqueConstraint(
|
||||
"project_id", "remote_id", "repo_type",
|
||||
name="project_source_repo_unique",
|
||||
),
|
||||
)
|
||||
id = sa.Column(sa.Integer, primary_key=True)
|
||||
remote_id = sa.Column(sa.Integer, nullable=False)
|
||||
created = sa.Column(sa.DateTime, nullable=False)
|
||||
|
|
Loading…
Reference in New Issue