Skip to content

Commit

Permalink
Unique constraint with NULL revision for artefact_build
Browse files Browse the repository at this point in the history
  • Loading branch information
nadzyah committed Aug 11, 2023
1 parent 55c88f1 commit 4640eff
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Unique constraint with NULL revision for artefact_build
Revision ID: ce0b50f657e9
Revises: 18b1066d92c3
Create Date: 2023-08-11 11:51:06.475246+00:00
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'ce0b50f657e9'
down_revision = '18b1066d92c3'
branch_labels = None
depends_on = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_constraint('artefact_build_artefact_id_architecture_revision_key', 'artefact_build', type_='unique')
op.drop_index('unique_artefact_build_null_revision', table_name='artefact_build')
op.create_index('idx_artefact_id_architecture_null_revision', 'artefact_build', ['artefact_id', 'architecture'], unique=True, postgresql_where=sa.text('revision IS NULL'))
op.create_index('idx_artefact_id_architecture_revision', 'artefact_build', ['artefact_id', 'architecture', 'revision'], unique=True, postgresql_where=sa.text('revision IS NOT NULL'))
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('idx_artefact_id_architecture_revision', table_name='artefact_build', postgresql_where=sa.text('revision IS NOT NULL'))
op.drop_index('idx_artefact_id_architecture_null_revision', table_name='artefact_build', postgresql_where=sa.text('revision IS NULL'))
op.create_index('unique_artefact_build_null_revision', 'artefact_build', ['artefact_id', 'architecture'], unique=False)
op.create_unique_constraint('artefact_build_artefact_id_architecture_revision_key', 'artefact_build', ['artefact_id', 'architecture', 'revision'])
# ### end Alembic commands ###
18 changes: 14 additions & 4 deletions backend/test_observer/data_access/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from typing import TypeVar
from datetime import datetime, date

from sqlalchemy import ForeignKey, String, UniqueConstraint
from sqlalchemy import ForeignKey, String, UniqueConstraint, Index, column
from sqlalchemy.sql import func
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import (
Expand Down Expand Up @@ -113,12 +113,22 @@ class ArtefactBuild(Base):
)

__table_args__ = (
UniqueConstraint(
# Unique constraint when revision is NULL
Index(
"idx_artefact_id_architecture_null_revision",
"artefact_id",
"architecture",
unique=True,
postgresql_where=(column("revision").is_(None)),
),
# Unique constraint when revision is NOT NULL
Index(
"idx_artefact_id_architecture_revision",
"artefact_id",
"architecture",
"revision",
name="unique_artefact_build",
postgresql_nulls_not_distinct=True,
unique=True,
postgresql_where=(column("revision").isnot(None)),
),
)

Expand Down

0 comments on commit 4640eff

Please sign in to comment.