
CREATE TABLE IF NOT EXISTS ApplicationFrameworkSettings (
    MaxExecTime                     INT DEFAULT 300,
    TaskExecutionEnabled            INT DEFAULT 1,
    MaxTasksRunning                 INT DEFAULT 2,
    LastTaskRunAt                   DATETIME DEFAULT NULL,
    TemplateLocationCache           MEDIUMBLOB DEFAULT NULL,
    TemplateLocationCacheExpiration DATETIME DEFAULT NULL,
    TemplateLocationCacheInterval   INT DEFAULT 15,
    ObjectLocationCache             MEDIUMBLOB DEFAULT NULL,
    ObjectLocationCacheExpiration   DATETIME DEFAULT NULL,
    ObjectLocationCacheInterval     INT DEFAULT 60,
    LoggingLevel                    INT DEFAULT 4,
    BasePath                        TEXT DEFAULT NULL,
    BasePathCheck                   TEXT DEFAULT NULL,
    LogSlowPageLoads                INT DEFAULT 0,
    SlowPageLoadThreshold           INT DEFAULT 10,
    LogHighMemoryUsage              INT DEFAULT 0,
    HighMemoryUsageThreshold        INT DEFAULT 90,
    PageCacheEnabled                INT DEFAULT 0,
    PageCacheExpirationPeriod       INT DEFAULT 600,
    UrlFingerprintingEnabled        INT DEFAULT 1,
    ScssSupportEnabled              INT DEFAULT 1,
    GenerateCompactCss              INT DEFAULT 1,
    UseMinimizedJavascript          INT DEFAULT 1,
    JavascriptMinimizationEnabled   INT DEFAULT 1
);

CREATE TABLE IF NOT EXISTS TaskQueue (
    TaskId      INT NOT NULL AUTO_INCREMENT,
    Callback    MEDIUMTEXT DEFAULT NULL,
    Parameters  MEDIUMTEXT DEFAULT NULL,
    Priority    INT DEFAULT 3,
    Description TEXT DEFAULT NULL,
    INDEX       (TaskId),
    INDEX       (Callback(64), Parameters(256))
);

-- (RunningTasks table must match TaskQueue table except for StartedAt and CrashInfo)
CREATE TABLE IF NOT EXISTS RunningTasks (
    TaskId      INT NOT NULL,
    Callback    MEDIUMTEXT DEFAULT NULL,
    Parameters  MEDIUMTEXT DEFAULT NULL,
    Priority    INT DEFAULT 3,
    Description TEXT DEFAULT NULL,
    StartedAt   TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    CrashInfo   MEDIUMBLOB DEFAULT NULL,
    INDEX       (TaskId),
    INDEX       (Callback(64), Parameters(256))
);

CREATE TABLE IF NOT EXISTS PeriodicEvents (
    Signature   TEXT DEFAULT NULL,
    LastRunAt   DATETIME DEFAULT NULL
);

CREATE TABLE IF NOT EXISTS AF_CachedPages (
    CacheId     INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Fingerprint TEXT DEFAULT NULL,
    PageContent MEDIUMBLOB DEFAULT NULL,
    CachedAt    TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX       Index_F (Fingerprint(48)),
    INDEX       Index_A (CachedAt)
);

CREATE TABLE IF NOT EXISTS AF_CachedPageTags (
    TagId       INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
    Tag         TEXT DEFAULT NULL,
    INDEX       Index_T (Tag(16)),
    INDEX       Index_I (TagId)
);

CREATE TABLE IF NOT EXISTS AF_CachedPageTagInts (
    TagId       INT NOT NULL,
    CacheId     INT NOT NULL,
    INDEX       Index_T (TagId),
    INDEX       Index_CT (CacheId, TagId)
);

CREATE TABLE IF NOT EXISTS AF_Locks (
    LockName    TEXT DEFAULT NULL,
    ObtainedAt  TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    INDEX       Index_N (LockName(16)),
    INDEX       Index_A (ObtainedAt)
);

