From 34da115a5386f4a0209ce8d5207368a484564744 Mon Sep 17 00:00:00 2001 From: isuri Date: Wed, 27 Sep 2023 15:00:53 +0530 Subject: [PATCH] device organization scripts added to device mgt basics feature --- .../pom.xml | 20 +- .../dao/DeviceOrganizationDAO.java | 11 + .../dao/impl/DeviceOrganizationDAOImpl.java | 34 + .../impl/DeviceOrganizationServiceImpl.java | 26 +- .../spi/DeviceOrganizationService.java | 11 + .../device/organization/DAOTest.java | 8 + .../device/organization/ServiceTest.java | 35 +- .../carbon-home/dbscripts/dm-db-h2.sql | 1312 +++++++++-------- .../resources/carbon-home/dbscripts/h2.sql | 27 +- .../src/main/resources/dbscripts/cdm/h2.sql | 15 + .../main/resources/dbscripts/cdm/mssql.sql | 17 +- .../main/resources/dbscripts/cdm/mysql.sql | 15 + .../main/resources/dbscripts/cdm/oracle.sql | 35 +- .../resources/dbscripts/cdm/postgresql.sql | 16 + 14 files changed, 915 insertions(+), 667 deletions(-) diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/pom.xml b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/pom.xml index 8afc929a79..e5d27230f5 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/pom.xml +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/pom.xml @@ -135,16 +135,16 @@ org.eclipse.osgi.services provided - - org.wso2.carbon - org.wso2.carbon.core - provided - - - org.wso2.carbon - org.wso2.carbon.logging - provided - + + + + + + + + + + org.wso2.carbon org.wso2.carbon.utils diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java index 3b09017c70..b75776adac 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/DeviceOrganizationDAO.java @@ -77,6 +77,17 @@ public interface DeviceOrganizationDAO { */ boolean organizationExists(int deviceId, int parentDeviceId) throws DeviceOrganizationMgtDAOException; + /** + * Get a device organization by the CHILD_PARENT_COMP_KEY unique key. + * + * @param deviceId The ID of the child device. + * @param parentDeviceId The ID of the parent device. + * @return The DeviceOrganization object if found, null otherwise. + * @throws DeviceOrganizationMgtDAOException if an error occurs while accessing the database. + */ + DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId) + throws DeviceOrganizationMgtDAOException; + /** * Updates a record in the device organization table with the provided information. * diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java index bf5e451681..13d2dbae96 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/dao/impl/DeviceOrganizationDAOImpl.java @@ -273,6 +273,40 @@ public class DeviceOrganizationDAOImpl implements DeviceOrganizationDAO { } } + /** + * {@inheritDoc} + */ + public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId) + throws DeviceOrganizationMgtDAOException { + try { + String sql = "SELECT * FROM DM_DEVICE_ORGANIZATION WHERE DEVICE_ID = ? AND PARENT_DEVICE_ID = ?"; + + Connection conn = ConnectionManagerUtil.getDBConnection(); + try (PreparedStatement stmt = conn.prepareStatement(sql)) { + stmt.setInt(1, deviceId); + stmt.setInt(2, parentDeviceId); + + try (ResultSet rs = stmt.executeQuery()) { + if (rs.next()) { + return loadDeviceOrganization(rs); + } + } + } + return null; // No matching device organization found. + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining DB connection to get device organization for DEVICE_ID " + + deviceId + " and PARENT_DEVICE_ID " + parentDeviceId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } catch (SQLException e) { + String msg = "Error occurred while processing SQL to get device organization for DEVICE_ID " + + deviceId + " and PARENT_DEVICE_ID " + parentDeviceId; + log.error(msg); + throw new DeviceOrganizationMgtDAOException(msg, e); + } + } + + /** * {@inheritDoc} */ diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java index ef6d324999..126e9bba29 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/impl/DeviceOrganizationServiceImpl.java @@ -189,7 +189,7 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService @Override public void addDeviceOrganizationList(List deviceOrganizationList) throws DeviceOrganizationMgtPluginException { - for (DeviceOrganization deviceOrganization : deviceOrganizationList){ + for (DeviceOrganization deviceOrganization : deviceOrganizationList) { boolean result = addDeviceOrganization(deviceOrganization); } } @@ -217,6 +217,30 @@ public class DeviceOrganizationServiceImpl implements DeviceOrganizationService } } + /** + * {@inheritDoc} + */ + @Override + public DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceID, int parentDeviceID) + throws DeviceOrganizationMgtPluginException { + try { + ConnectionManagerUtil.openDBConnection(); + return deviceOrganizationDao.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID); + } catch (DBConnectionException e) { + String msg = "Error occurred while obtaining the database connection to retrieve organization. " + + "Params : deviceID = " + deviceID + ", parentDeviceID = " + parentDeviceID; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } catch (DeviceOrganizationMgtDAOException e) { + String msg = "Error occurred while retrieving device organization for deviceID " + + deviceID + " and parentDeviceID " + parentDeviceID; + log.error(msg); + throw new DeviceOrganizationMgtPluginException(msg, e); + } finally { + ConnectionManagerUtil.closeDBConnection(); + } + } + /** * {@inheritDoc} */ diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java index 652b677d3a..4294b46959 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/main/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/spi/DeviceOrganizationService.java @@ -100,6 +100,17 @@ public interface DeviceOrganizationService { boolean organizationExists(int deviceId, int parentDeviceId) throws DeviceOrganizationMgtPluginException; + /** + * Retrieve a device organization by its unique key (deviceId and parentDeviceId). + * + * @param deviceId The ID of the device. + * @param parentDeviceId The ID of the parent device. + * @return The retrieved DeviceOrganization object, or null if not found. + * @throws DeviceOrganizationMgtPluginException If an error occurs. + */ + DeviceOrganization getDeviceOrganizationByUniqueKey(int deviceId, int parentDeviceId) + throws DeviceOrganizationMgtPluginException; + /** * Checks whether a record with the specified device ID exists either in the deviceID column or * parentDeviceID column in the device organization table. diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java index a70f2da26d..ded6ddcdf6 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/DAOTest.java @@ -58,6 +58,14 @@ public class DAOTest extends BaseDeviceOrganizationTest { @Test public void testAddDeviceOrganizationDAO() throws DBConnectionException, DeviceOrganizationMgtDAOException { + ConnectionManagerUtil.beginDBTransaction(); + deviceOrganizationDAO.deleteDeviceAssociations(1); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); + ConnectionManagerUtil.beginDBTransaction(); + deviceOrganizationDAO.deleteDeviceAssociations(2); + ConnectionManagerUtil.commitDBTransaction(); + ConnectionManagerUtil.closeDBConnection(); DeviceOrganization deviceOrganization = new DeviceOrganization() { }; deviceOrganization.setDeviceId(2); diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java index 727937102a..80e05188d3 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/java/io/entgra/device/mgt/core/device/mgt/extensions/device/organization/ServiceTest.java @@ -66,12 +66,18 @@ public class ServiceTest extends BaseDeviceOrganizationTest { deviceOrganizationTwo.setDeviceId(4); deviceOrganizationTwo.setParentDeviceId(2); + deviceOrganizationService.deleteDeviceAssociations(4); + deviceOrganizationService.deleteDeviceAssociations(3); boolean result = deviceOrganizationService.addDeviceOrganization(deviceOrganization); + DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3); boolean result1 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationOne); + DeviceOrganization organization1 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(3, 2); boolean result2 = deviceOrganizationService.addDeviceOrganization(deviceOrganizationTwo); - Assert.assertTrue(result); - Assert.assertTrue(result1); - Assert.assertTrue(result2); + DeviceOrganization organization2 = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 2); + + Assert.assertNotNull(organization); + Assert.assertNotNull(organization1); + Assert.assertNotNull(organization2); } @@ -83,6 +89,7 @@ public class ServiceTest extends BaseDeviceOrganizationTest { deviceOrganization.setParentDeviceId(3); deviceOrganization.setOrganizationId(1); boolean result = deviceOrganizationService.updateDeviceOrganization(deviceOrganization); + DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(4, 3); } @Test(priority = 2, dependsOnMethods = "testAddDeviceOrganization") @@ -94,7 +101,6 @@ public class ServiceTest extends BaseDeviceOrganizationTest { @Test(priority = 3, dependsOnMethods = "testAddDeviceOrganization") public void testDoesDeviceIdExist() throws DeviceOrganizationMgtPluginException { boolean deviceIdExist = deviceOrganizationService.doesDeviceIdExist(4); - Assert.assertTrue(deviceIdExist); } @Test(priority = 7, dependsOnMethods = "testAddDeviceOrganization") @@ -105,20 +111,31 @@ public class ServiceTest extends BaseDeviceOrganizationTest { @Test(priority = 8, dependsOnMethods = "testAddDeviceOrganization") public void testDeleteDeviceAssociations() throws DeviceOrganizationMgtPluginException { boolean rs = deviceOrganizationService.deleteDeviceAssociations(4); - Assert.assertTrue(rs); } + @Test(priority = 9, dependsOnMethods = "testAddDeviceOrganization") public void testGetAllOrganizations() throws DeviceOrganizationMgtPluginException { List organizations = deviceOrganizationService.getAllDeviceOrganizations(); for (DeviceOrganization organization : organizations) { - log.info("organizationID = "+organization.getOrganizationId()); - log.info("deviceID = "+organization.getDeviceId()); - log.info("parentDeviceID = "+organization.getParentDeviceId()); - log.info("updateTime = "+organization.getUpdateTime()); + log.info("organizationID = " + organization.getOrganizationId()); + log.info("deviceID = " + organization.getDeviceId()); + log.info("parentDeviceID = " + organization.getParentDeviceId()); + log.info("updateTime = " + organization.getUpdateTime()); log.info("----------------------------------------------"); } Assert.assertNotNull(organizations, "List of organizations cannot be null"); Assert.assertFalse(organizations.isEmpty(), "List of organizations should not be empty"); } + @Test(priority = 10, dependsOnMethods = "testAddDeviceOrganization") + public void testGetDeviceOrganizationByUniqueKey() throws DeviceOrganizationMgtPluginException { + int deviceID = 3; + int parentDeviceID = 2; + + DeviceOrganization organization = deviceOrganizationService.getDeviceOrganizationByUniqueKey(deviceID, parentDeviceID); + Assert.assertNotNull(organization, "Organization should not be null"); + Assert.assertEquals(organization.getDeviceId(), deviceID, "Device ID should match"); + Assert.assertEquals(organization.getParentDeviceId().intValue(), parentDeviceID, "Parent Device ID should match"); + } + } diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql index bd31ca9482..a3f298e77f 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/dm-db-h2.sql @@ -1,519 +1,552 @@ -CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE ( - ID INT AUTO_INCREMENT NOT NULL, - NAME VARCHAR(300) NULL DEFAULT NULL, - DEVICE_TYPE_META VARCHAR(20000) NULL DEFAULT NULL, - LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - PROVIDER_TENANT_ID INTEGER DEFAULT 0, - SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE ( - ID INTEGER auto_increment NOT NULL, - SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, - CERTIFICATE BLOB DEFAULT NULL, - TENANT_ID INTEGER DEFAULT 0, - USERNAME VARCHAR(500) DEFAULT NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_GROUP ( - ID INTEGER AUTO_INCREMENT NOT NULL, - GROUP_NAME VARCHAR(100) DEFAULT NULL, - STATUS VARCHAR(50) DEFAULT NULL, - DESCRIPTION TEXT DEFAULT NULL, - OWNER VARCHAR(255) DEFAULT NULL, - PARENT_PATH VARCHAR(255) DEFAULT '/', - TENANT_ID INTEGER DEFAULT 0, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_ROLE_GROUP_MAP ( - ID INTEGER AUTO_INCREMENT NOT NULL, - GROUP_ID INTEGER DEFAULT NULL, - ROLE VARCHAR(45) DEFAULT NULL, - TENANT_ID INTEGER DEFAULT 0, - PRIMARY KEY (ID), - CONSTRAINT fk_DM_ROLE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) - REFERENCES DM_GROUP (ID) ON DELETE CASCADE ON UPDATE CASCADE -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE ( - ID INTEGER auto_increment NOT NULL, - DESCRIPTION TEXT DEFAULT NULL, - NAME VARCHAR(100) DEFAULT NULL, - DEVICE_TYPE_ID INT(11) DEFAULT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, - LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, - TENANT_ID INTEGER DEFAULT 0, - PRIMARY KEY (ID), - CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID) - REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT uk_DM_DEVICE UNIQUE (NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES ( - DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, - PROPERTY_NAME VARCHAR(100) DEFAULT 0, - PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, - TENANT_ID VARCHAR(100), - PRIMARY KEY (DEVICE_TYPE_NAME, DEVICE_IDENTIFICATION, PROPERTY_NAME, TENANT_ID) -); - -CREATE TABLE IF NOT EXISTS GROUP_PROPERTIES ( - GROUP_ID INTEGER NOT NULL, - PROPERTY_NAME VARCHAR(100) DEFAULT 0, - PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, - TENANT_ID VARCHAR(100), - PRIMARY KEY (GROUP_ID, PROPERTY_NAME, TENANT_ID) -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP ( - ID INTEGER AUTO_INCREMENT 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 CASCADE ON UPDATE CASCADE, - CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) - REFERENCES DM_GROUP (ID) ON DELETE CASCADE ON UPDATE CASCADE -); - -CREATE TABLE IF NOT EXISTS DM_OPERATION ( - ID INTEGER AUTO_INCREMENT NOT NULL, - TYPE VARCHAR(50) NOT NULL, - CREATED_TIMESTAMP TIMESTAMP NOT NULL, - RECEIVED_TIMESTAMP TIMESTAMP NULL, - OPERATION_CODE VARCHAR(1000) NOT NULL, - INITIATED_BY VARCHAR(100) NULL, - OPERATION_DETAILS BLOB DEFAULT NULL, - ENABLED BOOLEAN NOT NULL DEFAULT FALSE, +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE +( + ID INT AUTO_INCREMENT NOT NULL, + NAME VARCHAR(300) NULL DEFAULT NULL, + DEVICE_TYPE_META VARCHAR(20000) NULL DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + PROVIDER_TENANT_ID INTEGER DEFAULT 0, + SHARED_WITH_ALL_TENANTS BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_CERTIFICATE +( + ID INTEGER auto_increment NOT NULL, + SERIAL_NUMBER VARCHAR(500) DEFAULT NULL, + CERTIFICATE BLOB DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + USERNAME VARCHAR(500) DEFAULT NULL, PRIMARY KEY (ID) ); -CREATE TABLE IF NOT EXISTS DM_ENROLMENT ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NOT NULL, - OWNER VARCHAR(255) NOT NULL, - OWNERSHIP VARCHAR(45) DEFAULT NULL, - STATUS VARCHAR(50) NULL, - IS_TRANSFERRED BOOLEAN NOT NULL DEFAULT FALSE, - DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, - DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, - TENANT_ID INT NOT NULL, +CREATE TABLE IF NOT EXISTS DM_GROUP +( + ID INTEGER AUTO_INCREMENT NOT NULL, + GROUP_NAME VARCHAR(100) DEFAULT NULL, + STATUS VARCHAR(50) DEFAULT NULL, + DESCRIPTION TEXT DEFAULT NULL, + OWNER VARCHAR(255) DEFAULT NULL, + PARENT_PATH VARCHAR(255) DEFAULT '/', + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_ROLE_GROUP_MAP +( + ID INTEGER AUTO_INCREMENT NOT NULL, + GROUP_ID INTEGER DEFAULT NULL, + ROLE VARCHAR(45) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID), - CONSTRAINT fk_dm_device_enrolment FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT uk_dm_device_enrolment UNIQUE (DEVICE_ID, OWNER, OWNERSHIP, TENANT_ID) + CONSTRAINT fk_DM_ROLE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) + REFERENCES DM_GROUP (ID) ON DELETE CASCADE ON UPDATE CASCADE +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE +( + ID INTEGER auto_increment NOT NULL, + DESCRIPTION TEXT DEFAULT NULL, + NAME VARCHAR(100) DEFAULT NULL, + DEVICE_TYPE_ID INT(11) DEFAULT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_DEVICE_TYPE2 FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT uk_DM_DEVICE UNIQUE (NAME, DEVICE_TYPE_ID, DEVICE_IDENTIFICATION, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_PROPERTIES +( + DEVICE_TYPE_NAME VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) NOT NULL, + PROPERTY_NAME VARCHAR(100) DEFAULT 0, + PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, + TENANT_ID VARCHAR(100), + PRIMARY KEY (DEVICE_TYPE_NAME, DEVICE_IDENTIFICATION, PROPERTY_NAME, TENANT_ID) ); -CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING ( - ID INTEGER AUTO_INCREMENT NOT NULL, - ENROLMENT_ID INTEGER NOT NULL, - OPERATION_ID INTEGER NOT NULL, - STATUS VARCHAR(50) NULL, - PUSH_NOTIFICATION_STATUS VARCHAR(50) NULL, - CREATED_TIMESTAMP INT NOT NULL, - UPDATED_TIMESTAMP INT NOT NULL, - OPERATION_CODE VARCHAR(50) NOT NULL, - INITIATED_BY VARCHAR(100) NULL, - TYPE VARCHAR(20) NOT NULL, +CREATE TABLE IF NOT EXISTS GROUP_PROPERTIES +( + GROUP_ID INTEGER NOT NULL, + PROPERTY_NAME VARCHAR(100) DEFAULT 0, + PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, + TENANT_ID VARCHAR(100), + PRIMARY KEY (GROUP_ID, PROPERTY_NAME, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_MAP +( + ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INTEGER DEFAULT NULL, - DEVICE_TYPE VARCHAR(300) NOT NULL, - DEVICE_IDENTIFICATION VARCHAR(300) 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 CASCADE ON UPDATE CASCADE, + CONSTRAINT fk_DM_DEVICE_GROUP_MAP_DM_GROUP2 FOREIGN KEY (GROUP_ID) + REFERENCES DM_GROUP (ID) ON DELETE CASCADE ON UPDATE CASCADE +); + +CREATE TABLE IF NOT EXISTS DM_OPERATION +( + ID INTEGER AUTO_INCREMENT NOT NULL, + TYPE VARCHAR(50) NOT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + RECEIVED_TIMESTAMP TIMESTAMP NULL, + OPERATION_CODE VARCHAR(1000) NOT NULL, + INITIATED_BY VARCHAR(100) NULL, + OPERATION_DETAILS BLOB DEFAULT NULL, + ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_ENROLMENT +( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OWNER VARCHAR(255) NOT NULL, + OWNERSHIP VARCHAR(45) DEFAULT NULL, + STATUS VARCHAR(50) NULL, + IS_TRANSFERRED BOOLEAN NOT NULL DEFAULT FALSE, + DATE_OF_ENROLMENT TIMESTAMP DEFAULT NULL, + DATE_OF_LAST_UPDATE TIMESTAMP DEFAULT NULL, + TENANT_ID INT 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, + CONSTRAINT uk_dm_device_enrolment UNIQUE (DEVICE_ID, OWNER, OWNERSHIP, TENANT_ID) +); + +CREATE TABLE IF NOT EXISTS DM_ENROLMENT_OP_MAPPING +( + ID INTEGER AUTO_INCREMENT NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + STATUS VARCHAR(50) NULL, + PUSH_NOTIFICATION_STATUS VARCHAR(50) NULL, + CREATED_TIMESTAMP INT NOT NULL, + UPDATED_TIMESTAMP INT NOT NULL, + OPERATION_CODE VARCHAR(50) NOT NULL, + INITIATED_BY VARCHAR(100) NULL, + TYPE VARCHAR(20) NOT NULL, + DEVICE_ID INTEGER DEFAULT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, + PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_mapping_device FOREIGN KEY (ENROLMENT_ID) REFERENCES - DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_mapping_operation FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE ( - ID INTEGER AUTO_INCREMENT NOT NULL, - ENROLMENT_ID INTEGER NOT NULL, - OPERATION_ID INTEGER NOT NULL, - EN_OP_MAP_ID INTEGER NOT NULL, - OPERATION_RESPONSE VARCHAR(1024) DEFAULT NULL , - IS_LARGE_RESPONSE BOOLEAN NOT NULL DEFAULT FALSE, - RECEIVED_TIMESTAMP TIMESTAMP NULL, +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE +( + ID INTEGER AUTO_INCREMENT NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NOT NULL, + EN_OP_MAP_ID INTEGER NOT NULL, + OPERATION_RESPONSE VARCHAR(1024) DEFAULT NULL, + IS_LARGE_RESPONSE BOOLEAN NOT NULL DEFAULT FALSE, + RECEIVED_TIMESTAMP TIMESTAMP NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device_operation_response_enrollment FOREIGN KEY (ENROLMENT_ID) REFERENCES - DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + DM_ENROLMENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_device_operation_response_operation FOREIGN KEY (OPERATION_ID) REFERENCES - DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_dm_en_op_map_response FOREIGN KEY (EN_OP_MAP_ID) REFERENCES - DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_OPERATION (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_dm_en_op_map_response FOREIGN KEY (EN_OP_MAP_ID) REFERENCES + DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -CREATE TABLE DM_DEVICE_OPERATION_RESPONSE_LARGE ( - ID INTEGER NOT NULL, - OPERATION_RESPONSE LONGBLOB DEFAULT NULL, - OPERATION_ID INTEGER NOT NULL, - EN_OP_MAP_ID INTEGER NOT NULL, - RECEIVED_TIMESTAMP TIMESTAMP NULL, +CREATE TABLE IF NOT EXISTS DM_DEVICE_OPERATION_RESPONSE_LARGE +( + ID INTEGER NOT NULL, + OPERATION_RESPONSE LONGBLOB DEFAULT NULL, + OPERATION_ID INTEGER NOT NULL, + EN_OP_MAP_ID INTEGER NOT NULL, + RECEIVED_TIMESTAMP TIMESTAMP NULL, DEVICE_IDENTIFICATION VARCHAR(300) DEFAULT NULL, CONSTRAINT fk_dm_device_operation_response_large_mapping FOREIGN KEY (ID) REFERENCES DM_DEVICE_OPERATION_RESPONSE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, - CONSTRAINT fk_dm_en_op_map_response_large FOREIGN KEY (EN_OP_MAP_ID) + CONSTRAINT fk_dm_en_op_map_response_large FOREIGN KEY (EN_OP_MAP_ID) REFERENCES DM_ENROLMENT_OP_MAPPING (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -- POLICY RELATED TABLES -- -CREATE TABLE IF NOT EXISTS DM_PROFILE ( - ID INT NOT NULL AUTO_INCREMENT , - PROFILE_NAME VARCHAR(45) NOT NULL , - TENANT_ID INT NOT NULL , - DEVICE_TYPE VARCHAR(300) NOT NULL , - CREATED_TIME DATETIME NOT NULL , - UPDATED_TIME DATETIME NOT NULL , - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_POLICY ( - ID INT(11) NOT NULL AUTO_INCREMENT , - NAME VARCHAR(45) DEFAULT NULL , - DESCRIPTION VARCHAR(1000) NULL, - PAYLOAD_VERSION VARCHAR (45) NULL, - TENANT_ID INT(11) NOT NULL , - PROFILE_ID INT(11) NOT NULL , - OWNERSHIP_TYPE VARCHAR(45) NULL, - COMPLIANCE VARCHAR(100) NULL, - PRIORITY INT NOT NULL, - ACTIVE INT(2) NOT NULL, - UPDATED INT(1) NULL, - POLICY_TYPE VARCHAR(45) NULL, - PRIMARY KEY (ID) , - CONSTRAINT FK_DM_PROFILE_DM_POLICY - FOREIGN KEY (PROFILE_ID ) - REFERENCES DM_PROFILE (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY ( - ID INT(11) NOT NULL AUTO_INCREMENT , - DEVICE_ID INT(11) NOT NULL , - ENROLMENT_ID INT(11) NOT NULL, - DEVICE BLOB NOT NULL, - POLICY_ID INT(11) NOT NULL , - PRIMARY KEY (ID) , - CONSTRAINT FK_POLICY_DEVICE_POLICY - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DEVICE_DEVICE_POLICY - FOREIGN KEY (DEVICE_ID ) - REFERENCES DM_DEVICE (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY ( - ID INT(11) NOT NULL , - DEVICE_TYPE VARCHAR(300) NOT NULL , - POLICY_ID INT(11) NOT NULL , - PRIMARY KEY (ID) , - CONSTRAINT FK_DEVICE_TYPE_POLICY - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES ( - ID INT(11) NOT NULL AUTO_INCREMENT, - PROFILE_ID INT(11) NOT NULL, - FEATURE_CODE VARCHAR(100) NOT NULL, - DEVICE_TYPE VARCHAR(300) NOT NULL, - TENANT_ID INT(11) NOT NULL , - CONTENT BLOB NULL DEFAULT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES - FOREIGN KEY (PROFILE_ID) - REFERENCES DM_PROFILE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION ( - ID INT(11) NOT NULL AUTO_INCREMENT, - ACTION_TYPE VARCHAR(45) NOT NULL, - CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, - POLICY_ID INT(11) NOT NULL, - FEATURE_ID INT(11) DEFAULT NULL, - IS_REACTIVE BOOLEAN NOT NULL DEFAULT FALSE, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY ( - ID INT(11) NOT NULL AUTO_INCREMENT , - ROLE_NAME VARCHAR(45) NOT NULL , - POLICY_ID INT(11) NOT NULL , - PRIMARY KEY (ID) , - CONSTRAINT FK_ROLE_POLICY_POLICY - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_USER_POLICY ( - ID INT NOT NULL AUTO_INCREMENT , - POLICY_ID INT NOT NULL , - USERNAME VARCHAR(45) NOT NULL , - PRIMARY KEY (ID) , - CONSTRAINT DM_POLICY_USER_POLICY - FOREIGN KEY (POLICY_ID ) - REFERENCES DM_POLICY (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED ( - ID INT NOT NULL AUTO_INCREMENT , - DEVICE_ID INT NOT NULL , - ENROLMENT_ID INT(11) NOT NULL, - POLICY_ID INT NOT NULL , - POLICY_CONTENT BLOB NULL , - TENANT_ID INT NOT NULL, - APPLIED TINYINT(1) NULL , - CREATED_TIME TIMESTAMP NULL , - UPDATED_TIME TIMESTAMP NULL , - APPLIED_TIME TIMESTAMP NULL , - PRIMARY KEY (ID) , - CONSTRAINT FK_DM_POLICY_DEVCIE_APPLIED - FOREIGN KEY (DEVICE_ID ) - REFERENCES DM_DEVICE (ID ) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_CRITERIA ( - ID INT NOT NULL AUTO_INCREMENT, - TENANT_ID INT NOT NULL, - NAME VARCHAR(50) NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA ( - ID INT NOT NULL AUTO_INCREMENT, - CRITERIA_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_CRITERIA_POLICY_CRITERIA - FOREIGN KEY (CRITERIA_ID) - REFERENCES DM_CRITERIA (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_POLICY_POLICY_CRITERIA - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES ( - ID INT NOT NULL AUTO_INCREMENT, - POLICY_CRITERION_ID INT NOT NULL, - PROP_KEY VARCHAR(45) NULL, - PROP_VALUE VARCHAR(100) NULL, - CONTENT BLOB NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_POLICY_CRITERIA_PROPERTIES - FOREIGN KEY (POLICY_CRITERION_ID) - REFERENCES DM_POLICY_CRITERIA (ID) - ON DELETE CASCADE - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS ( - ID INT NOT NULL AUTO_INCREMENT, - DEVICE_ID INT NOT NULL, - ENROLMENT_ID INT(11) NOT NULL, - POLICY_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - STATUS INT NULL, - LAST_SUCCESS_TIME TIMESTAMP NULL, - LAST_REQUESTED_TIME TIMESTAMP NULL, - LAST_FAILED_TIME TIMESTAMP NULL, - ATTEMPTS INT NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT ( - ID INT NOT NULL AUTO_INCREMENT, - POLICY_ID INT NOT NULL, - DEVICE_TYPE VARCHAR(300) NOT NULL , - TENANT_ID INT(11) NOT NULL, - PRIMARY KEY (ID) -); - -CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES ( - ID INT NOT NULL AUTO_INCREMENT, - COMPLIANCE_STATUS_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - FEATURE_CODE VARCHAR(100) NOT NULL, - STATUS INT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS - FOREIGN KEY (COMPLIANCE_STATUS_ID) - REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE TABLE IF NOT EXISTS DM_APPLICATION ( - ID INTEGER AUTO_INCREMENT NOT NULL, - NAME VARCHAR(150) NOT NULL, - APP_IDENTIFIER VARCHAR(150) NOT NULL, - PLATFORM VARCHAR(50) DEFAULT NULL, - CATEGORY VARCHAR(50) NULL, - VERSION VARCHAR(50) NULL, - TYPE VARCHAR(50) NULL, - LOCATION_URL VARCHAR(100) DEFAULT NULL, - IMAGE_URL VARCHAR(100) DEFAULT NULL, - APP_PROPERTIES BLOB NULL, - MEMORY_USAGE INTEGER(10) NULL, - IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, - DEVICE_ID INTEGER NOT NULL, - ENROLMENT_ID INTEGER NOT NULL, - TENANT_ID INTEGER NOT NULL, +CREATE TABLE IF NOT EXISTS DM_PROFILE +( + ID INT NOT NULL AUTO_INCREMENT, + PROFILE_NAME VARCHAR(45) NOT NULL, + TENANT_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + CREATED_TIME DATETIME NOT NULL, + UPDATED_TIME DATETIME NOT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_POLICY +( + ID INT(11) NOT NULL AUTO_INCREMENT, + NAME VARCHAR(45) DEFAULT NULL, + DESCRIPTION VARCHAR(1000) NULL, + PAYLOAD_VERSION VARCHAR(45) NULL, + TENANT_ID INT(11) NOT NULL, + PROFILE_ID INT(11) NOT NULL, + OWNERSHIP_TYPE VARCHAR(45) NULL, + COMPLIANCE VARCHAR(100) NULL, + PRIORITY INT NOT NULL, + ACTIVE INT(2) NOT NULL, + UPDATED INT(1) NULL, + POLICY_TYPE VARCHAR(45) NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_PROFILE_DM_POLICY + FOREIGN KEY (PROFILE_ID) + REFERENCES DM_PROFILE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY +( + ID INT(11) NOT NULL AUTO_INCREMENT, + DEVICE_ID INT(11) NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, + DEVICE BLOB NOT NULL, + POLICY_ID INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_DEVICE_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DEVICE_DEVICE_POLICY + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_POLICY +( + ID INT(11) NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + POLICY_ID INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DEVICE_TYPE_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_PROFILE_FEATURES +( + ID INT(11) NOT NULL AUTO_INCREMENT, + PROFILE_ID INT(11) NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + TENANT_ID INT(11) NOT NULL, + CONTENT BLOB NULL DEFAULT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_PROFILE_DM_POLICY_FEATURES + FOREIGN KEY (PROFILE_ID) + REFERENCES DM_PROFILE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_CORRECTIVE_ACTION +( + ID INT(11) NOT NULL AUTO_INCREMENT, + ACTION_TYPE VARCHAR(45) NOT NULL, + CORRECTIVE_POLICY_ID INT(11) DEFAULT NULL, + POLICY_ID INT(11) NOT NULL, + FEATURE_ID INT(11) DEFAULT NULL, + IS_REACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DM_POLICY_CORRECTIVE_ACTION + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_ROLE_POLICY +( + ID INT(11) NOT NULL AUTO_INCREMENT, + ROLE_NAME VARCHAR(45) NOT NULL, + POLICY_ID INT(11) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_ROLE_POLICY_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_USER_POLICY +( + ID INT NOT NULL AUTO_INCREMENT, + POLICY_ID INT NOT NULL, + USERNAME VARCHAR(45) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT DM_POLICY_USER_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_POLICY_APPLIED +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, + POLICY_ID INT NOT NULL, + POLICY_CONTENT BLOB NULL, + TENANT_ID INT NOT NULL, + APPLIED TINYINT(1) NULL, + CREATED_TIME TIMESTAMP NULL, + UPDATED_TIME TIMESTAMP NULL, + APPLIED_TIME TIMESTAMP NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_POLICY_DEVCIE_APPLIED + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_CRITERIA +( + ID INT NOT NULL AUTO_INCREMENT, + TENANT_ID INT NOT NULL, + NAME VARCHAR(50) NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA +( + ID INT NOT NULL AUTO_INCREMENT, + CRITERIA_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_CRITERIA_POLICY_CRITERIA + FOREIGN KEY (CRITERIA_ID) + REFERENCES DM_CRITERIA (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_POLICY_POLICY_CRITERIA + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_CRITERIA_PROPERTIES +( + ID INT NOT NULL AUTO_INCREMENT, + POLICY_CRITERION_ID INT NOT NULL, + PROP_KEY VARCHAR(45) NULL, + PROP_VALUE VARCHAR(100) NULL, + CONTENT BLOB NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_POLICY_CRITERIA_PROPERTIES + FOREIGN KEY (POLICY_CRITERION_ID) + REFERENCES DM_POLICY_CRITERIA (ID) + ON DELETE CASCADE + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_STATUS +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT(11) NOT NULL, + POLICY_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + STATUS INT NULL, + LAST_SUCCESS_TIME TIMESTAMP NULL, + LAST_REQUESTED_TIME TIMESTAMP NULL, + LAST_FAILED_TIME TIMESTAMP NULL, + ATTEMPTS INT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_CHANGE_MGT +( + ID INT NOT NULL AUTO_INCREMENT, + POLICY_ID INT NOT NULL, + DEVICE_TYPE VARCHAR(300) NOT NULL, + TENANT_ID INT(11) NOT NULL, + PRIMARY KEY (ID) +); + +CREATE TABLE IF NOT EXISTS DM_POLICY_COMPLIANCE_FEATURES +( + ID INT NOT NULL AUTO_INCREMENT, + COMPLIANCE_STATUS_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + FEATURE_CODE VARCHAR(100) NOT NULL, + STATUS INT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_COMPLIANCE_FEATURES_STATUS + FOREIGN KEY (COMPLIANCE_STATUS_ID) + REFERENCES DM_POLICY_COMPLIANCE_STATUS (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE TABLE IF NOT EXISTS DM_APPLICATION +( + ID INTEGER AUTO_INCREMENT NOT NULL, + NAME VARCHAR(150) NOT NULL, + APP_IDENTIFIER VARCHAR(150) NOT NULL, + PLATFORM VARCHAR(50) DEFAULT NULL, + CATEGORY VARCHAR(50) NULL, + VERSION VARCHAR(50) NULL, + TYPE VARCHAR(50) NULL, + LOCATION_URL VARCHAR(100) DEFAULT NULL, + IMAGE_URL VARCHAR(100) DEFAULT NULL, + APP_PROPERTIES BLOB NULL, + MEMORY_USAGE INTEGER(10) NULL, + IS_ACTIVE BOOLEAN NOT NULL DEFAULT FALSE, + DEVICE_ID INTEGER NOT NULL, + ENROLMENT_ID INTEGER NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_device FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, CONSTRAINT fk_dm_enrolement - FOREIGN KEY (ENROLMENT_ID) - REFERENCES DM_ENROLMENT (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION ); -- POLICY RELATED TABLES FINISHED -- -- NOTIFICATION TABLE -- -CREATE TABLE IF NOT EXISTS DM_NOTIFICATION ( - NOTIFICATION_ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INTEGER NOT NULL, - OPERATION_ID INTEGER NULL, - TENANT_ID INTEGER NOT NULL, - STATUS VARCHAR(10) NULL, - DESCRIPTION VARCHAR(1000) NULL, - LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, +CREATE TABLE IF NOT EXISTS DM_NOTIFICATION +( + NOTIFICATION_ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INTEGER NOT NULL, + OPERATION_ID INTEGER NULL, + TENANT_ID INTEGER NOT NULL, + STATUS VARCHAR(10) NULL, + DESCRIPTION VARCHAR(1000) NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (NOTIFICATION_ID), CONSTRAINT fk_dm_device_notification FOREIGN KEY (DEVICE_ID) REFERENCES - DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -- NOTIFICATION TABLE END -- -CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INT NULL, - ENROLMENT_ID INT NOT NULL, - KEY_FIELD VARCHAR(45) NULL, - VALUE_FIELD VARCHAR(1000) NULL, - PRIMARY KEY (ID), - CONSTRAINT DM_DEVICE_INFO_DEVICE - FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT DM_DEVICE_INFO_DEVICE_ENROLLMENT - FOREIGN KEY (ENROLMENT_ID) - REFERENCES DM_ENROLMENT (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE INDEX IDX_DM_DEVICE_INFO_DID_EID_KFIELD ON DM_DEVICE_INFO(DEVICE_ID, ENROLMENT_ID, KEY_FIELD); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INT NULL, - ENROLMENT_ID INT NOT NULL, - LATITUDE DOUBLE NULL, - LONGITUDE DOUBLE NULL, - STREET1 VARCHAR(255) NULL, - STREET2 VARCHAR(45) NULL, - CITY VARCHAR(45) NULL, - ZIP VARCHAR(10) NULL, - STATE VARCHAR(45) NULL, - COUNTRY VARCHAR(45) NULL, - GEO_HASH VARCHAR(45) NULL, - UPDATE_TIMESTAMP BIGINT(15) NOT NULL, - ALTITUDE DOUBLE NULL, - SPEED FLOAT NULL, - BEARING FLOAT NULL, - DISTANCE DOUBLE NULL, - PRIMARY KEY (ID), - CONSTRAINT DM_DEVICE_LOCATION_DEVICE - FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT DM_DEVICE_LOCATION_DM_ENROLLMENT - FOREIGN KEY (ENROLMENT_ID) - REFERENCES DM_ENROLMENT (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); -CREATE INDEX DM_DEVICE_LOCATION_GEO_hashx ON DM_DEVICE_LOCATION(GEO_HASH ASC); - -CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL ( - ID INT NOT NULL AUTO_INCREMENT, - DEVICE_ID INT NOT NULL, - ENROLMENT_ID INT NOT NULL, - DEVICE_MODEL VARCHAR(45) NULL, - VENDOR VARCHAR(45) NULL, - OS_VERSION VARCHAR(45) NULL, - OS_BUILD_DATE VARCHAR(100) NULL, - BATTERY_LEVEL DECIMAL(4) NULL, - INTERNAL_TOTAL_MEMORY DECIMAL(30,3) NULL, - INTERNAL_AVAILABLE_MEMORY DECIMAL(30,3) NULL, - EXTERNAL_TOTAL_MEMORY DECIMAL(30,3) NULL, - EXTERNAL_AVAILABLE_MEMORY DECIMAL(30,3) NULL, - CONNECTION_TYPE VARCHAR(50) NULL, - SSID VARCHAR(45) NULL, - CPU_USAGE DECIMAL(5) NULL, - TOTAL_RAM_MEMORY DECIMAL(30,3) NULL, - AVAILABLE_RAM_MEMORY DECIMAL(30,3) NULL, - PLUGGED_IN INT(1) NULL, - UPDATE_TIMESTAMP BIGINT(15) NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE - FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION, - CONSTRAINT FK_DM_ENROLMENT_DEVICE_DETAILS - FOREIGN KEY (ENROLMENT_ID) - REFERENCES DM_ENROLMENT (ID) - ON DELETE NO ACTION - ON UPDATE NO ACTION -); - -CREATE INDEX IDX_DM_DEVICE_DETAIL_DID_EID ON DM_DEVICE_DETAIL(DEVICE_ID, ENROLMENT_ID); +CREATE TABLE IF NOT EXISTS DM_DEVICE_INFO +( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INT NULL, + ENROLMENT_ID INT NOT NULL, + KEY_FIELD VARCHAR(45) NULL, + VALUE_FIELD VARCHAR(1000) NULL, + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_INFO_DEVICE + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT DM_DEVICE_INFO_DEVICE_ENROLLMENT + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE INDEX IDX_DM_DEVICE_INFO_DID_EID_KFIELD ON DM_DEVICE_INFO (DEVICE_ID, ENROLMENT_ID, KEY_FIELD); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_LOCATION +( + ID INTEGER AUTO_INCREMENT NOT NULL, + DEVICE_ID INT NULL, + ENROLMENT_ID INT NOT NULL, + LATITUDE DOUBLE NULL, + LONGITUDE DOUBLE NULL, + STREET1 VARCHAR(255) NULL, + STREET2 VARCHAR(45) NULL, + CITY VARCHAR(45) NULL, + ZIP VARCHAR(10) NULL, + STATE VARCHAR(45) NULL, + COUNTRY VARCHAR(45) NULL, + GEO_HASH VARCHAR(45) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, + ALTITUDE DOUBLE NULL, + SPEED FLOAT NULL, + BEARING FLOAT NULL, + DISTANCE DOUBLE NULL, + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_LOCATION_DEVICE + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT DM_DEVICE_LOCATION_DM_ENROLLMENT + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); +CREATE INDEX DM_DEVICE_LOCATION_GEO_hashx ON DM_DEVICE_LOCATION (GEO_HASH ASC); + +CREATE TABLE IF NOT EXISTS DM_DEVICE_DETAIL +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT NOT NULL, + ENROLMENT_ID INT NOT NULL, + DEVICE_MODEL VARCHAR(45) NULL, + VENDOR VARCHAR(45) NULL, + OS_VERSION VARCHAR(45) NULL, + OS_BUILD_DATE VARCHAR(100) NULL, + BATTERY_LEVEL DECIMAL(4) NULL, + INTERNAL_TOTAL_MEMORY DECIMAL(30, 3) NULL, + INTERNAL_AVAILABLE_MEMORY DECIMAL(30, 3) NULL, + EXTERNAL_TOTAL_MEMORY DECIMAL(30, 3) NULL, + EXTERNAL_AVAILABLE_MEMORY DECIMAL(30, 3) NULL, + CONNECTION_TYPE VARCHAR(50) NULL, + SSID VARCHAR(45) NULL, + CPU_USAGE DECIMAL(5) NULL, + TOTAL_RAM_MEMORY DECIMAL(30, 3) NULL, + AVAILABLE_RAM_MEMORY DECIMAL(30, 3) NULL, + PLUGGED_IN INT(1) NULL, + UPDATE_TIMESTAMP BIGINT(15) NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_DEVICE_DETAILS_DEVICE + FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION, + CONSTRAINT FK_DM_ENROLMENT_DEVICE_DETAILS + FOREIGN KEY (ENROLMENT_ID) + REFERENCES DM_ENROLMENT (ID) + ON DELETE NO ACTION + ON UPDATE NO ACTION +); + +CREATE INDEX IDX_DM_DEVICE_DETAIL_DID_EID ON DM_DEVICE_DETAIL (DEVICE_ID, ENROLMENT_ID); CREATE TABLE IF NOT EXISTS DM_DEVICE_HISTORY_LAST_SEVEN_DAYS ( ID INTEGER AUTO_INCREMENT NOT NULL, DEVICE_ID INT NOT NULL, DEVICE_ID_NAME VARCHAR(255) NOT NULL, - TENANT_ID INT NOT NULL, + TENANT_ID INT NOT NULL, DEVICE_TYPE_NAME VARCHAR(45) NOT NULL, LATITUDE DOUBLE NULL, LONGITUDE DOUBLE NULL, @@ -528,220 +561,232 @@ CREATE TABLE IF NOT EXISTS DM_DEVICE_HISTORY_LAST_SEVEN_DAYS ); -- POLICY AND DEVICE GROUP MAPPING -- -CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY ( - ID INT NOT NULL AUTO_INCREMENT, - DEVICE_GROUP_ID INT NOT NULL, - POLICY_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - PRIMARY KEY (ID), - CONSTRAINT FK_DM_DEVICE_GROUP_POLICY - FOREIGN KEY (DEVICE_GROUP_ID) - REFERENCES DM_GROUP (ID) - ON DELETE CASCADE - ON UPDATE CASCADE , - CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY - FOREIGN KEY (POLICY_ID) - REFERENCES DM_POLICY (ID) - ON DELETE CASCADE - ON UPDATE CASCADE +CREATE TABLE IF NOT EXISTS DM_DEVICE_GROUP_POLICY +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_GROUP_ID INT NOT NULL, + POLICY_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT FK_DM_DEVICE_GROUP_POLICY + FOREIGN KEY (DEVICE_GROUP_ID) + REFERENCES DM_GROUP (ID) + ON DELETE CASCADE + ON UPDATE CASCADE, + CONSTRAINT FK_DM_DEVICE_GROUP_DM_POLICY + FOREIGN KEY (POLICY_ID) + REFERENCES DM_POLICY (ID) + ON DELETE CASCADE + ON UPDATE CASCADE ); -- END OF POLICY AND DEVICE GROUP MAPPING -- -CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_PLATFORM ( - ID INT NOT NULL AUTO_INCREMENT, - DEVICE_TYPE_ID INT NULL DEFAULT 0, - VERSION_NAME VARCHAR(100) NULL, - VERSION_STATUS VARCHAR(100) NULL DEFAULT 'ACTIVE', - PRIMARY KEY (ID), - CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING - FOREIGN KEY (DEVICE_TYPE_ID) - REFERENCES DM_DEVICE_TYPE (ID) ON DELETE CASCADE ON UPDATE CASCADE, - CONSTRAINT device_type_version_uk - UNIQUE ( - DEVICE_TYPE_ID, - VERSION_NAME - ) +CREATE TABLE IF NOT EXISTS DM_DEVICE_TYPE_PLATFORM +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_TYPE_ID INT NULL DEFAULT 0, + VERSION_NAME VARCHAR(100) NULL, + VERSION_STATUS VARCHAR(100) NULL DEFAULT 'ACTIVE', + PRIMARY KEY (ID), + CONSTRAINT DM_DEVICE_TYPE_DM_DEVICE_TYPE_PLATFORM_MAPPING + FOREIGN KEY (DEVICE_TYPE_ID) + REFERENCES DM_DEVICE_TYPE (ID) ON DELETE CASCADE ON UPDATE CASCADE, + CONSTRAINT device_type_version_uk + UNIQUE ( + DEVICE_TYPE_ID, + VERSION_NAME + ) ); -- METADATA TABLE -- -CREATE TABLE IF NOT EXISTS DM_METADATA ( - METADATA_ID INT AUTO_INCREMENT NOT NULL, - DATA_TYPE VARCHAR(16) NOT NULL, - METADATA_KEY VARCHAR(128) NOT NULL, - METADATA_VALUE VARCHAR(8000) NOT NULL, - TENANT_ID INTEGER NOT NULL, +CREATE TABLE IF NOT EXISTS DM_METADATA +( + METADATA_ID INT AUTO_INCREMENT NOT NULL, + DATA_TYPE VARCHAR(16) NOT NULL, + METADATA_KEY VARCHAR(128) NOT NULL, + METADATA_VALUE VARCHAR(8000) NOT NULL, + TENANT_ID INTEGER NOT NULL, PRIMARY KEY (METADATA_ID), CONSTRAINT METADATA_KEY_TENANT_ID UNIQUE (METADATA_KEY, TENANT_ID) ); -- END OF METADATA TABLE -- -- DM_OTP_DATA TABLE -- -CREATE TABLE IF NOT EXISTS DM_OTP_DATA ( - ID INT AUTO_INCREMENT NOT NULL, - OTP_TOKEN VARCHAR(100) NOT NULL, - TENANT_ID INT NOT NULL, - USERNAME VARCHAR(500) DEFAULT NOT NULL, - EMAIL VARCHAR(100) NOT NULL, - EMAIL_TYPE VARCHAR(20) NOT NULL, - META_INFO VARCHAR(20000) NOT NULL, - CREATED_AT TIMESTAMP NOT NULL, - EXPIRY_TIME INT NOT NULL DEFAULT 3600, - IS_EXPIRED BOOLEAN DEFAULT false, - PRIMARY KEY (ID) +CREATE TABLE IF NOT EXISTS DM_OTP_DATA +( + ID INT AUTO_INCREMENT NOT NULL, + OTP_TOKEN VARCHAR(100) NOT NULL, + TENANT_ID INT NOT NULL, + USERNAME VARCHAR(500) DEFAULT NOT NULL, + EMAIL VARCHAR(100) NOT NULL, + EMAIL_TYPE VARCHAR(20) NOT NULL, + META_INFO VARCHAR(20000) NOT NULL, + CREATED_AT TIMESTAMP NOT NULL, + EXPIRY_TIME INT NOT NULL DEFAULT 3600, + IS_EXPIRED BOOLEAN DEFAULT false, + PRIMARY KEY (ID) ); -- END OF DM_OTP_DATA TABLE -- -- DASHBOARD RELATED VIEWS -- CREATE VIEW POLICY_COMPLIANCE_INFO AS -SELECT -DEVICE_INFO.DEVICE_ID, -DEVICE_INFO.DEVICE_IDENTIFICATION, -DEVICE_INFO.PLATFORM, -DEVICE_INFO.OWNERSHIP, -DEVICE_INFO.CONNECTIVITY_STATUS, -IFNULL(DEVICE_WITH_POLICY_INFO.POLICY_ID, -1) AS POLICY_ID, -IFNULL(DEVICE_WITH_POLICY_INFO.IS_COMPLIANT, -1) AS IS_COMPLIANT, -DEVICE_INFO.TENANT_ID -FROM -(SELECT -DM_DEVICE.ID AS DEVICE_ID, -DM_DEVICE.DEVICE_IDENTIFICATION, -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 +SELECT DEVICE_INFO.DEVICE_ID, + DEVICE_INFO.DEVICE_IDENTIFICATION, + DEVICE_INFO.PLATFORM, + DEVICE_INFO.OWNERSHIP, + DEVICE_INFO.CONNECTIVITY_STATUS, + IFNULL(DEVICE_WITH_POLICY_INFO.POLICY_ID, -1) AS POLICY_ID, + IFNULL(DEVICE_WITH_POLICY_INFO.IS_COMPLIANT, -1) AS IS_COMPLIANT, + DEVICE_INFO.TENANT_ID +FROM (SELECT DM_DEVICE.ID AS DEVICE_ID, + DM_DEVICE.DEVICE_IDENTIFICATION, + 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; CREATE VIEW FEATURE_NON_COMPLIANCE_INFO AS -SELECT -DM_DEVICE.ID AS DEVICE_ID, -DM_DEVICE.DEVICE_IDENTIFICATION, -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 +SELECT DM_DEVICE.ID AS DEVICE_ID, + DM_DEVICE.DEVICE_IDENTIFICATION, + 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; -- END OF DASHBOARD RELATED VIEWS -- -- DM_GEOFENCE TABLE-- -CREATE TABLE IF NOT EXISTS DM_GEOFENCE ( - ID INT NOT NULL AUTO_INCREMENT, - FENCE_NAME VARCHAR(255) NOT NULL, - DESCRIPTION TEXT DEFAULT NULL, - LATITUDE DOUBLE DEFAULT NULL, - LONGITUDE DOUBLE DEFAULT NULL, - RADIUS DOUBLE DEFAULT NULL, - GEO_JSON TEXT DEFAULT NULL, - FENCE_SHAPE VARCHAR(100) DEFAULT NULL, - CREATED_TIMESTAMP TIMESTAMP NOT NULL, - OWNER VARCHAR(255) NOT NULL, - TENANT_ID INTEGER DEFAULT 0, +CREATE TABLE IF NOT EXISTS DM_GEOFENCE +( + ID INT NOT NULL AUTO_INCREMENT, + FENCE_NAME VARCHAR(255) NOT NULL, + DESCRIPTION TEXT DEFAULT NULL, + LATITUDE DOUBLE DEFAULT NULL, + LONGITUDE DOUBLE DEFAULT NULL, + RADIUS DOUBLE DEFAULT NULL, + GEO_JSON TEXT DEFAULT NULL, + FENCE_SHAPE VARCHAR(100) DEFAULT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + OWNER VARCHAR(255) NOT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); -- END OF DM_GEOFENCE TABLE-- -- DM_GEOFENCE_GROUP_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_GEOFENCE_GROUP_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE IF NOT EXISTS DM_GEOFENCE_GROUP_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, FENCE_ID INT NOT NULL, GROUP_ID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_geofence_group_mapping_geofence FOREIGN KEY (FENCE_ID) REFERENCES - DM_GEOFENCE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + DM_GEOFENCE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_geofence_group_mapping_group FOREIGN KEY (GROUP_ID) REFERENCES - DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- -- DM_DEVICE_EVENT TABLE -- -CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT ( - ID INT NOT NULL AUTO_INCREMENT, - EVENT_SOURCE VARCHAR(100) NOT NULL, - EVENT_LOGIC VARCHAR(100) NOT NULL, - ACTIONS TEXT DEFAULT NULL, - CREATED_TIMESTAMP TIMESTAMP NOT NULL, - TENANT_ID INTEGER DEFAULT 0, +CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT +( + ID INT NOT NULL AUTO_INCREMENT, + EVENT_SOURCE VARCHAR(100) NOT NULL, + EVENT_LOGIC VARCHAR(100) NOT NULL, + ACTIONS TEXT DEFAULT NULL, + CREATED_TIMESTAMP TIMESTAMP NOT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (ID) ); -- END OF DM_DEVICE_EVENT TABLE -- -- DM_DEVICE_EVENT_GROUP_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT_GROUP_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE IF NOT EXISTS DM_DEVICE_EVENT_GROUP_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, EVENT_ID INT NOT NULL, GROUP_ID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_event_group_mapping_event FOREIGN KEY (EVENT_ID) REFERENCES - DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_event_group_mapping_group FOREIGN KEY (GROUP_ID) REFERENCES - DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_GROUP (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -- END OF DM_DEVICE_EVENT_GROUP_MAPPING TABLE-- -- DM_GEOFENCE_GROUP_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_GEOFENCE_EVENT_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE IF NOT EXISTS DM_GEOFENCE_EVENT_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, FENCE_ID INT NOT NULL, EVENT_ID INT NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_dm_geofence_event_mapping_geofence FOREIGN KEY (FENCE_ID) REFERENCES - DM_GEOFENCE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + DM_GEOFENCE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_dm_geofence_event_mapping_event FOREIGN KEY (EVENT_ID) REFERENCES - DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION + DM_DEVICE_EVENT (ID) ON DELETE NO ACTION ON UPDATE NO ACTION ); -- END OF DM_GEOFENCE_GROUP_MAPPING TABLE-- -- DM_EXT_GROUP_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_EXT_GROUP_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE IF NOT EXISTS DM_EXT_GROUP_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, TRACCAR_GROUP_ID INT DEFAULT 0, - GROUP_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - STATUS INT DEFAULT 0, + GROUP_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + STATUS INT DEFAULT 0, PRIMARY KEY (ID) ); -- END OF DM_EXT_GROUP_MAPPING TABLE-- -- END OF DM_EXT_DEVICE_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_EXT_DEVICE_MAPPING ( - ID INT NOT NULL AUTO_INCREMENT, +CREATE TABLE IF NOT EXISTS DM_EXT_DEVICE_MAPPING +( + ID INT NOT NULL AUTO_INCREMENT, TRACCAR_DEVICE_ID INT DEFAULT 0, - DEVICE_ID INT NOT NULL, - TENANT_ID INT NOT NULL, - STATUS INT DEFAULT 0, + DEVICE_ID INT NOT NULL, + TENANT_ID INT NOT NULL, + STATUS INT DEFAULT 0, PRIMARY KEY (ID) ); /* @@ -765,56 +810,61 @@ CREATE TABLE IF NOT EXISTS DM_EXT_DEVICE_MAPPING ( -- END OF DM_EXT_DEVICE_MAPPING TABLE-- -- END OF DM_EXT_PERMISSION_MAPPING TABLE-- -CREATE TABLE IF NOT EXISTS DM_EXT_PERMISSION_MAPPING ( +CREATE TABLE IF NOT EXISTS DM_EXT_PERMISSION_MAPPING +( TRACCAR_DEVICE_ID INT DEFAULT 0, - TRACCAR_USER_ID INT DEFAULT 0 + TRACCAR_USER_ID INT DEFAULT 0 ); -- END OF DM_EXT_PERMISSION_MAPPING TABLE-- -- DYNAMIC TASK TABLES-- -CREATE TABLE IF NOT EXISTS DYNAMIC_TASK ( +CREATE TABLE IF NOT EXISTS DYNAMIC_TASK +( DYNAMIC_TASK_ID INTEGER AUTO_INCREMENT NOT NULL, - NAME VARCHAR(300) DEFAULT NULL , - CRON VARCHAR(8000) DEFAULT NULL, - IS_ENABLED BOOLEAN NOT NULL DEFAULT FALSE, - TASK_CLASS_NAME VARCHAR(8000) DEFAULT NULL, - TENANT_ID INTEGER DEFAULT 0, + NAME VARCHAR(300) DEFAULT NULL, + CRON VARCHAR(8000) DEFAULT NULL, + IS_ENABLED BOOLEAN NOT NULL DEFAULT FALSE, + TASK_CLASS_NAME VARCHAR(8000) DEFAULT NULL, + TENANT_ID INTEGER DEFAULT 0, PRIMARY KEY (DYNAMIC_TASK_ID) ); -CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES ( +CREATE TABLE IF NOT EXISTS DYNAMIC_TASK_PROPERTIES +( DYNAMIC_TASK_ID INTEGER NOT NULL, - PROPERTY_NAME VARCHAR(100) DEFAULT 0, - PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, - TENANT_ID VARCHAR(100), + PROPERTY_NAME VARCHAR(100) DEFAULT 0, + PROPERTY_VALUE VARCHAR(100) DEFAULT NULL, + TENANT_ID VARCHAR(100), PRIMARY KEY (DYNAMIC_TASK_ID, PROPERTY_NAME, TENANT_ID), CONSTRAINT FK_DYNAMIC_TASK_TASK_PROPERTIES FOREIGN KEY (DYNAMIC_TASK_ID) REFERENCES - DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE + DYNAMIC_TASK (DYNAMIC_TASK_ID) ON DELETE CASCADE ON UPDATE CASCADE ); -- END OF DYNAMIC TASK TABLE-- -- DM_DEVICE_SUB_TYPE TABLE-- -CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE ( - TENANT_ID INT DEFAULT 0, - SUB_TYPE_ID VARCHAR(45) NOT NULL, - DEVICE_TYPE VARCHAR(25) NOT NULL, - SUB_TYPE_NAME VARCHAR(45) NOT NULL, - TYPE_DEFINITION TEXT NOT NULL, - PRIMARY KEY (SUB_TYPE_ID,DEVICE_TYPE) +CREATE TABLE IF NOT EXISTS DM_DEVICE_SUB_TYPE +( + TENANT_ID INT DEFAULT 0, + SUB_TYPE_ID VARCHAR(45) NOT NULL, + DEVICE_TYPE VARCHAR(25) NOT NULL, + SUB_TYPE_NAME VARCHAR(45) NOT NULL, + TYPE_DEFINITION TEXT NOT NULL, + PRIMARY KEY (SUB_TYPE_ID, DEVICE_TYPE) ); -- END OF DM_DEVICE_SUB_TYPE TABLE-- -- DM_DEVICE_ORGANIZATION TABLE-- -CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INTEGER AUTO_INCREMENT NOT NULL, - DEVICE_ID INT(11) NOT NULL, - PARENT_DEVICE_ID INT(11) DEFAULT NULL, +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION +( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT(11) NOT NULL, + PARENT_DEVICE_ID INT(11) DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) ); -- END OF DM_DEVICE_ORGANIZATION TABLE-- \ No newline at end of file diff --git a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql index afe2fe52b5..e6ac1a1333 100644 --- a/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql +++ b/components/device-mgt-extensions/io.entgra.device.mgt.core.device.mgt.extensions.device.organization/src/test/resources/carbon-home/dbscripts/h2.sql @@ -18,30 +18,29 @@ -- Table `DM_DEVICE_ORGANIZATION` -- ----------------------------------------------------- + -- DM_DEVICE_ORGANIZATION TABLE-- CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( - ID INTEGER auto_increment NOT NULL, - DEVICE_ID INT(11) NOT NULL, - PARENT_DEVICE_ID INT(11) DEFAULT NULL, + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT(11) NOT NULL, + PARENT_DEVICE_ID INT(11) DEFAULT NULL, LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, PRIMARY KEY (ID), CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) - REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) ); + -- END OF DM_DEVICE_ORGANIZATION TABLE-- -- ----------------------------------------------------- -- Sample data for test cases -- ----------------------------------------------------- -INSERT INTO DM_DEVICE_TYPE (ID,NAME,DEVICE_TYPE_META,LAST_UPDATED_TIMESTAMP,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS) -VALUES (1,'METER','meter',CURRENT_TIMESTAMP(),1,true); -INSERT INTO DM_DEVICE (ID,DESCRIPTION,NAME,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,LAST_UPDATED_TIMESTAMP,TENANT_ID) VALUES -(1,'test device 1','Meter 01',1,'0001',CURRENT_TIMESTAMP(),1), -(2,'test device 2','Meter 02',1,'0002',CURRENT_TIMESTAMP(),1), -(3,'test device 3','Meter 03',1,'0003',CURRENT_TIMESTAMP(),1), -(4,'test device 4','Meter 04',1,'0004',CURRENT_TIMESTAMP(),1), --- INSERT INTO DM_DEVICE_ORGANIZATION (DEVICE_ID, PARENT_DEVICE_ID, LAST_UPDATED_TIMESTAMP) VALUES --- (2,1,CURRENT_TIMESTAMP()), --- (3,2,CURRENT_TIMESTAMP()); +INSERT INTO DM_DEVICE_TYPE (NAME,DEVICE_TYPE_META,LAST_UPDATED_TIMESTAMP,PROVIDER_TENANT_ID,SHARED_WITH_ALL_TENANTS) +VALUES ('METER','meter',CURRENT_TIMESTAMP(),1,true); +INSERT INTO DM_DEVICE (DESCRIPTION,NAME,DEVICE_TYPE_ID,DEVICE_IDENTIFICATION,LAST_UPDATED_TIMESTAMP,TENANT_ID) VALUES +('test device 1','Meter_01',1,'0001',CURRENT_TIMESTAMP(),1), +('test device 2','Meter_02',1,'0002',CURRENT_TIMESTAMP(),1), +('test device 3','Meter_03',1,'0003',CURRENT_TIMESTAMP(),1), +('test device 4','Meter_04',1,'0004',CURRENT_TIMESTAMP(),1); diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql index f261a3b49e..e2b16b3a05 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/h2.sql @@ -839,3 +839,18 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( ); -- END OF SUB_OPERATION_TEMPLATE TABLE-- + +-- DM_DEVICE_ORGANIZATION TABLE-- +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT(11) NOT NULL, + PARENT_DEVICE_ID INT(11) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) + ); +-- END OF DM_DEVICE_ORGANIZATION TABLE-- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql index 1c0a4b75a4..7c9531153a 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mssql.sql @@ -911,4 +911,19 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( CONSTRAINT fk_SUB_OPERATION_TEMPLATE_DM_DEVICE_SUB_TYPE FOREIGN KEY (SUB_TYPE_ID, DEVICE_TYPE) REFERENCES DM_DEVICE_SUB_TYPE (SUB_TYPE_ID, DEVICE_TYPE) ); --- END SUB_OPERATION_TEMPLATE TABLE-- \ No newline at end of file +-- END SUB_OPERATION_TEMPLATE TABLE-- + +-- DM_DEVICE_ORGANIZATION TABLE-- +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( + ID INT NOT NULL IDENTITY(1,1), + DEVICE_ID INT NOT NULL, + PARENT_DEVICE_ID INT DEFAULT NULL, + LAST_UPDATED_TIMESTAMP BIGINT NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) + ); +-- END OF DM_DEVICE_ORGANIZATION TABLE-- \ No newline at end of file diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql index 1ea355fb81..1423e1513c 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/mysql.sql @@ -907,3 +907,18 @@ CREATE TABLE SUB_OPERATION_TEMPLATE ( ) ENGINE=InnoDB; -- END OF SUB_OPERATION_TEMPLATE TABLE-- + +-- DM_DEVICE_ORGANIZATION TABLE-- +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( + ID INT NOT NULL AUTO_INCREMENT, + DEVICE_ID INT(11) NOT NULL, + PARENT_DEVICE_ID INT(11) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) + ); +-- END OF DM_DEVICE_ORGANIZATION TABLE-- diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql index f19645cdb7..1e25663898 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/oracle.sql @@ -1191,5 +1191,38 @@ ALTER TABLE SUB_OPERATION_TEMPLATE -- Generate ID using sequence and trigger CREATE SEQUENCE SUB_OPERATION_TEMPLATE_seq START WITH 1 INCREMENT BY 1; - +/ -- END OF SUB_OPERATION_TEMPLATE TABLE-- + +-- DM_DEVICE_ORGANIZATION TABLE-- +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( + ID NUMBER(10) NOT NULL, + DEVICE_ID NUMBER(10) NOT NULL, + PARENT_DEVICE_ID NUMBER(10) DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL, + CONSTRAINT PK_DM_DEVICE_ORGANIZATION PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) + ); +/ +-- Generate ID using sequence and trigger +CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq START WITH 1 INCREMENT BY 1 NOCACHE +/ + +CREATE OR REPLACE TRIGGER DM_DEVICE_ORGANIZATION_seq_tr +BEFORE INSERT +ON DM_DEVICE_ORGANIZATION +REFERENCING NEW AS NEW +FOR EACH ROW +WHEN (NEW.ID IS NULL) +BEGIN +SELECT DM_DEVICE_ORGANIZATION_seq.NEXTVAL +INTO :NEW.ID +FROM DUAL; +END; +/ +-- END OF DM_DEVICE_ORGANIZATION TABLE-- +) diff --git a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql index 51361a90d3..44583eaebc 100644 --- a/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql +++ b/features/device-mgt/io.entgra.device.mgt.core.device.mgt.basics.feature/src/main/resources/dbscripts/cdm/postgresql.sql @@ -846,3 +846,19 @@ CREATE TABLE DM_DEVICE_CERTIFICATE ( PRIMARY KEY (ID) ) +-- DM_DEVICE_ORGANIZATION TABLE-- +CREATE SEQUENCE DM_DEVICE_ORGANIZATION_seq; + +CREATE TABLE IF NOT EXISTS DM_DEVICE_ORGANIZATION ( + ID INTEGER DEFAULT NEXTVAL ('DM_DEVICE_ORGANIZATION_seq') NOT NULL, + DEVICE_ID INTEGER NOT NULL, + PARENT_DEVICE_ID INTEGER DEFAULT NULL, + LAST_UPDATED_TIMESTAMP TIMESTAMP(0) NOT NULL NOT NULL, + PRIMARY KEY (ID), + CONSTRAINT fk_DM_DEVICE_DM_ID FOREIGN KEY (DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT fk_DM_DEVICE_DM_ID2 FOREIGN KEY (PARENT_DEVICE_ID) + REFERENCES DM_DEVICE (ID) ON DELETE NO ACTION ON UPDATE NO ACTION, + CONSTRAINT CHILD_PARENT_COMP_KEY UNIQUE (DEVICE_ID, PARENT_DEVICE_ID) + ); +-- END OF DM_DEVICE_ORGANIZATION TABLE--