create table if not exists orders ( id bigserial primary key, no varchar(64) not null unique, production_no varchar(64), drawing_no varchar(64), batch_no varchar(64), plan_num integer not null default 0, qualified integer not null default 0, unqualified integer not null default 0, status varchar(32) not null default 'NOT_START', inspection_mode varchar(32) not null default 'BEFORE_PLATING', start_tray_no integer not null default 1, start_slot_no integer not null default 1, begin_time timestamp, end_time timestamp, remark text, created_at timestamp not null default now(), updated_at timestamp not null default now(), constraint ck_orders_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')), constraint ck_orders_plan_num check (plan_num > 0), constraint ck_orders_start_tray_no check (start_tray_no > 0), constraint ck_orders_start_slot_no check (start_slot_no between 1 and 49) ); create table if not exists trays ( id bigserial primary key, code varchar(64), rfid varchar(64), rack_row integer not null, rack_column integer not null, present boolean not null default false, loaded_count integer not null default 0, qualified integer not null default 0, unqualified integer not null default 0, status varchar(32) not null default 'EMPTY', created_at timestamp not null default now(), updated_at timestamp not null default now(), unique (rack_row, rack_column) ); create table if not exists workpieces ( id bigserial primary key, barcode varchar(128) not null unique, order_id bigint references orders(id) on delete set null, tray_id bigint references trays(id) on delete set null, tray_pos integer not null default 0, row_no integer not null default 0, column_no integer not null default 0, state varchar(32) not null default 'PENDING', process_step varchar(32), created_at timestamp not null default now(), updated_at timestamp not null default now() ); create unique index if not exists ux_workpieces_tray_position on workpieces(tray_id, tray_pos) where tray_id is not null and tray_pos > 0; create table if not exists tray_slots ( id bigserial primary key, tray_id bigint not null references trays(id) on delete cascade, row_no integer not null, column_no integer not null, workpiece_id bigint references workpieces(id) on delete set null, state varchar(32) not null default 'EMPTY', unique (tray_id, row_no, column_no) ); create unique index if not exists ux_tray_slots_workpiece on tray_slots(workpiece_id) where workpiece_id is not null; create table if not exists devices ( id bigserial primary key, code varchar(64) not null unique, name varchar(128) not null, connection_state varchar(32) not null default 'OFFLINE', run_state varchar(32) not null default 'IDLE', current_action varchar(128), updated_at timestamp not null default now() ); create table if not exists operation_logs ( id bigserial primary key, type varchar(32) not null, module varchar(64) not null, action varchar(64) not null, content text, status varchar(32) not null default 'success', created_at timestamp not null default now() ); insert into trays (code, rack_row, rack_column, present, loaded_count, status) values ('TRAY-1', 1, 1, true, 49, 'PRESENT'), ('TRAY-2', 1, 2, true, 46, 'PRESENT'), ('TRAY-3', 1, 3, true, 43, 'PRESENT'), ('TRAY-4', 2, 1, true, 40, 'PRESENT'), ('TRAY-5', 2, 2, true, 37, 'PRESENT'), ('TRAY-6', 2, 3, true, 34, 'PRESENT') on conflict (rack_row, rack_column) do nothing; -- Only one unfinished order may exist at a time. create unique index if not exists ux_orders_single_unfinished on orders ((1)) where status in ('NOT_START', 'RUNNING', 'PAUSED'); -- Database schema version create table if not exists schema_version ( id smallint primary key default 1, version integer not null default 0, description varchar(256), updated_at timestamp not null default now(), constraint ck_schema_version_id check (id = 1) ); insert into schema_version (id, version, description) values (1, 7, 'Quality query indexes') on conflict (id) do update set version = excluded.version, description = excluded.description, updated_at = now(); -- Production runtime snapshot create table if not exists production_runs ( id bigserial primary key, order_id bigint not null references orders(id), current_workpiece_id bigint references workpieces(id) on delete set null, run_state varchar(32) not null default 'RUNNING', production_state varchar(64) not null, process_step varchar(64), inspection_mode varchar(32) not null default 'BEFORE_PLATING', start_tray_no integer not null default 1, start_slot_no integer not null default 1, plan_num integer not null default 0, timeout_state varchar(64), recovery_retry_count integer not null default 0, last_error text, started_at timestamp not null default now(), state_entered_at timestamp not null default now(), last_heartbeat_at timestamp not null default now(), ended_at timestamp, created_at timestamp not null default now(), updated_at timestamp not null default now(), constraint ck_production_run_state check (run_state in ( 'RUNNING', 'PAUSED', 'RECOVERY', 'ERROR', 'COMPLETED', 'STOPPED' )), constraint ck_production_retry_count check (recovery_retry_count >= 0), constraint ck_production_runs_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')), constraint ck_production_runs_start_tray_no check (start_tray_no > 0), constraint ck_production_runs_start_slot_no check (start_slot_no between 1 and 49), constraint ck_production_runs_plan_num check (plan_num >= 0) ); create unique index if not exists ux_production_runs_active_order on production_runs(order_id) where run_state in ('RUNNING', 'PAUSED', 'RECOVERY'); create index if not exists ix_production_runs_order_id on production_runs(order_id); create index if not exists ix_production_runs_workpiece_id on production_runs(current_workpiece_id); -- Production state history create table if not exists production_state_history ( id bigserial primary key, production_run_id bigint references production_runs(id) on delete cascade, order_id bigint references orders(id) on delete set null, workpiece_id bigint references workpieces(id) on delete set null, from_state varchar(64), to_state varchar(64) not null, process_step varchar(64), source varchar(16) not null default 'SYSTEM', reason varchar(128), message text, operator_name varchar(64), elapsed_ms integer, changed_at timestamp not null default now(), created_at timestamp not null default now(), constraint ck_state_history_source check (source in ('SYSTEM', 'PLC', 'OPERATOR', 'RECOVERY')), constraint ck_state_history_elapsed check (elapsed_ms is null or elapsed_ms >= 0) ); create index if not exists ix_state_history_run_id on production_state_history(production_run_id); create index if not exists ix_state_history_order_id on production_state_history(order_id); create index if not exists ix_state_history_workpiece_id on production_state_history(workpiece_id); create index if not exists ix_state_history_changed_at on production_state_history(changed_at); -- Inspection records create table if not exists inspection_records ( id bigserial primary key, order_id bigint references orders(id) on delete set null, workpiece_id bigint not null references workpieces(id), inspection_side varchar(16) not null, inspection_mode varchar(32) not null default 'BEFORE_PLATING', attempt_no integer not null default 1, raw_result integer, result_state varchar(16) not null, inspection_state varchar(16) not null default 'COMPLETED', source varchar(32) not null default 'PLC', plc_step integer, message text, started_at timestamp, completed_at timestamp, duration_ms integer, created_at timestamp not null default now(), constraint ck_inspection_side check (inspection_side in ('FRONT', 'BACK', 'FINAL')), constraint ck_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')), constraint ck_inspection_attempt check (attempt_no > 0), constraint ck_inspection_result_state check (result_state in ('OK', 'NG', 'ERROR', 'UNKNOWN')), constraint ck_inspection_state check (inspection_state in ('RUNNING', 'COMPLETED', 'FAILED')), constraint ck_inspection_source check (source in ('PLC', 'SIMULATION', 'MANUAL')), constraint ck_inspection_duration check (duration_ms is null or duration_ms >= 0), unique (workpiece_id, inspection_side, attempt_no) ); create index if not exists ix_inspection_records_order_id on inspection_records(order_id); create index if not exists ix_inspection_records_workpiece_id on inspection_records(workpiece_id); create index if not exists ix_inspection_records_created_at on inspection_records(created_at); create index if not exists ix_inspection_records_result on inspection_records(result_state, completed_at); create index if not exists ix_inspection_records_order_side on inspection_records(order_id, inspection_side, completed_at); do $$ begin if exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'inspection_records' and constraint_name = 'ck_inspection_side' ) then alter table public.inspection_records drop constraint ck_inspection_side; end if; alter table public.inspection_records add constraint ck_inspection_side check (inspection_side in ('FRONT', 'BACK', 'FINAL')); end $$; -- Workpiece process history create table if not exists workpiece_process_records ( id bigserial primary key, order_id bigint references orders(id) on delete set null, workpiece_id bigint not null references workpieces(id), process_step varchar(64) not null, inspection_mode varchar(32) not null default 'BEFORE_PLATING', attempt_no integer not null default 1, process_state varchar(16) not null default 'RUNNING', result_state varchar(16), plc_step integer, source varchar(16) not null default 'PLC', failure_reason text, started_at timestamp not null default now(), completed_at timestamp, duration_ms integer, created_at timestamp not null default now(), constraint ck_process_record_attempt check (attempt_no > 0), constraint ck_process_record_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')), constraint ck_process_record_state check (process_state in ('RUNNING', 'COMPLETED', 'FAILED', 'SKIPPED')), constraint ck_process_record_result check (result_state is null or result_state in ('OK', 'NG', 'ERROR', 'UNKNOWN')), constraint ck_process_record_source check (source in ('PLC', 'SIMULATION', 'MANUAL', 'RECOVERY')), constraint ck_process_record_duration check (duration_ms is null or duration_ms >= 0), unique (workpiece_id, process_step, attempt_no) ); create index if not exists ix_process_records_order_id on workpiece_process_records(order_id); create index if not exists ix_process_records_workpiece_id on workpiece_process_records(workpiece_id); create index if not exists ix_process_records_step on workpiece_process_records(process_step); create index if not exists ix_process_records_started_at on workpiece_process_records(started_at); create index if not exists ix_workpieces_order_state on workpieces(order_id, state); -- Inspection mode migration for existing databases do $$ begin if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'orders' and column_name = 'inspection_mode' ) then alter table public.orders add column inspection_mode varchar(32) not null default 'BEFORE_PLATING'; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'orders' and constraint_name = 'ck_orders_inspection_mode' ) then alter table public.orders add constraint ck_orders_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')); end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'orders' and constraint_name = 'ck_orders_plan_num' ) then alter table public.orders add constraint ck_orders_plan_num check (plan_num > 0); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'orders' and column_name = 'start_tray_no' ) then alter table public.orders add column start_tray_no integer not null default 1; end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'orders' and column_name = 'start_slot_no' ) then alter table public.orders add column start_slot_no integer not null default 1; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'orders' and constraint_name = 'ck_orders_start_tray_no' ) then alter table public.orders add constraint ck_orders_start_tray_no check (start_tray_no > 0); end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'orders' and constraint_name = 'ck_orders_start_slot_no' ) then alter table public.orders add constraint ck_orders_start_slot_no check (start_slot_no between 1 and 49); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'production_runs' and column_name = 'inspection_mode' ) then alter table public.production_runs add column inspection_mode varchar(32) not null default 'BEFORE_PLATING'; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'production_runs' and constraint_name = 'ck_production_runs_inspection_mode' ) then alter table public.production_runs add constraint ck_production_runs_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'production_runs' and column_name = 'start_tray_no' ) then alter table public.production_runs add column start_tray_no integer not null default 1; end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'production_runs' and column_name = 'start_slot_no' ) then alter table public.production_runs add column start_slot_no integer not null default 1; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'production_runs' and constraint_name = 'ck_production_runs_start_tray_no' ) then alter table public.production_runs add constraint ck_production_runs_start_tray_no check (start_tray_no > 0); end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'production_runs' and constraint_name = 'ck_production_runs_start_slot_no' ) then alter table public.production_runs add constraint ck_production_runs_start_slot_no check (start_slot_no between 1 and 49); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'production_runs' and column_name = 'plan_num' ) then alter table public.production_runs add column plan_num integer not null default 0; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'production_runs' and constraint_name = 'ck_production_runs_plan_num' ) then alter table public.production_runs add constraint ck_production_runs_plan_num check (plan_num >= 0); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'workpiece_process_records' and column_name = 'inspection_mode' ) then alter table public.workpiece_process_records add column inspection_mode varchar(32) not null default 'BEFORE_PLATING'; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'workpiece_process_records' and constraint_name = 'ck_process_record_inspection_mode' ) then alter table public.workpiece_process_records add constraint ck_process_record_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')); end if; if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'inspection_records' and column_name = 'inspection_mode' ) then alter table public.inspection_records add column inspection_mode varchar(32) not null default 'BEFORE_PLATING'; end if; if not exists ( select 1 from information_schema.table_constraints where table_schema = 'public' and table_name = 'inspection_records' and constraint_name = 'ck_inspection_mode' ) then alter table public.inspection_records add constraint ck_inspection_mode check (inspection_mode in ('BEFORE_PLATING', 'AFTER_PLATING')); end if; end $$; -- Alarm definitions create table if not exists alarm_definitions ( id bigserial primary key, code varchar(64) not null unique, name varchar(128) not null, level varchar(16) not null default 'ERROR', source varchar(32) not null, description text, recovery_hint text, auto_recover boolean not null default false, enabled boolean not null default true, created_at timestamp not null default now(), updated_at timestamp not null default now(), constraint ck_alarm_definition_level check (level in ('INFO', 'WARNING', 'ERROR', 'CRITICAL')), constraint ck_alarm_definition_source check (source in ( 'PLC', 'DEVICE', 'PRODUCTION', 'SYSTEM', 'OPERATOR' )) ); insert into alarm_definitions (code, name, level, source, description, recovery_hint, auto_recover) values ('PLC_DISCONNECTED', 'PLC disconnected', 'CRITICAL', 'PLC', 'PLC communication is unavailable', 'Check network and PLC power, then reconnect', false), ('EMERGENCY_STOP', 'Emergency stop active', 'CRITICAL', 'DEVICE', 'Emergency stop signal is active', 'Release emergency stop after confirming equipment safety', false), ('SAFETY_DOOR_OPEN', 'Safety door open', 'CRITICAL', 'DEVICE', 'Safety door is not closed', 'Close the safety door after confirming equipment safety', false), ('PRODUCTION_TIMEOUT_WARNING', 'Production timeout warning', 'WARNING', 'PRODUCTION', 'A production state is approaching its timeout', 'Check the equipment and continue if the process is safe', true), ('PRODUCTION_TIMEOUT', 'Production timeout', 'ERROR', 'PRODUCTION', 'A production state exceeded its timeout', 'Inspect the PLC step and recover or retry the current process', false), ('PLC_ERROR', 'PLC error', 'ERROR', 'PLC', 'The PLC reported an error code', 'Resolve the PLC error and clear the alarm', false) on conflict (code) do update set name = excluded.name, level = excluded.level, source = excluded.source, description = excluded.description, recovery_hint = excluded.recovery_hint, auto_recover = excluded.auto_recover, updated_at = now(); -- Alarm records create table if not exists alarm_records ( id bigserial primary key, code varchar(64) not null, level varchar(16) not null default 'ERROR', source varchar(32) not null, order_id bigint references orders(id) on delete set null, workpiece_id bigint references workpieces(id) on delete set null, message text not null, alarm_state varchar(16) not null default 'ACTIVE', raised_at timestamp not null default now(), acknowledged_at timestamp, cleared_at timestamp, acknowledged_by varchar(64), cleared_by varchar(64), recovery_action varchar(64), details jsonb, created_at timestamp not null default now(), constraint ck_alarm_level check (level in ('INFO', 'WARNING', 'ERROR', 'CRITICAL')), constraint ck_alarm_source check (source in ( 'PLC', 'DEVICE', 'PRODUCTION', 'SYSTEM', 'OPERATOR' )), constraint ck_alarm_state check (alarm_state in ('ACTIVE', 'ACK', 'CLEARED')), constraint ck_alarm_time_order check ( cleared_at is null or acknowledged_at is null or cleared_at >= acknowledged_at ) ); create index if not exists ix_alarm_records_active on alarm_records(alarm_state, raised_at); create index if not exists ix_alarm_records_code on alarm_records(code); create index if not exists ix_alarm_records_order_id on alarm_records(order_id); create index if not exists ix_alarm_records_workpiece_id on alarm_records(workpiece_id); create index if not exists ix_alarm_records_raised_at on alarm_records(raised_at); do $$ begin if not exists ( select 1 from information_schema.columns where table_schema = 'public' and table_name = 'alarm_records' and column_name = 'definition_id' ) then alter table public.alarm_records add column definition_id bigint; alter table public.alarm_records add constraint fk_alarm_records_definition foreign key (definition_id) references public.alarm_definitions(id) on delete set null; end if; end $$; create index if not exists ix_alarm_records_definition_id on alarm_records(definition_id); -- Device status history create table if not exists device_status_history ( id bigserial primary key, device_id bigint references devices(id) on delete set null, device_code varchar(64), plc_connected boolean not null, plc_ready boolean not null, robot_ready boolean not null, safety_door_closed boolean not null, emergency_stop boolean not null, current_step integer not null default 0, check_result integer not null default 0, error_code integer not null default 0, changed_at timestamp not null default now(), created_at timestamp not null default now() ); create index if not exists ix_device_status_history_device_id on device_status_history(device_id); create index if not exists ix_device_status_history_device_code on device_status_history(device_code); create index if not exists ix_device_status_history_changed_at on device_status_history(changed_at);