-- ipfon_sms_deliv_log
DROP TABLE IF EXISTS "ipfon_sms_deliv_log";
DROP SEQUENCE IF EXISTS "ipfon_sms_deliv_log_id_seq";

CREATE SEQUENCE "ipfon_sms_deliv_log_id_seq";
CREATE TABLE "public"."ipfon_sms_deliv_log" (
    "id" integer DEFAULT nextval('ipfon_sms_deliv_log_id_seq') NOT NULL,
    "msg_part_id" integer NOT NULL,
    "partno" smallint NOT NULL,
    "src_account_id" integer NOT NULL,
    "src_account_type" character varying NOT NULL,
    "src_proto" character varying NOT NULL,
    "dst_icpoint" integer NOT NULL,
    "status" character varying NOT NULL,
    "errcode" smallint NOT NULL,
    "time_delivered" timestamp NOT NULL,
    "time_sent" timestamp NOT NULL,
    "time_processed" timestamptz
);

COMMENT ON COLUMN "public"."ipfon_sms_deliv_log"."msg_part_id" IS 'ipfon_sms_out_log.id';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_log"."partno" IS 'Poradove cislo spravy - 0 je hlavna sprava (master), cislo 1 az N je poradove cislo casti v pripade delenej spravy';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_log"."src_account_type" IS 'person, sip_account, sms_account';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_log"."src_proto" IS 'smpp, http, stmp';


-- ipfon_sms_deliv_queue
DROP TABLE IF EXISTS "ipfon_sms_deliv_queue";
DROP SEQUENCE IF EXISTS "ipfon_sms_deliv_queue_id_seq";

CREATE SEQUENCE "ipfon_sms_deliv_queue_id_seq";
CREATE TABLE "public"."ipfon_sms_deliv_queue" (
    "id" integer DEFAULT nextval('ipfon_sms_deliv_queue_id_seq') NOT NULL,
    "msg_part_id" integer NOT NULL,
    "partno" smallint NOT NULL,
    "src_account_id" integer NOT NULL,
    "src_account_type" character varying NOT NULL,
    "src_proto" character varying NOT NULL,
    "dst_icpoint" integer NOT NULL,
    "status" character varying NOT NULL,
    "errcode" smallint NOT NULL,
    "time_delivered" timestamp NOT NULL,
    "time_processed" timestamptz,
    "retries" smallint NOT NULL DEFAULT '0'
);

COMMENT ON COLUMN "public"."ipfon_sms_deliv_queue"."msg_part_id" IS 'ipfon_sms_out_log.id';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_queue"."partno" IS 'Poradove cislo spravy - 0 je hlavna sprava (master), cislo 1 az N je poradove cislo casti v pripade delenej spravy';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_queue"."src_account_type" IS 'person, sip_account, sms_account';
COMMENT ON COLUMN "public"."ipfon_sms_deliv_queue"."src_proto" IS 'smpp, http, stmp';


-- ipfon_sms_out_log
ALTER TABLE "ipfon_sms_out_log" RENAME COLUMN "id" TO "msg_id";

CREATE SEQUENCE "ipfon_sms_out_log_id_seq";
CREATE SEQUENCE "ipfon_sms_local_id_seq";
CREATE SEQUENCE "ipfon_sms_msg_id_seq";

ALTER TABLE "ipfon_sms_out_log"
ADD "refno" smallint NULL,
ADD "src_account_id" integer NULL,
ADD "src_account_type" character varying NULL,
ADD "src_proto" character varying NULL,
ADD "all_parts_exists" boolean NULL,
ADD "local_id" smallint NULL,
ADD "id" integer DEFAULT nextval('ipfon_sms_out_log_id_seq') NOT NULL;

CREATE INDEX "ipfon_sms_out_log_id_idx" ON "ipfon_sms_out_log" ("id");
CREATE INDEX "ipfon_sms_out_log_local_id_idx" ON "ipfon_sms_out_log" ("local_id");
CREATE INDEX "ipfon_sms_out_log_msg_id_idx" ON "ipfon_sms_out_log" ("msg_id");
CREATE INDEX "ipfon_sms_out_log_remote_id_idx" ON "ipfon_sms_out_log" ("remote_id");
CREATE INDEX "ipfon_sms_out_log_src_account_id" ON "ipfon_sms_out_log" ("src_account_id");
CREATE INDEX "ipfon_sms_out_log_src_account_type" ON "ipfon_sms_out_log" ("src_account_type");
CREATE INDEX "ipfon_sms_out_log_src_proto" ON "ipfon_sms_out_log" ("src_proto");


-- ipfon_sms_out_queue
ALTER TABLE "ipfon_sms_out_queue" RENAME COLUMN "id" TO "msg_id";
ALTER TABLE "ipfon_sms_out_queue" ALTER COLUMN "msg_id" DROP DEFAULT;

ALTER TABLE ipfon_sms_out_queue
ADD "parts" smallint DEFAULT 1,
ADD "partno" smallint DEFAULT 0,
ADD "refno" smallint NULL,
ADD "src_account_id" integer NULL,
ADD "src_account_type" character varying NULL,
ADD "src_proto" character varying NULL,
ADD "id" integer DEFAULT nextval('ipfon_sms_out_queue_id_seq'),
ADD "local_id" smallint NULL;


-- ipfon_sms_test_log
ALTER TABLE ipfon_sms_test_log
ADD "src" character varying,
ADD "dst" character varying,
ADD "dst_icpoint" integer;


-- ipfon_sms_account
ALTER TABLE ipfon_sms_account
ADD max_connections smallint DEFAULT 0
