From e055519d4ca6b22c96dc6bb92f0660fb83590884 Mon Sep 17 00:00:00 2001 From: Dileesha Rajapakse Date: Wed, 11 May 2016 11:02:43 +0530 Subject: [PATCH 1/2] Fixed errors in MSSQL DB script --- .../main/resources/dbscripts/cdm/mssql.sql | 161 ++++++++++-------- 1 file changed, 93 insertions(+), 68 deletions(-) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index ffbeff63e0e..c4bd77fa100 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -2,10 +2,21 @@ CREATE TABLE DM_DEVICE_TYPE ( ID INTEGER IDENTITY NOT NULL, NAME VARCHAR(300) DEFAULT NULL, PROVIDER_TENANT_ID INTEGER DEFAULT 0, - SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE, + SHARED_WITH_ALL_TENANTS INTEGER NOT NULL DEFAULT 0, PRIMARY KEY (ID) ); +CREATE TABLE DM_GROUP ( + ID INTEGER IDENTITY NOT NULL, + GROUP_NAME VARCHAR(100) DEFAULT NULL, + DESCRIPTION VARCHAR(max) DEFAULT NULL, + DATE_OF_CREATE BIGINT DEFAULT NULL, + DATE_OF_LAST_UPDATE BIGINT DEFAULT NULL, + OWNER VARCHAR(45) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); + CREATE TABLE DM_DEVICE_CERTIFICATE ( ID INTEGER IDENTITY NOT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, @@ -18,7 +29,7 @@ CREATE TABLE DM_DEVICE ( ID INTEGER identity NOT NULL, DESCRIPTION VARCHAR(max) DEFAULT NULL, NAME VARCHAR(100) DEFAULT NULL, - DEVICE_TYPE_ID INT DEFAULT NULL, + DEVICE_TYPE_ID INTEGER DEFAULT NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), @@ -26,6 +37,18 @@ CREATE TABLE DM_DEVICE ( REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +CREATE TABLE DM_DEVICE_GROUP_MAP ( + ID INTEGER IDENTITY NOT NULL, + DEVICE_ID INTEGER DEFAULT NULL, + GROUP_ID INTEGER DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_DEVICE2 FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) + REFERENCES DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION +); + CREATE TABLE DM_OPERATION ( ID INTEGER IDENTITY NOT NULL, TYPE VARCHAR(50) NOT NULL, @@ -77,7 +100,7 @@ CREATE TABLE DM_ENROLMENT ( STATUS VARCHAR(50) NULL, DATE_OF_ENROLMENT DATETIME2(0) DEFAULT NULL, DATE_OF_LAST_UPDATE DATETIME2(0) DEFAULT NULL, - TENANT_ID INT NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION @@ -113,10 +136,10 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( CREATE TABLE DM_PROFILE ( - ID INT NOT NULL IDENTITY , + ID INTEGER NOT NULL IDENTITY , PROFILE_NAME VARCHAR(45) NOT NULL , - TENANT_ID INT NOT NULL , - DEVICE_TYPE_ID INT NOT NULL , + TENANT_ID INTEGER NOT NULL , + DEVICE_TYPE_ID INTEGER NOT NULL , CREATED_TIME DATETIME2(0) NOT NULL , UPDATED_TIME DATETIME2(0) NOT NULL , PRIMARY KEY (ID) , @@ -132,16 +155,16 @@ CREATE TABLE DM_PROFILE ( CREATE TABLE DM_POLICY ( - ID INT NOT NULL IDENTITY , + ID INTEGER NOT NULL IDENTITY , NAME VARCHAR(45) DEFAULT NULL , DESCRIPTION VARCHAR(1000) NULL, - TENANT_ID INT NOT NULL , - PROFILE_ID INT NOT NULL , + TENANT_ID INTEGER NOT NULL , + PROFILE_ID INTEGER NOT NULL , OWNERSHIP_TYPE VARCHAR(45) NULL, COMPLIANCE VARCHAR(100) NULL, - PRIORITY INT NOT NULL, - ACTIVE INT NOT NULL, - UPDATED INT NULL, + PRIORITY INTEGER NOT NULL, + ACTIVE INTEGER NOT NULL, + UPDATED INTEGER NULL, PRIMARY KEY (ID) , CONSTRAINT FK_DM_PROFILE_DM_POLICY FOREIGN KEY (PROFILE_ID ) @@ -154,11 +177,11 @@ CREATE TABLE DM_POLICY ( CREATE TABLE DM_DEVICE_POLICY ( - ID INT NOT NULL IDENTITY , - DEVICE_ID INT NOT NULL , - ENROLMENT_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY , + DEVICE_ID INTEGER NOT NULL , + ENROLMENT_ID INTEGER NOT NULL, DEVICE VARBINARY(max) NOT NULL, - POLICY_ID INT NOT NULL , + POLICY_ID INTEGER NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_POLICY_DEVICE_POLICY FOREIGN KEY (POLICY_ID ) @@ -176,9 +199,9 @@ CREATE TABLE DM_DEVICE_POLICY ( CREATE TABLE DM_DEVICE_TYPE_POLICY ( - ID INT NOT NULL , - DEVICE_TYPE_ID INT NOT NULL , - POLICY_ID INT NOT NULL , + ID INTEGER NOT NULL , + DEVICE_TYPE_ID INTEGER NOT NULL , + POLICY_ID INTEGER NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_DEVICE_TYPE_POLICY FOREIGN KEY (POLICY_ID ) @@ -197,11 +220,11 @@ CREATE TABLE DM_DEVICE_TYPE_POLICY ( CREATE TABLE DM_PROFILE_FEATURES ( - ID INT NOT NULL IDENTITY, - PROFILE_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + PROFILE_ID INTEGER NOT NULL, FEATURE_CODE VARCHAR(100) NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, - TENANT_ID INT NOT NULL , + DEVICE_TYPE_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL , CONTENT VARBINARY(max) NULL DEFAULT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES @@ -215,9 +238,9 @@ CREATE TABLE DM_PROFILE_FEATURES ( CREATE TABLE DM_ROLE_POLICY ( - ID INT NOT NULL IDENTITY , + ID INTEGER NOT NULL IDENTITY , ROLE_NAME VARCHAR(45) NOT NULL , - POLICY_ID INT NOT NULL , + POLICY_ID INTEGER NOT NULL , PRIMARY KEY (ID) , CONSTRAINT FK_ROLE_POLICY_POLICY FOREIGN KEY (POLICY_ID ) @@ -230,8 +253,8 @@ CREATE TABLE DM_ROLE_POLICY ( CREATE TABLE DM_USER_POLICY ( - ID INT NOT NULL IDENTITY , - POLICY_ID INT NOT NULL , + ID INTEGER NOT NULL IDENTITY , + POLICY_ID INTEGER NOT NULL , USERNAME VARCHAR(45) NOT NULL , PRIMARY KEY (ID) , CONSTRAINT DM_POLICY_USER_POLICY @@ -243,12 +266,12 @@ CREATE TABLE DM_USER_POLICY ( CREATE TABLE DM_DEVICE_POLICY_APPLIED ( - ID INT NOT NULL IDENTITY , - DEVICE_ID INT NOT NULL , - ENROLMENT_ID INT NOT NULL, - POLICY_ID INT NOT NULL , + ID INTEGER NOT NULL IDENTITY , + DEVICE_ID INTEGER NOT NULL , + ENROLMENT_ID INTEGER NOT NULL, + POLICY_ID INTEGER NOT NULL , POLICY_CONTENT VARBINARY(max) NULL , - TENANT_ID INT NOT NULL, + TENANT_ID INTEGER NOT NULL, APPLIED SMALLINT NULL , CREATED_TIME DATETIME2(0) NULL , UPDATED_TIME DATETIME2(0) NULL , @@ -264,8 +287,8 @@ CREATE TABLE DM_USER_POLICY ( CREATE TABLE DM_CRITERIA ( - ID INT NOT NULL IDENTITY, - TENANT_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + TENANT_ID INTEGER NOT NULL, NAME VARCHAR(50) NULL, PRIMARY KEY (ID) ); @@ -273,9 +296,9 @@ CREATE TABLE DM_CRITERIA ( CREATE TABLE DM_POLICY_CRITERIA ( - ID INT NOT NULL IDENTITY, - CRITERIA_ID INT NOT NULL, - POLICY_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + CRITERIA_ID INTEGER NOT NULL, + POLICY_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_CRITERIA_POLICY_CRITERIA FOREIGN KEY (CRITERIA_ID) @@ -290,8 +313,8 @@ CREATE TABLE DM_POLICY_CRITERIA ( ); CREATE TABLE DM_POLICY_CRITERIA_PROPERTIES ( - ID INT NOT NULL IDENTITY, - POLICY_CRITERION_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + POLICY_CRITERION_ID INTEGER NOT NULL, PROP_KEY VARCHAR(45) NULL, PROP_VALUE VARCHAR(100) NULL, CONTENT VARBINARY(max) NULL , @@ -304,35 +327,35 @@ CREATE TABLE DM_POLICY_CRITERIA_PROPERTIES ( ); CREATE TABLE DM_POLICY_COMPLIANCE_STATUS ( - ID INT NOT NULL IDENTITY, - DEVICE_ID INT NOT NULL, - ENROLMENT_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - STATUS INT NULL, + ID INTEGER NOT NULL IDENTITY, + DEVICE_ID INTEGER NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, + POLICY_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, + STATUS INTEGER NULL, LAST_SUCCESS_TIME DATETIME2(0) NULL, LAST_REQUESTED_TIME DATETIME2(0) NULL, LAST_FAILED_TIME DATETIME2(0) NULL, - ATTEMPTS INT NULL, + ATTEMPTS INTEGER NULL, PRIMARY KEY (ID) ); CREATE TABLE DM_POLICY_CHANGE_MGT ( - ID INT NOT NULL IDENTITY, - POLICY_ID INT NOT NULL, - DEVICE_TYPE_ID INT NOT NULL, - TENANT_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + POLICY_ID INTEGER NOT NULL, + DEVICE_TYPE_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID) ); CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( - ID INT NOT NULL IDENTITY, - COMPLIANCE_STATUS_ID INT NOT NULL, - TENANT_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + COMPLIANCE_STATUS_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, FEATURE_CODE VARCHAR(100) NOT NULL, - STATUS INT NULL, + STATUS INTEGER NULL, PRIMARY KEY (ID), CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS FOREIGN KEY (COMPLIANCE_STATUS_ID) @@ -342,10 +365,10 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ); CREATE TABLE DM_DEVICE_GROUP_POLICY ( - ID INT NOT NULL IDENTITY, - DEVICE_GROUP_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - TENANT_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + DEVICE_GROUP_ID INTEGER NOT NULL, + POLICY_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_GROUP_POLICY FOREIGN KEY (DEVICE_GROUP_ID) @@ -370,7 +393,7 @@ CREATE TABLE DM_APPLICATION ( LOCATION_URL VARCHAR(100) DEFAULT NULL, IMAGE_URL VARCHAR(100) DEFAULT NULL, APP_PROPERTIES VARBINARY(max) NULL, - MEMORY_USAGE INTEGER(10) NULL, + MEMORY_USAGE INTEGER NULL, TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID) ); @@ -405,12 +428,12 @@ CREATE TABLE DM_NOTIFICATION ( ); -- NOTIFICATION TABLE END -- - -DROP TABLE IF EXISTS DM_DEVICE_INFO; +IF EXISTS (SELECT * FROM sys.objects WHERE name = 'DM_DEVICE_INFO' and type = 'u') + DROP TABLE DM_DEVICE_INFO; CREATE TABLE DM_DEVICE_INFO ( ID INTEGER IDENTITY NOT NULL, - DEVICE_ID INT NULL, + DEVICE_ID INTEGER NULL, KEY_FIELD VARCHAR(45) NULL, VALUE_FIELD VARCHAR(100) NULL, PRIMARY KEY (ID), @@ -423,11 +446,12 @@ CREATE TABLE DM_DEVICE_INFO ( CREATE INDEX DM_DEVICE_INFO_DEVICE_idx ON DM_DEVICE_INFO (DEVICE_ID ASC); -DROP TABLE IF EXISTS DM_DEVICE_LOCATION; +IF EXISTS (SELECT * FROM sys.objects WHERE name = 'DM_DEVICE_LOCATION' and type = 'u') + DROP TABLE DM_DEVICE_LOCATION; CREATE TABLE DM_DEVICE_LOCATION ( ID INTEGER IDENTITY NOT NULL, - DEVICE_ID INT NULL, + DEVICE_ID INTEGER NULL, LATITUDE FLOAT NULL, LONGITUDE FLOAT NULL, STREET1 VARCHAR(45) NULL, @@ -447,11 +471,12 @@ CREATE TABLE DM_DEVICE_LOCATION ( CREATE INDEX DM_DEVICE_LOCATION_DEVICE_idx ON DM_DEVICE_LOCATION (DEVICE_ID ASC); -DROP TABLE IF EXISTS DM_DEVICE_DETAIL; +IF EXISTS (SELECT * FROM sys.objects WHERE name = 'DM_DEVICE_DETAIL' and type = 'u') + DROP TABLE DM_DEVICE_DETAIL; CREATE TABLE DM_DEVICE_DETAIL ( - ID INT NOT NULL IDENTITY, - DEVICE_ID INT NOT NULL, + ID INTEGER NOT NULL IDENTITY, + DEVICE_ID INTEGER NOT NULL, DEVICE_MODEL VARCHAR(45) NULL, VENDOR VARCHAR(45) NULL, OS_VERSION VARCHAR(45) NULL, @@ -465,7 +490,7 @@ CREATE TABLE DM_DEVICE_DETAIL ( CPU_USAGE DECIMAL(5) NULL, TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, - PLUGGED_IN INT NULL, + PLUGGED_IN INTEGER NULL, UPDATE_TIMESTAMP BIGINT NOT NULL, PRIMARY KEY (ID), CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE From 754195bee6b908c57238d9e9ca02abbc647febc4 Mon Sep 17 00:00:00 2001 From: dilanua Date: Wed, 11 May 2016 12:01:39 +0530 Subject: [PATCH 2/2] Adding mssql support to dashboard analytics feature --- .../main/resources/dbscripts/cdm/mssql.sql | 61 +++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql index c4bd77fa100..e709195f193 100644 --- a/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/org.wso2.carbon.device.mgt.server.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -502,3 +502,64 @@ CREATE TABLE DM_DEVICE_DETAIL ( CREATE INDEX FK_DM_DEVICE_DETAILS_DEVICE_idx ON DM_DEVICE_DETAIL (DEVICE_ID ASC); +-- DASHBOARD RELATED VIEWS -- + +CREATE VIEW DEVICES_VIEW_1 AS +SELECT TOP 100 PERCENT +DEVICE_INFO.DEVICE_ID, +DEVICE_INFO.PLATFORM, +DEVICE_INFO.OWNERSHIP, +DEVICE_INFO.CONNECTIVITY_STATUS, +ISNULL(DEVICE_WITH_POLICY_INFO.POLICY_ID, -1) AS POLICY_ID, +ISNULL(DEVICE_WITH_POLICY_INFO.IS_COMPLIANT, -1) AS +IS_COMPLIANT, +DEVICE_INFO.TENANT_ID +FROM +(SELECT +DM_DEVICE.ID AS DEVICE_ID, +DM_DEVICE_TYPE.NAME AS PLATFORM, +DM_ENROLMENT.OWNERSHIP, +DM_ENROLMENT.STATUS AS CONNECTIVITY_STATUS, +DM_DEVICE.TENANT_ID +FROM DM_DEVICE, DM_DEVICE_TYPE, DM_ENROLMENT +WHERE DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND DM_DEVICE.ID = DM_ENROLMENT.DEVICE_ID) DEVICE_INFO +LEFT JOIN +(SELECT +DEVICE_ID, +POLICY_ID, +STATUS AS IS_COMPLIANT +FROM +DM_POLICY_COMPLIANCE_STATUS) DEVICE_WITH_POLICY_INFO +ON DEVICE_INFO.DEVICE_ID = DEVICE_WITH_POLICY_INFO.DEVICE_ID +ORDER BY DEVICE_INFO.DEVICE_ID; +GO + +CREATE VIEW DEVICES_VIEW_2 AS +SELECT TOP 100 PERCENT +DM_DEVICE.ID AS DEVICE_ID, +DM_DEVICE_DETAIL.DEVICE_MODEL, +DM_DEVICE_DETAIL.VENDOR, +DM_DEVICE_DETAIL.OS_VERSION, +DM_ENROLMENT.OWNERSHIP, +DM_ENROLMENT.OWNER, +DM_ENROLMENT.STATUS AS CONNECTIVITY_STATUS, +DM_POLICY_COMPLIANCE_STATUS.POLICY_ID, +DM_DEVICE_TYPE.NAME +AS PLATFORM, +DM_POLICY_COMPLIANCE_FEATURES.FEATURE_CODE, +DM_POLICY_COMPLIANCE_FEATURES.STATUS AS IS_COMPLAINT, +DM_DEVICE.TENANT_ID +FROM +DM_POLICY_COMPLIANCE_FEATURES, DM_POLICY_COMPLIANCE_STATUS, DM_ENROLMENT, DM_DEVICE, DM_DEVICE_TYPE, DM_DEVICE_DETAIL +WHERE +DM_POLICY_COMPLIANCE_FEATURES.COMPLIANCE_STATUS_ID = DM_POLICY_COMPLIANCE_STATUS.ID AND +DM_POLICY_COMPLIANCE_STATUS.ENROLMENT_ID = +DM_ENROLMENT.ID AND +DM_POLICY_COMPLIANCE_STATUS.DEVICE_ID = DM_DEVICE.ID AND +DM_DEVICE.DEVICE_TYPE_ID = DM_DEVICE_TYPE.ID AND +DM_DEVICE.ID = DM_DEVICE_DETAIL.DEVICE_ID +ORDER BY TENANT_ID, DEVICE_ID; +GO + +-- END OF DASHBOARD RELATED VIEWS -- +