Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
First crack at a new journal schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chadwhitacre committed Jul 13, 2015
1 parent f5c4c8b commit 848f975
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions sql/branch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
BEGIN;

CREATE TYPE account_type AS ENUM ('asset', 'liability', 'income', 'expense');

CREATE TABLE accounts
( id serial PRIMARY KEY
, type account_type NOT NULL
, participant text DEFAULT NULL UNIQUE REFERENCES participants
ON UPDATE CASCADE ON DELETE RESTRICT
, team text DEFAULT NULL UNIQUE REFERENCES teams
ON UPDATE CASCADE ON DELETE RESTRICT
, system text DEFAULT NULL UNIQUE

, CONSTRAINT exactly_one_foreign_key CHECK (
CASE WHEN participant IS NULL THEN 0 ELSE 1 END +
CASE WHEN team IS NULL THEN 0 ELSE 1 END +
CASE WHEN system IS NULL THEN 0 ELSE 1 END = 1
)
);

INSERT INTO accounts (type, system) VALUES ('asset', 'escrow');
INSERT INTO accounts (type, system) VALUES ('asset', 'escrow receivable');
INSERT INTO accounts (type, system) VALUES ('liability', 'escrow payable');
INSERT INTO accounts (type, system) VALUES ('income', 'processing fee revenues');
INSERT INTO accounts (type, system) VALUES ('expense', 'processing fee expenses');
INSERT INTO accounts (type, system) VALUES ('income', 'earned interest');
INSERT INTO accounts (type, system) VALUES ('expense', 'chargeback expenses');


CREATE TABLE journal
( id bigserial PRIMARY KEY
, ts timestamp_tz NOT NULL DEFAULT CURRENT_TIMESTAMP
, amount numeric(35, 2) NOT NULL
, debit bigint NOT NULL REFERENCES accounts
, credit bigint NOT NULL REFERENCES accounts
, payday int DEFAULT NULL REFERENCES paydays
, route bigint DEFAULT NULL REFERENCES exchange_routes
, status exchange_status DEFAULT NULL
, recorder text DEFAULT NULL REFERENCES participants
ON UPDATE CASCADE ON DELETE RESTRICT
);

CREATE TABLE journal_notes
( id bigserial PRIMARY KEY
, body text NOT NULL
, author text NOT NULL REFERENCES participants
ON UPDATE CASCADE ON DELETE RESTRICT
, is_private boolean NOT NULL DEFAULT TRUE
);
END;

0 comments on commit 848f975

Please sign in to comment.