tenseleyflow/shithub / 138fda6

Browse files

migrations: seed user_billing_states + AFTER INSERT trigger on users

Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
138fda60bc4807d3bc1927ae354c540d0874d74c
Parents
70c62bd
Tree
f231029

1 changed file

StatusFile+-
A internal/migrationsfs/migrations/0073_user_billing_state_seed.sql 39 0
internal/migrationsfs/migrations/0073_user_billing_state_seed.sqladded
@@ -0,0 +1,39 @@
1
+-- SPDX-License-Identifier: AGPL-3.0-or-later
2
+--
3
+-- PAYMENTS PRO03 — seed user_billing_states for every user.
4
+--
5
+-- Mirrors 0061's org billing seed pattern. Without the dense backfill
6
+-- the entitlements loader (PRO05) would blow up on any user created
7
+-- before 0072 landed. ON CONFLICT DO NOTHING keeps the migration
8
+-- idempotent across reruns.
9
+
10
+-- +goose Up
11
+
12
+INSERT INTO user_billing_states (user_id, plan)
13
+SELECT id, 'free'::user_plan
14
+FROM users
15
+ON CONFLICT (user_id) DO NOTHING;
16
+
17
+-- +goose StatementBegin
18
+CREATE OR REPLACE FUNCTION tg_user_billing_state_seed() RETURNS trigger AS $$
19
+BEGIN
20
+    INSERT INTO user_billing_states (user_id, plan)
21
+    VALUES (NEW.id, 'free'::user_plan)
22
+    ON CONFLICT (user_id) DO NOTHING;
23
+    RETURN NEW;
24
+END;
25
+$$ LANGUAGE plpgsql;
26
+-- +goose StatementEnd
27
+
28
+CREATE TRIGGER tg_user_billing_state_seed_ai
29
+    AFTER INSERT ON users
30
+    FOR EACH ROW EXECUTE FUNCTION tg_user_billing_state_seed();
31
+
32
+-- +goose Down
33
+
34
+DROP TRIGGER IF EXISTS tg_user_billing_state_seed_ai ON users;
35
+DROP FUNCTION IF EXISTS tg_user_billing_state_seed();
36
+
37
+-- Down deliberately leaves the backfilled rows; the previous migration
38
+-- (0072) drops user_billing_states entirely, which is the right place
39
+-- for that cleanup.