· 8 years ago · Aug 30, 2018, 07:52 PM
1import * as pg from "pg"
2
3const pool = new pg.Pool({
4 database: process.env.GF_DATABASE_NAME,
5 user: process.env.GF_DATABASE_USER,
6 password: process.env.GF_DATABASE_PASSWORD
7})
8
9async function prepare() {
10 const client = await pool.connect()
11 const schema = process.env.GF_DATABASE_PRIVATE_SCHEMA
12
13 await client.query(` CREATE SCHEMA IF NOT EXISTS ${schema}`)
14 await client.query(`SET search_path TO ${schema}`)
15 await client.query(`
16
17 -- DROP TABLE IF EXISTS noblockchain_cryptomaterial;
18 CREATE TABLE IF NOT EXISTS noblockchain_cryptomaterial (
19 pubkey BYTEA NOT NULL,
20 privkey BYTEA NOT null,
21 UNIQUE (pubkey)
22 );
23
24 DROP TABLE IF EXISTS investor CASCADE;
25 CREATE TABLE IF NOT EXISTS investor (
26 rowid SERIAL PRIMARY KEY,
27 tuid text not null,
28 email text not null,
29 password text not null,
30 username text not null,
31 aux_data json not null,
32 UNIQUE (tuid)
33 );
34
35 DROP TABLE IF EXISTS portfolio;
36 CREATE TABLE IF NOT EXISTS portfolio (
37 rowid SERIAL PRIMARY KEY,
38 tuid text not null,
39 name text not null,
40 owner text references investor(tuid) ON DELETE CASCADE,
41 aux_data text not null
42 );
43
44 DROP TABLE IF EXISTS portfolio_bond;
45 CREATE TABLE IF NOT EXISTS portfolio_bond (
46 portfolio_tuid text not null,
47 bond_tuid text not null,
48 amount double precision not null,
49 aux_data jsonb not null default '{}'::jsonb,
50 PRIMARY KEY (bond_tuid, portfolio_tuid)
51 );
52 `)
53 console.log(`done`)
54 process.exit()
55}
56
57prepare()