--
-- File generated with SQLiteStudio v3.1.1 on Tue Mar 13 19:16:57 2018
--
-- Text encoding used: UTF-8
--
PRAGMA foreign_keys = off;
BEGIN TRANSACTION;

-- Table: Activation
DROP TABLE IF EXISTS Activation;

CREATE TABLE Activation (
    activationId  VARCHAR (40) PRIMARY KEY,
    appId         VARCHAR (20) NOT NULL,
    licenseNum    VARCHAR (24) NOT NULL,
    activatedOn   DATETIME     NOT NULL,
    deactivatedOn DATETIME,
    FOREIGN KEY (
        appId,
        licenseNum
    )
    REFERENCES License (appId,
    licenseNum) 
);


-- Table: ActivationParams
DROP TABLE IF EXISTS ActivationParams;

CREATE TABLE ActivationParams (
    activationId VARCHAR (40) NOT NULL,
    paramId      VARCHAR (20) NOT NULL,
    paramValue   VARCHAR      NOT NULL,
    flag         INTEGER      NOT NULL
                              DEFAULT (0),
    PRIMARY KEY (
        activationId,
        paramId
    ),
    FOREIGN KEY (
        activationId
    )
    REFERENCES Activation (activationId) 
);


-- Table: CheckLog
DROP TABLE IF EXISTS CheckLog;

CREATE TABLE CheckLog (
    checkLogId   VARCHAR (40) PRIMARY KEY
                              NOT NULL,
    activationId VARCHAR (40) NOT NULL,
    checkedOn    DATETIME     NOT NULL,
    FOREIGN KEY (
        activationId
    )
    REFERENCES Activation (activationId) 
);

-- Table: CheckLogParams
DROP TABLE IF EXISTS CheckLogParams;

CREATE TABLE CheckLogParams (
    checkLogId VARCHAR (40) REFERENCES CheckLog (checkLogId) 
                            NOT NULL,
    paramId    VARCHAR (20) NOT NULL,
    paramValue VARCHAR      NOT NULL,
    PRIMARY KEY (
        checkLogId,
        paramId
    )
);


-- Table: CheckLogVersion
DROP TABLE IF EXISTS CheckLogVersion;

CREATE TABLE CheckLogVersion (
    checkLogId VARCHAR (40) NOT NULL
                            REFERENCES CheckLog (checkLogId),
    moduleId   VARCHAR (40) NOT NULL,
    version    INTEGER      NOT NULL,
    PRIMARY KEY (
        checkLogId,
        moduleId
    )
);


-- Table: License
DROP TABLE IF EXISTS License;

CREATE TABLE License (
    appId      VARCHAR (20) NOT NULL,
    licenseNum VARCHAR (24) NOT NULL,
    customerId VARCHAR (20) NOT NULL,
    PRIMARY KEY (
        appId,
        licenseNum
    )
);

INSERT INTO License (
                        appId,
                        licenseNum,
                        customerId
                    )
                    VALUES (
                        'coc',
                        'T3HZIFATHLN52I57HAGLV24R',
                        '111'
                    );

INSERT INTO License (
                        appId,
                        licenseNum,
                        customerId
                    )
                    VALUES (
                        'coc',
                        'JK33BTBSBKSKV63YEVLMQMBZ',
                        '111'
                    );


-- Table: LicensedModule
DROP TABLE IF EXISTS LicensedModule;

CREATE TABLE LicensedModule (
    appId      VARCHAR (20) NOT NULL,
    licenseNum VARCHAR (24) NOT NULL,
    moduleId   VARCHAR (40) NOT NULL,
    PRIMARY KEY (
        appId,
        licenseNum,
        moduleId
    ),
    FOREIGN KEY (
        appId,
        licenseNum
    )
    REFERENCES License (appId,
    licenseNum) 
);

