From 6becd0d40eaee9abe548aeecf3cb2de3505c52ee Mon Sep 17 00:00:00 2001
From: Harshan Liyanage <harshan@wso2.com>
Date: Fri, 8 Sep 2017 14:19:53 +0530
Subject: [PATCH] Fixed issues in Oracle & MSSQL scripts related to
 wso2/product-iots#1393.

---
 .../resources/dbscripts/plugins/mssql.sql     |  6 ++---
 .../resources/dbscripts/plugins/oracle.sql    | 25 ++++++++++++++++---
 2 files changed, 25 insertions(+), 6 deletions(-)

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 7f79b94495..f6314b9925 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
@@ -3,7 +3,7 @@
 -- -----------------------------------------------------
 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,
+  ID INTEGER IDENTITY(1,1) NOT NULL,
   CODE VARCHAR(45) NOT NULL,
   NAME VARCHAR(100) NULL,
   DESCRIPTION VARCHAR(200) NULL,
@@ -36,9 +36,9 @@ CREATE  TABLE WIN_DEVICE (
 -- -----------------------------------------------------
 IF NOT  EXISTS (SELECT * FROM SYS.OBJECTS WHERE OBJECT_ID = OBJECT_ID(N'[DBO].[WINDOWS_ENROLLMENT_TOKEN]') AND TYPE IN (N'U'))
 CREATE TABLE WINDOWS_ENROLLMENT_TOKEN (
-  ID INT NOT NULL IDENTITY,
+  ID INTEGER IDENTITY(1,1) NOT NULL,
   TENANT_DOMAIN VARCHAR(45) NOT NULL,
-  TENANT_ID INT DEFAULT 0,
+  TENANT_ID INTEGER DEFAULT 0,
   ENROLLMENT_TOKEN VARCHAR (100) NULL DEFAULT NULL,
   DEVICE_ID VARCHAR (100) NOT NULL,
   USERNAME VARCHAR (45) NOT NULL,
diff --git a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql
index 2e69878b1c..e390112fc4 100644
--- a/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql
+++ b/features/mobile-plugins-feature/windows-plugin-feature/org.wso2.carbon.device.mgt.mobile.windows.feature/src/main/resources/dbscripts/plugins/oracle.sql
@@ -23,7 +23,7 @@ CREATE TABLE WIN_DEVICE (
 -- Table `WIN_FEATURE`
 -- -----------------------------------------------------
 CREATE TABLE WIN_FEATURE (
-  ID INT NOT NULL,
+  ID NUMBER(10) NOT NULL,
   CODE VARCHAR(45) NOT NULL,
   NAME VARCHAR(100) NOT NULL,
   DESCRIPTION VARCHAR(200) NULL,
@@ -35,9 +35,9 @@ CREATE TABLE WIN_FEATURE (
 -- Table `WINDOWS_ENROLLMENT_TOKEN`
 -- -----------------------------------------------------
 CREATE TABLE WINDOWS_ENROLLMENT_TOKEN (
-  ID INT NOT NULL,
+  ID NUMBER(10) NOT NULL,
   TENANT_DOMAIN VARCHAR(45) NOT NULL,
-  TENANT_ID INT DEFAULT 0,
+  TENANT_ID NUMBER(10) DEFAULT 0,
   ENROLLMENT_TOKEN VARCHAR(100) NULL,
   DEVICE_ID VARCHAR(100) NULL,
   USERNAME VARCHAR(45) NULL,
@@ -63,4 +63,23 @@ FOR EACH ROW
   BEGIN
     SELECT WIN_FEATURE_ID_INC_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
   END;
+/
+
+-- -----------------------------------------------------
+-- Sequence `WINDOWS_ENROLLMENT_TOKEN_ID_INC_SEQ`
+-- -----------------------------------------------------
+CREATE SEQUENCE WINDOWS_ENROLLMENT_TOKEN_ID_INC_SEQ START WITH 1 INCREMENT BY 1 NOCACHE
+/
+
+-- -----------------------------------------------------
+-- Trigger `WINDOWS_ENROLLMENT_TOKEN_ID_INC_TRIG`
+-- -----------------------------------------------------
+CREATE OR REPLACE TRIGGER WINDOWS_ENROLLMENT_TOKEN_ID_INC_TRIG
+BEFORE INSERT
+ON WINDOWS_ENROLLMENT_TOKEN
+REFERENCING NEW AS NEW
+FOR EACH ROW
+  BEGIN
+    SELECT WINDOWS_ENROLLMENT_TOKEN_ID_INC_SEQ.NEXTVAL INTO :NEW.ID FROM DUAL;
+  END;
 /
\ No newline at end of file