sqla (mapping): fix missing PEP constraint:
A constraint name was missing on the `PubsubNode.pep` column, resulting in a crash when trying to generate the initial database on launch with an empty `local_dir`. Migration script introducing the column (f4cb5fc600c7_add_missing_pep_check_constraint.py) has been updated too. Another migration script has been made to add the constraint to existing DB (only if they are missing).
This commit is contained in:
parent
de13f16791
commit
7107d64a5b
3 changed files with 42 additions and 2 deletions
|
|
@ -21,7 +21,7 @@ def upgrade():
|
||||||
batch_op.add_column(
|
batch_op.add_column(
|
||||||
sa.Column(
|
sa.Column(
|
||||||
"pep",
|
"pep",
|
||||||
sa.Boolean(create_constraint=True),
|
sa.Boolean(create_constraint=True, name="pep_bool"),
|
||||||
server_default=sa.text("0"),
|
server_default=sa.text("0"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
)
|
)
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,40 @@
|
||||||
|
"""add missing pep check constraint
|
||||||
|
|
||||||
|
Revision ID: f4cb5fc600c7
|
||||||
|
Revises: ccd879fef17e
|
||||||
|
Create Date: 2026-06-20 10:07:46.457551
|
||||||
|
|
||||||
|
"""
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision = "f4cb5fc600c7"
|
||||||
|
down_revision = "ccd879fef17e"
|
||||||
|
branch_labels = None
|
||||||
|
depends_on = None
|
||||||
|
|
||||||
|
|
||||||
|
def _existing_check_constraints(table_name: str) -> set[str | None]:
|
||||||
|
insp = inspect(op.get_bind())
|
||||||
|
return {c["name"] for c in insp.get_check_constraints(table_name) if c.get("name")}
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade():
|
||||||
|
existing = _existing_check_constraints("pubsub_nodes")
|
||||||
|
|
||||||
|
if "ck_pubsub_nodes_pep_bool" not in existing:
|
||||||
|
with op.batch_alter_table("pubsub_nodes") as batch_op:
|
||||||
|
batch_op.create_check_constraint(
|
||||||
|
"ck_pubsub_nodes_pep_bool",
|
||||||
|
"pep IN (0, 1)",
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade():
|
||||||
|
existing = _existing_check_constraints("pubsub_nodes")
|
||||||
|
|
||||||
|
if "ck_pubsub_nodes_pep_bool" in existing:
|
||||||
|
with op.batch_alter_table("pubsub_nodes") as batch_op:
|
||||||
|
batch_op.drop_constraint("ck_pubsub_nodes_pep_bool", type_="check")
|
||||||
|
|
@ -629,7 +629,7 @@ class PubsubNode(Base):
|
||||||
service = Column(JID)
|
service = Column(JID)
|
||||||
name = Column(Text, nullable=False)
|
name = Column(Text, nullable=False)
|
||||||
pep = Column(
|
pep = Column(
|
||||||
Boolean(create_constraint=True),
|
Boolean(create_constraint=True, name="pep_bool"),
|
||||||
nullable=False,
|
nullable=False,
|
||||||
server_default=false(),
|
server_default=false(),
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue