jl/db_deploy.sql

170 lines
5.3 KiB
MySQL
Raw Normal View History

2018-09-12 14:48:59 -04:00
BEGIN TRANSACTION;
--\conninfo
--------------------------build schema----------------------------------------------
2018-09-19 22:24:27 -04:00
DROP SCHEMA IF EXISTS evt cascade;
2018-09-12 14:48:59 -04:00
CREATE SCHEMA evt;
2018-09-20 20:03:08 -04:00
CREATE EXTENSION IF NOT EXISTS ltree;
2018-09-12 14:48:59 -04:00
COMMENT ON SCHEMA evt IS 'event log';
--------------------------event log table-------------------------------------------
CREATE TABLE evt.bpr (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
,bpr JSONB
,bprh JSONB
,stmp timestamptz
);
COMMENT ON COLUMN evt.bpr.bpr IS 'basic pecuniary record';
COMMENT ON COLUMN evt.bpr.bprh IS 'basic pecuniary record history';
COMMENT ON COLUMN evt.bpr.stmp IS 'insert time';
--------------------------account master---------------------------------------------
--the account master should be dynamically created
CREATE TABLE evt.acct (
acct text PRIMARY KEY
,prop jsonb
);
COMMENT ON COLUMN evt.acct.acct IS 'account';
COMMENT ON COLUMN evt.acct.prop IS 'properties';
2018-09-19 12:10:20 -04:00
------------------------fiscal periods------------------------
CREATE TABLE evt.fspr (
2018-09-20 20:16:32 -04:00
id ltree PRIMARY KEY
2018-09-19 12:10:20 -04:00
,dur tstzrange
2018-09-20 20:03:08 -04:00
);
2018-09-20 15:57:02 -04:00
COMMENT ON COLUMN evt.fspr.id IS 'fiscal period';
COMMENT ON COLUMN evt.fspr.dur IS 'duration of period as timestamp range';
CREATE INDEX id_gist ON evt.fspr USING GIST (id);
2018-09-19 12:10:20 -04:00
2018-09-20 20:16:32 -04:00
2018-09-12 14:48:59 -04:00
--------------------------relational ledger------------------------------------------
CREATE TABLE evt.gl (
id INT GENERATED ALWAYS AS IDENTITY PRIMARY KEY
2018-09-17 23:42:01 -04:00
,bprid INT REFERENCES evt.bpr (id)
2018-09-12 14:48:59 -04:00
,acct text REFERENCES evt.acct (acct)
2018-09-20 20:16:32 -04:00
,pstmp timestamptz DEFAULT CURRENT_TIMESTAMP
--populates by trigger join to evt.fspr
,tstmp timestamptz
,fspr ltree REFERENCES evt.fspr (id)
2018-09-12 14:48:59 -04:00
,amount numeric (12,2)
2018-09-17 23:42:01 -04:00
,glline INT
,bprkeys JSONB
2018-09-12 14:48:59 -04:00
);
2018-09-17 23:42:01 -04:00
COMMENT ON COLUMN evt.gl.id IS 'gl id';
COMMENT ON COLUMN evt.gl.bprid IS 'id of initial basic pecuniary record';
2018-09-12 14:48:59 -04:00
COMMENT ON COLUMN evt.gl.acct IS 'account code';
2018-09-20 20:16:32 -04:00
COMMENT ON COLUMN evt.gl.pstmp IS 'post time stamp';
COMMENT ON COLUMN evt.gl.tstmp IS 'transaction time stamp';
COMMENT ON COLUMN evt.gl.fspr IS 'fiscal period';
2018-09-12 14:48:59 -04:00
COMMENT ON COLUMN evt.gl.amount IS 'amount';
2018-09-17 23:42:01 -04:00
COMMENT ON COLUMN evt.gl.glline IS 'gl line number';
COMMENT ON COLUMN evt.gl.bprkeys IS 'extract from initial basic pecuniary record';
2018-09-12 14:48:59 -04:00
--------------------------balances----------------------------------------------------
CREATE TABLE evt.bal (
acct TEXT REFERENCES evt.acct(acct)
,perd daterange
,obal numeric(12,2)
,debits numeric(12,2)
,credits numeric(12,2)
,cbal numeric(12,2)
);
COMMENT ON COLUMN evt.bal.acct IS 'account';
COMMENT ON COLUMN evt.bal.perd IS 'period';
COMMENT ON COLUMN evt.bal.obal IS 'opening balance';
COMMENT ON COLUMN evt.bal.debits IS 'total debits';
COMMENT ON COLUMN evt.bal.credits IS 'total credits';
COMMENT ON COLUMN evt.bal.cbal IS 'closing balance';
2018-09-20 22:13:06 -04:00
---------------------------handle new logged event----------------------------------------
2018-09-18 00:11:59 -04:00
CREATE OR REPLACE FUNCTION evt.log_insert() RETURNS trigger
LANGUAGE plpgsql
AS
$func$
BEGIN
WITH
2018-09-20 22:13:06 -04:00
--full extraction
2018-09-18 00:11:59 -04:00
full_ex AS (
SELECT
ins.id
--th econtents of the gl line
,a.i gl_line
--the array position of the gl line
,a.rn gl_rownum
--array of references
,ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text] gl_ref
--each item in the reference array
,p.i ref_line
--array postition of the reference item
,p.rn ref_rownum
--follow the path
,ins.bpr#>(p.i->>0)::text[] bpr_extract
FROM
ins
--gl array hold each gl line
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr->'gl'->'lines') WITH ORDINALITY a(i, rn) ON TRUE
--for each
LEFT JOIN LATERAL JSONB_ARRAY_ELEMENTS(ins.bpr#>ARRAY['gl','jpath',(a.rn - 1)::text]) WITH ORDINALITY p(i, rn) ON TRUE
)
--select * from full_ex
2018-09-20 22:13:06 -04:00
--re-ggregate extraction to gl line level
2018-09-18 00:11:59 -04:00
,ex_gl_line AS (
SELECT
id
,gl_line->>'account' account
,(gl_line->>'amount')::numeric amount
,gl_rownum
--aggregate all the path references back to the gl line
,public.jsonb_concat(bpr_extract) bprkeys
FROM
full_ex
GROUP BY
id
,gl_line
,gl_rownum
)
--select * from ex_gl_line
,upsert_acct_mast AS (
INSERT INTO
evt.acct (acct,prop)
SELECT DISTINCT
account
,'{}'::jsonb prop
FROM
ex_gl_line
2018-09-18 23:07:10 -04:00
ON CONFLICT DO NOTHING
2018-09-18 00:11:59 -04:00
RETURNING *
)
INSERT INTO
2018-09-20 22:13:06 -04:00
evt.gl (bprid,acct, amount,tstmp , fspr, glline, bprkeys)
2018-09-18 00:11:59 -04:00
SELECT
id
,account
,amount
2018-09-20 22:13:06 -04:00
,(bprkeys->>'date')::timestamptz
,p.fspr
2018-09-18 00:11:59 -04:00
,gl_rownum
,bprkeys
FROM
2018-09-20 22:13:06 -04:00
ex_gl_line
LEFT OUTER JOIN evt.fspr p ON
p.dur @> (bprkeys->>'date')::timestamptz;
2018-09-18 00:11:59 -04:00
RETURN NULL;
END;
$func$;
CREATE TRIGGER log_insert
AFTER INSERT ON evt.bpr
REFERENCING NEW TABLE AS ins
FOR EACH STATEMENT
EXECUTE PROCEDURE evt.log_insert();
COMMIT;