read_replicate/schema_replicate.sql
Rama main · réplica de lectura del repositorio original.
BEGIN;
DROP TABLE IF EXISTS public.version_files, public.delta_files, public.delta_sessions, public.channel_devices, public.manifest, public.onboarding_demo_data, public.app_versions, public.channels, public.apps, public.notifications, public.org_users, public.orgs, public.stripe_info CASCADE;
DROP SEQUENCE IF EXISTS public.app_versions_id_seq, public.channel_devices_id_seq, public.channel_id_seq, public.manifest_id_seq, public.org_users_id_seq, public.stripe_info_id_seq CASCADE;
DROP FUNCTION IF EXISTS public.one_month_ahead();
DROP TYPE IF EXISTS public.channel_update_package, public.manifest_entry, public.disable_update, public.stripe_status;
--
--
--
-- Name: channel_update_package; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.channel_update_package AS ENUM (
'all',
'zip',
'delta',
'zip_from_builtin',
'delta_from_builtin'
);
--
-- Name: disable_update; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.disable_update AS ENUM (
'major',
'minor',
'patch',
'version_number',
'none'
);
--
-- Name: manifest_entry; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.manifest_entry AS (
file_name character varying,
s3_path character varying,
file_hash character varying
);
--
-- Name: stripe_status; Type: TYPE; Schema: public; Owner: -
--
CREATE TYPE public.stripe_status AS ENUM (
'created',
'succeeded',
'updated',
'failed',
'deleted',
'canceled'
);
--
-- Name: one_month_ahead(); Type: FUNCTION; Schema: public; Owner: -
--
CREATE FUNCTION public.one_month_ahead() RETURNS timestamp without time zone
LANGUAGE plpgsql
SET search_path TO ''
AS $$
BEGIN
RETURN NOW() + INTERVAL '1 month';
END;
$$;
--
--
--
--
--
-- Name: apps; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.apps (
created_at timestamp with time zone DEFAULT now(),
app_id character varying NOT NULL,
icon_url character varying NOT NULL,
user_id uuid,
name character varying,
last_version character varying,
updated_at timestamp with time zone,
id uuid DEFAULT gen_random_uuid(),
retention bigint DEFAULT '2592000'::bigint NOT NULL,
owner_org uuid NOT NULL,
default_upload_channel character varying DEFAULT 'production'::character varying NOT NULL,
transfer_history jsonb[] DEFAULT '{}'::jsonb[],
channel_device_count bigint DEFAULT 0 NOT NULL,
manifest_bundle_count bigint DEFAULT 0 NOT NULL,
expose_metadata boolean DEFAULT false NOT NULL,
allow_preview boolean DEFAULT false NOT NULL,
allow_device_custom_id boolean DEFAULT true NOT NULL,
need_onboarding boolean DEFAULT false NOT NULL,
existing_app boolean DEFAULT false NOT NULL,
ios_store_url text,
android_store_url text,
build_timeout_seconds bigint DEFAULT 900 NOT NULL,
build_timeout_updated_at timestamp with time zone DEFAULT now() NOT NULL,
block_provider_infra_requests boolean DEFAULT true NOT NULL,
rollout_channel_count bigint DEFAULT 0 NOT NULL,
rollout_paused_version_names character varying[] DEFAULT '{}'::character varying[] NOT NULL,
created_from_onboarding boolean DEFAULT false NOT NULL,
onboarding_completed_at timestamp with time zone,
onboarding jsonb DEFAULT '{}'::jsonb NOT NULL,
device_data_collection jsonb DEFAULT '{"country": true, "is_prod": true, "platform": true, "os_version": true, "is_emulator": true, "version_build": true, "install_source": true, "plugin_version": true}'::jsonb NOT NULL,
CONSTRAINT apps_build_timeout_seconds_check CHECK (((build_timeout_seconds >= 300) AND (build_timeout_seconds <= 21600)))
);
ALTER TABLE ONLY public.apps REPLICA IDENTITY FULL;
--
-- Name: app_versions; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.app_versions (
id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now(),
app_id character varying NOT NULL,
name character varying NOT NULL,
updated_at timestamp with time zone DEFAULT now(),
deleted boolean DEFAULT false NOT NULL,
external_url character varying,
checksum character varying,
session_key character varying,
storage_provider text DEFAULT 'r2'::text NOT NULL,
min_update_version character varying,
native_packages jsonb[],
owner_org uuid NOT NULL,
user_id uuid,
r2_path character varying,
manifest public.manifest_entry[],
link text,
comment text,
manifest_count integer DEFAULT 0 NOT NULL,
key_id character varying(20),
cli_version character varying,
deleted_at timestamp with time zone,
created_by_apikey_rbac_id uuid
)
WITH (autovacuum_vacuum_scale_factor='0.05', autovacuum_analyze_scale_factor='0.02');
ALTER TABLE ONLY public.app_versions REPLICA IDENTITY FULL;
--
-- Name: app_versions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.app_versions ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.app_versions_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: channel_devices; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.channel_devices (
created_at timestamp with time zone DEFAULT now(),
channel_id bigint NOT NULL,
app_id character varying NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
device_id text NOT NULL,
id bigint NOT NULL,
owner_org uuid NOT NULL,
is_self_set boolean DEFAULT false NOT NULL
);
ALTER TABLE ONLY public.channel_devices REPLICA IDENTITY FULL;
--
-- Name: channel_devices_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.channel_devices ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.channel_devices_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: channels; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.channels (
id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
name character varying NOT NULL,
app_id character varying NOT NULL,
version bigint,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
public boolean DEFAULT false NOT NULL,
disable_auto_update_under_native boolean DEFAULT true NOT NULL,
ios boolean DEFAULT true NOT NULL,
android boolean DEFAULT true NOT NULL,
allow_device_self_set boolean DEFAULT false NOT NULL,
allow_emulator boolean DEFAULT true NOT NULL,
allow_dev boolean DEFAULT true NOT NULL,
disable_auto_update public.disable_update DEFAULT 'major'::public.disable_update NOT NULL,
owner_org uuid NOT NULL,
created_by uuid NOT NULL,
allow_device boolean DEFAULT true NOT NULL,
allow_prod boolean DEFAULT true NOT NULL,
electron boolean DEFAULT true NOT NULL,
rbac_id uuid DEFAULT gen_random_uuid() NOT NULL,
rollout_version bigint,
rollout_percentage_bps integer DEFAULT 0 NOT NULL,
rollout_enabled boolean DEFAULT false NOT NULL,
rollout_id uuid DEFAULT gen_random_uuid() NOT NULL,
rollout_paused_at timestamp with time zone,
rollout_pause_reason text,
rollout_cache_ttl_seconds integer DEFAULT 2592000 NOT NULL,
auto_pause_enabled boolean DEFAULT false NOT NULL,
auto_pause_window_minutes integer DEFAULT 60 NOT NULL,
auto_pause_failure_rate_bps integer,
auto_pause_confidence numeric(5,4) DEFAULT 0.9500 NOT NULL,
auto_pause_min_attempts integer,
auto_pause_min_failures integer,
auto_pause_action text DEFAULT 'pause'::text NOT NULL,
auto_pause_cooldown_minutes integer DEFAULT 60 NOT NULL,
auto_pause_last_triggered_at timestamp with time zone,
auto_pause_last_checked_at timestamp with time zone,
update_package public.channel_update_package DEFAULT 'all'::public.channel_update_package NOT NULL,
paused_at timestamp with time zone,
CONSTRAINT channels_auto_pause_action_check CHECK ((auto_pause_action = ANY (ARRAY['pause'::text, 'rollback'::text, 'notify'::text]))),
CONSTRAINT channels_auto_pause_confidence_check CHECK (((auto_pause_confidence > (0)::numeric) AND (auto_pause_confidence < (1)::numeric))),
CONSTRAINT channels_auto_pause_cooldown_minutes_check CHECK (((auto_pause_cooldown_minutes >= 0) AND (auto_pause_cooldown_minutes <= 10080))),
CONSTRAINT channels_auto_pause_failure_rate_bps_check CHECK (((auto_pause_failure_rate_bps IS NULL) OR ((auto_pause_failure_rate_bps >= 0) AND (auto_pause_failure_rate_bps <= 10000)))),
CONSTRAINT channels_auto_pause_min_attempts_check CHECK (((auto_pause_min_attempts IS NULL) OR (auto_pause_min_attempts >= 0))),
CONSTRAINT channels_auto_pause_min_failures_check CHECK (((auto_pause_min_failures IS NULL) OR (auto_pause_min_failures >= 0))),
CONSTRAINT channels_auto_pause_window_minutes_check CHECK (((auto_pause_window_minutes > 0) AND (auto_pause_window_minutes <= 10080))),
CONSTRAINT channels_rollout_cache_ttl_seconds_check CHECK (((rollout_cache_ttl_seconds >= 60) AND (rollout_cache_ttl_seconds <= 31536000))),
CONSTRAINT channels_rollout_percentage_bps_check CHECK (((rollout_percentage_bps >= 0) AND (rollout_percentage_bps <= 10000)))
);
ALTER TABLE ONLY public.channels REPLICA IDENTITY FULL;
--
-- Name: channel_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.channels ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.channel_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: delta_files; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.delta_files (
id bigint NOT NULL,
app_id character varying NOT NULL,
s3_path character varying NOT NULL,
file_name character varying NOT NULL,
file_hash character varying NOT NULL,
file_size bigint DEFAULT 0 NOT NULL,
path_key bytea NOT NULL,
entry_key bytea NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
path_org uuid,
path_session bytea,
path_sha bytea,
file_hash_bin bytea,
path_session_id integer
);
--
-- Name: delta_files_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.delta_files ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.delta_files_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: delta_sessions; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.delta_sessions (
id integer NOT NULL,
app_id character varying NOT NULL,
session bytea NOT NULL
);
--
-- Name: delta_sessions_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.delta_sessions ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.delta_sessions_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: version_files; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.version_files (
app_version_id bigint NOT NULL,
file_ids bigint[] NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL
);
--
-- Name: notifications; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.notifications (
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
last_send_at timestamp with time zone DEFAULT now() NOT NULL,
total_send bigint DEFAULT '1'::bigint NOT NULL,
owner_org uuid NOT NULL,
event character varying(255) NOT NULL,
uniq_id character varying(255) NOT NULL
);
--
-- Name: onboarding_demo_data; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.onboarding_demo_data (
id uuid DEFAULT gen_random_uuid() NOT NULL,
created_at timestamp with time zone DEFAULT now() NOT NULL,
app_id character varying NOT NULL,
owner_org uuid NOT NULL,
relation_name text NOT NULL,
row_key text NOT NULL,
seed_id uuid NOT NULL,
CONSTRAINT onboarding_demo_data_relation_name_check CHECK ((relation_name = ANY (ARRAY['app_versions'::text, 'app_versions_meta'::text, 'manifest'::text, 'channels'::text, 'channel_devices'::text, 'deploy_history'::text, 'devices'::text, 'build_requests'::text])))
);
--
-- Name: org_users; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.org_users (
id bigint NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
user_id uuid NOT NULL,
org_id uuid NOT NULL,
app_id character varying,
channel_id bigint,
rbac_role_name text DEFAULT 'org_member'::text,
is_invite boolean DEFAULT false NOT NULL
);
--
-- Name: org_users_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
ALTER TABLE public.org_users ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
SEQUENCE NAME public.org_users_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1
);
--
-- Name: orgs; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.orgs (
id uuid DEFAULT gen_random_uuid() NOT NULL,
created_by uuid NOT NULL,
created_at timestamp with time zone DEFAULT now(),
updated_at timestamp with time zone DEFAULT now(),
logo text,
name text NOT NULL,
management_email text NOT NULL,
customer_id character varying,
stats_updated_at timestamp without time zone,
last_stats_updated_at timestamp without time zone,
enforcing_2fa boolean DEFAULT false NOT NULL,
email_preferences jsonb DEFAULT '{"onboarding": true, "usage_limit": true, "credit_usage": true, "device_error": true, "weekly_stats": true, "monthly_stats": true, "bundle_created": true, "bundle_deployed": true, "deploy_stats_24h": true, "billing_period_stats": true, "channel_self_rejected": true}'::jsonb NOT NULL,
password_policy_config jsonb,
enforce_hashed_api_keys boolean DEFAULT false NOT NULL,
require_apikey_expiration boolean DEFAULT false NOT NULL,
max_apikey_expiration_days integer,
enforce_encrypted_bundles boolean DEFAULT false NOT NULL,
required_encryption_key character varying(21) DEFAULT NULL::character varying,
has_usage_credits boolean DEFAULT false NOT NULL,
website text,
stats_refresh_requested_at timestamp without time zone,
onboarding jsonb DEFAULT '{"intent": "unknown"}'::jsonb NOT NULL,
auto_top_up_enabled boolean DEFAULT false NOT NULL,
auto_top_up_threshold numeric(18,6) DEFAULT 10 NOT NULL,
auto_top_up_last_attempt_at timestamp with time zone,
support_channel_type text,
support_channel_url text,
support_channel_set_at timestamp with time zone,
auto_top_up_monthly_limit numeric(18,6) DEFAULT 0 NOT NULL,
signup_plugin text,
CONSTRAINT orgs_max_apikey_expiration_days_valid CHECK (((max_apikey_expiration_days IS NULL) OR ((max_apikey_expiration_days >= 1) AND (max_apikey_expiration_days <= 365)))),
CONSTRAINT orgs_password_policy_config_min_length_check CHECK (((password_policy_config IS NULL) OR ((jsonb_typeof(password_policy_config) = 'object'::text) AND ((NOT (password_policy_config ? 'min_length'::text)) OR ((jsonb_typeof((password_policy_config -> 'min_length'::text)) = 'number'::text) AND (((password_policy_config ->> 'min_length'::text))::numeric = trunc(((password_policy_config ->> 'min_length'::text))::numeric)) AND ((((password_policy_config ->> 'min_length'::text))::numeric >= (6)::numeric) AND (((password_policy_config ->> 'min_length'::text))::numeric <= (72)::numeric))))))),
CONSTRAINT orgs_required_encryption_key_valid CHECK (((required_encryption_key IS NULL) OR (length((required_encryption_key)::text) = ANY (ARRAY[20, 21])))),
CONSTRAINT orgs_support_channel_type_check CHECK (((support_channel_type IS NULL) OR (support_channel_type = ANY (ARRAY['slack'::text, 'discord'::text, 'teams'::text])))),
CONSTRAINT orgs_support_channel_url_check CHECK ((((support_channel_type IS NULL) AND (support_channel_url IS NULL)) OR ((support_channel_type IS NOT NULL) AND (support_channel_url IS NOT NULL) AND (char_length(support_channel_url) <= 2048) AND (support_channel_url ~ '^https://[^/\s]+\S*$'::text))))
);
ALTER TABLE ONLY public.orgs REPLICA IDENTITY FULL;
--
-- Name: stripe_info; Type: TABLE; Schema: public; Owner: -
--
CREATE TABLE public.stripe_info (
created_at timestamp with time zone DEFAULT now() NOT NULL,
updated_at timestamp with time zone DEFAULT now() NOT NULL,
subscription_id character varying,
customer_id character varying NOT NULL,
status public.stripe_status,
product_id character varying NOT NULL,
trial_at timestamp with time zone DEFAULT now() NOT NULL,
price_id character varying,
is_good_plan boolean DEFAULT true,
plan_usage bigint DEFAULT '0'::bigint,
subscription_anchor_start timestamp with time zone DEFAULT now() NOT NULL,
subscription_anchor_end timestamp with time zone DEFAULT public.one_month_ahead() NOT NULL,
canceled_at timestamp with time zone,
mau_exceeded boolean DEFAULT false,
storage_exceeded boolean DEFAULT false,
bandwidth_exceeded boolean DEFAULT false,
id integer NOT NULL,
plan_calculated_at timestamp with time zone,
build_time_exceeded boolean DEFAULT false,
upgraded_at timestamp with time zone,
paid_at timestamp with time zone,
customer_country character varying(2),
last_stripe_event_at timestamp with time zone,
past_due_at timestamp with time zone,
churn_reason text,
is_above_plan boolean,
extra_mau bigint DEFAULT 0 NOT NULL,
billing_account text DEFAULT 'ee'::text NOT NULL,
CONSTRAINT stripe_info_billing_account_check CHECK ((billing_account = ANY (ARRAY['ee'::text, 'us'::text])))
);
ALTER TABLE ONLY public.stripe_info REPLICA IDENTITY FULL;
--
-- Name: stripe_info_id_seq; Type: SEQUENCE; Schema: public; Owner: -
--
CREATE SEQUENCE public.stripe_info_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
--
-- Name: stripe_info_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: -
--
ALTER SEQUENCE public.stripe_info_id_seq OWNED BY public.stripe_info.id;
--
-- Name: stripe_info id; Type: DEFAULT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.stripe_info ALTER COLUMN id SET DEFAULT nextval('public.stripe_info_id_seq'::regclass);
--
-- Name: app_versions app_versions_name_app_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.app_versions
ADD CONSTRAINT app_versions_name_app_id_key UNIQUE (name, app_id);
--
-- Name: app_versions app_versions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.app_versions
ADD CONSTRAINT app_versions_pkey PRIMARY KEY (id);
--
-- Name: apps apps_id_unique; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.apps
ADD CONSTRAINT apps_id_unique UNIQUE (id);
--
-- Name: apps apps_onboarding_valid; Type: CHECK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE public.apps
ADD CONSTRAINT apps_onboarding_valid CHECK (((jsonb_typeof(onboarding) = 'object'::text) AND ((NOT (onboarding ? 'features'::text)) OR (jsonb_typeof((onboarding -> 'features'::text)) = 'object'::text)) AND ((NOT (onboarding ? 'setup'::text)) OR ((jsonb_typeof((onboarding -> 'setup'::text)) = 'object'::text) AND ((NOT ((onboarding -> 'setup'::text) ? 'source'::text)) OR (((onboarding -> 'setup'::text) ->> 'source'::text) = ANY (ARRAY['manual'::text, 'cli'::text, 'mcp'::text, 'ai'::text]))) AND ((NOT ((onboarding -> 'setup'::text) ? 'outcome'::text)) OR (((onboarding -> 'setup'::text) ->> 'outcome'::text) = ANY (ARRAY['in_progress'::text, 'completed'::text, 'skipped'::text, 'switched_to_manual'::text]))))) AND ((NOT (onboarding ? 'source'::text)) OR ((onboarding ->> 'source'::text) = ANY (ARRAY['manual'::text, 'cli'::text, 'mcp'::text, 'ai'::text]))) AND ((NOT (onboarding ? 'outcome'::text)) OR ((onboarding ->> 'outcome'::text) = ANY (ARRAY['in_progress'::text, 'completed'::text, 'skipped'::text, 'switched_to_manual'::text]))))) NOT VALID;
--
-- Name: apps apps_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.apps
ADD CONSTRAINT apps_pkey PRIMARY KEY (app_id);
--
-- Name: channel_devices channel_devices_app_id_device_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.channel_devices
ADD CONSTRAINT channel_devices_app_id_device_id_key UNIQUE (app_id, device_id);
--
-- Name: channel_devices channel_devices_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.channel_devices
ADD CONSTRAINT channel_devices_pkey PRIMARY KEY (id);
--
-- Name: channels channel_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.channels
ADD CONSTRAINT channel_pkey PRIMARY KEY (id);
--
-- Name: channels channels_rbac_id_key; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.channels
ADD CONSTRAINT channels_rbac_id_key UNIQUE (rbac_id);
--
-- Name: delta_files delta_files_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.delta_files
ADD CONSTRAINT delta_files_pkey PRIMARY KEY (id);
--
-- Name: delta_sessions delta_sessions_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.delta_sessions
ADD CONSTRAINT delta_sessions_pkey PRIMARY KEY (id);
--
-- Name: notifications notifications_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.notifications
ADD CONSTRAINT notifications_pkey PRIMARY KEY (owner_org, event, uniq_id);
--
-- Name: onboarding_demo_data onboarding_demo_data_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.onboarding_demo_data
ADD CONSTRAINT onboarding_demo_data_pkey PRIMARY KEY (id);
--
-- Name: org_users org_users_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.org_users
ADD CONSTRAINT org_users_pkey PRIMARY KEY (id);
--
-- Name: orgs orgs_auto_top_up_monthly_limit_valid; Type: CHECK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE public.orgs
ADD CONSTRAINT orgs_auto_top_up_monthly_limit_valid CHECK (((auto_top_up_monthly_limit >= (0)::numeric) AND (auto_top_up_monthly_limit = trunc(auto_top_up_monthly_limit)) AND (auto_top_up_monthly_limit < 'Infinity'::numeric) AND ((auto_top_up_monthly_limit = (0)::numeric) OR (auto_top_up_monthly_limit >= auto_top_up_threshold)))) NOT VALID;
--
-- Name: orgs orgs_auto_top_up_threshold_min; Type: CHECK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE public.orgs
ADD CONSTRAINT orgs_auto_top_up_threshold_min CHECK (((auto_top_up_threshold >= (10)::numeric) AND (auto_top_up_threshold = trunc(auto_top_up_threshold)) AND (auto_top_up_threshold < 'Infinity'::numeric) AND (auto_top_up_threshold > '-Infinity'::numeric))) NOT VALID;
--
-- Name: orgs orgs_onboarding_valid; Type: CHECK CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE public.orgs
ADD CONSTRAINT orgs_onboarding_valid CHECK (((jsonb_typeof(onboarding) = 'object'::text) AND ((NOT (onboarding ? 'intent'::text)) OR ((onboarding ->> 'intent'::text) = ANY (ARRAY['unknown'::text, 'ota'::text, 'builder'::text, 'both'::text, 'exploring'::text, 'publish'::text]))) AND ((NOT (onboarding ? 'development_environment'::text)) OR ((jsonb_typeof((onboarding -> 'development_environment'::text)) = 'string'::text) AND ((onboarding ->> 'development_environment'::text) = ANY (ARRAY['hosted_builder'::text, 'ai_assistant'::text, 'hand_coded'::text, 'other'::text, 'local_project'::text, 'exploring'::text, 'skipped'::text])))))) NOT VALID;
--
-- Name: orgs orgs_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.orgs
ADD CONSTRAINT orgs_pkey PRIMARY KEY (id);
--
-- Name: stripe_info stripe_info_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.stripe_info
ADD CONSTRAINT stripe_info_pkey PRIMARY KEY (customer_id);
--
-- Name: orgs unique customer_id on orgs; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.orgs
ADD CONSTRAINT "unique customer_id on orgs" UNIQUE (customer_id);
--
-- Name: channels unique_name_app_id; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.channels
ADD CONSTRAINT unique_name_app_id UNIQUE (name, app_id);
--
-- Name: orgs unique_name_created_by; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.orgs
ADD CONSTRAINT unique_name_created_by UNIQUE (name, created_by);
--
-- Name: version_files version_files_pkey; Type: CONSTRAINT; Schema: public; Owner: -
--
ALTER TABLE ONLY public.version_files
ADD CONSTRAINT version_files_pkey PRIMARY KEY (app_version_id);
--
-- Name: app_versions_cli_version_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX app_versions_cli_version_idx ON public.app_versions USING btree (cli_version);
--
-- Name: app_versions_r2_path_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX app_versions_r2_path_idx ON public.app_versions USING btree (r2_path);
--
-- Name: apps_created_at_onboarding_setup_source_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX apps_created_at_onboarding_setup_source_idx ON public.apps USING btree (created_at, (((onboarding -> 'setup'::text) ->> 'source'::text)));
--
-- Name: channel_devices_device_id_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX channel_devices_device_id_idx ON public.channel_devices USING btree (device_id);
--
-- Name: channels_one_public_android_per_app_key; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX channels_one_public_android_per_app_key ON public.channels USING btree (app_id) WHERE ((public = true) AND (android = true));
--
-- Name: channels_one_public_electron_per_app_key; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX channels_one_public_electron_per_app_key ON public.channels USING btree (app_id) WHERE ((public = true) AND (electron = true));
--
-- Name: channels_one_public_ios_per_app_key; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX channels_one_public_ios_per_app_key ON public.channels USING btree (app_id) WHERE ((public = true) AND (ios = true));
--
-- Name: finx_app_versions_owner_org; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_app_versions_owner_org ON public.app_versions USING btree (owner_org);
--
-- Name: finx_apps_owner_org; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_apps_owner_org ON public.apps USING btree (owner_org);
--
-- Name: finx_apps_user_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_apps_user_id ON public.apps USING btree (user_id);
--
-- Name: finx_channel_devices_channel_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_channel_devices_channel_id ON public.channel_devices USING btree (channel_id);
--
-- Name: finx_channels_app_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_channels_app_id ON public.channels USING btree (app_id);
--
-- Name: finx_channels_owner_org; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_channels_owner_org ON public.channels USING btree (owner_org);
--
-- Name: finx_channels_version; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_channels_version ON public.channels USING btree (version);
--
-- Name: finx_org_users_channel_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_org_users_channel_id ON public.org_users USING btree (channel_id);
--
-- Name: finx_org_users_org_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_org_users_org_id ON public.org_users USING btree (org_id);
--
-- Name: finx_org_users_user_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_org_users_user_id ON public.org_users USING btree (user_id);
--
-- Name: finx_orgs_created_by; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_orgs_created_by ON public.orgs USING btree (created_by);
--
-- Name: finx_orgs_stripe_info; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX finx_orgs_stripe_info ON public.stripe_info USING btree (product_id);
--
-- Name: idx_app_id_app_versions; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_id_app_versions ON public.app_versions USING btree (app_id);
--
-- Name: idx_app_id_name_app_versions; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_id_name_app_versions ON public.app_versions USING btree (app_id, name);
--
-- Name: idx_app_versions_app_deleted_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_app_deleted_created_at ON public.app_versions USING btree (app_id, deleted, created_at DESC);
--
-- Name: idx_app_versions_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_created_at ON public.app_versions USING btree (created_at);
--
-- Name: idx_app_versions_created_at_app_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_created_at_app_id ON public.app_versions USING btree (created_at, app_id);
--
-- Name: idx_app_versions_deleted; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_deleted ON public.app_versions USING btree (deleted);
--
-- Name: idx_app_versions_deleted_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_deleted_at ON public.app_versions USING btree (deleted_at) WHERE (deleted_at IS NOT NULL);
--
-- Name: idx_app_versions_deleted_at_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_deleted_at_id ON public.app_versions USING btree (deleted_at, id) WHERE (deleted = true);
--
-- Name: idx_app_versions_deleted_manifest_pending; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_deleted_manifest_pending ON public.app_versions USING btree (updated_at NULLS FIRST, id) WHERE ((deleted = true) AND ((manifest_count > 0) OR (manifest IS NOT NULL)));
--
-- Name: idx_app_versions_deleted_with_manifest; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_deleted_with_manifest ON public.app_versions USING btree (id) WHERE ((deleted = true) AND (manifest_count > 0));
--
-- Name: idx_app_versions_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_id ON public.app_versions USING btree (id);
--
-- Name: idx_app_versions_name; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_name ON public.app_versions USING btree (name);
--
-- Name: idx_app_versions_owner_org_not_deleted; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_owner_org_not_deleted ON public.app_versions USING btree (owner_org) WHERE (deleted = false);
--
-- Name: idx_app_versions_retention_cleanup; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_app_versions_retention_cleanup ON public.app_versions USING btree (deleted, created_at, app_id) WHERE (deleted = false);
--
-- Name: idx_apps_created_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_apps_created_at ON public.apps USING btree (created_at);
--
-- Name: idx_apps_default_upload_channel; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_apps_default_upload_channel ON public.apps USING btree (default_upload_channel);
--
-- Name: idx_apps_onboarding_login_creator; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_apps_onboarding_login_creator ON public.apps USING btree (((onboarding ->> 'created_by_user_id'::text))) WHERE ((onboarding #>> '{setup,todo_list_version}'::text[]) = ANY (ARRAY['2'::text, '3'::text, '4'::text]));
--
-- Name: idx_apps_onboarding_ota_stage; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_apps_onboarding_ota_stage ON public.apps USING btree (((((onboarding -> 'features'::text) -> 'ota'::text) ->> 'stage'::text)));
--
-- Name: idx_apps_owner_org_app_id; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX idx_apps_owner_org_app_id ON public.apps USING btree (owner_org, app_id);
--
-- Name: idx_channels_app_id_name; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_app_id_name ON public.channels USING btree (app_id, name);
--
-- Name: idx_channels_app_id_version; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_app_id_version ON public.channels USING btree (app_id, version);
--
-- Name: idx_channels_public_app_id_android; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_public_app_id_android ON public.channels USING btree (public, app_id, android);
--
-- Name: idx_channels_public_app_id_ios; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_public_app_id_ios ON public.channels USING btree (public, app_id, ios);
--
-- Name: idx_channels_rollout_targets; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_rollout_targets ON public.channels USING btree (app_id, rollout_version) WHERE (rollout_version IS NOT NULL);
--
-- Name: idx_channels_rollout_version; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_channels_rollout_version ON public.channels USING btree (rollout_version) WHERE (rollout_version IS NOT NULL);
--
-- Name: idx_orgs_customer_id; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_orgs_customer_id ON public.orgs USING btree (customer_id);
--
-- Name: idx_orgs_email_preferences; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_orgs_email_preferences ON public.orgs USING gin (email_preferences);
--
-- Name: idx_stripe_info_customer_covering; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_stripe_info_customer_covering ON public.stripe_info USING btree (customer_id) INCLUDE (product_id, subscription_anchor_start, subscription_anchor_end);
--
-- Name: idx_stripe_info_past_due_at; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_stripe_info_past_due_at ON public.stripe_info USING btree (past_due_at) WHERE (past_due_at IS NOT NULL);
--
-- Name: idx_stripe_info_trial; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX idx_stripe_info_trial ON public.stripe_info USING btree (trial_at) WHERE (trial_at IS NOT NULL);
--
-- Name: notifications_uniq_id_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX notifications_uniq_id_idx ON public.notifications USING btree (uniq_id);
--
-- Name: onboarding_demo_data_app_relation_row_key_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX onboarding_demo_data_app_relation_row_key_idx ON public.onboarding_demo_data USING btree (app_id, relation_name, row_key);
--
-- Name: onboarding_demo_data_seed_id_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX onboarding_demo_data_seed_id_idx ON public.onboarding_demo_data USING btree (seed_id);
--
-- Name: org_users_app_id_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX org_users_app_id_idx ON public.org_users USING btree (app_id);
--
-- Name: orgs_enforce_hashed_api_keys_true_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX orgs_enforce_hashed_api_keys_true_idx ON public.orgs USING btree (id) WHERE (enforce_hashed_api_keys = true);
--
-- Name: si_customer_cover_uidx; Type: INDEX; Schema: public; Owner: -
--
CREATE UNIQUE INDEX si_customer_cover_uidx ON public.stripe_info USING btree (customer_id) INCLUDE (status, trial_at, mau_exceeded, storage_exceeded, bandwidth_exceeded);
--
-- Name: si_customer_status_trial_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX si_customer_status_trial_idx ON public.stripe_info USING btree (customer_id, status, trial_at) INCLUDE (mau_exceeded, storage_exceeded, bandwidth_exceeded);
--
-- Name: stripe_info_paid_at_idx; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX stripe_info_paid_at_idx ON public.stripe_info USING btree (paid_at) WHERE (paid_at IS NOT NULL);
--
--
COMMIT;