1
-- M1 Place scope foundation. Places extend durable Locations without replacing them.
2
create table if not exists places (
3
id uuid primary key default gen_random_uuid(),
4
organization_id uuid not null references organizations(id) on delete restrict,
5
location_id uuid not null references locations(id) on delete restrict,
6
name text not null,
7
code text,
8
place_type text not null default 'permanent',
9
status text not null default 'planned',
10
starts_at timestamptz,
11
ends_at timestamptz,
12
created_by_user_id uuid references user_accounts(id) on delete set null,
13
created_at timestamptz not null default now(),
14
updated_at timestamptz not null default now(),
15
archived_at timestamptz,
16
version bigint not null default 1,
17
constraint places_org_location_code_unique unique (organization_id, location_id, code),
18
constraint places_type_check check (place_type in ('permanent','temporary_fixed','seasonal','recurring','mobile','external')),
19
constraint places_status_check check (status in ('planned','active','closing','closed','archived')),
20
constraint places_period_check check (ends_at is null or starts_at is null or ends_at > starts_at)
21
);
22
create index if not exists idx_places_org_location_status on places(organization_id, location_id, status);
23
create index if not exists idx_places_active_period on places(organization_id, starts_at, ends_at);
24