Skip to content

Commit 731217c

Browse files
committed
fix(sync-engine): use schema-scoped enum checks in migrations
Fix enum existence checks to be schema-scoped by joining pg_type with pg_namespace. Previously, if an enum with the same name existed in the public schema, the migration would skip creating it in the stripe schema, causing table creation to fail. Affected migrations: - 0003_prices.sql (pricing_type, pricing_tiers) - 0004_subscriptions.sql (subscription_status) - 0005_invoices.sql (invoice_status) - 0024_subscription_schedules.sql (subscription_schedule_status) Closes #191
1 parent 4d41e35 commit 731217c

5 files changed

Lines changed: 27 additions & 10 deletions

File tree

packages/sync-engine/src/database/migrations/0003_prices.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
DO $$
22
BEGIN
3-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pricing_type') THEN
3+
IF NOT EXISTS (
4+
SELECT 1 FROM pg_type t
5+
JOIN pg_namespace n ON t.typnamespace = n.oid
6+
WHERE t.typname = 'pricing_type' AND n.nspname = 'stripe'
7+
) THEN
48
create type "stripe"."pricing_type" as enum ('one_time', 'recurring');
59
END IF;
6-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pricing_tiers') THEN
7-
create type "stripe"."pricing_tiers" as enum ('graduated', 'volume');
10+
IF NOT EXISTS (
11+
SELECT 1 FROM pg_type t
12+
JOIN pg_namespace n ON t.typnamespace = n.oid
13+
WHERE t.typname = 'pricing_tiers' AND n.nspname = 'stripe'
14+
) THEN
15+
create type "stripe"."pricing_tiers" as enum ('graduated', 'volume');
816
END IF;
9-
--more types here...
1017
END
1118
$$;
1219

packages/sync-engine/src/database/migrations/0004_subscriptions.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
DO $$
33
BEGIN
4-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'subscription_status') THEN
4+
IF NOT EXISTS (
5+
SELECT 1 FROM pg_type t
6+
JOIN pg_namespace n ON t.typnamespace = n.oid
7+
WHERE t.typname = 'subscription_status' AND n.nspname = 'stripe'
8+
) THEN
59
create type "stripe"."subscription_status" as enum (
610
'trialing',
711
'active',

packages/sync-engine/src/database/migrations/0005_invoices.sql

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11

22
DO $$
33
BEGIN
4-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'invoice_status') THEN
4+
IF NOT EXISTS (
5+
SELECT 1 FROM pg_type t
6+
JOIN pg_namespace n ON t.typnamespace = n.oid
7+
WHERE t.typname = 'invoice_status' AND n.nspname = 'stripe'
8+
) THEN
59
create type "stripe"."invoice_status" as enum ('draft', 'open', 'paid', 'uncollectible', 'void');
610
END IF;
711
END

packages/sync-engine/src/database/migrations/0012_add_updated_at.sql

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ begin
88
end;
99
$$;
1010

11-
alter function set_updated_at() owner to postgres;
12-
1311
alter table stripe.subscriptions
1412
add updated_at timestamptz default timezone('utc'::text, now()) not null;
1513

packages/sync-engine/src/database/migrations/0024_subscription_schedules.sql

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
1-
do $$
1+
DO $$
22
BEGIN
3-
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'subscription_schedule_status') THEN
3+
IF NOT EXISTS (
4+
SELECT 1 FROM pg_type t
5+
JOIN pg_namespace n ON t.typnamespace = n.oid
6+
WHERE t.typname = 'subscription_schedule_status' AND n.nspname = 'stripe'
7+
) THEN
48
create type "stripe"."subscription_schedule_status" as enum ('not_started', 'active', 'completed', 'released', 'canceled');
59
END IF;
610
END

0 commit comments

Comments
 (0)