Files
khb_microservices/packages/khb_common/migrations/002_native_tables.sql
Yuri Yaryshev ebb1cb7950
Some checks failed
ci / test (push) Failing after 20s
ci / deploy (push) Has been skipped
Каркас monorepo: scheduler, data_collector, khbuild_assistant_prep, khb_common; миграции; CI/CD
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-11 09:44:55 +03:00

164 lines
7.1 KiB
SQL

-- Native-таблицы: люди, проекты ассистента, справочники, планировщик, пакеты входных данных, результаты агента
create table person (
id uuid primary key default gen_random_uuid(),
display_name text not null,
email text,
jt_membership_id text,
slack_user_id text,
is_active boolean not null default true,
note text
);
create table construction_manager (
person_id uuid primary key references person(id),
timezone text not null default 'America/New_York',
is_active boolean not null default true,
paused_until date
);
create table assistant_project (
id uuid primary key default gen_random_uuid(),
jt_job_id text not null unique,
manager_person_id uuid not null references construction_manager(person_id),
slack_channel_id text,
mode text not null default 'test' check (mode in ('test','prod')),
is_enabled boolean not null default true,
created_at timestamptz not null default now()
);
create table purchase_task_ref (
id uuid primary key default gen_random_uuid(),
task_name text not null,
note text,
is_verified boolean not null default false,
verified_by text,
verified_at timestamptz
);
create unique index purchase_task_ref_name_uq on purchase_task_ref (lower(task_name));
insert into purchase_task_ref (task_name, note) values
('Purchase 0', 'реальное название из JobTread 2026-10-11'),
('Purchase 0: Contract for Pilings Installation', 'реальное название из JobTread 2026-10-11'),
('Purchase 1 order start', 'реальное название из JobTread 2026-10-11'),
('Purchase 2 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 3 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 4 Start', 'реальное название из JobTread 2026-10-11'),
('Purchase 4 Delivered', 'реальное название из JobTread 2026-10-11'),
('Purchase 5 delivered', 'реальное название из JobTread 2026-10-11'),
('Start Purchase 5', 'реальное название из JobTread 2026-10-11');
create table calendar_day (
day date primary key,
is_workday boolean not null,
holiday_name text,
source text not null check (source in ('nager','manual')),
is_verified boolean not null default false,
verified_by text,
verified_at timestamptz,
loaded_at timestamptz not null default now()
);
create table reminder_history (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
jt_task_id text,
reminded_at timestamptz not null,
reminder_text text,
reply_at timestamptz,
reply_summary text,
reply_outcome text check (reply_outcome in ('done','rescheduled','no_answer','blocked','other'))
);
create table sched_run (
id uuid primary key default gen_random_uuid(),
job_name text not null,
scheduled_minute timestamptz not null,
status text not null check (status in ('started','ok','error','missed','skipped')),
noticed_at timestamptz not null default now(),
finished_at timestamptz,
http_status int,
response_ref text,
message text,
unique (job_name, scheduled_minute)
);
create table ap_input_package (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
package_date date not null,
kind text not null check (kind in ('morning','noon')),
source_snapshot_ts timestamptz,
content_hash text not null,
item_count int not null,
changed_since_morning boolean,
created_at timestamptz not null default now(),
unique (assistant_project_id, package_date, kind)
);
create table ap_input_package_item (
id bigserial primary key,
package_id uuid not null references ap_input_package(id) on delete cascade,
input_no smallint not null check (input_no in (1,2,3,4,5,7)),
jt_task_id text,
name text not null,
start_date date,
end_date date,
is_overdue boolean not null default false,
is_purchase boolean not null default false,
assignees text[] not null default '{}',
related_task_id text,
days_until_start int,
payload jsonb
);
create index ap_input_package_item_idx on ap_input_package_item (package_id, input_no);
create table ap_agent_invocation (
id uuid primary key default gen_random_uuid(),
package_id uuid not null references ap_input_package(id),
reason text not null check (reason in ('morning','noon_changed')),
requested_at timestamptz not null default now(),
status text not null check (status in ('requested','accepted','failed')),
http_status int,
session_ref text,
error text,
attempt int not null default 1
);
create table active_task_list (
id uuid primary key default gen_random_uuid(),
assistant_project_id uuid not null references assistant_project(id),
package_id uuid not null references ap_input_package(id),
list_date date not null,
summary_text text,
is_current boolean not null default true,
created_at timestamptz not null default now()
);
create table active_task (
id uuid primary key default gen_random_uuid(),
list_id uuid not null references active_task_list(id) on delete cascade,
rank int not null,
jt_task_id text,
title text not null,
rationale text,
importance text check (importance in ('high','medium','low')),
remind_next boolean not null default false
);
create view v_agent_input as
select p.id as package_id, ap.id as assistant_project_id, j.name as project_name, ap.jt_job_id,
per.display_name as manager_name, p.package_date, p.kind,
i.input_no, i.jt_task_id, i.name, i.start_date, i.end_date, i.is_overdue, i.is_purchase,
i.assignees, i.related_task_id, i.days_until_start, i.payload
from ap_input_package p
join assistant_project ap on ap.id = p.assistant_project_id
join person per on per.id = ap.manager_person_id
left join jt_job j on j.id = ap.jt_job_id
join ap_input_package_item i on i.package_id = p.id;
alter table person enable row level security;
alter table construction_manager enable row level security;
alter table assistant_project enable row level security;
alter table purchase_task_ref enable row level security;
alter table calendar_day enable row level security;
alter table reminder_history enable row level security;
alter table sched_run enable row level security;
alter table ap_input_package enable row level security;
alter table ap_input_package_item enable row level security;
alter table ap_agent_invocation enable row level security;
alter table active_task_list enable row level security;
alter table active_task enable row level security;