···1+-- user_subscriptions: tracks Stripe subscription state per identity
2+create table "public"."user_subscriptions" (
3+ "identity_id" uuid not null,
4+ "stripe_customer_id" text not null,
5+ "stripe_subscription_id" text,
6+ "plan" text,
7+ "status" text,
8+ "current_period_end" timestamp with time zone,
9+ "created_at" timestamp with time zone not null default now(),
10+ "updated_at" timestamp with time zone not null default now()
11+);
12+13+alter table "public"."user_subscriptions" enable row level security;
14+15+CREATE UNIQUE INDEX user_subscriptions_pkey ON public.user_subscriptions USING btree (identity_id);
16+17+alter table "public"."user_subscriptions" add constraint "user_subscriptions_pkey" PRIMARY KEY using index "user_subscriptions_pkey";
18+19+CREATE UNIQUE INDEX user_subscriptions_stripe_customer_id_key ON public.user_subscriptions USING btree (stripe_customer_id);
20+21+CREATE UNIQUE INDEX user_subscriptions_stripe_subscription_id_key ON public.user_subscriptions USING btree (stripe_subscription_id);
22+23+alter table "public"."user_subscriptions" add constraint "user_subscriptions_identity_id_fkey" FOREIGN KEY (identity_id) REFERENCES identities(id) ON DELETE CASCADE;
24+25+grant delete on table "public"."user_subscriptions" to "anon";
26+grant insert on table "public"."user_subscriptions" to "anon";
27+grant references on table "public"."user_subscriptions" to "anon";
28+grant select on table "public"."user_subscriptions" to "anon";
29+grant trigger on table "public"."user_subscriptions" to "anon";
30+grant truncate on table "public"."user_subscriptions" to "anon";
31+grant update on table "public"."user_subscriptions" to "anon";
32+33+grant delete on table "public"."user_subscriptions" to "authenticated";
34+grant insert on table "public"."user_subscriptions" to "authenticated";
35+grant references on table "public"."user_subscriptions" to "authenticated";
36+grant select on table "public"."user_subscriptions" to "authenticated";
37+grant trigger on table "public"."user_subscriptions" to "authenticated";
38+grant truncate on table "public"."user_subscriptions" to "authenticated";
39+grant update on table "public"."user_subscriptions" to "authenticated";
40+41+grant delete on table "public"."user_subscriptions" to "service_role";
42+grant insert on table "public"."user_subscriptions" to "service_role";
43+grant references on table "public"."user_subscriptions" to "service_role";
44+grant select on table "public"."user_subscriptions" to "service_role";
45+grant trigger on table "public"."user_subscriptions" to "service_role";
46+grant truncate on table "public"."user_subscriptions" to "service_role";
47+grant update on table "public"."user_subscriptions" to "service_role";
48+49+-- user_entitlements: feature access decoupled from billing
50+create table "public"."user_entitlements" (
51+ "identity_id" uuid not null,
52+ "entitlement_key" text not null,
53+ "granted_at" timestamp with time zone not null default now(),
54+ "expires_at" timestamp with time zone,
55+ "source" text,
56+ "metadata" jsonb
57+);
58+59+alter table "public"."user_entitlements" enable row level security;
60+61+CREATE UNIQUE INDEX user_entitlements_pkey ON public.user_entitlements USING btree (identity_id, entitlement_key);
62+63+alter table "public"."user_entitlements" add constraint "user_entitlements_pkey" PRIMARY KEY using index "user_entitlements_pkey";
64+65+CREATE INDEX user_entitlements_identity_id_idx ON public.user_entitlements USING btree (identity_id);
66+67+CREATE INDEX user_entitlements_expires_at_idx ON public.user_entitlements USING btree (expires_at);
68+69+alter table "public"."user_entitlements" add constraint "user_entitlements_identity_id_fkey" FOREIGN KEY (identity_id) REFERENCES identities(id) ON DELETE CASCADE;
70+71+grant delete on table "public"."user_entitlements" to "anon";
72+grant insert on table "public"."user_entitlements" to "anon";
73+grant references on table "public"."user_entitlements" to "anon";
74+grant select on table "public"."user_entitlements" to "anon";
75+grant trigger on table "public"."user_entitlements" to "anon";
76+grant truncate on table "public"."user_entitlements" to "anon";
77+grant update on table "public"."user_entitlements" to "anon";
78+79+grant delete on table "public"."user_entitlements" to "authenticated";
80+grant insert on table "public"."user_entitlements" to "authenticated";
81+grant references on table "public"."user_entitlements" to "authenticated";
82+grant select on table "public"."user_entitlements" to "authenticated";
83+grant trigger on table "public"."user_entitlements" to "authenticated";
84+grant truncate on table "public"."user_entitlements" to "authenticated";
85+grant update on table "public"."user_entitlements" to "authenticated";
86+87+grant delete on table "public"."user_entitlements" to "service_role";
88+grant insert on table "public"."user_entitlements" to "service_role";
89+grant references on table "public"."user_entitlements" to "service_role";
90+grant select on table "public"."user_entitlements" to "service_role";
91+grant trigger on table "public"."user_entitlements" to "service_role";
92+grant truncate on table "public"."user_entitlements" to "service_role";
93+grant update on table "public"."user_entitlements" to "service_role";