CREATE TABLE e_address_settings ( singleton boolean PRIMARY KEY DEFAULT true CHECK (singleton), trusted_proxy_cidrs text[] NOT NULL DEFAULT '{}', default_address_id uuid, rule_revision bigint NOT NULL DEFAULT 1, updated_by uuid REFERENCES users(id), updated_at timestamptz NOT NULL DEFAULT now() ); INSERT INTO e_address_settings(singleton) VALUES (true); CREATE TABLE e_addresses ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), name text NOT NULL CHECK (length(btrim(name)) BETWEEN 1 AND 128), public_url text NOT NULL CHECK (public_url ~ '^https?://[^/?#[:space:]]+'), 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), node_group text NOT NULL DEFAULT '', priority integer NOT NULL DEFAULT 100 CHECK (priority BETWEEN -100000 AND 100000), enabled boolean NOT NULL DEFAULT true, healthy boolean NOT NULL DEFAULT false, license_valid boolean NOT NULL DEFAULT false, allow_user_publish boolean NOT NULL DEFAULT true, maintenance boolean NOT NULL DEFAULT false, description text NOT NULL DEFAULT '', created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE UNIQUE INDEX e_addresses_public_url_idx ON e_addresses(public_url); CREATE INDEX e_addresses_selection_idx ON e_addresses(enabled,healthy,license_valid,allow_user_publish,maintenance,priority); ALTER TABLE e_address_settings ADD CONSTRAINT e_address_settings_default_fk FOREIGN KEY(default_address_id) REFERENCES e_addresses(id) ON DELETE SET NULL; CREATE TABLE e_address_rules ( id uuid PRIMARY KEY DEFAULT gen_random_uuid(), priority integer NOT NULL DEFAULT 100 CHECK (priority BETWEEN -100000 AND 100000), country text NOT NULL DEFAULT '', region text NOT NULL DEFAULT '', carrier text NOT NULL DEFAULT '', asn bigint NOT NULL DEFAULT 0 CHECK (asn BETWEEN 0 AND 4294967295), node_group text NOT NULL DEFAULT '', address_id uuid NOT NULL REFERENCES e_addresses(id) ON DELETE CASCADE, enabled boolean NOT NULL DEFAULT true, created_at timestamptz NOT NULL DEFAULT now(), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX e_address_rules_match_idx ON e_address_rules(enabled,priority,country,region,carrier,asn,node_group); CREATE TABLE e_user_assignments ( user_id uuid PRIMARY KEY REFERENCES users(id) ON DELETE CASCADE, address_id uuid NOT NULL REFERENCES e_addresses(id), fixed boolean NOT NULL DEFAULT false, rule_revision bigint NOT NULL DEFAULT 1, reason text NOT NULL DEFAULT '', source_ip_hash text NOT NULL DEFAULT '', assigned_at timestamptz NOT NULL DEFAULT now(), expires_at timestamptz, updated_by uuid REFERENCES users(id), updated_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX e_user_assignments_expiry_idx ON e_user_assignments(expires_at); CREATE TABLE e_assignment_history ( id bigint GENERATED ALWAYS AS IDENTITY PRIMARY KEY, user_id uuid NOT NULL REFERENCES users(id) ON DELETE CASCADE, address_id uuid REFERENCES e_addresses(id) ON DELETE SET NULL, source_ip_hash text NOT NULL DEFAULT '', reason text NOT NULL DEFAULT '', rule_revision bigint NOT NULL, created_at timestamptz NOT NULL DEFAULT now() ); CREATE INDEX e_assignment_history_user_idx ON e_assignment_history(user_id,created_at DESC);