Skip to content

Commit

Permalink
Merge pull request #24 from supabase/chore/migrations
Browse files Browse the repository at this point in the history
feat: self-managed migrations
  • Loading branch information
kiwicopple authored Dec 30, 2021
2 parents 29dcdc9 + 55ed572 commit c48c6d2
Show file tree
Hide file tree
Showing 19 changed files with 122 additions and 715 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,9 @@ This server synchronizes your Stripe account to a Postgres database. It can be a
- Update your Stripe account with all valid webhooks and get the webhook secret
- `mv .env.sample .env` and then rename all the variables
- Make sure the database URL has search_path `stripe`. eg: `DATABASE_URL=postgres://postgres:postgres@hostname:5432/postgres?sslmode=disable&search_path=stripe`
- Run `dbmate up`
- Deploy the [docker image](https://hub.docker.com/r/supabase/stripe-sync-engine) to your favourite hosting service and expose port `8080`
- eg: `docker run -e PORT=8080 --env-file .env supabase/stripe-sync-engine`
- This will automatically run any migrations on your database
- Point your Stripe webooks to your deployed app.
## Future ideas

Expand All @@ -96,7 +96,6 @@ This server synchronizes your Stripe account to a Postgres database. It can be a
- Create a Postgres database on [supabase.com](https://supabase.com) (or another Postgres provider)
- Update Stripe with all valid webhooks and get the webhook secret
- `mv .env.sample .env` and then rename all the variables
- run `dbmate up` to run all the database migrations

**Develop**

Expand Down
1 change: 1 addition & 0 deletions db/migrations/0000_initial_migration.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
select 1;
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- migrate:up

create table "stripe"."products" (
create table if not exists "stripe"."products" (
"id" text primary key,
"object" text,
"active" boolean,
Expand All @@ -17,7 +15,3 @@ create table "stripe"."products" (
"updated" integer,
"url" text
);

-- migrate:down

drop table "stripe"."products";
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- migrate:up


create table "stripe"."customers" (
create table if not exists "stripe"."customers" (
"id" text primary key,
"object" text,
"address" jsonb,
Expand All @@ -24,10 +21,3 @@ create table "stripe"."customers" (
"preferred_locales" jsonb,
"tax_exempt" text
);



-- migrate:down


drop table "stripe"."customers";
Original file line number Diff line number Diff line change
@@ -1,9 +1,17 @@
-- migrate:up
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pricing_type') THEN
create type "stripe"."pricing_type" as enum ('one_time', 'recurring');
END IF;
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'pricing_tiers') THEN
create type "stripe"."pricing_tiers" as enum ('graduated', 'volume');
END IF;
--more types here...
END
$$;

create type "stripe"."pricing_type" as enum ('one_time', 'recurring');
create type "stripe"."pricing_tiers" as enum ('graduated', 'volume');

create table "stripe"."prices" (
create table if not exists "stripe"."prices" (
"id" text primary key,
"object" text,
"active" boolean,
Expand All @@ -24,9 +32,3 @@ create table "stripe"."prices" (
"product" text references stripe.products
);

-- migrate:down

drop table "stripe"."prices";
drop type "stripe"."pricing_type";
drop type "stripe"."pricing_tiers";

Original file line number Diff line number Diff line change
@@ -1,7 +1,21 @@
-- migrate:up

create type "stripe"."subscription_status" as enum ('trialing', 'active', 'canceled', 'incomplete', 'incomplete_expired', 'past_due', 'unpaid');
create table "stripe"."subscriptions" (
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'subscription_status') THEN
create type "stripe"."subscription_status" as enum (
'trialing',
'active',
'canceled',
'incomplete',
'incomplete_expired',
'past_due',
'unpaid'
);
END IF;
END
$$;

create table if not exists "stripe"."subscriptions" (
"id" text primary key,
"object" text,
"cancel_at_period_end" boolean,
Expand Down Expand Up @@ -40,8 +54,3 @@ create table "stripe"."subscriptions" (
"plan" text -- not yet joined
);


-- migrate:down

drop table "stripe"."subscriptions";
drop type "stripe"."subscription_status";
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
-- migrate:up

DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'invoice_status') THEN
create type "stripe"."invoice_status" as enum ('draft', 'open', 'paid', 'uncollectible', 'void');
END IF;
END
$$;

create type "stripe"."invoice_status" as enum ('draft', 'open', 'paid', 'uncollectible', 'void');
create table "stripe"."invoices" (

create table if not exists "stripe"."invoices" (
"id" text primary key,
"object" text,
"auto_advance" boolean,
Expand Down Expand Up @@ -69,10 +75,3 @@ create table "stripe"."invoices" (
"on_behalf_of" text, -- not yet implemented
"charge" text -- not yet implemented
);



-- migrate:down

drop table "stripe"."invoices";
drop type "stripe"."invoice_status";
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
-- migrate:up

CREATE TABLE stripe.charges (
create table if not exists "stripe".charges (
id text primary key,
object text,
card jsonb,
Expand Down Expand Up @@ -42,7 +41,3 @@ CREATE TABLE stripe.charges (
statement_description text,
payment_method_details jsonb
);

-- migrate:down

drop table "stripe"."charges";
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- migrate:up

CREATE TABLE stripe.coupons (
create table if not exists "stripe".coupons (
id text primary key,
object text,
name text,
Expand All @@ -19,7 +17,3 @@ CREATE TABLE stripe.coupons (
duration_in_months bigint,
percent_off_precise double precision
);

-- migrate:down

drop table "stripe"."coupons";
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
-- migrate:up

CREATE TABLE stripe.disputes (
create table if not exists "stripe".disputes (
id text primary key,
object text,
amount bigint,
Expand All @@ -17,8 +15,3 @@ CREATE TABLE stripe.disputes (
balance_transactions jsonb,
is_charge_refundable boolean
);


-- migrate:down

drop table "stripe"."disputes";
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- migrate:up


CREATE TABLE stripe.events (
create table if not exists "stripe".events (
id text primary key,
object text,
data jsonb,
Expand All @@ -13,8 +10,3 @@ CREATE TABLE stripe.events (
api_version text,
pending_webhooks bigint
);


-- migrate:down

drop table "stripe"."events";
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- migrate:up


CREATE TABLE stripe.payouts (
create table if not exists "stripe".payouts (
id text primary key,
object text,
date text,
Expand Down Expand Up @@ -31,8 +28,3 @@ CREATE TABLE stripe.payouts (
statement_description text,
failure_balance_transaction text
);


-- migrate:down

drop table "stripe"."payouts";
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
-- migrate:up


CREATE TABLE "stripe"."plans" (
create table if not exists "stripe"."plans" (
id text primary key,
object text,
name text,
Expand All @@ -26,8 +23,3 @@ CREATE TABLE "stripe"."plans" (
statement_descriptor text,
statement_description text
);


-- migrate:down

drop table "stripe"."plans";
8 changes: 0 additions & 8 deletions db/migrations/20210428143758_init_schema.sql

This file was deleted.

Loading

0 comments on commit c48c6d2

Please sign in to comment.