1CREATE TABLE IF NOT EXISTS billing_customers (
 2    id SERIAL PRIMARY KEY,
 3    created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT now(),
 4    user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
 5    stripe_customer_id TEXT NOT NULL
 6);
 7
 8CREATE UNIQUE INDEX "uix_billing_customers_on_user_id" ON billing_customers (user_id);
 9CREATE UNIQUE INDEX "uix_billing_customers_on_stripe_customer_id" ON billing_customers (stripe_customer_id);
10
11-- Make `billing_subscriptions` reference `billing_customers` instead of having its
12-- own `user_id` and `stripe_customer_id`.
13DROP INDEX IF EXISTS "ix_billing_subscriptions_on_user_id";
14DROP INDEX IF EXISTS "ix_billing_subscriptions_on_stripe_customer_id";
15ALTER TABLE billing_subscriptions DROP COLUMN user_id;
16ALTER TABLE billing_subscriptions DROP COLUMN stripe_customer_id;
17ALTER TABLE billing_subscriptions ADD COLUMN billing_customer_id INTEGER NOT NULL REFERENCES billing_customers (id) ON DELETE CASCADE;
18CREATE INDEX "ix_billing_subscriptions_on_billing_customer_id" ON billing_subscriptions (billing_customer_id);