~netlandish/links

d35a122d057bc73c9da087e583ffd459fd762927 — Peter Sanchez a day ago d1d4f9a enums
Moving invoice status to enums
M migrations/0001_initial.down.sql => migrations/0001_initial.down.sql +1 -0
@@ 35,3 35,4 @@ DROP TABLE IF EXISTS users;
DROP TYPE IF EXISTS domain_level;
DROP TYPE IF EXISTS domain_service;
DROP TYPE IF EXISTS domain_status;
DROP TYPE IF EXISTS invoice_status;

M migrations/0001_initial.up.sql => migrations/0001_initial.up.sql +12 -1
@@ 40,6 40,17 @@ EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

DO $$ BEGIN
  CREATE TYPE invoice_status AS ENUM (
    'PENDING',
    'PAID',
    'REFUNDED',
    'PARTIAL_REFUND'
  );
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;


CREATE TABLE users (
  id SERIAL PRIMARY KEY,


@@ 500,7 511,7 @@ CREATE TRIGGER update_subscriptions_modtime BEFORE UPDATE ON subscriptions FOR E

CREATE TABLE invoices (
  id SERIAL PRIMARY KEY,
  status INT DEFAULT 0,
  status invoice_status DEFAULT 'PENDING',
  user_id INT REFERENCES users (id) ON DELETE CASCADE NOT NULL,
  org_id INT REFERENCES organizations (id) ON DELETE CASCADE NOT NULL,
  stripe_id VARCHAR(150),

M models/invoice.go => models/invoice.go +8 -4
@@ 12,10 12,10 @@ import (
)

const (
	InvoiceStatusPending int = iota
	InvoiceStatusPaid
	InvoiceStatusRefunded
	InvoiceStatusRefundedPartial
	InvoiceStatusPending         string = "PENDING"
	InvoiceStatusPaid            string = "PAID"
	InvoiceStatusRefunded        string = "REFUNDED"
	InvoiceStatusRefundedPartial string = "PARTIAL_REFUND"
)

// GetInvoices returns payment methods


@@ 118,6 118,10 @@ func (i *Invoice) Store(ctx context.Context) error {
	err := database.WithTx(ctx, nil, func(tx *sql.Tx) error {
		var err error
		if i.ID == 0 {
			// Added for fail safes after migration to enums. Can be removed later
			if i.Status == "" {
				i.Status = InvoiceStatusPending
			}
			err = sq.
				Insert("invoices").
				Columns("status", "user_id", "org_id", "stripe_id", "subscription_id", "currency", "amount", "amount_paid",

M models/models.go => models/models.go +1 -1
@@ 400,7 400,7 @@ type Subscription struct {
type Invoice struct {
	ID               int       `db:"id"`
	UserID           int       `db:"user_id"`
	Status           int       `db:"status"`
	Status           string    `db:"status"`
	OrgID            int       `db:"org_id"`
	SubscriptionID   int       `db:"subscription_id"`
	StripeID         string    `db:"stripe_id"`

M models/schema.sql => models/schema.sql +7 -1
@@ 27,6 27,12 @@ CREATE TYPE domain_status AS ENUM (
  'ERROR'
);

CREATE TYPE invoice_status AS ENUM (
  'PENDING',
  'PAID',
  'REFUNDED',
  'PARTIAL_REFUND'
);

CREATE TABLE users (
  id SERIAL PRIMARY KEY,


@@ 487,7 493,7 @@ CREATE TRIGGER update_subscriptions_modtime BEFORE UPDATE ON subscriptions FOR E

CREATE TABLE invoices (
  id SERIAL PRIMARY KEY,
  status INT DEFAULT 0,
  status invoice_status DEFAULT 'PENDING',
  user_id INT REFERENCES users (id) ON DELETE CASCADE NOT NULL,
  org_id INT REFERENCES organizations (id) ON DELETE CASCADE NOT NULL,
  stripe_id VARCHAR(150),