CREATE SEQUENCE trades_id_seq
  INCREMENT 1
  MINVALUE 1
  MAXVALUE 9223372036854775807
  START 1
  CACHE 1;

CREATE TABLE exchanges
(

  id smallint NOT NULL,
  symbol character varying(4) NOT NULL,

  CONSTRAINT exchanges_pkey PRIMARY KEY (id)
);

CREATE TABLE currencies
(
  id smallint NOT NULL,
  symbol character varying(6) NOT NULL,

  CONSTRAINT currencies_pkey PRIMARY KEY (id)
);

CREATE TABLE trades
(
  id integer NOT NULL DEFAULT nextval('trades_id_seq'::regclass),
  "time" timestamp with time zone NOT NULL,
  exch smallint NOT NULL,
  base smallint NOT NULL,
  quote smallint NOT NULL,
  amount double precision NOT NULL,
  price double precision NOT NULL,
  side smallint NULL,                           -- side has no fk ... bid=1, ask=2
  server_time timestamp with time zone NULL,

  CONSTRAINT trades_pkey PRIMARY KEY (id),

  CONSTRAINT exch_fk FOREIGN KEY (exch)
      REFERENCES exchanges (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,

  CONSTRAINT base_fk FOREIGN KEY (base)
      REFERENCES currencies (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED,

  CONSTRAINT quote_fk FOREIGN KEY (quote)
      REFERENCES currencies (id) MATCH SIMPLE
      ON UPDATE NO ACTION ON DELETE NO ACTION DEFERRABLE INITIALLY DEFERRED
);

CREATE INDEX trades_time_abcdefg
  ON trades
  USING btree
  ("time");

CREATE INDEX trades_base_quote_f6b2eeda
  ON trades
  USING btree
  (base, quote);

CREATE INDEX trades_exchange_5d5c6971
  ON trades
  USING btree
  (exch);

-- fill in exchanges/currencies

INSERT INTO exchanges (id, symbol) VALUES (1, 'plnx');
INSERT INTO exchanges (id, symbol) VALUES (2, 'krkn');
INSERT INTO exchanges (id, symbol) VALUES (3, 'gdax');
INSERT INTO exchanges (id, symbol) VALUES (4, 'exmo');
INSERT INTO exchanges (id, symbol) VALUES (5, 'bits');
INSERT INTO exchanges (id, symbol) VALUES (6, 'bmex');
INSERT INTO exchanges (id, symbol) VALUES (7, 'btfx');
INSERT INTO exchanges (id, symbol) VALUES (8, 'bnce');
INSERT INTO exchanges (id, symbol) VALUES (9, 'okex');
INSERT INTO exchanges (id, symbol) VALUES (10, 'drbt');
INSERT INTO currencies (id, symbol) VALUES (1, 'btc');
INSERT INTO currencies (id, symbol) VALUES (2, 'eth');
INSERT INTO currencies (id, symbol) VALUES (3, 'xmr');
INSERT INTO currencies (id, symbol) VALUES (4, 'usdt');
INSERT INTO currencies (id, symbol) VALUES (5, 'ltc');
INSERT INTO currencies (id, symbol) VALUES (6, 'dash');
INSERT INTO currencies (id, symbol) VALUES (7, 'nvc');
INSERT INTO currencies (id, symbol) VALUES (8, 'ppc');
INSERT INTO currencies (id, symbol) VALUES (9, 'zec');
INSERT INTO currencies (id, symbol) VALUES (10, 'xrp');
INSERT INTO currencies (id, symbol) VALUES (11, 'gnt');
INSERT INTO currencies (id, symbol) VALUES (12, 'steem');
INSERT INTO currencies (id, symbol) VALUES (13, 'rep');
INSERT INTO currencies (id, symbol) VALUES (14, 'gno');
INSERT INTO currencies (id, symbol) VALUES (15, 'etc');
INSERT INTO currencies (id, symbol) VALUES (16, 'icn');
INSERT INTO currencies (id, symbol) VALUES (17, 'xlm');
INSERT INTO currencies (id, symbol) VALUES (18, 'mln');
INSERT INTO currencies (id, symbol) VALUES (19, 'bcn');
INSERT INTO currencies (id, symbol) VALUES (20, 'bch');
INSERT INTO currencies (id, symbol) VALUES (21, 'doge');
INSERT INTO currencies (id, symbol) VALUES (22, 'eos');
INSERT INTO currencies (id, symbol) VALUES (23, 'nxt');
INSERT INTO currencies (id, symbol) VALUES (24, 'sc');
INSERT INTO currencies (id, symbol) VALUES (25, 'zrx');
INSERT INTO currencies (id, symbol) VALUES (26, 'bat');
INSERT INTO currencies (id, symbol) VALUES (27, 'ada');
INSERT INTO currencies (id, symbol) VALUES (28, 'usdc');
INSERT INTO currencies (id, symbol) VALUES (29, 'dai');
INSERT INTO currencies (id, symbol) VALUES (30, 'mkr');
INSERT INTO currencies (id, symbol) VALUES (31, 'loom');
INSERT INTO currencies (id, symbol) VALUES (32, 'cvc');
INSERT INTO currencies (id, symbol) VALUES (33, 'mana');
INSERT INTO currencies (id, symbol) VALUES (34, 'dnt');
INSERT INTO currencies (id, symbol) VALUES (35, 'zil');
INSERT INTO currencies (id, symbol) VALUES (36, 'link');
INSERT INTO currencies (id, symbol) VALUES (37, 'algo');
INSERT INTO currencies (id, symbol) VALUES (38, 'xtz');
INSERT INTO currencies (id, symbol) VALUES (39, 'oxt');
INSERT INTO currencies (id, symbol) VALUES (40, 'atom');
INSERT INTO currencies (id, symbol) VALUES (100, 'usd');
INSERT INTO currencies (id, symbol) VALUES (101, 'eur');
INSERT INTO currencies (id, symbol) VALUES (102, 'rur');
INSERT INTO currencies (id, symbol) VALUES (103, 'jpy');
INSERT INTO currencies (id, symbol) VALUES (104, 'gbp');
INSERT INTO currencies (id, symbol) VALUES (105, 'chf');
INSERT INTO currencies (id, symbol) VALUES (106, 'cad');
INSERT INTO currencies (id, symbol) VALUES (107, 'aud');
INSERT INTO currencies (id, symbol) VALUES (108, 'zar');
INSERT INTO currencies (id, symbol) VALUES (109, 'mxn');
