﻿-- Generated by SQLite Compare utility

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

-- CREATE BY WG 2020-11-26 12:36

CREATE TABLE iot_UserRole_temp (
	[Id] text NOT NULL PRIMARY KEY, 
	[IsDeleted] text, 
	[UserId] text NOT NULL, 
	[RoleId] text NOT NULL
);

INSERT INTO iot_UserRole_temp (Id,IsDeleted,UserId,RoleId) SELECT Id,IsDeleted,UserId,RoleId FROM iot_UserRole;

CREATE TABLE iot_RolePermission_temp (
	[Id] text NOT NULL PRIMARY KEY, 
	[IsDeleted] text, 
	[RoleId] text NOT NULL, 
	[PermissionId] text NOT NULL,
    [IsReadOnly] INTEGER NOT NULL
);

INSERT INTO iot_RolePermission_temp (Id,IsDeleted,RoleId,PermissionId,IsReadOnly) SELECT Id,IsDeleted,RoleId,PermissionId,IsReadOnly FROM iot_RolePermission;

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

CREATE TABLE temp_iot_Category_534011718
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_Category PRIMARY KEY,
    IsDeleted TEXT NULL,
    DisplayOrder INTEGER NOT NULL,
    [Left] INTEGER NOT NULL,
    Right INTEGER NOT NULL,
    ParentId TEXT NULL,
    Name TEXT NULL,
    Number TEXT NULL,
    Image TEXT NULL,
    CONSTRAINT FK_iot_Category_iot_Category_ParentId FOREIGN KEY (ParentId) REFERENCES iot_Category(Id) ON DELETE RESTRICT
);

-- CREATE BY WG 2020-11-26 12:36

INSERT INTO temp_iot_Category_534011718 (Id,IsDeleted,DisplayOrder,[Left],Right,Name,Number,Image) SELECT Id,IsDeleted,0,0,0,Name,Number,Image FROM iot_Category;

-- Droping the original table and renaming the temporary table
DROP TABLE iot_Category;
ALTER TABLE temp_iot_Category_534011718 RENAME TO iot_Category;

-- Creating associated indexes from scratch

CREATE UNIQUE INDEX IX_iot_Category_Number
ON iot_Category
(
    Number
);
CREATE INDEX IX_iot_Category_ParentId
ON iot_Category
(
    ParentId
);

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

CREATE TABLE temp_iot_Node_1655911537
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_Node PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NULL,
    Number TEXT NOT NULL,
    IsOnline INTEGER NOT NULL,
    Image TEXT NULL,
    Version TEXT NULL,
    DisplayOrder INTEGER NOT NULL,
    Hidden INTEGER NOT NULL
);

-- Copying rows from original table to the new table

INSERT INTO temp_iot_Node_1655911537 (Id,IsDeleted,Name,Number,IsOnline,Image,Version,DisplayOrder,Hidden) SELECT Id,IsDeleted,Name,Number,IsOnline,Image,Version,DisplayOrder,Hidden FROM iot_Node;

-- Droping the original table and renaming the temporary table

DROP TABLE iot_Node;
ALTER TABLE temp_iot_Node_1655911537 RENAME TO iot_Node;

-- Creating associated indexes from scratch

CREATE UNIQUE INDEX IX_iot_Node_Number
ON iot_Node
(
    Number
);

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

CREATE TABLE temp_iot_PermissionCategory_630327709
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_PermissionCategory PRIMARY KEY,
    IsDeleted TEXT NULL,
    DisplayOrder INTEGER NOT NULL,
    [Left] INTEGER NOT NULL,
    Right INTEGER NOT NULL,
    ParentId TEXT NULL,
    Name TEXT NULL,
    Number TEXT NULL,
    CONSTRAINT FK_iot_PermissionCategory_iot_PermissionCategory_ParentId FOREIGN KEY (ParentId) REFERENCES iot_PermissionCategory(Id) ON DELETE SET NULL
);

-- CREATE BY WG 2020-11-26 12:41

INSERT INTO temp_iot_PermissionCategory_630327709 (Id,IsDeleted,DisplayOrder,[Left],Right,Name,Number) SELECT Id,IsDeleted,0,0,0,Name,Number FROM iot_Category;


DROP TABLE iot_PermissionCategory;
ALTER TABLE temp_iot_PermissionCategory_630327709 RENAME TO iot_PermissionCategory;

