-- V16: typed static Resource drafts (Story 3.6, D-15). -- -- Байты не хранятся здесь: содержимое версии — это content-addressed blob -- (`artifact_blobs`) и owning-ссылка (`artifact_sources`), поэтому Resource -- участвует в существующем учёте владения и sweep-е (AD-10). -- Filesystem path, raw filename и mutable external reference намеренно -- отсутствуют (D-12). create table resources ( workspace_id text not null references workspaces(id) on delete cascade, resource_id text not null, uri text not null, name text not null, description text not null default '', status text not null default 'draft', kind text not null, current_draft_version integer not null, created_at timestamptz not null, updated_at timestamptz not null, constraint resources_resource_id_check check (resource_id ~ '^res_[A-Za-z0-9_-]{1,96}$'), constraint resources_uri_len_check check (octet_length(uri) between 10 and 200), constraint resources_name_len_check check (octet_length(name) between 1 and 64), constraint resources_description_len check (octet_length(description) <= 1024), constraint resources_status_check check (status in ('draft', 'published', 'archived')), constraint resources_kind_check check (kind in ('text', 'json', 'binary')), constraint resources_current_version_check check (current_draft_version >= 1), constraint resources_timestamps_check check (updated_at >= created_at), constraint resources_workspace_uri_unique unique (workspace_id, uri), constraint resources_workspace_name_unique unique (workspace_id, name), primary key (workspace_id, resource_id) ); create table resource_versions ( workspace_id text not null, resource_id text not null, version integer not null, kind text not null, mime_type text not null, size_bytes bigint not null, digest text not null, sensitivity text not null, artifact_source_id text not null, created_at timestamptz not null, superseded_at timestamptz null, constraint resource_versions_version_check check (version >= 1), constraint resource_versions_kind_check check (kind in ('text', 'json', 'binary')), constraint resource_versions_mime_type_check check ( octet_length(mime_type) between 3 and 255 and mime_type ~ '^[A-Za-z0-9!#$&^_.+-]+/[A-Za-z0-9!#$&^_.+-]+$' ), constraint resource_versions_size_check check (size_bytes between 1 and 262144), constraint resource_versions_digest_check check (digest ~ '^[0-9a-f]{64}$'), constraint resource_versions_sensitivity_check check ( sensitivity in ('public', 'internal', 'secret') ), constraint resource_versions_source_id_check check ( artifact_source_id ~ '^src_[A-Za-z0-9_-]{1,128}$' ), constraint resource_versions_supersede_check check ( superseded_at is null or superseded_at >= created_at ), primary key (workspace_id, resource_id, version), constraint resource_versions_resource_fkey foreign key (workspace_id, resource_id) references resources(workspace_id, resource_id) on delete cascade, constraint resource_versions_source_fkey foreign key (workspace_id, artifact_source_id) references artifact_sources(workspace_id, source_id) ); -- Ровно одна текущая черновая версия на Resource: пометка прежней версии -- обязана произойти до вставки новой (D-15 шаг 4), индекс не ослабляется. create unique index resource_versions_current_draft_unique on resource_versions (workspace_id, resource_id) where superseded_at is null; create index resources_workspace_created_idx on resources (workspace_id, created_at, resource_id);