
--Migration_RCCP_20221007_171758.txt
EXECUTE sp_rename @objname = N'dbo.Rules1', @newname = N'Rules', @objtype = N'OBJECT'
IF object_id('[PK_dbo.Rules1]') IS NOT NULL BEGIN
    EXECUTE sp_rename @objname = N'[PK_dbo.Rules1]', @newname = N'PK_dbo.Rules', @objtype = N'OBJECT'
END
DROP TABLE [dbo].[Rules]

--Migration_RCCP_20221007_173049.txt
IF object_id(N'[dbo].[FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[RuleEngineActions] DROP CONSTRAINT [FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_RuleId' AND object_id = object_id(N'[dbo].[RuleEngineActions]', N'U'))
    DROP INDEX [IX_RuleId] ON [dbo].[RuleEngineActions]
DROP TABLE [dbo].[Rules1]
DROP TABLE [dbo].[RuleEngineActions]
DROP TABLE [dbo].[Rules]

--Migration_RCCP_20221007_173330.txt
IF object_id(N'[dbo].[FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[RuleEngineActions] DROP CONSTRAINT [FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_RuleId' AND object_id = object_id(N'[dbo].[RuleEngineActions]', N'U'))
    DROP INDEX [IX_RuleId] ON [dbo].[RuleEngineActions]
DROP TABLE [dbo].[Rules1]
DROP TABLE [dbo].[RuleEngineActions]
DROP TABLE [dbo].[Rules]

--Migration_RCCP_20221007_174218.txt
EXECUTE sp_rename @objname = N'dbo.Rules1', @newname = N'Rules', @objtype = N'OBJECT'
IF object_id('[PK_dbo.Rules1]') IS NOT NULL BEGIN
    EXECUTE sp_rename @objname = N'[PK_dbo.Rules1]', @newname = N'PK_dbo.Rules', @objtype = N'OBJECT'
END
IF object_id(N'[dbo].[FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[RuleEngineActions] DROP CONSTRAINT [FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_RuleId' AND object_id = object_id(N'[dbo].[RuleEngineActions]', N'U'))
    DROP INDEX [IX_RuleId] ON [dbo].[RuleEngineActions]
DROP TABLE [dbo].[RuleEngineActions]
DROP TABLE [dbo].[Rules]

--Migration_RCCP_20221007_175233.txt
IF object_id(N'[dbo].[FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]', N'F') IS NOT NULL
    ALTER TABLE [dbo].[RuleEngineActions] DROP CONSTRAINT [FK_dbo.RuleEngineActions_dbo.Rules1_RuleId]
IF EXISTS (SELECT name FROM sys.indexes WHERE name = N'IX_RuleId' AND object_id = object_id(N'[dbo].[RuleEngineActions]', N'U'))
    DROP INDEX [IX_RuleId] ON [dbo].[RuleEngineActions]
DROP TABLE [dbo].[RuleEngineActions]

--Migration_RCCP_20221007_175927.txt
DROP TABLE [dbo].[Rules1]
DROP TABLE [dbo].[Rules]

--Migration_RCCP_20221007_180227.txt
CREATE TABLE [dbo].[Rules] (
    [ID] [bigint] NOT NULL IDENTITY,
    [Name] [nvarchar](max),
    [RuleType] [int] NOT NULL,
    [Params] [nvarchar](max),
    [IsActive] [bit] NOT NULL,
    [CompanyId] [bigint] NOT NULL,
    CONSTRAINT [PK_dbo.Rules] PRIMARY KEY ([ID])
)
CREATE TABLE [dbo].[RuleEngineActions] (
    [ID] [bigint] NOT NULL IDENTITY,
    [Gateway] [int] NOT NULL,
    [Payload] [nvarchar](max),
    [RuleId] [bigint] NOT NULL,
    CONSTRAINT [PK_dbo.RuleEngineActions] PRIMARY KEY ([ID])
)
CREATE INDEX [IX_RuleId] ON [dbo].[RuleEngineActions]([RuleId])
ALTER TABLE [dbo].[RuleEngineActions] ADD CONSTRAINT [FK_dbo.RuleEngineActions_dbo.Rules_RuleId] FOREIGN KEY ([RuleId]) REFERENCES [dbo].[Rules] ([ID]) ON DELETE CASCADE
