CREATE TABLE IF NOT EXISTS market_klines (
  source TEXT NOT NULL,
  symbol TEXT NOT NULL,
  interval TEXT NOT NULL,

  open_time BIGINT NOT NULL,
  close_time BIGINT NOT NULL,

  open NUMERIC(36, 18) NOT NULL,
  high NUMERIC(36, 18) NOT NULL,
  low NUMERIC(36, 18) NOT NULL,
  close NUMERIC(36, 18) NOT NULL,

  volume NUMERIC(36, 18) NOT NULL,
  quote_volume NUMERIC(36, 18) NOT NULL,

  trade_count BIGINT NOT NULL,

  taker_buy_base_volume NUMERIC(36, 18) NOT NULL,
  taker_buy_quote_volume NUMERIC(36, 18) NOT NULL,

  created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),

  PRIMARY KEY (source, symbol, interval, open_time)
);

CREATE INDEX IF NOT EXISTS idx_market_klines_symbol_interval_time
ON market_klines(symbol, interval, open_time DESC);

SELECT create_hypertable('market_klines', 'open_time',
  chunk_time_interval => 604800000,
  if_not_exists => TRUE,
  migrate_data => TRUE);