-- Creating associated indexes from scratch

CREATE UNIQUE INDEX IX_iot_PermissionCategory_Number
ON iot_PermissionCategory
(
    Number
);
CREATE INDEX IX_iot_PermissionCategory_ParentId
ON iot_PermissionCategory
(
    ParentId
);

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

CREATE TABLE temp_iot_Role_1752227528
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_Role PRIMARY KEY,
    IsDeleted TEXT NULL,
    Name TEXT NOT NULL,
    Number TEXT NULL,
    IsReadOnly INTEGER NOT NULL
);

-- Copying rows from original table to the new table

INSERT INTO temp_iot_Role_1752227528 (Id,IsDeleted,Name,IsReadOnly,Number) SELECT Id,IsDeleted,Name,IsReadOnly,NULL AS Number FROM iot_Role;

-- Droping the original table and renaming the temporary table

DROP TABLE iot_Role;
ALTER TABLE temp_iot_Role_1752227528 RENAME TO iot_Role;

-- Creating associated indexes from scratch

CREATE UNIQUE INDEX IX_iot_Role_Name
ON iot_Role
(
    Name
);

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

CREATE TABLE temp_iot_Scene_726643700
(
    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_726643700 (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_726643700 RENAME TO iot_Scene;

-- Creating associated indexes from scratch

CREATE INDEX IX_iot_Scene_NodeId
ON iot_Scene
(
    NodeId
);

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

CREATE TABLE temp_iot_User_1848543519
(
    Id TEXT NOT NULL CONSTRAINT PK_iot_User PRIMARY KEY,
    IsDeleted TEXT NULL,
    UserName TEXT NOT NULL,
    SecurityStamp TEXT NULL,
    PasswordHash TEXT NULL,
    PasswordConfirmed INTEGER NOT NULL,
    Email TEXT NULL,
    EmailConfirmed INTEGER NOT NULL,
    PhoneNumber TEXT NULL,
    PhoneNumberConfirmed INTEGER NOT NULL,
    RealName TEXT NULL,
    IdentityNumber TEXT NULL,
    IdentityConfirmed INTEGER NOT NULL,
    NickName TEXT NULL,
    Avatar TEXT NULL,
    Sex INTEGER NULL,
    Birthday TEXT NULL,
    LockoutEnabled INTEGER NOT NULL,
    AccessFailedCount INTEGER NOT NULL,
    LockoutEnd TEXT NULL,
    RowVersion TEXT NULL
);

-- CREATE BY WG 2020-11-26 12:41

INSERT INTO temp_iot_User_1848543519 
(Id,IsDeleted,UserName,SecurityStamp,PasswordHash,PasswordConfirmed,Email,EmailConfirmed,PhoneNumberConfirmed,IdentityConfirmed,NickName,Avatar,LockoutEnabled,AccessFailedCount) 
SELECT 
Id,IsDeleted,UserName,SecurityStamp,PasswordHash,1,Email,0,0,0,NickName,Avatar,0,0
FROM iot_User;

-- Droping the original table and renaming the temporary table

DROP TABLE iot_User;
ALTER TABLE temp_iot_User_1848543519 RENAME TO iot_User;

-- Creating associated indexes from scratch

CREATE UNIQUE INDEX IX_iot_User_Email
ON iot_User
(
    Email
);
CREATE UNIQUE INDEX IX_iot_User_PhoneNumber
ON iot_User
(
    PhoneNumber
);
CREATE UNIQUE INDEX IX_iot_User_UserName
ON iot_User
(
    UserName
);

-- CREATE BY WG 2020-11-26 13:25

INSERT INTO iot_UserRole (Id,IsDeleted,UserId,RoleId) SELECT Id,IsDeleted,UserId,RoleId FROM iot_UserRole_temp;

DROP TABLE iot_UserRole_temp;

INSERT INTO iot_RolePermission (Id,IsDeleted,RoleId,PermissionId,IsReadOnly) SELECT Id,IsDeleted,RoleId,PermissionId,IsReadOnly FROM iot_RolePermission_temp;

DROP TABLE iot_RolePermission_temp;

-- update notify:host
update iot_Setting set Value = replace(Value,'IoTCenter','platform') where Name='notify:host';
-- update db version
update iot_Node set Version='1.1.0.2';
