feat: add agreement workflow and project-scoped work items
This commit is contained in:
@@ -5,6 +5,8 @@ MIGRATION = "20260925_agreement_boq_fk"
|
||||
BASE_LINK_MIGRATION = "20260925_agreement_base_boq"
|
||||
SCOPE_MIGRATION = "20260925_agreement_scope_boq"
|
||||
ITEM_WORK_ITEM_MIGRATION = "20260925_agreement_item_work_item_required"
|
||||
WORK_ITEM_OPTIONAL_BOQ_MIGRATION = "20260925_work_item_optional_boq"
|
||||
DAILY_REPORT_OPTIONAL_SYSTEM_MIGRATION = "20260925_daily_report_optional_system"
|
||||
|
||||
|
||||
def run_migrations(engine):
|
||||
@@ -101,6 +103,72 @@ def run_migrations(engine):
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_agreement_items_work_item_id ON agreement_items (work_item_id)")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(ITEM_WORK_ITEM_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
if WORK_ITEM_OPTIONAL_BOQ_MIGRATION not in applied:
|
||||
work_item_cols = {row[1]: row[3] for row in connection.execute("PRAGMA table_info(work_items)")}
|
||||
if work_item_cols.get("boq_document_id", 0):
|
||||
connection.execute("""CREATE TABLE work_items_new (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
project_id INTEGER NOT NULL REFERENCES projects(id),
|
||||
boq_document_id INTEGER REFERENCES boq_documents(id),
|
||||
system_id INTEGER REFERENCES systems(id),
|
||||
section_id INTEGER REFERENCES sections(id),
|
||||
source_number VARCHAR,
|
||||
name VARCHAR NOT NULL,
|
||||
model VARCHAR,
|
||||
manufacturer VARCHAR,
|
||||
unit VARCHAR,
|
||||
quantity NUMERIC(15, 4),
|
||||
unit_price NUMERIC(18, 12),
|
||||
amount NUMERIC(18, 12),
|
||||
created_at DATETIME,
|
||||
updated_at DATETIME
|
||||
)""")
|
||||
connection.execute("""INSERT INTO work_items_new (
|
||||
id, project_id, boq_document_id, system_id, section_id, source_number,
|
||||
name, model, manufacturer, unit, quantity, unit_price, amount, created_at, updated_at
|
||||
) SELECT id, project_id, boq_document_id, system_id, section_id, source_number,
|
||||
name, model, manufacturer, unit, quantity, unit_price, amount, created_at, updated_at
|
||||
FROM work_items""")
|
||||
connection.execute("DROP TABLE work_items")
|
||||
connection.execute("ALTER TABLE work_items_new RENAME TO work_items")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_work_items_id ON work_items (id)")
|
||||
agreement_item_cols = {row[1] for row in connection.execute("PRAGMA table_info(agreement_items)")}
|
||||
if "source_boq_document_id" not in agreement_item_cols:
|
||||
connection.execute("ALTER TABLE agreement_items ADD COLUMN source_boq_document_id INTEGER REFERENCES boq_documents(id)")
|
||||
# Older agreement-created positions live in an agreement document. Keep their
|
||||
# source BOQ scope where one was explicitly selected on the Agreement.
|
||||
connection.execute("""UPDATE agreement_items
|
||||
SET source_boq_document_id = (
|
||||
SELECT agreements.boq_document_id FROM agreements
|
||||
WHERE agreements.id = agreement_items.agreement_id
|
||||
)
|
||||
WHERE change_type = 'new_position' AND source_boq_document_id IS NULL""")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(WORK_ITEM_OPTIONAL_BOQ_MIGRATION, datetime.now(timezone.utc).isoformat()))
|
||||
if DAILY_REPORT_OPTIONAL_SYSTEM_MIGRATION not in applied:
|
||||
report_cols = {row[1]: row[3] for row in connection.execute("PRAGMA table_info(daily_reports)")}
|
||||
if report_cols.get("system_id", 0):
|
||||
connection.execute("""CREATE TABLE daily_reports_new (
|
||||
id INTEGER NOT NULL PRIMARY KEY,
|
||||
project_id INTEGER NOT NULL REFERENCES projects(id),
|
||||
system_id INTEGER REFERENCES systems(id),
|
||||
report_date DATE NOT NULL,
|
||||
notes TEXT,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)""")
|
||||
connection.execute("""INSERT INTO daily_reports_new (
|
||||
id, project_id, system_id, report_date, notes, created_at, updated_at
|
||||
) SELECT id, project_id, system_id, report_date, notes, created_at, updated_at
|
||||
FROM daily_reports""")
|
||||
connection.execute("DROP TABLE daily_reports")
|
||||
connection.execute("ALTER TABLE daily_reports_new RENAME TO daily_reports")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_daily_reports_id ON daily_reports (id)")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_daily_reports_project_id ON daily_reports (project_id)")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_daily_reports_system_id ON daily_reports (system_id)")
|
||||
connection.execute("CREATE INDEX IF NOT EXISTS ix_daily_reports_report_date ON daily_reports (report_date)")
|
||||
connection.execute("INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(DAILY_REPORT_OPTIONAL_SYSTEM_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