/*
UPDATE sessions                 SET log_id          = NULL;
UPDATE uploaded_file_sheets     SET result_log_id   = NULL;
--
DROP TABLE log_events       PURGE;
DROP TABLE logs_blacklist   PURGE;
DROP TABLE logs             PURGE;
--
SELECT * FROM logs ORDER BY 1 DESC;
*/
CREATE TABLE logs (
    log_id              INTEGER         CONSTRAINT nn_logs_log_id       NOT NULL,   -- NUMBER GENERATED BY DEFAULT AS IDENTITY NOT NULL
    log_parent          INTEGER,
    --
    app_id              NUMBER(4)       CONSTRAINT nn_logs_app_id       NOT NULL,
    page_id             NUMBER(6),
    user_id             VARCHAR2(30),
    flag                CHAR(1)         CONSTRAINT nn_logs_flag         NOT NULL,
    --
    action_name         VARCHAR2(32),
    module_name         VARCHAR2(48),                                               -- 48 chars, DBMS_APPLICATION_INFO limit
    module_line         NUMBER(8),
    module_timer        VARCHAR2(12),   -- 00:00:00.000 - INTERVAL DAY(1) TO SECOND(3),
    arguments           VARCHAR2(2000),
    payload             VARCHAR2(4000),
    --
    session_id          INTEGER,
    created_at          TIMESTAMP       CONSTRAINT nn_logs_created_at   NOT NULL,
    --
    CONSTRAINT pk_logs
        PRIMARY KEY (log_id)
    --
    -- NO MORE CONSTRAINTS TO KEEP THIS AS FAST AS POSSIBLE
    --
)
PARTITION BY RANGE (created_at)
INTERVAL (NUMTODSINTERVAL(1, 'DAY')) (
    PARTITION P00 VALUES LESS THAN (TIMESTAMP '2021-01-01 00:00:00')
/*
    --today               VARCHAR2(10)    CONSTRAINT nn_logs_today        NOT NULL,   -- virtual column?
PARTITION BY LIST (today, app_id) AUTOMATIC (
    PARTITION "P_20210101_0"  VALUES (('2021-01-01', 0)) SEGMENT CREATION IMMEDIATE
*/
);
--
COMMENT ON TABLE  logs                  IS '[CORE] Various logs raised in application; daily partitions';
--
COMMENT ON COLUMN logs.log_id           IS 'Log ID generated from LOG_ID sequence';
COMMENT ON COLUMN logs.log_parent       IS 'Parent log record; dont use FK to avoid deadlocks';
COMMENT ON COLUMN logs.app_id           IS 'APEX Application ID';
COMMENT ON COLUMN logs.user_id          IS 'User ID';
COMMENT ON COLUMN logs.page_id          IS 'APEX Application PAGE ID';
COMMENT ON COLUMN logs.flag             IS 'Type of error listed in tree package specification; FK missing for performance reasons';
COMMENT ON COLUMN logs.action_name      IS 'Action name to distinguish position in module or use it as warning/error names';
COMMENT ON COLUMN logs.module_name      IS 'Module name (procedure or function name)';
COMMENT ON COLUMN logs.module_line      IS 'Line in the module';
COMMENT ON COLUMN logs.module_timer     IS 'Time interval indicating successful finish';
COMMENT ON COLUMN logs.arguments        IS 'Arguments passed to module';
COMMENT ON COLUMN logs.payload          IS 'Formatted call stack, error stack or query with DML error';
COMMENT ON COLUMN logs.session_id       IS 'Session ID (generated by APEX) from sessions table';
COMMENT ON COLUMN logs.created_at       IS 'Timestamp of creation';

