Skip to content

Commit

Permalink
merge branch.sql
Browse files Browse the repository at this point in the history
  • Loading branch information
Changaco committed Mar 30, 2017
1 parent 187ec1b commit bf86f69
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 47 deletions.
4 changes: 4 additions & 0 deletions sql/app-conf-defaults.sql
Expand Up @@ -34,6 +34,10 @@ INSERT INTO app_conf (key, value) VALUES
('openstreetmap_id', '"w4eQbkobmMzpkJFwS4tM16a3lq9AFbcoNCKNcGBE"'::jsonb),
('openstreetmap_secret', '"W08UgEhxQnh7nMzB7GfSFcqcwPnZMmKMNyxWdcw4"'::jsonb),
('password_rounds', '1'::jsonb),
('s3_endpoint', '""'::jsonb),
('s3_public_access_key', '""'::jsonb),
('s3_secret_key', '""'::jsonb),
('s3_region', '"eu-west-1"'::jsonb),
('send_newsletters_every', '60'::jsonb),
('socket_timeout', '10.0'::jsonb),
('twitter_callback', '"http://127.0.0.1:8339/on/twitter/associate"'::jsonb),
Expand Down
45 changes: 0 additions & 45 deletions sql/branch.sql

This file was deleted.

35 changes: 35 additions & 0 deletions sql/migrations.sql
Expand Up @@ -321,3 +321,38 @@ CREATE TRIGGER update_nsubscribers
BEFORE INSERT OR UPDATE OR DELETE ON subscriptions
FOR EACH ROW
EXECUTE PROCEDURE update_nsubscribers();

-- migration #30
ALTER TYPE transfer_context ADD VALUE 'expense';
CREATE TYPE invoice_nature AS ENUM ('expense');
CREATE TYPE invoice_status AS ENUM
('pre', 'canceled', 'new', 'retracted', 'accepted', 'paid', 'rejected');
CREATE TABLE invoices
( id serial PRIMARY KEY
, ctime timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
, sender bigint NOT NULL REFERENCES participants
, addressee bigint NOT NULL REFERENCES participants
, nature invoice_nature NOT NULL
, amount numeric(35,2) NOT NULL CHECK (amount > 0)
, description text NOT NULL
, details text
, documents jsonb NOT NULL
, status invoice_status NOT NULL
);
CREATE TABLE invoice_events
( id serial PRIMARY KEY
, invoice int NOT NULL REFERENCES invoices
, participant bigint NOT NULL REFERENCES participants
, ts timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
, status invoice_status NOT NULL
, message text
);
ALTER TABLE participants ADD COLUMN allow_invoices boolean;
ALTER TABLE transfers
ADD COLUMN invoice int REFERENCES invoices,
ADD CONSTRAINT expense_chk CHECK (NOT (context='expense' AND invoice IS NULL));
INSERT INTO app_conf VALUES
('s3_endpoint', '""'::jsonb),
('s3_public_access_key', '""'::jsonb),
('s3_secret_key', '""'::jsonb),
('s3_region', '"eu-west-1"'::jsonb);
38 changes: 36 additions & 2 deletions sql/schema.sql
Expand Up @@ -23,7 +23,7 @@ COMMENT ON EXTENSION pg_stat_statements IS 'track execution statistics of all SQ

-- database metadata
CREATE TABLE db_meta (key text PRIMARY KEY, value jsonb);
INSERT INTO db_meta (key, value) VALUES ('schema_version', '29'::jsonb);
INSERT INTO db_meta (key, value) VALUES ('schema_version', '30'::jsonb);


-- app configuration
Expand Down Expand Up @@ -79,6 +79,8 @@ CREATE TABLE participants

, nsubscribers int NOT NULL DEFAULT 0

, allow_invoices boolean

, CONSTRAINT balance_chk CHECK (NOT ((status <> 'active' OR kind IN ('group', 'community')) AND balance <> 0))
, CONSTRAINT giving_chk CHECK (NOT (kind IN ('group', 'community') AND giving <> 0))
, CONSTRAINT goal_chk CHECK (NOT (kind IN ('group', 'community') AND status='active' AND goal IS NOT NULL AND goal <= 0))
Expand Down Expand Up @@ -175,7 +177,7 @@ CREATE TRIGGER update_current_tip INSTEAD OF UPDATE ON current_tips
-- transfers -- balance transfers from one user to another

CREATE TYPE transfer_context AS ENUM
('tip', 'take', 'final-gift', 'refund');
('tip', 'take', 'final-gift', 'refund', 'expense');

CREATE TYPE transfer_status AS ENUM ('pre', 'failed', 'succeeded');

Expand All @@ -190,8 +192,10 @@ CREATE TABLE transfers
, status transfer_status NOT NULL
, error text
, refund_ref bigint REFERENCES transfers
, invoice int REFERENCES invoices
, CONSTRAINT team_chk CHECK (NOT (context='take' AND team IS NULL))
, CONSTRAINT self_chk CHECK (tipper <> tippee)
, CONSTRAINT expense_chk CHECK (NOT (context='expense' AND invoice IS NULL))
);

CREATE INDEX transfers_tipper_idx ON transfers (tipper);
Expand Down Expand Up @@ -505,6 +509,36 @@ CREATE INDEX newsletter_texts_not_sent_idx
WHERE sent_at IS NULL AND scheduled_for IS NOT NULL;


-- invoices

CREATE TYPE invoice_nature AS ENUM ('expense');

CREATE TYPE invoice_status AS ENUM
('pre', 'canceled', 'new', 'retracted', 'accepted', 'paid', 'rejected');

CREATE TABLE invoices
( id serial PRIMARY KEY
, ctime timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
, sender bigint NOT NULL REFERENCES participants
, addressee bigint NOT NULL REFERENCES participants
, nature invoice_nature NOT NULL
, amount numeric(35,2) NOT NULL CHECK (amount > 0)
, description text NOT NULL
, details text
, documents jsonb NOT NULL
, status invoice_status NOT NULL
);

CREATE TABLE invoice_events
( id serial PRIMARY KEY
, invoice int NOT NULL REFERENCES invoices
, participant bigint NOT NULL REFERENCES participants
, ts timestamptz NOT NULL DEFAULT CURRENT_TIMESTAMP
, status invoice_status NOT NULL
, message text
);


-- composite types, keep this at the end of the file

\i sql/composites.sql

0 comments on commit bf86f69

Please sign in to comment.