BEGIN TRANSACTION;

create table if not exists "flow_log" (
    "id" bigserial not null primary key ,
    "filename" varchar(255),
    "flow_id" bigint,
    "flowname" varchar(255),
    "origin_id" bigint,
    "origin_hostid" varchar(255),
    "destination_hostid" varchar(255),
    "starttime" timestamp with time zone,
    "endtime" timestamp with time zone,
    "duration" bigint,
    "size" bigint,
    "speed" real);

create table if not exists "flow_log_dest" (
    "flow_id" bigint not null,
    "instance_id" bigint not null, primary key ("flow_id", "instance_id"));

create table if not exists "users"(
    "id" bigserial not null primary key,
    "firstname" varchar(255),
    "lastname" varchar(255),
    "password" varchar(255),
    "login" varchar(255) unique);

create table if not exists "trace_entry"(
    "id" bigserial not null primary key,
    "action" varchar(255) not null,
    "target" varchar(255) not null,
    "object_id" bigint not null,
    "user_id" bigint not null,
    "user_login" varchar(255),
    "date" timestamp with time zone);

ALTER TABLE trace_entry ALTER COLUMN action SET DATA TYPE varchar(255);
ALTER TABLE trace_entry ALTER COLUMN target SET DATA TYPE varchar(255);

create table if not exists "transfer_log" (
    "id" bigserial not null primary key ,
    "flow_log_id" varchar(255),
    "flow_id" bigint,
    "flowname" varchar(255),
    "filename" varchar(255),
    "rulename" varchar(255),
    "origin_id" bigint,
    "origin_hostid" varchar(255),
    "destination_id" bigint,
    "destination_hostid" varchar(255),
    "starttime" timestamp with time zone,
    "endtime" timestamp with time zone,
    "duration" bigint,
    "size" bigint,
    "speed" real,
    "status" varchar(255),
    "statusinfo" varchar(255),
    "origin_transfer_id" bigint,
    "destination_transfer_id" bigint,
    "updated" boolean);

create table if not exists "sessions" (
    "session_id" varchar(255) not null primary key,
    "expiration_date" timestamp with time zone,
    "user_id" bigint);


DO $$
BEGIN
    BEGIN
        alter table "instances" add column "last_log_sync" timestamp with time zone;
    EXCEPTION
        WHEN duplicate_column THEN RAISE NOTICE 'Info : Column not added, already exists';
    END;
END;
$$;


DO $$
BEGIN
    BEGIN
        alter table "flow_log" add column "status" varchar(255);
    EXCEPTION
        WHEN duplicate_column THEN RAISE NOTICE 'Info : Column not added, already exists';
    END;
END;
$$;

COMMIT;