feat: add agreement cancellation with conservative KS-2 usage guard
This commit is contained in:
@@ -12,6 +12,7 @@ PAYMENT_STATUS_MIGRATION = "20260926_payment_status"
|
||||
KS3_NUMBER_UNIQUE_MIGRATION = "20260927_ks3_number_unique"
|
||||
BOQ_DOCUMENT_REQUISITES_MIGRATION = "20260928_boq_document_requisites"
|
||||
DOCUMENT_ARTIFACTS_MIGRATION = "20260928_document_artifacts"
|
||||
AGREEMENT_STATUS_CANCELLED_MIGRATION = "20260930_agreement_status_cancelled"
|
||||
|
||||
|
||||
def run_migrations(engine):
|
||||
@@ -248,6 +249,36 @@ def run_migrations(engine):
|
||||
)
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(DOCUMENT_ARTIFACTS_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
if AGREEMENT_STATUS_CANCELLED_MIGRATION not in applied:
|
||||
tables = {row[0] for row in connection.execute("SELECT name FROM sqlite_master WHERE type='table'")}
|
||||
if "agreements" in tables:
|
||||
# SQLite CHECK constraints are baked into table definition.
|
||||
# Recreate agreements with the expanded status set.
|
||||
connection.execute("""CREATE TABLE agreements_new (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
project_id INTEGER NOT NULL REFERENCES projects(id),
|
||||
boq_document_id INTEGER REFERENCES boq_documents(id),
|
||||
number VARCHAR NOT NULL,
|
||||
date DATE NOT NULL,
|
||||
title VARCHAR NOT NULL,
|
||||
description TEXT,
|
||||
status VARCHAR NOT NULL DEFAULT 'draft',
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CONSTRAINT ck_agreements_status CHECK (status IN ('draft', 'approved', 'cancelled')),
|
||||
CONSTRAINT uq_agreements_project_number UNIQUE (project_id, number)
|
||||
)""")
|
||||
connection.execute("""INSERT INTO agreements_new
|
||||
(id, project_id, boq_document_id, number, date, title, description, status, created_at, updated_at)
|
||||
SELECT id, project_id, boq_document_id, number, date, title, description, status, created_at, updated_at
|
||||
FROM agreements""")
|
||||
connection.execute("DROP TABLE agreements")
|
||||
connection.execute("ALTER TABLE agreements_new RENAME TO agreements")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_agreements_id ON agreements (id)")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_agreements_project_id ON agreements (project_id)")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_agreements_boq_document_id ON agreements (boq_document_id)")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(AGREEMENT_STATUS_CANCELLED_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
violations = connection.execute("PRAGMA foreign_key_check").fetchall()
|
||||
if violations:
|
||||
raise RuntimeError(f"Foreign key violations after agreement migration: {violations[:10]}")
|
||||
|
||||
Reference in New Issue
Block a user