CREATE TABLE IF NOT EXISTS singeling_article ( article_id BIGINT PRIMARY KEY, -- Auto-incrementing primary key article_number TEXT UNIQUE NOT NULL, -- Unique identifier from ERP external_id TEXT, -- ERP reference description TEXT, -- Product details properties JSONB, -- Article metadata created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Creation timestamp updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW() -- Update timestamp ); CREATE TABLE IF NOT EXISTS singeling_work_order ( order_id BIGINT PRIMARY KEY, -- Auto-incrementing primary key external_order_id TEXT UNIQUE NOT NULL, -- External reference from ERP article_id BIGINT NOT NULL, -- Foreign key referencing Article planned_quantity INT NOT NULL, -- Quantity from ERP status TEXT NOT NULL, --CHECK (status IN ('NOT_ACTIVE', 'PLANNED', 'IN_PROGRESS', 'COMPLETE')), -- Valid statuses archived BOOLEAN NOT NULL DEFAULT FALSE, -- Default archive status planned_start TIMESTAMPTZ NOT NULL, -- Planned start time planned_end TIMESTAMPTZ NOT NULL, -- Planned end time actual_start TIMESTAMPTZ, -- Execution start actual_end TIMESTAMPTZ, -- Execution end created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Creation timestamp created_by TEXT NOT NULL, -- Created by user or system CONSTRAINT fk_article FOREIGN KEY (article_id) REFERENCES singeling_article (article_id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS singeling_asset ( asset_id BIGINT PRIMARY KEY, -- Auto-incrementing primary key name TEXT NOT NULL, -- Asset identifier type TEXT NOT NULL, -- Asset classification enterprise TEXT, -- ISA-95 enterprise level site TEXT, -- ISA-95 site level area TEXT, -- ISA-95 area level line TEXT, -- ISA-95 line level workcell TEXT, -- ISA-95 workcell level origin_id TEXT, -- External reference capabilities JSONB, -- Asset metadata active BOOLEAN NOT NULL DEFAULT TRUE, -- Is the asset active created_at TIMESTAMPTZ NOT NULL DEFAULT NOW() -- Creation timestamp ); CREATE TABLE IF NOT EXISTS singeling_operation ( operation_id BIGINT PRIMARY KEY, -- Auto-incrementing primary key order_id BIGINT NOT NULL, -- Foreign key referencing WorkOrder asset_id BIGINT NOT NULL, -- Foreign key referencing Asset sequence_number INT NOT NULL, -- Operation order in the process target_quantity INT NOT NULL, -- Target quantity for the operation actual_quantity INT NOT NULL, -- Running total of completed units scrap_quantity INT NOT NULL DEFAULT 0, -- Quantity of scrap units start_time TIMESTAMPTZ, -- Operation start end_time TIMESTAMPTZ, -- Operation end created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Creation timestamp CONSTRAINT fk_order FOREIGN KEY (order_id) REFERENCES singeling_work_order (order_id) ON DELETE CASCADE, CONSTRAINT fk_asset FOREIGN KEY (asset_id) REFERENCES singeling_asset (asset_id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS singeling_shift ( shift_id BIGINT PRIMARY KEY, -- Auto-incrementing primary key asset_id BIGINT NOT NULL, -- Foreign key referencing Asset shift_name TEXT NOT NULL, -- Shift name (Morning, Night, etc.) start_time TIMESTAMPTZ NOT NULL, -- Shift start end_time TIMESTAMPTZ NOT NULL, -- Shift end is_active BOOLEAN NOT NULL DEFAULT TRUE, -- Current validity external_id TEXT, -- ERP reference created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), -- Creation timestamp CONSTRAINT fk_shift_asset FOREIGN KEY (asset_id) REFERENCES singeling_asset (asset_id) ON DELETE CASCADE ); CREATE TABLE IF NOT EXISTS singeling_state_message ( timestamp_ms BIGINT NOT NULL, -- Primary key part 1: timestamp in milliseconds asset_id BIGINT NOT NULL, -- Primary key part 2: Foreign key referencing Asset type TEXT NOT NULL CHECK (type IN ('IDLE', 'RUNNING', 'FAILURE', 'MAINTENANCE', 'OFFLINE')), -- State type description TEXT, -- Event details comment TEXT, -- Optional notes "user" TEXT, -- Who/what recorded the event terminal TEXT, -- Source system details JSONB, -- Additional context in JSON PRIMARY KEY (timestamp_ms, asset_id), -- Composite primary key CONSTRAINT fk_state_asset FOREIGN KEY (asset_id) REFERENCES singeling_asset (asset_id) ON DELETE CASCADE );