-- Table: back.integracao_log

-- DROP TABLE IF EXISTS back.integracao_log;

CREATE TABLE IF NOT EXISTS back.integracao_log
(
    cdintegracao_log bigint NOT NULL DEFAULT nextval('back.integracao_log_cdintegracao_log_seq'::regclass),
    cdintegracao integer NOT NULL,
    cdempresa integer NOT NULL,
    cdorigem character varying(100) COLLATE pg_catalog."default" NOT NULL,
    dtmovimento date,
    dscomplemento text COLLATE pg_catalog."default",
    cdrealizado integer,
    flstatus character varying(20) COLLATE pg_catalog."default" NOT NULL,
    dsmensagem text COLLATE pg_catalog."default",
    tpintegracao character varying(10) COLLATE pg_catalog."default" NOT NULL,
    cdusuario integer NOT NULL DEFAULT 0,
    dthsinc timestamp without time zone NOT NULL DEFAULT now(),
    CONSTRAINT integracao_log_pkey PRIMARY KEY (cdintegracao_log),
    CONSTRAINT integracao_log_cdempresa_fkey FOREIGN KEY (cdempresa)
        REFERENCES back.empresa (cdempresa) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT integracao_log_cdintegracao_fkey FOREIGN KEY (cdintegracao)
        REFERENCES back.integracao (cdintegracao) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT integracao_log_cdrealizado_fkey FOREIGN KEY (cdrealizado)
        REFERENCES back.realizado (cdrealizado) MATCH SIMPLE
        ON UPDATE NO ACTION
        ON DELETE NO ACTION,
    CONSTRAINT integracao_log_flstatus_check CHECK (flstatus::text = ANY (ARRAY['pendente'::character varying, 'sucesso'::character varying, 'ignorado'::character varying, 'erro'::character varying]::text[])),
    CONSTRAINT integracao_log_tpintegracao_check CHECK (tpintegracao::text = ANY (ARRAY['manual'::character varying, 'automatica'::character varying]::text[]))
)

TABLESPACE pg_default;

ALTER TABLE IF EXISTS back.integracao_log
    OWNER to postgres;
-- Index: idx_intlog_cdorigem

-- DROP INDEX IF EXISTS back.idx_intlog_cdorigem;

CREATE INDEX IF NOT EXISTS idx_intlog_cdorigem
    ON back.integracao_log USING btree
    (cdorigem COLLATE pg_catalog."default" ASC NULLS LAST)
    TABLESPACE pg_default;
-- Index: idx_intlog_dthsinc

-- DROP INDEX IF EXISTS back.idx_intlog_dthsinc;

CREATE INDEX IF NOT EXISTS idx_intlog_dthsinc
    ON back.integracao_log USING btree
    (dthsinc DESC NULLS FIRST)
    TABLESPACE pg_default;
-- Index: idx_intlog_integracao

-- DROP INDEX IF EXISTS back.idx_intlog_integracao;

CREATE INDEX IF NOT EXISTS idx_intlog_integracao
    ON back.integracao_log USING btree
    (cdintegracao ASC NULLS LAST, cdempresa ASC NULLS LAST)
    TABLESPACE pg_default;
-- Index: idx_intlog_status

-- DROP INDEX IF EXISTS back.idx_intlog_status;

CREATE INDEX IF NOT EXISTS idx_intlog_status
    ON back.integracao_log USING btree
    (flstatus COLLATE pg_catalog."default" ASC NULLS LAST)
    TABLESPACE pg_default;