From df6b0f9612a06a02c2bd596eafb1fe7cbfefa4c5 Mon Sep 17 00:00:00 2001
From: harshanl <harshan@wso2.com>
Date: Mon, 3 Oct 2016 12:16:45 +0530
Subject: [PATCH] Updated MSSQL scripts to have IF-NOT-EXISTS statements

---
 .../src/main/resources/dbscripts/plugins/mssql.sql            | 2 ++
 .../src/main/resources/dbscripts/plugins/android/mssql.sql    | 2 ++
 .../src/main/resources/dbscripts/plugins/ios/mssql.sql        | 4 +++-
 .../src/main/resources/dbscripts/plugins/windows/mssql.sql    | 4 +++-
 .../src/main/resources/dbscripts/plugins/mssql.sql            | 4 +++-
 5 files changed, 13 insertions(+), 3 deletions(-)

diff --git a/features/mobile-plugins-feature/android-plugin-feature/org.wso2.carbon.device.mgt.mobile.android.feature/src/main/resources/dbscripts/plugins/mssql.sql b/features/mobile-plugins-feature/android-plugin-feature/org.wso2.carbon.device.mgt.mobile.android.feature/src/main/resources/dbscripts/plugins/mssql.sql
index 2269e4c19..3cd14ad6a 100644
--- a/features/mobile-plugins-feature/android-plugin-feature/org.wso2.carbon.device.mgt.mobile.android.feature/src/main/resources/dbscripts/plugins/mssql.sql
+++ b/features/mobile-plugins-feature/android-plugin-feature/org.wso2.carbon.device.mgt.mobile.android.feature/src/main/resources/dbscripts/plugins/mssql.sql
@@ -1,6 +1,7 @@
 -- -----------------------------------------------------
 -- Table `AD_DEVICE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AD_DEVICE]') AND TYPE IN (N'U'))
 CREATE  TABLE AD_DEVICE (
   DEVICE_ID VARCHAR(45) NOT NULL,
   GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL,
@@ -21,6 +22,7 @@ CREATE  TABLE AD_DEVICE (
 -- -----------------------------------------------------
 -- Table `AD_FEATURE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AD_FEATURE]') AND TYPE IN (N'U'))
 CREATE TABLE AD_FEATURE (
   ID INT NOT NULL IDENTITY,
   CODE VARCHAR(45) NOT NULL,
diff --git a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql
index 2269e4c19..3cd14ad6a 100644
--- a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql
+++ b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/android/mssql.sql
@@ -1,6 +1,7 @@
 -- -----------------------------------------------------
 -- Table `AD_DEVICE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AD_DEVICE]') AND TYPE IN (N'U'))
 CREATE  TABLE AD_DEVICE (
   DEVICE_ID VARCHAR(45) NOT NULL,
   GCM_TOKEN VARCHAR(1000) NULL DEFAULT NULL,
@@ -21,6 +22,7 @@ CREATE  TABLE AD_DEVICE (
 -- -----------------------------------------------------
 -- Table `AD_FEATURE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[AD_FEATURE]') AND TYPE IN (N'U'))
 CREATE TABLE AD_FEATURE (
   ID INT NOT NULL IDENTITY,
   CODE VARCHAR(45) NOT NULL,
diff --git a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/ios/mssql.sql b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/ios/mssql.sql
index 5205ff6f2..9d816fe8f 100644
--- a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/ios/mssql.sql
+++ b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/ios/mssql.sql
@@ -1,6 +1,7 @@
 -- -----------------------------------------------------
 -- Table `IOS_FEATURE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[IOS_FEATURE]') AND TYPE IN (N'U'))
 CREATE TABLE IOS_FEATURE (
   ID INT NOT NULL IDENTITY,
   CODE VARCHAR(45) NOT NULL,
@@ -12,7 +13,8 @@ CREATE TABLE IOS_FEATURE (
 -- -----------------------------------------------------
 -- Table `IOS_DEVICE`
 -- -----------------------------------------------------
-  CREATE  TABLE IOS_DEVICE (
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[IOS_DEVICE]') AND TYPE IN (N'U'))
+ CREATE  TABLE IOS_DEVICE (
   MOBILE_DEVICE_ID VARCHAR(45) NOT NULL,
   APNS_PUSH_TOKEN VARCHAR(100) NULL DEFAULT NULL,
   MAGIC_TOKEN VARCHAR(100) NULL DEFAULT NULL,
diff --git a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql
index 593515185..524a483c7 100644
--- a/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql
+++ b/features/mobile-plugins-feature/mobile-base-plugin-feature/org.wso2.carbon.device.mgt.mobile.feature/src/main/resources/dbscripts/plugins/windows/mssql.sql
@@ -1,6 +1,7 @@
 -- -----------------------------------------------------
 -- Table `WINDOWS_FEATURE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WIN_FEATURE]') AND TYPE IN (N'U'))
 CREATE TABLE WIN_FEATURE (
   ID INT NOT NULL IDENTITY,
   CODE VARCHAR(45) NOT NULL,
@@ -12,7 +13,8 @@ CREATE TABLE WIN_FEATURE (
 -- -----------------------------------------------------
 -- Table `WINDOWS_DEVICE`
 -- -----------------------------------------------------
-  CREATE  TABLE WIN_DEVICE (
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WIN_DEVICE]') AND TYPE IN (N'U'))
+CREATE  TABLE WIN_DEVICE (
   DEVICE_ID VARCHAR(45) NOT NULL,
   CHANNEL_URI VARCHAR(100) NULL DEFAULT NULL,
   DEVICE_INFO TEXT NULL DEFAULT NULL,
diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql
index 593515185..524a483c7 100644
--- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql
+++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/mssql.sql
@@ -1,6 +1,7 @@
 -- -----------------------------------------------------
 -- Table `WINDOWS_FEATURE`
 -- -----------------------------------------------------
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WIN_FEATURE]') AND TYPE IN (N'U'))
 CREATE TABLE WIN_FEATURE (
   ID INT NOT NULL IDENTITY,
   CODE VARCHAR(45) NOT NULL,
@@ -12,7 +13,8 @@ CREATE TABLE WIN_FEATURE (
 -- -----------------------------------------------------
 -- Table `WINDOWS_DEVICE`
 -- -----------------------------------------------------
-  CREATE  TABLE WIN_DEVICE (
+IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WIN_DEVICE]') AND TYPE IN (N'U'))
+CREATE  TABLE WIN_DEVICE (
   DEVICE_ID VARCHAR(45) NOT NULL,
   CHANNEL_URI VARCHAR(100) NULL DEFAULT NULL,
   DEVICE_INFO TEXT NULL DEFAULT NULL,