INSERT INTO LicensedModule (
                               appId,
                               licenseNum,
                               moduleId
                           )
                           VALUES (
                               'coc',
                               'JK33BTBSBKSKV63YEVLMQMBZ',
                               'coc-engine'
                           );

INSERT INTO LicensedModule (
                               appId,
                               licenseNum,
                               moduleId
                           )
                           VALUES (
                               'coc',
                               'JK33BTBSBKSKV63YEVLMQMBZ',
                               'coc-testdata'
                           );

INSERT INTO LicensedModule (
                               appId,
                               licenseNum,
                               moduleId
                           )
                           VALUES (
                               'coc',
                               'T3HZIFATHLN52I57HAGLV24R',
                               'coc-testdata'
                           );

INSERT INTO LicensedModule (
                               appId,
                               licenseNum,
                               moduleId
                           )
                           VALUES (
                               'coc',
                               'T3HZIFATHLN52I57HAGLV24R',
                               'coc-engine'
                           );


-- Table: ModuleUpdate
DROP TABLE IF EXISTS ModuleUpdate;

CREATE TABLE ModuleUpdate (
    moduleId  VARCHAR (40) NOT NULL,
    version   INTEGER      NOT NULL,
    checksum  VARCHAR (32) NOT NULL,
    updateUri VARCHAR      NOT NULL,
    instPath  VARCHAR      NOT NULL,
    flag      INTEGER      NOT NULL
                           DEFAULT (0),
    PRIMARY KEY (
        moduleId,
        version
    )
);

INSERT INTO ModuleUpdate (
                             moduleId,
                             version,
                             checksum,
                             updateUri,
                             instPath,
                             flag
                         )
                         VALUES (
                             'coc-testdata',
                             2,
                             'a515353daae35dc1b3e9e06e52b95a53690984cc3172bb4e6b44c6b516afa040',
                             'http://localhost:3000/static/testsite-v2-incremental.zip',
                             'data',
                             1
                         );

INSERT INTO ModuleUpdate (
                             moduleId,
                             version,
                             checksum,
                             updateUri,
                             instPath,
                             flag
                         )
                         VALUES (
                             'coc-testdata',
                             1,
                             '6c878854d349752eceb0d52658e8838c2ae3cca53962c942a276e8944da25731',
                             'http://localhost:3000/static/testsite-v1.zip',
                             'data',
                             0
                         );


-- Table: PreactivationParams
DROP TABLE IF EXISTS PreactivationParams;

CREATE TABLE PreactivationParams (
    appId      VARCHAR (20)  NOT NULL,
    licenseNum VARCHAR (24)  NOT NULL,
    paramId    VARCHAR (20)  NOT NULL,
    paramValue VARCHAR (20)  NOT NULL,
    paramOrig  VARCHAR (255),
    PRIMARY KEY (
        appId,
        licenseNum,
        paramId
    ),
    FOREIGN KEY (
        appId,
        licenseNum
    )
    REFERENCES License (appId,
    licenseNum) 
);

INSERT INTO PreactivationParams (
                                    appId,
                                    licenseNum,
                                    paramId,
                                    paramValue,
                                    paramOrig
                                )
                                VALUES (
                                    'coc',
                                    'T3HZIFATHLN52I57HAGLV24R',
                                    'biosSerialNum',
                                    '8690a8fb436070a9',
                                    'R80CW80'
                                );


-- View: ActiveActivationView
DROP VIEW IF EXISTS ActiveActivationView;
CREATE VIEW ActiveActivationView AS
    SELECT *
      FROM Activation
     WHERE deactivatedOn IS NULL;


-- View: LastFullVersionView
DROP VIEW IF EXISTS LastFullVersionView;
CREATE VIEW LastFullVersionView AS
    SELECT moduleId,
           MAX(version) AS lastFullVersion
      FROM ModuleUpdate
     WHERE flag <> 1
     GROUP BY moduleId;


COMMIT TRANSACTION;
PRAGMA foreign_keys = on;
