mirror of
https://framagit.org/framasoft/framaspace/argos.git
synced 2025-04-28 18:02:41 +02:00
🗃 — Add ON DELETE CASCADE to results’ task_id constraint
This commit is contained in:
parent
cf609eae6b
commit
064e43dc01
5 changed files with 52 additions and 6 deletions
|
@ -28,6 +28,7 @@ def run_migrations_offline() -> None:
|
||||||
context.configure(
|
context.configure(
|
||||||
url=url,
|
url=url,
|
||||||
target_metadata=target_metadata,
|
target_metadata=target_metadata,
|
||||||
|
render_as_batch=True,
|
||||||
literal_binds=True,
|
literal_binds=True,
|
||||||
dialect_opts={"paramstyle": "named"},
|
dialect_opts={"paramstyle": "named"},
|
||||||
)
|
)
|
||||||
|
@ -50,7 +51,10 @@ def run_migrations_online() -> None:
|
||||||
)
|
)
|
||||||
|
|
||||||
with connectable.connect() as connection:
|
with connectable.connect() as connection:
|
||||||
context.configure(connection=connection, target_metadata=target_metadata)
|
context.configure(connection=connection,
|
||||||
|
target_metadata=target_metadata,
|
||||||
|
render_as_batch=True,
|
||||||
|
)
|
||||||
|
|
||||||
with context.begin_transaction():
|
with context.begin_transaction():
|
||||||
context.run_migrations()
|
context.run_migrations()
|
||||||
|
|
|
@ -53,6 +53,7 @@ def upgrade() -> None:
|
||||||
sa.ForeignKeyConstraint(
|
sa.ForeignKeyConstraint(
|
||||||
["task_id"],
|
["task_id"],
|
||||||
["tasks.id"],
|
["tasks.id"],
|
||||||
|
name="results_task_id_fkey",
|
||||||
),
|
),
|
||||||
sa.PrimaryKeyConstraint("id"),
|
sa.PrimaryKeyConstraint("id"),
|
||||||
)
|
)
|
||||||
|
|
|
@ -0,0 +1,33 @@
|
||||||
|
"""Add ON DELETE CASCADE to results’ task_id
|
||||||
|
|
||||||
|
Revision ID: defda3f2952d
|
||||||
|
Revises: 1a3497f9f71b
|
||||||
|
Create Date: 2024-03-18 15:09:34.544573
|
||||||
|
|
||||||
|
"""
|
||||||
|
from typing import Sequence, Union
|
||||||
|
|
||||||
|
from alembic import op
|
||||||
|
|
||||||
|
|
||||||
|
# revision identifiers, used by Alembic.
|
||||||
|
revision: str = 'defda3f2952d'
|
||||||
|
down_revision: Union[str, None] = '1a3497f9f71b'
|
||||||
|
branch_labels: Union[str, Sequence[str], None] = None
|
||||||
|
depends_on: Union[str, Sequence[str], None] = None
|
||||||
|
|
||||||
|
|
||||||
|
def upgrade() -> None:
|
||||||
|
with op.batch_alter_table('results', schema=None) as batch_op:
|
||||||
|
batch_op.drop_constraint('results_task_id_fkey', type_='foreignkey')
|
||||||
|
batch_op.create_foreign_key('results_task_id_fkey',
|
||||||
|
'tasks',
|
||||||
|
['task_id'],
|
||||||
|
['id'],
|
||||||
|
ondelete='CASCADE')
|
||||||
|
|
||||||
|
|
||||||
|
def downgrade() -> None:
|
||||||
|
with op.batch_alter_table('results', schema=None) as batch_op:
|
||||||
|
batch_op.drop_constraint('results_task_id_fkey', type_='foreignkey')
|
||||||
|
batch_op.create_foreign_key('results_task_id_fkey', 'tasks', ['task_id'], ['id'])
|
|
@ -3,7 +3,7 @@ import sys
|
||||||
from fastapi import FastAPI
|
from fastapi import FastAPI
|
||||||
from fastapi.staticfiles import StaticFiles
|
from fastapi.staticfiles import StaticFiles
|
||||||
from pydantic import ValidationError
|
from pydantic import ValidationError
|
||||||
from sqlalchemy import create_engine
|
from sqlalchemy import create_engine, event
|
||||||
from sqlalchemy.orm import sessionmaker
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
|
||||||
from argos.logging import logger
|
from argos.logging import logger
|
||||||
|
@ -94,6 +94,13 @@ def setup_database(appli):
|
||||||
settings.database_url,
|
settings.database_url,
|
||||||
**extra_settings
|
**extra_settings
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def _fk_pragma_on_connect(dbapi_con, con_record):
|
||||||
|
dbapi_con.execute('pragma foreign_keys=ON')
|
||||||
|
|
||||||
|
if settings.database_url.startswith("sqlite:////"):
|
||||||
|
event.listen(engine, 'connect', _fk_pragma_on_connect)
|
||||||
|
|
||||||
appli.state.SessionLocal = sessionmaker(
|
appli.state.SessionLocal = sessionmaker(
|
||||||
autocommit=False, autoflush=False, bind=engine
|
autocommit=False, autoflush=False, bind=engine
|
||||||
)
|
)
|
||||||
|
|
|
@ -47,7 +47,9 @@ class Task(Base):
|
||||||
)
|
)
|
||||||
last_severity_update: Mapped[datetime] = mapped_column(nullable=True)
|
last_severity_update: Mapped[datetime] = mapped_column(nullable=True)
|
||||||
|
|
||||||
results: Mapped[List["Result"]] = relationship(back_populates="task")
|
results: Mapped[List["Result"]] = relationship(back_populates="task",
|
||||||
|
cascade="all, delete",
|
||||||
|
passive_deletes=True,)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return f"DB Task {self.url} - {self.check} - {self.expected}"
|
return f"DB Task {self.url} - {self.check} - {self.expected}"
|
||||||
|
@ -92,9 +94,8 @@ class Result(Base):
|
||||||
"""
|
"""
|
||||||
__tablename__ = "results"
|
__tablename__ = "results"
|
||||||
id: Mapped[int] = mapped_column(primary_key=True)
|
id: Mapped[int] = mapped_column(primary_key=True)
|
||||||
task_id: Mapped[int] = mapped_column(ForeignKey("tasks.id"))
|
task_id: Mapped[int] = mapped_column(ForeignKey("tasks.id", ondelete="CASCADE"))
|
||||||
task: Mapped["Task"] = relationship(back_populates="results",
|
task: Mapped["Task"] = relationship(back_populates="results")
|
||||||
cascade="save-update, merge, delete")
|
|
||||||
agent_id: Mapped[str] = mapped_column(nullable=True)
|
agent_id: Mapped[str] = mapped_column(nullable=True)
|
||||||
|
|
||||||
submitted_at: Mapped[datetime] = mapped_column()
|
submitted_at: Mapped[datetime] = mapped_column()
|
||||||
|
|
Loading…
Reference in a new issue