87 lines
4.2 KiB
SQL
87 lines
4.2 KiB
SQL
CREATE TABLE gateway_settings (
|
|
singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton),
|
|
public_origin text NOT NULL DEFAULT '',
|
|
trusted_proxy_cidrs text[] NOT NULL DEFAULT '{}',
|
|
active_revision bigint,
|
|
updated_by uuid REFERENCES users(id),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
INSERT INTO gateway_settings(singleton) VALUES (true);
|
|
|
|
CREATE TABLE gateway_service_pools (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
code text NOT NULL UNIQUE CHECK (code ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$'),
|
|
name text NOT NULL CHECK (length(btrim(name)) BETWEEN 1 AND 128),
|
|
enabled boolean NOT NULL DEFAULT true,
|
|
is_default boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE UNIQUE INDEX gateway_service_pools_one_default_idx ON gateway_service_pools(is_default) WHERE is_default;
|
|
INSERT INTO gateway_service_pools(code,name,is_default) VALUES ('default','Default',true);
|
|
|
|
CREATE TABLE gateway_nodes (
|
|
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
node_id text NOT NULL UNIQUE CHECK (node_id ~ '^[A-Za-z0-9][A-Za-z0-9._-]{0,63}$'),
|
|
name text NOT NULL CHECK (length(btrim(name)) BETWEEN 1 AND 128),
|
|
gateway_url text NOT NULL CHECK (length(gateway_url) BETWEEN 8 AND 2048),
|
|
pool_id uuid NOT NULL REFERENCES gateway_service_pools(id),
|
|
country text NOT NULL DEFAULT '',
|
|
region text NOT NULL DEFAULT '',
|
|
city text NOT NULL DEFAULT '',
|
|
carrier text NOT NULL DEFAULT '',
|
|
asn bigint NOT NULL DEFAULT 0 CHECK (asn BETWEEN 0 AND 4294967295),
|
|
weight integer NOT NULL DEFAULT 100 CHECK (weight BETWEEN 1 AND 10000),
|
|
max_connections bigint NOT NULL DEFAULT 0 CHECK (max_connections >= 0),
|
|
max_playback_sessions bigint NOT NULL DEFAULT 0 CHECK (max_playback_sessions >= 0),
|
|
max_transcode_sessions bigint NOT NULL DEFAULT 0 CHECK (max_transcode_sessions >= 0),
|
|
bandwidth_capacity_mbps bigint NOT NULL DEFAULT 0 CHECK (bandwidth_capacity_mbps >= 0),
|
|
library_ids text[] NOT NULL DEFAULT '{}',
|
|
storage_ids text[] NOT NULL DEFAULT '{}',
|
|
enabled boolean NOT NULL DEFAULT true,
|
|
allow_login boolean NOT NULL DEFAULT true,
|
|
allow_new_playback boolean NOT NULL DEFAULT true,
|
|
maintenance boolean NOT NULL DEFAULT false,
|
|
draining boolean NOT NULL DEFAULT false,
|
|
created_at timestamptz NOT NULL DEFAULT now(),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX gateway_nodes_pool_idx ON gateway_nodes(pool_id,enabled,node_id);
|
|
CREATE INDEX gateway_nodes_route_idx ON gateway_nodes(enabled,maintenance,draining,country,region,carrier,asn);
|
|
|
|
CREATE TABLE gateway_user_routes (
|
|
user_id uuid PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE,
|
|
forced_node_id text REFERENCES gateway_nodes(node_id) ON UPDATE CASCADE,
|
|
forced_pool_id uuid REFERENCES gateway_service_pools(id),
|
|
preferred_country text NOT NULL DEFAULT '',
|
|
preferred_region text NOT NULL DEFAULT '',
|
|
updated_by uuid REFERENCES users(id),
|
|
updated_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TABLE gateway_config_versions (
|
|
revision bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
snapshot jsonb NOT NULL CHECK (jsonb_typeof(snapshot)='object'),
|
|
source_revision bigint REFERENCES gateway_config_versions(revision),
|
|
created_by uuid NOT NULL REFERENCES users(id),
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX gateway_config_versions_created_idx ON gateway_config_versions(created_at DESC);
|
|
ALTER TABLE gateway_settings ADD CONSTRAINT gateway_settings_active_revision_fk FOREIGN KEY(active_revision) REFERENCES gateway_config_versions(revision);
|
|
|
|
CREATE TABLE gateway_route_events (
|
|
id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
request_id text NOT NULL,
|
|
node_id text,
|
|
outcome text NOT NULL CHECK (outcome IN ('routed','rejected','upstream_failed')),
|
|
reason text NOT NULL DEFAULT '',
|
|
country text NOT NULL DEFAULT '',
|
|
region text NOT NULL DEFAULT '',
|
|
carrier text NOT NULL DEFAULT '',
|
|
asn bigint NOT NULL DEFAULT 0,
|
|
latency_ms integer NOT NULL DEFAULT 0 CHECK (latency_ms >= 0),
|
|
created_at timestamptz NOT NULL DEFAULT now()
|
|
);
|
|
CREATE INDEX gateway_route_events_recent_idx ON gateway_route_events(created_at DESC);
|
|
CREATE INDEX gateway_route_events_node_idx ON gateway_route_events(node_id,created_at DESC);
|