drop table if exists trades;
create table trades
(
    trade_id bigserial,
    account_id text not null,
    bought_at timestamp default current_timestamp,
    figi text not null,
    ticker text not null,
    price decimal not null,
    count decimal not null,
    count_lots decimal not null,
    archive_status int not null default 0,
    direction int not null default 1,
    position_type int not null default 1,
    asset_type int not null default 1,
    primary key (trade_id,archive_status)
) partition by list (archive_status);

create table assets_active partition of trades for values in (0);
create table assets_archive partition of trades for values in (1);

CREATE INDEX assets_account_id_index ON trades USING btree(account_id);
CREATE INDEX assets_figi_index ON trades USING btree(figi);
drop table if exists price_changes;
create table price_changes
(
    id bigserial,
    time timestamp default current_timestamp,
    figi text not null,
    ticker text not null,
    value decimal not null,
    primary key (id)
);

CREATE INDEX price_changes_figi_index ON price_changes USING btree(figi, time);
drop table if exists declisions;
create table declisions
(
    id bigserial,
    time timestamp default current_timestamp,
    figi text not null,
    price decimal not null,
    ticker text not null,
    account_id text not null,
    action int not null default 0,
    primary key (id)
);

CREATE INDEX declisions_index ON declisions USING btree(figi, time);
