﻿-- Generated by SQLite Compare utility

-- The script can be used to migrate database
-- D:\Desktop\iotnode.db schema
-- to the schema of database
-- D:\1.0\ZHXY\projects\IoTNode\iotnode.db



CREATE TABLE iot_NodeCategory
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_NodeCategory PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NULL,
    Number TEXT NULL,
    Template TEXT NULL,
    DisplayOrder INTEGER NOT NULL
);
CREATE UNIQUE INDEX IX_iot_NodeCategory_Name
ON iot_NodeCategory
(
    Name
);

CREATE TABLE iot_NodeCategoryNode
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_NodeCategoryNode PRIMARY KEY,
    IsDeleted TEXT NULL,
    CategoryId TEXT NOT NULL,
    NodeId TEXT NOT NULL,
    CONSTRAINT FK_iot_NodeCategoryNode_iot_NodeCategory_CategoryId FOREIGN KEY (CategoryId) REFERENCES iot_NodeCategory(Id) ON DELETE CASCADE,
    CONSTRAINT FK_iot_NodeCategoryNode_iot_Node_NodeId FOREIGN KEY (NodeId) REFERENCES iot_Node(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_NodeCategoryNode_NodeId
ON iot_NodeCategoryNode
(
    NodeId
);
CREATE UNIQUE INDEX IX_iot_NodeCategoryNode_CategoryId_NodeId
ON iot_NodeCategoryNode
(
    CategoryId,
    NodeId
);

CREATE TABLE iot_Organ
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_Organ PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NULL,
    Number TEXT NOT NULL,
    Image TEXT NULL
);
CREATE UNIQUE INDEX IX_iot_Organ_Number
ON iot_Organ
(
    Number
);

CREATE TABLE iot_OrganNode
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganNode PRIMARY KEY,
    IsDeleted TEXT NULL,
    OrganId TEXT NOT NULL,
    NodeId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganNode_iot_Node_NodeId FOREIGN KEY (NodeId) REFERENCES iot_Node(Id) ON DELETE CASCADE,
    CONSTRAINT FK_iot_OrganNode_iot_Organ_OrganId FOREIGN KEY (OrganId) REFERENCES iot_Organ(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganNode_NodeId
ON iot_OrganNode
(
    NodeId
);
CREATE UNIQUE INDEX IX_iot_OrganNode_OrganId_NodeId
ON iot_OrganNode
(
    OrganId,
    NodeId
);

CREATE TABLE iot_OrganScene
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganScene PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NOT NULL,
    Image TEXT NOT NULL,
    Hidden INTEGER NOT NULL,
    DisplayOrder INTEGER NOT NULL,
    OrganId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganScene_iot_Organ_OrganId FOREIGN KEY (OrganId) REFERENCES iot_Organ(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganScene_OrganId
ON iot_OrganScene
(
    OrganId
);

CREATE TABLE iot_OrganSceneCommand
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganSceneCommand PRIMARY KEY,
    IsDeleted TEXT NULL,
    OrganSceneId TEXT NOT NULL,
    CommandId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganSceneCommand_iot_Command_CommandId FOREIGN KEY (CommandId) REFERENCES iot_Command(Id) ON DELETE CASCADE,
    CONSTRAINT FK_iot_OrganSceneCommand_iot_OrganScene_OrganSceneId FOREIGN KEY (OrganSceneId) REFERENCES iot_OrganScene(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganSceneCommand_CommandId
ON iot_OrganSceneCommand
(
    CommandId
);
CREATE INDEX IX_iot_OrganSceneCommand_OrganSceneId
ON iot_OrganSceneCommand
(
    OrganSceneId
);

CREATE TABLE iot_OrganSceneTigger
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganSceneTigger PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NULL,
    Condition TEXT NULL,
    Disabled INTEGER NOT NULL,
    DataId TEXT NOT NULL,
    OrganSceneId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganSceneTigger_iot_Data_DataId FOREIGN KEY (DataId) REFERENCES iot_Data(Id) ON DELETE CASCADE,
    CONSTRAINT FK_iot_OrganSceneTigger_iot_OrganScene_OrganSceneId FOREIGN KEY (OrganSceneId) REFERENCES iot_OrganScene(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganSceneTigger_DataId
ON iot_OrganSceneTigger
(
    DataId
);
CREATE INDEX IX_iot_OrganSceneTigger_OrganSceneId
ON iot_OrganSceneTigger
(
    OrganSceneId
);

CREATE TABLE iot_OrganSceneTimer
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganSceneTimer PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NULL,
    Cron TEXT NULL,
    Disabled INTEGER NOT NULL,
    OrganSceneId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganSceneTimer_iot_OrganScene_OrganSceneId FOREIGN KEY (OrganSceneId) REFERENCES iot_OrganScene(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganSceneTimer_OrganSceneId
ON iot_OrganSceneTimer
(
    OrganSceneId
);

CREATE TABLE iot_OrganUser
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_OrganUser PRIMARY KEY,
    IsDeleted TEXT NULL,
    OrganId TEXT NOT NULL,
    UserId TEXT NOT NULL,
    CONSTRAINT FK_iot_OrganUser_iot_Organ_OrganId FOREIGN KEY (OrganId) REFERENCES iot_Organ(Id) ON DELETE CASCADE,
    CONSTRAINT FK_iot_OrganUser_iot_User_UserId FOREIGN KEY (UserId) REFERENCES iot_User(Id) ON DELETE CASCADE
);
CREATE INDEX IX_iot_OrganUser_UserId
ON iot_OrganUser
(
    UserId
);
CREATE UNIQUE INDEX IX_iot_OrganUser_OrganId_UserId
ON iot_OrganUser
(
    OrganId,
    UserId
);

-- Creating table iot_Scene from scratch (simple ALTER TABLE is not enough)

CREATE TABLE temp_iot_Scene_534011718
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_Scene PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NOT NULL,
    Image TEXT NOT NULL,
    Hidden INTEGER NOT NULL,
    DisplayOrder INTEGER NOT NULL,
    NodeId TEXT NULL,
    CONSTRAINT FK_iot_Scene_iot_Node_NodeId FOREIGN KEY (NodeId) REFERENCES iot_Node(Id) ON DELETE CASCADE
);

-- Copying rows from original table to the new table

INSERT INTO temp_iot_Scene_534011718 (Id,IsDeleted,Name,Image,Hidden,DisplayOrder,NodeId) SELECT Id,IsDeleted,Name,Image,Hidden,DisplayOrder,NodeId FROM iot_Scene;

-- Droping the original table and renaming the temporary table

DROP TABLE iot_Scene;
ALTER TABLE temp_iot_Scene_534011718 RENAME TO iot_Scene;

-- Creating associated indexes from scratch

CREATE INDEX IX_iot_Scene_NodeId
ON iot_Scene
(
    NodeId
);