From ee97631eacab25f438b99755d70ef4c3d38a69e3 Mon Sep 17 00:00:00 2001 From: harshanl Date: Mon, 3 Oct 2016 12:16:01 +0530 Subject: [PATCH] Updated MSSQL scripts to have IF-NOT-EXISTS statements --- .../resources/dbscripts/certMgt/mssql.sql | 1 + .../main/resources/dbscripts/cdm/mssql.sql | 45 ++++++++++++++++++- 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/features/certificate-mgt/org.wso2.carbon.certificate.mgt.server.feature/src/main/resources/dbscripts/certMgt/mssql.sql b/features/certificate-mgt/org.wso2.carbon.certificate.mgt.server.feature/src/main/resources/dbscripts/certMgt/mssql.sql index 7abe97f1df1..316b06d726f 100644 --- a/features/certificate-mgt/org.wso2.carbon.certificate.mgt.server.feature/src/main/resources/dbscripts/certMgt/mssql.sql +++ b/features/certificate-mgt/org.wso2.carbon.certificate.mgt.server.feature/src/main/resources/dbscripts/certMgt/mssql.sql @@ -1,3 +1,4 @@ +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_CERTIFICATE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_CERTIFICATE ( ID INTEGER IDENTITY(1,1) NOT NULL, SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, 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 0495a390d99..40adb654e09 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 @@ -1,3 +1,4 @@ +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_TYPE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_TYPE ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(300) DEFAULT NULL, @@ -7,8 +8,10 @@ CREATE TABLE DM_DEVICE_TYPE ( CONSTRAINT DEVICE_TYPE_NAME UNIQUE(NAME) ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DEVICE_TYPE' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_TYPE')) CREATE INDEX IDX_DEVICE_TYPE ON DM_DEVICE_TYPE (NAME); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE ( ID INTEGER IDENTITY(1,1) NOT NULL, DESCRIPTION VARCHAR(MAX) DEFAULT NULL, @@ -22,8 +25,10 @@ CREATE TABLE DM_DEVICE ( REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_DM_DEVICE' AND OBJECT_ID = OBJECT_ID('DM_DEVICE')) CREATE INDEX IDX_DM_DEVICE ON DM_DEVICE(TENANT_ID, DEVICE_TYPE_ID); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_OPERATION]') AND TYPE IN (N'U')) CREATE TABLE DM_OPERATION ( ID INTEGER IDENTITY(1,1) NOT NULL, TYPE VARCHAR(20) NOT NULL, @@ -33,6 +38,7 @@ CREATE TABLE DM_OPERATION ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_CONFIG_OPERATION]') AND TYPE IN (N'U')) CREATE TABLE DM_CONFIG_OPERATION ( OPERATION_ID INTEGER NOT NULL, OPERATION_CONFIG VARBINARY(MAX) DEFAULT NULL, @@ -41,6 +47,7 @@ CREATE TABLE DM_CONFIG_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_COMMAND_OPERATION]') AND TYPE IN (N'U')) CREATE TABLE DM_COMMAND_OPERATION ( OPERATION_ID INTEGER NOT NULL, ENABLED BIT NOT NULL DEFAULT 0, @@ -49,6 +56,7 @@ CREATE TABLE DM_COMMAND_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_OPERATION]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_OPERATION ( OPERATION_ID INTEGER NOT NULL, ENABLED INTEGER NOT NULL DEFAULT 0, @@ -58,6 +66,7 @@ CREATE TABLE DM_POLICY_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_PROFILE_OPERATION]') AND TYPE IN (N'U')) CREATE TABLE DM_PROFILE_OPERATION ( OPERATION_ID INTEGER NOT NULL, ENABLED INTEGER NOT NULL DEFAULT 0, @@ -67,6 +76,7 @@ CREATE TABLE DM_PROFILE_OPERATION ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_ENROLMENT]') AND TYPE IN (N'U')) CREATE TABLE DM_ENROLMENT ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -81,9 +91,12 @@ CREATE TABLE DM_ENROLMENT ( DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENROLMENT_FK_DEVICE_ID' AND OBJECT_ID = OBJECT_ID('DM_ENROLMENT')) CREATE INDEX IDX_ENROLMENT_FK_DEVICE_ID ON DM_ENROLMENT(DEVICE_ID); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENROLMENT_DEVICE_ID_TENANT_ID' AND OBJECT_ID = OBJECT_ID('DM_ENROLMENT')) CREATE INDEX IDX_ENROLMENT_DEVICE_ID_TENANT_ID ON DM_ENROLMENT(DEVICE_ID, TENANT_ID); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_ENROLMENT_OP_MAPPING]') AND TYPE IN (N'U')) CREATE TABLE DM_ENROLMENT_OP_MAPPING ( ID INTEGER IDENTITY(1,1) NOT NULL, ENROLMENT_ID INTEGER NOT NULL, @@ -98,10 +111,14 @@ CREATE TABLE DM_ENROLMENT_OP_MAPPING ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENROLMENT_OP_MAPPING' AND OBJECT_ID = OBJECT_ID('DM_ENROLMENT_OP_MAPPING')) CREATE INDEX IDX_ENROLMENT_OP_MAPPING ON DM_ENROLMENT_OP_MAPPING (UPDATED_TIMESTAMP); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_EN_OP_MAPPING_EN_ID' AND OBJECT_ID = OBJECT_ID('DM_ENROLMENT_OP_MAPPING')) CREATE INDEX IDX_EN_OP_MAPPING_EN_ID ON DM_ENROLMENT_OP_MAPPING(ENROLMENT_ID); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_EN_OP_MAPPING_OP_ID' AND OBJECT_ID = OBJECT_ID('DM_ENROLMENT_OP_MAPPING')) CREATE INDEX IDX_EN_OP_MAPPING_OP_ID ON DM_ENROLMENT_OP_MAPPING(OPERATION_ID); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_OPERATION_RESPONSE]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( ID INTEGER IDENTITY(1,1) NOT NULL, ENROLMENT_ID INTEGER NOT NULL, @@ -115,10 +132,12 @@ CREATE TABLE DM_DEVICE_OPERATION_RESPONSE ( DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.INDEXES WHERE NAME = 'IDX_ENID_OPID' AND OBJECT_ID = OBJECT_ID('DM_DEVICE_OPERATION_RESPONSE')) CREATE INDEX IDX_ENID_OPID ON DM_DEVICE_OPERATION_RESPONSE(OPERATION_ID, ENROLMENT_ID); -- POLICY RELATED TABLES -- +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_PROFILE]') AND TYPE IN (N'U')) CREATE TABLE DM_PROFILE ( ID INTEGER IDENTITY(1,1) NOT NULL, PROFILE_NAME VARCHAR(45) NOT NULL , @@ -131,6 +150,7 @@ CREATE TABLE DM_PROFILE ( DM_DEVICE_TYPE (NAME) ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(45) DEFAULT NULL , @@ -147,6 +167,7 @@ CREATE TABLE DM_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL , @@ -160,6 +181,7 @@ CREATE TABLE DM_DEVICE_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_TYPE_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_TYPE_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_TYPE_ID INTEGER NOT NULL , @@ -171,6 +193,7 @@ CREATE TABLE DM_DEVICE_TYPE_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_PROFILE_FEATURES]') AND TYPE IN (N'U')) CREATE TABLE DM_PROFILE_FEATURES ( ID INTEGER IDENTITY(1,1) NOT NULL, PROFILE_ID INTEGER NOT NULL, @@ -183,6 +206,7 @@ CREATE TABLE DM_PROFILE_FEATURES ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_ROLE_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_ROLE_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, ROLE_NAME VARCHAR(45) NOT NULL , @@ -192,6 +216,7 @@ CREATE TABLE DM_ROLE_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_USER_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_USER_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, POLICY_ID INTEGER NOT NULL , @@ -201,6 +226,7 @@ CREATE TABLE DM_USER_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_POLICY_APPLIED]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_POLICY_APPLIED ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL , @@ -217,6 +243,7 @@ CREATE TABLE DM_USER_POLICY ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_CRITERIA]') AND TYPE IN (N'U')) CREATE TABLE DM_CRITERIA ( ID INTEGER IDENTITY(1,1) NOT NULL, TENANT_ID INTEGER NOT NULL, @@ -224,6 +251,7 @@ CREATE TABLE DM_CRITERIA ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_CRITERIA]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_CRITERIA ( ID INTEGER IDENTITY(1,1) NOT NULL, CRITERIA_ID INTEGER NOT NULL, @@ -235,6 +263,7 @@ CREATE TABLE DM_POLICY_CRITERIA ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_CRITERIA_PROPERTIES]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_CRITERIA_PROPERTIES ( ID INTEGER IDENTITY(1,1) NOT NULL, POLICY_CRITERION_ID INTEGER NOT NULL, @@ -246,6 +275,7 @@ CREATE TABLE DM_POLICY_CRITERIA_PROPERTIES ( ON DELETE CASCADE ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_COMPLIANCE_STATUS]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_COMPLIANCE_STATUS ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -260,6 +290,7 @@ CREATE TABLE DM_POLICY_COMPLIANCE_STATUS ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_CHANGE_MGT]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_CHANGE_MGT ( ID INTEGER IDENTITY(1,1) NOT NULL, POLICY_ID INTEGER NOT NULL, @@ -268,6 +299,7 @@ CREATE TABLE DM_POLICY_CHANGE_MGT ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_POLICY_COMPLIANCE_FEATURES]') AND TYPE IN (N'U')) CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ID INTEGER IDENTITY(1,1) NOT NULL, COMPLIANCE_STATUS_ID INTEGER NOT NULL, @@ -279,6 +311,7 @@ CREATE TABLE DM_POLICY_COMPLIANCE_FEATURES ( ON DELETE NO ACTION ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_APPLICATION]') AND TYPE IN (N'U')) CREATE TABLE DM_APPLICATION ( ID INTEGER IDENTITY(1,1) NOT NULL, NAME VARCHAR(150) NOT NULL, @@ -296,6 +329,7 @@ CREATE TABLE DM_APPLICATION ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_APPLICATION_MAPPING]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_APPLICATION_MAPPING ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -312,7 +346,7 @@ CREATE TABLE DM_DEVICE_APPLICATION_MAPPING ( -- DEVICE GROUP TABLES -- - +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_GROUP]') AND TYPE IN (N'U')) CREATE TABLE DM_GROUP ( ID INTEGER IDENTITY(1,1) NOT NULL, GROUP_NAME VARCHAR(100) DEFAULT NULL, @@ -324,6 +358,7 @@ CREATE TABLE DM_GROUP ( PRIMARY KEY (ID) ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_GROUP_MAP]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_GROUP_MAP ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER DEFAULT NULL, @@ -339,7 +374,7 @@ CREATE TABLE DM_DEVICE_GROUP_MAP ( -- END OF DEVICE GROUP TABLES -- -- POLICY AND DEVICE GROUP MAPPING -- - +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_GROUP_POLICY]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_GROUP_POLICY ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_GROUP_ID INTEGER NOT NULL, @@ -361,6 +396,7 @@ CREATE TABLE DM_DEVICE_GROUP_POLICY ( -- END OF POLICY AND DEVICE GROUP MAPPING -- -- NOTIFICATION TABLE -- +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_NOTIFICATION]') AND TYPE IN (N'U')) CREATE TABLE DM_NOTIFICATION ( NOTIFICATION_ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -376,6 +412,7 @@ CREATE TABLE DM_NOTIFICATION ( ); -- NOTIFICATION TABLE END -- +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_INFO]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_INFO ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NULL, @@ -387,6 +424,7 @@ CREATE TABLE DM_DEVICE_INFO ( ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_LOCATION]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_LOCATION ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NULL, @@ -408,6 +446,7 @@ CREATE TABLE DM_DEVICE_LOCATION ( ON UPDATE NO ACTION ); +IF NOT EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[DM_DEVICE_DETAIL]') AND TYPE IN (N'U')) CREATE TABLE DM_DEVICE_DETAIL ( ID INTEGER IDENTITY(1,1) NOT NULL, DEVICE_ID INTEGER NOT NULL, @@ -438,6 +477,7 @@ CREATE TABLE DM_DEVICE_DETAIL ( -- DASHBOARD RELATED VIEWS -- +IF NOT EXISTS (SELECT * FROM SYS.VIEWS WHERE NAME = 'POLICY_COMPLIANCE_INFO') exec('CREATE VIEW POLICY_COMPLIANCE_INFO AS SELECT TOP 100 PERCENT DEVICE_INFO.DEVICE_ID, @@ -469,6 +509,7 @@ 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'); +IF NOT EXISTS (SELECT * FROM SYS.VIEWS WHERE NAME = 'CREATE VIEW FEATURE_NON_COMPLIANCE_INFO') exec('CREATE VIEW FEATURE_NON_COMPLIANCE_INFO AS SELECT TOP 100 PERCENT DM_DEVICE.ID AS DEVICE_ID,