Files
story/migrations/000063_ble_binding.up.sql
T

23 lines
1.0 KiB
PL/PgSQL
Raw Normal View History

BEGIN;
CREATE TABLE device_ble_binding (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
token_hash char(64) NOT NULL UNIQUE,
device_id uuid NOT NULL,
tenant_id uuid NOT NULL,
user_id uuid NOT NULL REFERENCES user_account(id),
household_id uuid NOT NULL,
subject_id uuid NOT NULL,
expected_generation bigint NOT NULL,
status text NOT NULL DEFAULT 'PENDING' CHECK(status IN ('PENDING','COMMITTED','CANCELLED')),
expires_at timestamptz NOT NULL,
binding_id uuid REFERENCES device_binding(id),
created_at timestamptz NOT NULL DEFAULT now(),
completed_at timestamptz,
FOREIGN KEY(device_id,tenant_id) REFERENCES device(id,tenant_id),
FOREIGN KEY(household_id,tenant_id) REFERENCES household(id,tenant_id),
FOREIGN KEY(subject_id,tenant_id) REFERENCES subject_profile(id,tenant_id)
);
CREATE INDEX device_ble_binding_device ON device_ble_binding(device_id,created_at DESC);
CREATE UNIQUE INDEX device_ble_binding_one_pending_actor ON device_ble_binding(device_id,user_id) WHERE status='PENDING';
COMMIT;