SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
IF EXISTS (SELECT * FROM tempdb..sysobjects WHERE id=OBJECT_ID('tempdb..#tmpErrors')) DROP TABLE #tmpErrors
GO
CREATE TABLE #tmpErrors (Error int)
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
BEGIN TRANSACTION
GO
PRINT N'Dropping foreign keys from [dbo].[FeatureLicenses]'
GO
ALTER TABLE [dbo].[FeatureLicenses] DROP CONSTRAINT[FK_FeatureLicenses_FeatureEditions]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Dropping constraints from [dbo].[FeatureLicenses]'
GO
ALTER TABLE [dbo].[FeatureLicenses] DROP CONSTRAINT [PK_Licenses]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Rebuilding [dbo].[FeatureLicenses]'
GO
CREATE TABLE [dbo].[tmp_rg_xx_FeatureLicenses]
(
[ID] [int] NOT NULL IDENTITY(1, 1),
[EditionID] [int] NULL,
[FeatureID] [int] NULL,
[NumberOfLicenses] [int] NOT NULL,
[FarmID] [int] NOT NULL CONSTRAINT [DF_FeatureLicenses_FarmID] DEFAULT ((1))
)
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
SET IDENTITY_INSERT [dbo].[tmp_rg_xx_FeatureLicenses] ON
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
DECLARE @idVal BIGINT
SELECT @idVal = IDENT_CURRENT(N'[dbo].[FeatureLicenses]')
IF @idVal IS NOT NULL
    DBCC CHECKIDENT(N'[dbo].[tmp_rg_xx_FeatureLicenses]', RESEED, @idVal)
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
DROP TABLE [dbo].[FeatureLicenses]
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
EXEC sp_rename N'[dbo].[tmp_rg_xx_FeatureLicenses]', N'FeatureLicenses'
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Creating primary key [PK_Licenses] on [dbo].[FeatureLicenses]'
GO
ALTER TABLE [dbo].[FeatureLicenses] ADD CONSTRAINT [PK_Licenses] PRIMARY KEY CLUSTERED  ([ID])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Altering [dbo].[Farms]'
GO
ALTER TABLE [dbo].[Farms] ADD
[CentralAdministrationUrl] [nvarchar] (255) NULL,
[Version] [int] NULL,
[UsageDatabaseConnectionString] [nvarchar] (255) NULL
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
PRINT N'Adding foreign keys to [dbo].[FeatureLicenses]'
GO
ALTER TABLE [dbo].[FeatureLicenses] ADD CONSTRAINT [FK_FeatureLicenses_FeatureEditions] FOREIGN KEY ([EditionID]) REFERENCES [dbo].[FeatureEditions] ([ID])
ALTER TABLE [dbo].[FeatureLicenses] ADD CONSTRAINT [FK_FeatureLicenses_Features] FOREIGN KEY ([FeatureID]) REFERENCES [dbo].[Features] ([ID])
ALTER TABLE [dbo].[FeatureLicenses] ADD CONSTRAINT [FK_FeatureLicenses_Farms] FOREIGN KEY ([FarmID]) REFERENCES [dbo].[Farms] ([ID])
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
Declare @QueriesID INT
Select @QueriesID=ID FROM Rules Where Name LIKE 'Queries'
Declare @SampleQueriesID INT
Select @SampleQueriesID=ID FROM Rules Where Name LIKE 'Samples' And ParentID = @QueriesID
INSERT INTO Rules VALUES(100, 'Lists Above Default List View Threshold', 0, 1, 
'<RuleConfiguration><RuleSchedule xmlns:p2="http://www.w3.org/2001/XMLSchema-instance" p2:type="OneTimeSchedule"><StartTime>2015-10-27T00:00:00</StartTime></RuleSchedule><EmailRecipients /><QueryDescription>Finds all lists with more than 5000 items. You cannot have more than 5,000 items (by default) returned in your list view.</QueryDescription><QueryConfiguration><DataSource>Lists</DataSource><FieldNames><string>Title</string><string>Url</string><string>ItemCount</string></FieldNames><FieldSorting><string>N</string><string>N</string><string>DESC</string></FieldSorting></QueryConfiguration><RuleDataSource>None</RuleDataSource><GroupCondition><Enabled>True</Enabled><LogicalOperators>0</LogicalOperators><Conditions><GroupCondition><Enabled>True</Enabled><LogicalOperators>0</LogicalOperators><Conditions><SimpleCondition><Enabled>True</Enabled><FieldInfo><FieldName>ItemCount</FieldName><FieldType>System.Int32</FieldType><Alias /></FieldInfo><Parameter>5000</Parameter><ConditionApplies>0</ConditionApplies><ConditionTypeID>4</ConditionTypeID></SimpleCondition></Conditions></GroupCondition></Conditions></GroupCondition></RuleConfiguration>'
, '2000-01-01 00:00:00.000', @SampleQueriesID, 0, 0)
UPDATE Options SET OptionValue = '6.0.0' WHERE OptionID = 14
GO
IF @@ERROR<>0 AND @@TRANCOUNT>0 ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT=0 BEGIN INSERT INTO #tmpErrors (Error) SELECT 1 BEGIN TRANSACTION END
GO
IF EXISTS (SELECT * FROM #tmpErrors) ROLLBACK TRANSACTION
GO
IF @@TRANCOUNT>0 BEGIN
PRINT 'The database update succeeded'
COMMIT TRANSACTION
END
ELSE PRINT 'The database update failed'
GO
DROP TABLE #tmpErrors
GO

