Skip to content

Commit

Permalink
Fix style issues
Browse files Browse the repository at this point in the history
  • Loading branch information
nadzyah committed Aug 8, 2023
1 parent 7dafc4c commit 60d506f
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,41 @@
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '451ec6d4c102'
down_revision = '158244992900'
revision = "451ec6d4c102"
down_revision = "158244992900"
branch_labels = None
depends_on = None


def upgrade() -> None:
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_unique_constraint('unique_artefact_build', 'artefact_build', ['artefact_id', 'architecture', 'revision'])
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_unique_constraint(
"unique_artefact_build",
"artefact_build",
["artefact_id", "architecture", "revision"],
)
# ### end Alembic commands ###


def downgrade() -> None:
op.drop_constraint('unique_artefact_build', 'artefact_build', type_='unique')
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'])
op.drop_constraint("unique_artefact_build", "artefact_build", type_="unique")
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 ###
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.sql import text


Expand All @@ -21,14 +20,17 @@ def upgrade() -> None:
conn = op.get_bind()

# Ensure all 'snap' artefacts have the "store" key
artefacts = conn.execute(text("""
artefacts = conn.execute(
text(
"""
SELECT a.id, a.source
FROM artefact AS a
JOIN stage AS s ON a.stage_id = s.id
JOIN family AS f ON s.family_id = f.id
WHERE f.name = 'snap'
""")).fetchall()

"""
)
).fetchall()

for artefact in artefacts:
source_data = artefact.source
Expand All @@ -41,7 +43,8 @@ def upgrade() -> None:
{"source": source_data, "id": artefact.id},
)
# Create the trigger function
op.execute("""
op.execute(
"""
CREATE OR REPLACE FUNCTION ensure_store_key_for_snap() RETURNS TRIGGER AS $$
BEGIN
IF (EXISTS (
Expand All @@ -50,33 +53,41 @@ def upgrade() -> None:
JOIN family f ON s.family_id = f.id
WHERE s.id = NEW.stage_id AND f.name = 'snap'
) AND NOT NEW.source ? 'store') THEN
RAISE EXCEPTION 'The "store" key is required in source for artefacts with the snap family';
RAISE EXCEPTION
'The "store" key is required in source for artefacts with the snap family';
END IF;
RETURN NEW;
END;
$$ LANGUAGE plpgsql;
""")
"""
)

# Attach the trigger to the artefact table
op.execute("""
op.execute(
"""
CREATE TRIGGER trigger_ensure_store_key_for_snap
BEFORE INSERT OR UPDATE
ON artefact
FOR EACH ROW
EXECUTE FUNCTION ensure_store_key_for_snap();
""")
"""
)


def downgrade() -> None:
conn = op.get_bind()
artefacts = conn.execute(text("""
artefacts = conn.execute(
text(
"""
SELECT a.id, a.source
FROM artefact AS a
JOIN stage AS s ON a.stage_id = s.id
JOIN family AS f ON s.family_id = f.id
WHERE f.name = 'snap'
""")).fetchall()
"""
)
).fetchall()

for artefact in artefacts:
source_data = artefact.source
Expand Down

0 comments on commit 60d506f

Please sign in to comment.