Files
tilbudgivern/backend/migrations/ordrestyring_engine_sprint1.sql

72 lines
2.6 KiB
SQL

-- Ordrestyring Engine Sprint 1 baseline tables
CREATE TABLE IF NOT EXISTS ordrestyring_case_latest (
case_number VARCHAR(64) NOT NULL PRIMARY KEY,
customer_number VARCHAR(255) NULL,
offer_number INT NULL,
description TEXT NULL,
remarks TEXT NULL,
work_done TEXT NULL,
status INT NULL,
created_at_ordrestyring DATETIME NULL,
updated_at_ordrestyring DATETIME NULL,
source_case_id INT NULL,
synced_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
INDEX idx_case_latest_customer_number (customer_number),
INDEX idx_case_latest_offer_number (offer_number),
INDEX idx_case_latest_updated_at (updated_at_ordrestyring)
);
CREATE TABLE IF NOT EXISTS ordrestyring_materials_normalized (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
case_number VARCHAR(64) NOT NULL,
line_id INT NOT NULL,
raw_unit VARCHAR(64) NULL,
normalized_unit VARCHAR(32) NOT NULL,
quantity_raw DECIMAL(14,3) NULL,
quantity_normalized DECIMAL(14,3) NULL,
unit_price DECIMAL(14,2) NULL,
line_total DECIMAL(14,2) NULL,
product_text TEXT NULL,
product_number VARCHAR(255) NULL,
material_key VARCHAR(255) NULL,
created_at_ordrestyring DATETIME NULL,
synced_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uq_materials_norm_case_line (case_number, line_id),
INDEX idx_materials_norm_case (case_number),
INDEX idx_materials_norm_key (material_key),
INDEX idx_materials_norm_unit (normalized_unit)
);
CREATE TABLE IF NOT EXISTS ordrestyring_hours_normalized (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
case_number VARCHAR(64) NOT NULL,
hour_id INT NOT NULL,
is_case_linked TINYINT(1) NOT NULL DEFAULT 1,
start_at DATETIME NULL,
stop_at DATETIME NULL,
duration_hours DECIMAL(10,2) NOT NULL DEFAULT 0,
remark TEXT NULL,
hour_type INT NULL,
task_key VARCHAR(255) NULL,
synced_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
UNIQUE KEY uq_hours_norm_case_hour (case_number, hour_id),
INDEX idx_hours_norm_case (case_number),
INDEX idx_hours_norm_case_linked (is_case_linked),
INDEX idx_hours_norm_start (start_at)
);
CREATE TABLE IF NOT EXISTS ordrestyring_data_quality_issues (
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
issue_type VARCHAR(64) NOT NULL,
severity VARCHAR(16) NOT NULL,
entity_type VARCHAR(32) NOT NULL,
entity_key VARCHAR(255) NULL,
issue_details_json JSON NULL,
detected_at TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
resolved_at DATETIME NULL,
INDEX idx_quality_issue_type (issue_type),
INDEX idx_quality_entity (entity_type, entity_key),
INDEX idx_quality_detected (detected_at)
);