SET NOCOUNT ON;

DECLARE	 @workingVersion	NVARCHAR(255)
		,@databaseVersion	NVARCHAR(255)
		,@majorVersion		NVARCHAR(255)
		,@minorVersion		NVARCHAR(255)
		,@buildNumber		NVARCHAR(255)
SET		@workingVersion = ISNULL((SELECT TOP 1 [Value] FROM [meta].[BimlFlexSettings] WHERE [Name] = 'BimlFlex.DatabaseVersion'), '0.0.0000')
SET		@databaseVersion = @workingVersion
SET		@majorVersion = CASE WHEN CHARINDEX('.', @workingVersion) > 1 THEN SUBSTRING(@workingVersion, 1, CHARINDEX('.', @workingVersion)-1) ELSE '0' END
SET		@workingVersion = REPLACE(@workingVersion, @majorVersion + '.', '')
SET		@minorVersion = CASE WHEN CHARINDEX('.', @workingVersion) > 1 THEN SUBSTRING(@workingVersion, 1, CHARINDEX('.', @workingVersion)-1) ELSE '0' END
SET		@buildNumber = REPLACE(@workingVersion, @minorVersion + '.', '')

DECLARE @tblInsert	TABLE(
	[Name]				NVARCHAR(255)	PRIMARY KEY CLUSTERED,
	[Value]				NVARCHAR(255)	NOT NULL,
	[IsEnabled]			BIT				NOT NULL)

INSERT INTO @tblInsert ([Name],[Value],[IsEnabled])
VALUES	 ('BimlFlex.PreviousDatabaseVersion', @databaseVersion, 0)
		,('BimlFlex.PreviousMajorVersion', @majorVersion, 0)
		,('BimlFlex.PreviousMinorVersion', @minorVersion, 0)
		,('BimlFlex.PreviousBuildNumber', @buildNumber, 0)

MERGE [meta].[BimlFlexSettings] AS TARGET
USING 
(	SELECT	 [Name]
			,[Value]
	FROM	@tblInsert
) AS src
	ON	TARGET.[Name] = src.[Name]
WHEN MATCHED THEN 
	UPDATE 
	SET		 [Value] = src.[Value]
WHEN NOT MATCHED THEN
	INSERT	([Name]
			,[Value])
	VALUES	([Name]
			,[Value]);

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'SateliteAttribute') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Configurations].[SateliteAttribute]', @newname=N'SatelliteAttribute', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'SateliteAttributeId') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Configurations].[SateliteAttributeId]', @newname=N'SatelliteAttributeId', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[mdv].[SAT_Configurations_Detail]') AND name = 'SateliteAttributeId') BEGIN EXEC dbo.sp_rename @objname=N'[mdv].[SAT_Configurations_Detail].[SateliteAttributeId]', @newname=N'SatelliteAttributeId', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DataTypeMappings]') AND name = 'Comment') BEGIN ALTER TABLE [app].[DataTypeMappings] DROP COLUMN [Comment] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DataTypeMappings]') AND name = 'MappedTo') BEGIN ALTER TABLE [vsto].[DataTypeMappings] DROP COLUMN [MappedTo] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DataTypeMappings]') AND name = 'MappingName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[DataTypeMappings].[MappingName]', @newname=N'Mapping', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DataTypeMappings]') AND name = 'MappedTo') BEGIN ALTER TABLE [app].[DataTypeMappings] DROP COLUMN [MappedTo] END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[Parameters]') AND name = 'DF_appParameters_IsStoredParameter') BEGIN ALTER TABLE [app].[Parameters] DROP CONSTRAINT [DF_appParameters_IsStoredParameter] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Parameters]') AND name = 'IsStoredParameter') BEGIN ALTER TABLE [app].[Parameters] DROP COLUMN [IsStoredParameter] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Parameters]') AND name = 'IsStoredParameter') BEGIN ALTER TABLE [vsto].[Parameters] DROP COLUMN [IsStoredParameter] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[mdv].[SAT_Parameters]') AND name = 'IsStoredParameter') BEGIN ALTER TABLE [mdv].[SAT_Parameters] DROP COLUMN [IsStoredParameter] END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[Parameters]') AND name = 'DF_appParameters_IsPersisted') BEGIN ALTER TABLE [app].[Parameters] DROP CONSTRAINT [DF_appParameters_IsPersisted] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Parameters]') AND name = 'IsPersisted') BEGIN ALTER TABLE [app].[Parameters] DROP COLUMN [IsPersisted] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Parameters]') AND name = 'IsPersisted') BEGIN ALTER TABLE [vsto].[Parameters] DROP COLUMN [IsPersisted] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[mdv].[SAT_Parameters]') AND name = 'IsPersisted') BEGIN ALTER TABLE [mdv].[SAT_Parameters] DROP COLUMN [IsPersisted] END

IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetBatchInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetBatchInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetColumnInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetColumnInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetConfigurationInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetConfigurationInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetConnectionInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetConnectionInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetCustomAttributeInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetCustomAttributeInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetObjectInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetObjectInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetParameterInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetParameterInfo]
IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[wcf].[GetProjectInfo]') AND xtype IN (N'FN', N'IF', N'TF')) DROP FUNCTION [wcf].[GetProjectInfo]

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[meta].[DatabaseVersion]') AND type IN(N'U')) DROP TABLE [meta].[DatabaseVersion]
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[wcf].[GetVersion]')) BEGIN DROP PROCEDURE [wcf].[GetVersion] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Batches]') AND name = 'ExecutionOrder') BEGIN ALTER TABLE [vsto].[Batches] DROP COLUMN [ExecutionOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Batches]') AND name = 'ParentBatch') BEGIN ALTER TABLE [vsto].[Batches] DROP COLUMN [ParentBatch] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [ModelOverrideShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ObjectName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[ObjectName]', @newname=N'Object', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'SsisDataType') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [SsisDataType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'IsDeployed') BEGIN ALTER TABLE [vsto].[Configurations] DROP COLUMN [IsDeployed] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'IsInternal') BEGIN ALTER TABLE [vsto].[Configurations] DROP COLUMN [IsInternal] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Connections].[Name]', @newname=N'Connection', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[CustomAttributes]') AND name = 'ObjectName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[CustomAttributes].[ObjectName]', @newname=N'Object', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'CompressionType') BEGIN ALTER TABLE [vsto].[Objects] DROP COLUMN [CompressionType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'PartitionScheme') BEGIN ALTER TABLE [vsto].[Objects] DROP COLUMN [PartitionScheme] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Parameters]') AND name = 'ObjectName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Parameters].[ObjectName]', @newname=N'Object', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Projects]') AND name = 'CubeConnection') BEGIN ALTER TABLE [vsto].[Projects] DROP COLUMN [CubeConnection] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Projects]') AND name = 'TabularCubeConnection') BEGIN ALTER TABLE [vsto].[Projects] DROP COLUMN [TabularCubeConnection] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[TestCases]') AND name = 'ObjectName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[TestCases].[ObjectName]', @newname=N'Object', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'CubeConnectionUID')
BEGIN 
	IF (OBJECT_ID('app.FK_appProjects_REF_appCubeConnectionUID', 'F') IS NOT NULL) ALTER TABLE [app].[Projects] DROP CONSTRAINT [FK_appProjects_REF_appCubeConnectionUID]
	ALTER TABLE [app].[Projects] DROP COLUMN [CubeConnectionUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'TabularCubeConnectionUID')
BEGIN 
	IF (OBJECT_ID('app.FK_appProjects_REF_appTabularCubeConnection', 'F') IS NOT NULL) ALTER TABLE [app].[Projects] DROP CONSTRAINT [FK_appProjects_REF_appTabularCubeConnection]
	ALTER TABLE [app].[Projects] DROP COLUMN [TabularCubeConnectionUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Projects]') AND name = 'CubeConnectionUID') BEGIN ALTER TABLE [archive].[Projects] DROP COLUMN [CubeConnectionUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Projects]') AND name = 'TabularCubeConnectionUID') BEGIN ALTER TABLE [archive].[Projects] DROP COLUMN [TabularCubeConnectionUID] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelAddKeyedInstance') BEGIN 
	IF EXISTS (SELECT name FROM sys.objects WHERE type_desc = 'DEFAULT_CONSTRAINT' AND name = 'DF_appColumns_ModelAddKeyedInstance') ALTER TABLE [app].[Columns] DROP CONSTRAINT [DF_appColumns_ModelAddKeyedInstance]
	ALTER TABLE [vsto].[Columns] DROP COLUMN [ModelAddKeyedInstance] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ModelAddKeyedInstance') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [ModelAddKeyedInstance] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ModelAddKeyedInstance') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [ModelAddKeyedInstance] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Projects]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [archive].[Projects] DROP COLUMN [ExcludeFromBuild] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [app].[Projects] DROP COLUMN [ExcludeFromBuild] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Projects]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [vsto].[Projects] DROP COLUMN [ExcludeFromBuild] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [archive].[Connections] DROP COLUMN [ExcludeFromBuild] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [app].[Connections] DROP COLUMN [ExcludeFromBuild] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'ExcludeFromBuild') BEGIN ALTER TABLE [vsto].[Connections] DROP COLUMN [ExcludeFromBuild] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Projects]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [archive].[Projects] DROP COLUMN [ExcludeFromValidation] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [app].[Projects] DROP COLUMN [ExcludeFromValidation] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Projects]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [vsto].[Projects] DROP COLUMN [ExcludeFromValidation] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [archive].[Connections] DROP COLUMN [ExcludeFromValidation] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [app].[Connections] DROP COLUMN [ExcludeFromValidation] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'ExcludeFromValidation') BEGIN ALTER TABLE [vsto].[Connections] DROP COLUMN [ExcludeFromValidation] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Configurations].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Configurations].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Configurations]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Configurations].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Configurations].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Configurations].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Configurations]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Configurations].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DataTypeMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[DataTypeMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DataTypeMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[DataTypeMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[DataTypeMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[DataTypeMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DataTypeMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[DataTypeMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DataTypeMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[DataTypeMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[DataTypeMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[DataTypeMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DatasetMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[DatasetMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DatasetMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[DatasetMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[DatasetMappings]') AND name = 'SqlExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[DatasetMappings].[SqlExpression]', @newname=N'SqlSourceExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[DatasetMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[DatasetMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[DatasetMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[DatasetMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[DatasetMappings]') AND name = 'SsisExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[DatasetMappings].[SsisExpression]', @newname=N'SsisDataflowExpression', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'IsAltBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[IsAltBusinessKey]', @newname=N'IsSourceKey', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'IsBusinessKey') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[IsBusinessKey]', @newname=N'IsIntegrationKey', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'SsisDataflowExpression') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[SsisDataflowExpression]', @newname=N'DataflowExpression', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'SsisDataType') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[BimlColumnData].[SsisDataType]', @newname=N'DataflowDataType', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [AdfDataflowExpression] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [AdfDataflowExpression] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [AdfDataflowExpression] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [stage].[Columns] DROP COLUMN [AdfDataflowExpression] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [wcf].[Columns] DROP COLUMN [AdfDataflowExpression] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'AdfDataflowExpression') BEGIN ALTER TABLE [wcf].[BimlColumnData] DROP COLUMN [AdfDataflowExpression] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [DisplayFolder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [DisplayFolder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [DisplayFolder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [stage].[Columns] DROP COLUMN [DisplayFolder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [wcf].[Columns] DROP COLUMN [DisplayFolder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[BimlColumnData]') AND name = 'DisplayFolder') BEGIN ALTER TABLE [wcf].[BimlColumnData] DROP COLUMN [DisplayFolder] END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[DataLineageColumns]') AND type IN(N'U')) DROP TABLE [vsto].[DataLineageColumns];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[DataLineageColumns]') AND type IN(N'U')) DROP TABLE [archive].[DataLineageColumns];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[DataLineageColumns]') AND type IN(N'U')) DROP TABLE [app].[DataLineageColumns];  
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[DataLineageObjects]') AND type IN(N'U')) DROP TABLE [vsto].[DataLineageObjects];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[DataLineageObjects]') AND type IN(N'U')) DROP TABLE [archive].[DataLineageObjects];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[DataLineageObjects]') AND type IN(N'U')) DROP TABLE [app].[DataLineageObjects];   

DELETE FROM [stage].[Columns];

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaMappingConnectors]') AND type IN(N'U')) DROP TABLE [util].[InformaticaMappingConnectors];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaMappingTableAttributes]') AND type IN(N'U')) DROP TABLE [util].[InformaticaMappingTableAttributes];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaMappingTargetLoadOrders]') AND type IN(N'U')) DROP TABLE [util].[InformaticaMappingTargetLoadOrders];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaMappingTransformationFields]') AND type IN(N'U')) DROP TABLE [util].[InformaticaMappingTransformationFields];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaMappingTransformations]') AND type IN(N'U')) DROP TABLE [util].[InformaticaMappingTransformations];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaSessionExtensions]') AND type IN(N'U')) DROP TABLE [util].[InformaticaSessionExtensions];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaSourceColumns]') AND type IN(N'U')) DROP TABLE [util].[InformaticaSourceColumns];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaSources]') AND type IN(N'U')) DROP TABLE [util].[InformaticaSources];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaTargetColumns]') AND type IN(N'U')) DROP TABLE [util].[InformaticaTargetColumns];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaTargets]') AND type IN(N'U')) DROP TABLE [util].[InformaticaTargets];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[util].[InformaticaTransformations]') AND type IN(N'U')) DROP TABLE [util].[InformaticaTransformations];

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'Server') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Connections].[Server]', @newname=N'DataSource', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'Server') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Connections].[Server]', @newname=N'DataSource', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'Server') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Connections].[Server]', @newname=N'DataSource', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Connections].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Connections].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Connections].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Connections].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Connections].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Connections].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [archive].[Connections] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [app].[Connections] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [vsto].[Connections] DROP COLUMN [SqlAuthentication] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Environments]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Environments].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Environments]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Environments].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Environments]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Environments].[SqlUserName]', @newname=N'UserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Environments]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Environments].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Environments]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Environments].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Environments]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Environments].[SqlPassword]', @newname=N'Password', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Environments]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [archive].[Environments] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Environments]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [app].[Environments] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Environments]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [vsto].[Environments] DROP COLUMN [SqlAuthentication] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[UserSettings]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[UserSettings].[SqlUserName]', @newname=N'ConnectionUserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[UserSettings]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[UserSettings].[SqlUserName]', @newname=N'ConnectionUserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[UserSettings]') AND name = 'SqlUserName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[UserSettings].[SqlUserName]', @newname=N'ConnectionUserName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[UserSettings]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[app].[UserSettings].[SqlPassword]', @newname=N'ConnectionPassword', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[UserSettings]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[UserSettings].[SqlPassword]', @newname=N'ConnectionPassword', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[UserSettings]') AND name = 'SqlPassword') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[UserSettings].[SqlPassword]', @newname=N'ConnectionPassword', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[UserSettings]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [archive].[UserSettings] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[UserSettings]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [app].[UserSettings] DROP COLUMN [SqlAuthentication] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[UserSettings]') AND name = 'SqlAuthentication') BEGIN ALTER TABLE [vsto].[UserSettings] DROP COLUMN [SqlAuthentication] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'IsDeployed') BEGIN ALTER TABLE [vsto].[Configurations] DROP COLUMN [IsDeployed] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'IsDeployed') BEGIN ALTER TABLE [app].[Configurations] DROP COLUMN [IsDeployed] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Configurations]') AND name = 'IsDeployed') BEGIN ALTER TABLE [archive].[Configurations] DROP COLUMN [IsDeployed] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'IsInternal') BEGIN ALTER TABLE [vsto].[Configurations] DROP COLUMN [IsInternal] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'IsInternal') BEGIN ALTER TABLE [app].[Configurations] DROP COLUMN [IsInternal] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Configurations]') AND name = 'IsInternal') BEGIN ALTER TABLE [archive].[Configurations] DROP COLUMN [IsInternal] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'AdfDataflowExpression') 
	AND NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Configurations]') AND name = 'AdfCopyExpression') 
BEGIN 
	EXEC dbo.sp_rename @objname=N'[app].[Configurations].[AdfDataflowExpression]', @newname=N'AdfCopyExpression', @objtype='COLUMN'
END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[DataSource]') AND type IN(N'U')) DROP TABLE [app].[DataSource]
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[ImportRequest]') AND type IN(N'U')) DROP TABLE [app].[ImportRequest]

IF EXISTS(SELECT 1 FROM [meta].[ConnectionType] WHERE [Id] = 26 AND [Code] = 'AZKV')
BEGIN 

	SET	IDENTITY_INSERT [meta].[ConnectionType] ON;
	INSERT INTO[meta].[ConnectionType] ([Id],[Code],[Name],[IsEnabled])
	VALUES	(99, 'AZKV_99', 'Azure Key Vault', 0);
	SET	IDENTITY_INSERT [meta].[ConnectionType] OFF;

	UPDATE	[app].[Connections]
	SET		[ConnectionTypeId] = 99
	WHERE	[ConnectionTypeId] = 26

	DELETE 
	FROM	[meta].[ConnectionType]
	WHERE	[Id] = 26

	DECLARE @sqlAzkv NVARCHAR(MAX)
	IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND name = 'Url') 
	BEGIN 
		SET @sqlAzkv = '
	UPDATE	c
	SET		[ConnectionString] = CASE WHEN LTRIM(RTRIM(ISNULL(c.[ConnectionString], ''''))) = '''' THEN ls.[Url] ELSE c.[ConnectionString] END'
	END
	ELSE
	BEGIN
		SET @sqlAzkv = '
	UPDATE	c
	SET		[ConnectionString] = CASE WHEN LTRIM(RTRIM(ISNULL(c.[ConnectionString], ''''))) = '''' THEN ls.[Name] ELSE c.[ConnectionString] END'
	END
	SET	@sqlAzkv = @sqlAzkv + '
			,[Catalog] = CASE WHEN LTRIM(RTRIM(ISNULL(c.[Catalog], ''''))) = '''' THEN ls.[Name] ELSE c.[Catalog] END
	FROM	[app].[Connections] c
	INNER JOIN [app].[AdfLinkedServices] ls
		ON	c.[UID] = ls.[ConnectionUID]
	WHERE	[ConnectionTypeId] = 99
		AND	(LTRIM(RTRIM(ISNULL(c.[ConnectionString], ''''))) = '''' OR LTRIM(RTRIM(ISNULL(c.[Catalog], ''''))) = '''')'
	EXEC sp_executesql @sqlAzkv
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') AND EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND name = 'ConnectionStringKVS_StoreName') 
BEGIN 
	EXEC ('DROP TABLE [app].[AdfLinkedServices]')
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') AND EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'ConnectionStringKVS_StoreName') 
BEGIN 
	EXEC ('DROP TABLE [vsto].[AdfLinkedServices]')
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') AND EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]') AND name = 'ConnectionStringKVS_StoreName') 
BEGIN 
	EXEC ('DROP TABLE [archive].[AdfLinkedServices]')
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[SystemType]') AND name = 'SqlConcatenantePattern') ALTER TABLE [meta].[SystemType] DROP COLUMN [SqlConcatenantePattern]
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[SystemType]') AND name = 'SqlStringLoadPattern') ALTER TABLE [meta].[SystemType] DROP COLUMN [SqlStringLoadPattern]
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[SystemType]') AND name = 'SqlDateLoadPattern') ALTER TABLE [meta].[SystemType] DROP COLUMN [SqlDateLoadPattern]
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[LinkedServiceType]') AND name = 'SqlConcatenantePattern') ALTER TABLE [meta].[LinkedServiceType] DROP COLUMN [SqlConcatenantePattern]
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[LinkedServiceType]') AND name = 'SqlStringLoadPattern') ALTER TABLE [meta].[LinkedServiceType] DROP COLUMN [SqlStringLoadPattern]
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[LinkedServiceType]') AND name = 'SqlDateLoadPattern') ALTER TABLE [meta].[LinkedServiceType] DROP COLUMN [SqlDateLoadPattern]

IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[meta].[ObjectType]') AND name = 'DF_metaObjectType_BusinessModel') 
BEGIN 
	ALTER TABLE [meta].[ObjectType] DROP CONSTRAINT [DF_metaObjectType_BusinessModel] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[meta].[ObjectType]') AND name = 'IsBusinessModel') ALTER TABLE [meta].[ObjectType] DROP COLUMN [IsBusinessModel]

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') EXEC ('ALTER TABLE [app].[AdfLinkedServices] DROP COLUMN [LogicalDisplayFolder]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [LogicalDisplayFolder]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]') AND name = 'LogicalDisplayFolder') EXEC ('ALTER TABLE [archive].[AdfLinkedServices] DROP COLUMN [LogicalDisplayFolder]')

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ShortName') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [ShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ShortName') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [ShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'ShortName') BEGIN ALTER TABLE [stage].[Columns] DROP COLUMN [ShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ShortName') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [ShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'ShortName') BEGIN ALTER TABLE [wcf].[Columns] DROP COLUMN [ShortName] END 

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'ShortName') BEGIN ALTER TABLE [app].[Objects] DROP COLUMN [ShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'ShortName') BEGIN ALTER TABLE [archive].[Objects] DROP COLUMN [ShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Objects]') AND name = 'ShortName') BEGIN ALTER TABLE [stage].[Objects] DROP COLUMN [ShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'ShortName') BEGIN ALTER TABLE [vsto].[Objects] DROP COLUMN [ShortName] END 

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [wcf].[Columns] DROP COLUMN [ModelOverrideShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [ModelOverrideShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [ModelOverrideShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [stage].[Columns] DROP COLUMN [ModelOverrideShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelOverrideShortName') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [ModelOverrideShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'BusinessShortName') BEGIN ALTER TABLE [wcf].[Columns] DROP COLUMN [BusinessShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'BusinessShortName') BEGIN ALTER TABLE [app].[Columns] DROP COLUMN [BusinessShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'BusinessShortName') BEGIN ALTER TABLE [archive].[Columns] DROP COLUMN [BusinessShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'BusinessShortName') BEGIN ALTER TABLE [stage].[Columns] DROP COLUMN [BusinessShortName] END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'BusinessShortName') BEGIN ALTER TABLE [vsto].[Columns] DROP COLUMN [BusinessShortName] END 


IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'ModelObjectTypeId') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Objects].[ModelObjectTypeId]', @newname=N'AcceleratorTypeId', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'ModelObjectType') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Objects].[ModelObjectType]', @newname=N'AcceleratorType', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'ModelObjectTypeId') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Objects].[ModelObjectTypeId]', @newname=N'AcceleratorTypeId', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'AcceleratorLinkName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[AcceleratorLinkName]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
ELSE IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ModelReference') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[ModelReference]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'AcceleratorLinkName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[AcceleratorLinkName]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
ELSE IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelReference') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[ModelReference]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'AcceleratorLinkName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[AcceleratorLinkName]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
ELSE IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ModelReference') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[ModelReference]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'AcceleratorLinkName') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[AcceleratorLinkName]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
ELSE IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'ModelReference') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[ModelReference]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'AcceleratorLinkName') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[AcceleratorLinkName]', @newname=N'BusinessReference', @objtype= 'COLUMN' END
ELSE IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'ModelReference') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[ModelReference]', @newname=N'BusinessReference', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Objects].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Objects].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Objects].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Objects]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Objects].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'ModelOverrideName') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[ModelOverrideName]', @newname=N'BusinessName', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'ModelOverrideShortName') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Objects].[ModelOverrideShortName]', @newname=N'BusinessShortName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'ModelOverrideShortName') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Objects].[ModelOverrideShortName]', @newname=N'BusinessShortName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'ModelOverrideShortName') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Objects].[ModelOverrideShortName]', @newname=N'BusinessShortName', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Objects]') AND name = 'ModelOverrideShortName') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Objects].[ModelOverrideShortName]', @newname=N'BusinessShortName', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Objects].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Objects].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Objects].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'ModelGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[ModelGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Objects]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Objects].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Objects]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Objects].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Objects]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Objects].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Columns]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Columns].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Columns]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Columns].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Columns]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Columns].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[stage].[Columns]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[stage].[Columns].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[wcf].[Columns]') AND name = 'BusinessGrouping') BEGIN EXEC dbo.sp_rename @objname=N'[wcf].[Columns].[BusinessGrouping]', @newname=N'BusinessSubject', @objtype= 'COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Configurations]') AND name = 'ConfigurationGrouping') BEGIN ALTER TABLE [vsto].[Configurations] ALTER COLUMN [ConfigurationGrouping] NVARCHAR(20) NULL END 

DECLARE @qry NVARCHAR(MAX);
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND name = 'CustomerUID' AND is_nullable = 1)
BEGIN 
	SELECT @qry = (

	SELECT'DROP INDEX ['+i.name+'] ON ['+s.name+'].['+o.name+'];'
	FROM sys.indexes i 
		INNER JOIN sys.objects o ON i.object_id=o.object_id
		INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
	WHERE o.type <> 'S' AND is_primary_key <> 1 AND index_id > 0
	AND s.name <> 'sys' AND s.name <> 'sys' AND is_unique_constraint = 0
	AND o.object_id = OBJECT_ID(N'[app].[AdfLinkedServices]')
	FOR XML PATH(''));

	EXEC sp_executesql @qry
	EXEC ('ALTER TABLE [app].[AdfLinkedServices] ALTER COLUMN [CustomerUID] UNIQUEIDENTIFIER NOT NULL')
END 
IF EXISTS (SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]') AND name = 'CustomerUID' AND is_nullable = 1)
BEGIN
	SELECT @qry = (

	SELECT 'DROP INDEX ['+i.name+'] ON ['+s.name+'].['+o.name+'];'
	FROM sys.indexes i 
		INNER JOIN sys.objects o ON i.object_id=o.object_id
		INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
	WHERE o.type <> 'S' AND is_primary_key <> 1 AND index_id > 0
	AND s.name <> 'sys' AND s.name <> 'sys' AND is_unique_constraint = 0
	AND o.object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]')
	FOR XML PATH(''));

	EXEC sp_executesql @qry
	EXEC ('ALTER TABLE [archive].[AdfLinkedServices] ALTER COLUMN [CustomerUID] UNIQUEIDENTIFIER NOT NULL')
END


IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'ConnectionStringKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [ConnectionStringKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'PasswordKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [PasswordKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'ServicePrincipalKeyKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [ServicePrincipalKeyKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'AccountKeyKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [AccountKeyKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'CredentialKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [CredentialKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'CredStringKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [CredStringKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SasUriKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SasUriKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SasTokenKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SasTokenKVS_UID]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SecurityTokenKVS_UID') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SecurityTokenKVS_UID]')

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'ConnectionStringKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [ConnectionStringKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'PasswordKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [PasswordKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'ServicePrincipalKeyKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [ServicePrincipalKeyKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'AccountKeyKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [AccountKeyKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'CredentialKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [CredentialKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'CredStringKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [CredStringKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SasUriKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SasUriKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SasTokenKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SasTokenKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'SecurityTokenKVS_Name') EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] DROP COLUMN [SecurityTokenKVS_Name]')
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'Connection' AND max_length > 256) 
BEGIN 
	SELECT @qry = (

	SELECT 'DROP INDEX ['+i.name+'] ON ['+s.name+'].['+o.name+'];'
	FROM sys.indexes i 
		INNER JOIN sys.objects o ON i.object_id=o.object_id
		INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
	WHERE o.type <> 'S' AND is_primary_key <> 1 AND index_id > 0
	AND s.name <> 'sys' AND s.name <> 'sys' AND is_unique_constraint = 0
	AND o.object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]')
	FOR XML PATH(''));

	EXEC sp_executesql @qry
	EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] ALTER COLUMN [Connection] NVARCHAR(128) NULL')
END


IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Batches]') AND name = 'ParentUID')
BEGIN
	IF (OBJECT_ID('app.FK_appBatches_REF_appParentBatch', 'F') IS NOT NULL) ALTER TABLE [app].[Batches] DROP CONSTRAINT [FK_appBatches_REF_appParentBatch]
	ALTER TABLE [app].[Batches] DROP COLUMN [ParentUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Batches]') AND name = 'ParentUID') BEGIN ALTER TABLE [archive].[Batches] DROP COLUMN [ParentUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Batches]') AND name = 'ParentUID') BEGIN ALTER TABLE [vsto].[Batches] DROP COLUMN [ParentUID] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Batches]') AND name = 'ExecutionOrder') BEGIN ALTER TABLE [archive].[Batches] DROP COLUMN [ExecutionOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Batches]') AND name = 'ExecutionOrder') BEGIN ALTER TABLE [app].[Batches] DROP COLUMN [ExecutionOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Batches]') AND name = 'ExecutionOrder') BEGIN ALTER TABLE [vsto].[Batches] DROP COLUMN [ExecutionOrder] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Batches]') AND name = 'PackageSubpath') BEGIN ALTER TABLE [archive].[Batches] DROP COLUMN [PackageSubpath] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Batches]') AND name = 'PackageSubpath') BEGIN ALTER TABLE [app].[Batches] DROP COLUMN [PackageSubpath] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Batches]') AND name = 'PackageSubpath') BEGIN ALTER TABLE [vsto].[Batches] DROP COLUMN [PackageSubpath] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [archive].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [app].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [vsto].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [archive].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [app].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'Description') BEGIN ALTER TABLE [vsto].[BusinessEntityRelationships] DROP COLUMN [Description] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityRelationships]') AND name = 'Tags') BEGIN ALTER TABLE [archive].[BusinessEntityRelationships] DROP COLUMN [Tags] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'Tags') BEGIN ALTER TABLE [app].[BusinessEntityRelationships] DROP COLUMN [Tags] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'Tags') BEGIN ALTER TABLE [vsto].[BusinessEntityRelationships] DROP COLUMN [Tags] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityTypes]') AND name = 'DisplayOrder') BEGIN ALTER TABLE [archive].[BusinessEntityTypes] DROP COLUMN [DisplayOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityTypes]') AND name = 'DisplayOrder') BEGIN ALTER TABLE [app].[BusinessEntityTypes] DROP COLUMN [DisplayOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'DisplayOrder') BEGIN ALTER TABLE [vsto].[BusinessEntityTypes] DROP COLUMN [DisplayOrder] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityRelationships]') AND name = 'RelationshipType') BEGIN ALTER TABLE [archive].[BusinessEntityRelationships] DROP COLUMN [RelationshipType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'RelationshipType')
BEGIN 
	SELECT @qry = (

	SELECT'DROP INDEX ['+i.name+'] ON ['+s.name+'].['+o.name+'];'
	FROM sys.indexes i 
		INNER JOIN sys.objects o ON i.object_id=o.object_id
		INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
	WHERE o.type <> 'S' AND is_primary_key <> 1 AND index_id > 0
	AND s.name <> 'sys' AND s.name <> 'sys' AND is_unique = 1
	AND o.object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]')
	FOR XML PATH(''));

	EXEC sp_executesql @qry
END 
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'DF_appBusinessEntityRelationships_RelationshipType') BEGIN ALTER TABLE [app].[BusinessEntityRelationships] DROP CONSTRAINT [DF_appBusinessEntityRelationships_RelationshipType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityRelationships]') AND name = 'RelationshipType') BEGIN ALTER TABLE [app].[BusinessEntityRelationships] DROP COLUMN [RelationshipType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'RelationshipType')
BEGIN 
	SELECT @qry = (

	SELECT'DROP INDEX ['+i.name+'] ON ['+s.name+'].['+o.name+'];'
	FROM sys.indexes i 
		INNER JOIN sys.objects o ON i.object_id=o.object_id
		INNER JOIN sys.schemas s ON o.schema_id = s.schema_id
	WHERE o.type <> 'S' AND is_primary_key <> 1 AND index_id > 0
	AND s.name <> 'sys' AND s.name <> 'sys' AND is_unique = 1
	AND o.object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]')
	FOR XML PATH(''));

	EXEC sp_executesql @qry
END 
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'RelationshipType') BEGIN ALTER TABLE [vsto].[BusinessEntityRelationships] DROP COLUMN [RelationshipType] END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModelEntityRelationships]') AND type IN(N'U')) DROP TABLE [vsto].[BusinessModelEntityRelationships];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[BusinessModelEntityRelationships]') AND type IN(N'U')) DROP TABLE [archive].[BusinessModelEntityRelationships];
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[BusinessModelEntityRelationships]') AND type IN(N'U')) DROP TABLE [app].[BusinessModelEntityRelationships]; 

IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'DF_appBusinessClassifications_IsModelEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP CONSTRAINT [DF_appBusinessClassifications_IsModelEnabled] END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'DF_appBusinessClassifications_IsEntityEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP CONSTRAINT [DF_appBusinessClassifications_IsEntityEnabled] END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'DF_appBusinessClassifications_IsAttributeEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP CONSTRAINT [DF_appBusinessClassifications_IsAttributeEnabled] END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'DF_appBusinessClassifications_IsRelationshipEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP CONSTRAINT [DF_appBusinessClassifications_IsRelationshipEnabled] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'IsAttributeEnabled') BEGIN ALTER TABLE [vsto].[BusinessClassifications] DROP COLUMN [IsAttributeEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'IsAttributeEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP COLUMN [IsAttributeEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessClassifications]') AND name = 'IsAttributeEnabled') BEGIN ALTER TABLE [archive].[BusinessClassifications] DROP COLUMN [IsAttributeEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'IsEntityEnabled') BEGIN ALTER TABLE [vsto].[BusinessClassifications] DROP COLUMN [IsEntityEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'IsEntityEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP COLUMN [IsEntityEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessClassifications]') AND name = 'IsEntityEnabled') BEGIN ALTER TABLE [archive].[BusinessClassifications] DROP COLUMN [IsEntityEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'IsModelEnabled') BEGIN ALTER TABLE [vsto].[BusinessClassifications] DROP COLUMN [IsModelEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'IsModelEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP COLUMN [IsModelEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessClassifications]') AND name = 'IsModelEnabled') BEGIN ALTER TABLE [archive].[BusinessClassifications] DROP COLUMN [IsModelEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'IsRelationshipEnabled') BEGIN ALTER TABLE [vsto].[BusinessClassifications] DROP COLUMN [IsRelationshipEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'IsRelationshipEnabled') BEGIN ALTER TABLE [app].[BusinessClassifications] DROP COLUMN [IsRelationshipEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessClassifications]') AND name = 'IsRelationshipEnabled') BEGIN ALTER TABLE [archive].[BusinessClassifications] DROP COLUMN [IsRelationshipEnabled] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessClassifications]') AND name = 'ParentUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessClassifications_REF_appBusinessClassifications', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessClassifications] DROP CONSTRAINT [FK_appBusinessClassifications_REF_appBusinessClassifications]
	ALTER TABLE [app].[BusinessClassifications] DROP COLUMN [ParentUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'Parent') BEGIN ALTER TABLE [vsto].[BusinessClassifications] DROP COLUMN [Parent] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessClassifications]') AND name = 'ParentUID') BEGIN ALTER TABLE [archive].[BusinessClassifications] DROP COLUMN [ParentUID] END


IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[DeleteBusinessTypes]')) BEGIN DROP PROCEDURE [archive].[DeleteBusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[CloneBusinessTypes]')) BEGIN DROP PROCEDURE [vsto].[CloneBusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[SetBusinessTypes]')) BEGIN DROP PROCEDURE [vsto].[SetBusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[wcf].[GetVstoBusinessTypes]')) BEGIN DROP PROCEDURE [wcf].[GetVstoBusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[BusinessTypes]') AND type IN(N'U')) BEGIN DROP TABLE [vsto].[BusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[SetBusinessTypes]')) BEGIN DROP PROCEDURE [archive].[SetBusinessTypes] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[BusinessTypes]') AND type IN(N'U')) BEGIN DROP TABLE [archive].[BusinessTypes] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntities]') AND name = 'BusinessTypeUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessEntities_REF_appBusinessType', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessEntities] DROP CONSTRAINT [FK_appBusinessEntities_REF_appBusinessType]
	ALTER TABLE [app].[BusinessEntities] DROP COLUMN [BusinessTypeUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntities]') AND name = 'BusinessClassificationUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessEntities_REF_appBusinessClassification', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessEntities] DROP CONSTRAINT [FK_appBusinessEntities_REF_appBusinessClassification]
	ALTER TABLE [app].[BusinessEntities] DROP COLUMN [BusinessClassificationUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessAttributes]') AND name = 'BusinessTypeUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessAttributes_REF_appBusinessType', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessAttributes] DROP CONSTRAINT [FK_appBusinessAttributes_REF_appBusinessType]
	ALTER TABLE [app].[BusinessAttributes] DROP COLUMN [BusinessTypeUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessAttributes]') AND name = 'BusinessClassificationUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessAttributes_REF_appBusinessClassification', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessAttributes] DROP CONSTRAINT [FK_appBusinessAttributes_REF_appBusinessClassification]
	ALTER TABLE [app].[BusinessAttributes] DROP COLUMN [BusinessClassificationUID] 
END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[BusinessTypes]') AND type IN(N'U')) BEGIN DROP TABLE [archive].[BusinessTypes] END


IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessModels]') AND name = 'BusinessTypeUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessModels_REF_appBusinessType', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessModels] DROP CONSTRAINT [FK_appBusinessModels_REF_appBusinessType]
	ALTER TABLE [app].[BusinessModels] DROP COLUMN [BusinessTypeUID] 
END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[BusinessTypes]') AND type IN(N'U')) BEGIN DROP TABLE [app].[BusinessTypes] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntities]') AND name = 'BusinessSubjectUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessEntities_REF_appBusinessSubject', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessEntities] DROP CONSTRAINT [FK_appBusinessEntities_REF_appBusinessSubject]
	ALTER TABLE [app].[BusinessEntities] DROP COLUMN [BusinessSubjectUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessModels]') AND name = 'BusinessClassificationUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessModels_REF_appBusinessClassification', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessModels] DROP CONSTRAINT [FK_appBusinessModels_REF_appBusinessClassification]
	ALTER TABLE [app].[BusinessModels] DROP COLUMN [BusinessClassificationUID] 
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessModels]') AND name = 'BusinessSubjectUID') 
BEGIN 
	IF (OBJECT_ID('app.FK_appBusinessModels_REF_appBusinessSubject', 'F') IS NOT NULL) ALTER TABLE [app].[BusinessModels] DROP CONSTRAINT [FK_appBusinessModels_REF_appBusinessSubject]
	ALTER TABLE [app].[BusinessModels] DROP COLUMN [BusinessSubjectUID] 
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntities]') AND name = 'ObjectTypeId') 
BEGIN 
	DECLARE @beSql NVARCHAR(MAX) = 'UPDATE [app].[BusinessEntities] SET [ObjectTypeId] = NULL WHERE [ObjectTypeId] IS NOT NULL'
	EXEC sp_executesql @beSql
	EXEC dbo.sp_rename @objname=N'[app].[BusinessEntities].[ObjectTypeId]', @newname=N'BusinessObjectTypeId', @objtype= 'COLUMN'
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntities]') AND name = 'ObjectTypeId') BEGIN ALTER TABLE [archive].[BusinessEntities] DROP COLUMN [ObjectTypeId] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntities]') AND name = 'ObjectType') BEGIN ALTER TABLE [vsto].[BusinessEntities] DROP COLUMN [ObjectType] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'IsMatrixEnabled') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessEntityTypes].[IsMatrixEnabled]', @newname=N'IsEvent', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityTypes]') AND name = 'IsMatrixEnabled') BEGIN EXEC dbo.sp_rename @objname=N'[app].[BusinessEntityTypes].[IsMatrixEnabled]', @newname=N'IsEvent', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityTypes]') AND name = 'IsMatrixEnabled') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[BusinessEntityTypes].[IsMatrixEnabled]', @newname=N'IsEvent', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.default_constraints WHERE parent_object_id = OBJECT_ID(N'[app].[BusinessEntityTypes]') AND name = 'DF_appBusinessEntityTypes_IsMatrixEnabled') 
BEGIN 
	ALTER TABLE [app].[BusinessEntityTypes] DROP CONSTRAINT [DF_appBusinessEntityTypes_IsMatrixEnabled] 
END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[SetAdfLinkedServices]')) BEGIN DROP PROCEDURE [vsto].[SetAdfLinkedServices] END
IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[CloneAdfLinkedServices]')) BEGIN DROP PROCEDURE [vsto].[CloneAdfLinkedServices] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessAttributes]') AND name = 'BusinessTypeUID') BEGIN ALTER TABLE [archive].[BusinessAttributes] DROP COLUMN [BusinessTypeUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntities]') AND name = 'BusinessTypeUID') BEGIN ALTER TABLE [archive].[BusinessEntities] DROP COLUMN [BusinessTypeUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntities]') AND name = 'BusinessClassificationUID') BEGIN ALTER TABLE [archive].[BusinessEntities] DROP COLUMN [BusinessClassificationUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessAttributes]') AND name = 'BusinessClassificationUID') BEGIN ALTER TABLE [archive].[BusinessAttributes] DROP COLUMN [BusinessClassificationUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntities]') AND name = 'BusinessSubjectUID') BEGIN ALTER TABLE [archive].[BusinessEntities] DROP COLUMN [BusinessSubjectUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessModels]') AND name = 'BusinessSubjectUID') BEGIN ALTER TABLE [archive].[BusinessModels] DROP COLUMN [BusinessSubjectUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessModels]') AND name = 'BusinessClassificationUID') BEGIN ALTER TABLE [archive].[BusinessModels] DROP COLUMN [BusinessClassificationUID] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessModels]') AND name = 'BusinessTypeUID') BEGIN ALTER TABLE [archive].[BusinessModels] DROP COLUMN [BusinessTypeUID] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessAttributes]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessAttributes].[Name]', @newname=N'BusinessAttribute', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessAttributes]') AND name = 'BusinessType') BEGIN ALTER TABLE [vsto].[BusinessAttributes] DROP COLUMN [BusinessType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessAttributes]') AND name = 'BusinessClassification') BEGIN ALTER TABLE [vsto].[BusinessAttributes] DROP COLUMN [BusinessClassification] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntities]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessEntities].[Name]', @newname=N'BusinessEntity', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntities]') AND name = 'BusinessType') BEGIN ALTER TABLE [vsto].[BusinessEntities] DROP COLUMN [BusinessType] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntities]') AND name = 'BusinessClassification') BEGIN ALTER TABLE [vsto].[BusinessEntities] DROP COLUMN [BusinessClassification] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModels]') AND name = 'BusinessClassification') BEGIN ALTER TABLE [vsto].[BusinessModels] DROP COLUMN [BusinessClassification] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModels]') AND name = 'BusinessSubject') BEGIN ALTER TABLE [vsto].[BusinessModels] DROP COLUMN [BusinessSubject] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModels]') AND name = 'BusinessType') BEGIN ALTER TABLE [vsto].[BusinessModels] DROP COLUMN [BusinessType] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessEntityTypes].[Name]', @newname=N'BusinessEntityType', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'ModelTypes') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessEntityTypes].[ModelTypes]', @newname=N'Methodology', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntityTypes]') AND name = 'ModelTypes') BEGIN EXEC dbo.sp_rename @objname=N'[app].[BusinessEntityTypes].[ModelTypes]', @newname=N'Methodology', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntityTypes]') AND name = 'ModelTypes') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[BusinessEntityTypes].[ModelTypes]', @newname=N'Methodology', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModelEntities]') AND name = 'IsPrimaryMatrixEntity') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessModelEntities].[IsPrimaryMatrixEntity]', @newname=N'IsPrimaryEvent', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessModelEntities]') AND name = 'IsPrimaryMatrixEntity') BEGIN EXEC dbo.sp_rename @objname=N'[app].[BusinessModelEntities].[IsPrimaryMatrixEntity]', @newname=N'IsPrimaryEvent', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessModelEntities]') AND name = 'IsPrimaryMatrixEntity') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[BusinessModelEntities].[IsPrimaryMatrixEntity]', @newname=N'IsPrimaryEvent', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModelEntities]') AND name = 'EntitySortOrder') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessModelEntities].[EntitySortOrder]', @newname=N'SortOrder', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessModelEntities]') AND name = 'EntitySortOrder') BEGIN EXEC dbo.sp_rename @objname=N'[app].[BusinessModelEntities].[EntitySortOrder]', @newname=N'SortOrder', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessModelEntities]') AND name = 'EntitySortOrder') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[BusinessModelEntities].[EntitySortOrder]', @newname=N'SortOrder', @objtype='COLUMN' END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessAttributes]') AND name = 'ShortName') BEGIN ALTER TABLE [vsto].[BusinessAttributes] DROP COLUMN [ShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessAttributes]') AND name = 'ShortName') BEGIN ALTER TABLE [app].[BusinessAttributes] DROP COLUMN [ShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessAttributes]') AND name = 'ShortName') BEGIN ALTER TABLE [archive].[BusinessAttributes] DROP COLUMN [ShortName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntities]') AND name = 'BusinessName') BEGIN ALTER TABLE [vsto].[BusinessEntities] DROP COLUMN [BusinessName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessEntities]') AND name = 'BusinessName') BEGIN ALTER TABLE [app].[BusinessEntities] DROP COLUMN [BusinessName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessEntities]') AND name = 'BusinessName') BEGIN ALTER TABLE [archive].[BusinessEntities] DROP COLUMN [BusinessName] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessAttributes]') AND name = 'Grouping') BEGIN ALTER TABLE [vsto].[BusinessAttributes] DROP COLUMN [Grouping] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[BusinessAttributes]') AND name = 'Grouping') BEGIN ALTER TABLE [app].[BusinessAttributes] DROP COLUMN [Grouping] END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[BusinessAttributes]') AND name = 'Grouping') BEGIN ALTER TABLE [archive].[BusinessAttributes] DROP COLUMN [Grouping] END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessClassifications]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessClassifications].[Name]', @newname=N'BusinessClassification', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityRelationships]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessEntityRelationships].[Name]', @newname=N'BusinessEntityRelationship', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModelConfigurations]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessModelConfigurations].[Name]', @newname=N'BusinessModelConfiguration', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModelEntities]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessModelEntities].[Name]', @newname=N'BusinessModelEntity', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessModels]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessModels].[Name]', @newname=N'BusinessModel', @objtype='COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessTags]') AND name = 'Name') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[BusinessTags].[Name]', @newname=N'BusinessTag', @objtype='COLUMN' END

DECLARE @vstoSql NVARCHAR(MAX);

SELECT	@vstoSql = (
	SELECT	'DELETE FROM [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] WHERE [CustomerUID] <> ''00000000-0000-0000-0000-000000000000'' AND [ChangeDate] < DATEADD(DD, -2, GETDATE()); '
	FROM	INFORMATION_SCHEMA.TABLES
	WHERE	TABLE_SCHEMA = 'vsto'
		AND	TABLE_NAME NOT IN ('Customers', 'Versions')
FOR XML PATH(''));

SET	@vstoSql = REPLACE(REPLACE(@vstoSql, '&lt;', '<'), '&gt;', '>')
EXEC sp_executesql @vstoSql;

SELECT @vstoSql = (
	SELECT	'ALTER TABLE [' + s.name + '].[' + o.name + '] DROP CONSTRAINT ['+i.name+'];'
	FROM	sys.indexes i 
	INNER JOIN sys.objects o 
		ON	i.object_id=o.object_id
	INNER JOIN sys.schemas s 
		ON	o.schema_id = s.schema_id
	INNER JOIN sys.index_columns ic 
		ON	i.object_id=ic.object_id 
		AND	i.index_id = ic.index_id
	INNER JOIN sys.columns c 
		ON	ic.object_id = c.object_id 
		AND	ic.column_id = c.column_id
	WHERE	o.type <> 'S' 
		AND	is_primary_key = 1 
		AND	c.name = 'id'
		AND	s.name = 'vsto'
FOR XML PATH(''));
EXEC sp_executesql @vstoSql

SELECT	@vstoSql = (
	SELECT	'ALTER TABLE [' + TABLE_SCHEMA + '].[' + TABLE_NAME + '] DROP COLUMN [' + COLUMN_NAME + '];'
	FROM	INFORMATION_SCHEMA.COLUMNS
	WHERE	TABLE_SCHEMA = 'vsto'
		AND	COLUMN_NAME = 'id'
FOR XML PATH(''));
EXEC sp_executesql @vstoSql

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Customers] d
	ON	us.[CustomerUID] = d.[UID]
WHERE	us.[CustomerUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Versions] d
	ON	us.[VersionUID] = d.[UID]
WHERE	us.[VersionUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Batches] d
	ON	us.[BatchUID] = d.[UID]
WHERE	us.[BatchUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Projects] d
	ON	us.[ProjectUID] = d.[UID]
WHERE	us.[ProjectUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Connections] d
	ON	us.[ConnectionUID] = d.[UID]
WHERE	us.[ConnectionUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Objects] d
	ON	us.[ObjectUID] = d.[UID]
WHERE	us.[ObjectUID] IS NOT NULL
	AND	d.[UID] IS NULL

DELETE us
FROM	[app].[UserSettings] us
LEFT OUTER JOIN [app].[Columns] d
	ON	us.[ColumnUID] = d.[UID]
WHERE	us.[ColumnUID] IS NOT NULL
	AND	d.[UID] IS NULL

-- Clean up faulty registered BusinessEntityTypes where [Name]/[BusinessEntityType] is null for the template customer
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'BusinessEntityType') 
BEGIN
	DECLARE @sqlBusinessEntityType NVARCHAR(MAX)
	SET @sqlBusinessEntityType = 'DELETE FROM [vsto].[BusinessEntityTypes] WHERE [BusinessEntityType] IS NULL AND CustomerUID = ''00000000-0000-0000-0000-000000000000'''
	EXEC sp_executesql @sqlBusinessEntityType
END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[BusinessEntityTypes]') AND name = 'Name') 
BEGIN
	DECLARE @sqlBusinessEntityTypeName NVARCHAR(MAX)
	SET @sqlBusinessEntityTypeName = 'DELETE FROM [vsto].[BusinessEntityTypes] WHERE [Name] IS NULL AND CustomerUID = ''00000000-0000-0000-0000-000000000000'''
	EXEC sp_executesql @sqlBusinessEntityTypeName
END

IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AccountKey') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AccountKey] VARCHAR(512) NULL END

IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Type') 
	AND EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[AdfLinkedServices]') AND type IN(N'U'))
BEGIN
	IF EXISTS (SELECT * FROM sysobjects WHERE id = OBJECT_ID(N'[app].[GetEligibleAdfLinkedServices]') AND xtype IN (N'FN', N'IF', N'TF')) BEGIN DROP FUNCTION [app].[GetEligibleAdfLinkedServices] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[meta].[LinkedServiceType]') AND type IN(N'U')) BEGIN DROP TABLE [meta].[LinkedServiceType] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[app].[GetAdfLinkedServiceList]')) BEGIN DROP PROCEDURE [app].[GetAdfLinkedServiceList] END
	--IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[DeleteAdfLinkedServices]')) BEGIN DROP PROCEDURE [archive].[DeleteAdfLinkedServices] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[CloneAdfLinkedServices]')) BEGIN DROP PROCEDURE [vsto].[CloneAdfLinkedServices] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[SetAdfLinkedServices]')) BEGIN DROP PROCEDURE [vsto].[SetAdfLinkedServices] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[wcf].[GetModelAdfLinkedServices]')) BEGIN DROP PROCEDURE [wcf].[GetModelAdfLinkedServices] END
	IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[wcf].[GetVstoAdfLinkedServices]')) BEGIN DROP PROCEDURE [wcf].[GetVstoAdfLinkedServices] END
	--IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND type IN(N'U')) BEGIN DROP TABLE [vsto].[AdfLinkedServices] END
	--IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[SetAdfLinkedServices]')) BEGIN DROP PROCEDURE [archive].[SetAdfLinkedServices] END
	--IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[archive].[AdfLinkedServices]') AND type IN(N'U')) BEGIN DROP TABLE [archive].[AdfLinkedServices] END

	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Type') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Type] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ConnectVia_IntegrationRuntime') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ConnectVia_IntegrationRuntime] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ConnectionString') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ConnectionString] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ConnectionStringKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ConnectionStringKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ConnectionStringKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ConnectionStringKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ConnectionStringKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ConnectionStringKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_PasswordKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_PasswordKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_PasswordKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_PasswordKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_PasswordKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_PasswordKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServicePrincipalId') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServicePrincipalId] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServicePrincipalKey') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServicePrincipalKey] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServicePrincipalKeyKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServicePrincipalKeyKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServicePrincipalKeyKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServicePrincipalKeyKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServicePrincipalKeyKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServicePrincipalKeyKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Tenant') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Tenant] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Username') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Username] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Password') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Password] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AuthenticationMethod') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AuthenticationMethod] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AccountKeyKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AccountKeyKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AccountKeyKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AccountKeyKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AccountKeyKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AccountKeyKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Db') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Db] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Credential') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Credential] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredentialKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredentialKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredentialKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredentialKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredentialKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredentialKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_AuthenticationType') BEGIN ALTER TABLE [app].[Connections] ADD [LS_AuthenticationType] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Server') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Server] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredStringKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredStringKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredStringKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredStringKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_CredStringKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_CredStringKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_EncryptedCredential') BEGIN ALTER TABLE [app].[Connections] ADD [LS_EncryptedCredential] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasUri') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasUri] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasUriKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasUriKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasUriKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasUriKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasUriKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasUriKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasTokenKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasTokenKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasTokenKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasTokenKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SasTokenKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SasTokenKVS_SecretVersion] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Endpoint') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Endpoint] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Url') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Url] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_DeploymentType') BEGIN ALTER TABLE [app].[Connections] ADD [LS_DeploymentType] VARCHAR(100) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Organization') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Organization] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Host') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Host] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_ServiceUri') BEGIN ALTER TABLE [app].[Connections] ADD [LS_ServiceUri] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Port') BEGIN ALTER TABLE [app].[Connections] ADD [LS_Port] VARCHAR(20) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SecurityToken') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SecurityToken] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SecurityTokenKVS_UID') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SecurityTokenKVS_UID] UNIQUEIDENTIFIER NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SecurityTokenKVS_SecretName') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SecurityTokenKVS_SecretName] VARCHAR(512) NULL END
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_SecurityTokenKVS_SecretVersion') BEGIN ALTER TABLE [app].[Connections] ADD [LS_SecurityTokenKVS_SecretVersion] VARCHAR(512) NULL END

	SET	IDENTITY_INSERT [meta].[IntegrationStage] ON;
	MERGE [meta].[IntegrationStage] AS TARGET
	USING 
	(	SELECT	 99 AS [Id]
				,'AKV' AS [Code]
				,'Azure Key Vault' AS [Name]
				,1 AS [IsEnabled]
	) AS src
		ON	TARGET.[Id] = src.[Id]
	WHEN MATCHED THEN 
		UPDATE 
		SET		 [Code] = src.[Code]
				,[Name] = src.[Name]
				,[IsEnabled] = src.[IsEnabled]
	WHEN NOT MATCHED THEN
		INSERT	([Id]
				,[Code]
				,[Name]
				,[IsEnabled])
		VALUES	([Id]
				,[Code]
				,[Name]
				,[IsEnabled]);
	SET	IDENTITY_INSERT [meta].[IntegrationStage] OFF;

	DECLARE @sqlConn NVARCHAR(MAX)
	SET @sqlConn = '
	MERGE [app].[Connections] AS TARGET
	USING (
		SELECT	 [UID]
				,[CustomerUID]
				,[VersionUID]
				,[Name]
				,[Url] AS [ConnectionString]
				,99 AS [ConnectionTypeId]
				,99 AS [SystemTypeId]
				,99 AS [IntegrationStageId]
				,1 AS [IsCloudEnabled]
				,[LinkedServiceType] AS [LS_Type]
				,[Url] AS [LS_Url]
		FROM	[app].[AdfLinkedServices]
		WHERE	[LinkedServiceType] = ''AstAdfAzureKeyVaultLinkedServiceNode''
		) AS src
		ON TARGET.[UID] = src.[UID]
	WHEN MATCHED THEN
		UPDATE
		SET  [UID] = src.[UID]
			,[Name] = src.[Name]
			,[CustomerUID] = src.[CustomerUID]
			,[VersionUID] = src.[VersionUID]
			,[ConnectionString] = src.[ConnectionString]
			,[ConnectionTypeId] = src.[ConnectionTypeId]
			,[SystemTypeId] = src.[SystemTypeId]
			,[IntegrationStageId] = src.[IntegrationStageId]
			,[IsCloudEnabled] = src.[IsCloudEnabled]
			,[LS_Type] = src.[LS_Type]
			,[LS_Url] = src.[LS_Url]
	WHEN NOT MATCHED THEN
		INSERT	([UID]
				,[Name]
				,[CustomerUID]
				,[VersionUID]
				,[ConnectionString]
				,[ConnectionTypeId]
				,[SystemTypeId]
				,[IntegrationStageId]
				,[IsCloudEnabled]
				,[LS_Type]
				,[LS_Url])
		VALUES	(src.[UID]
				,src.[Name]
				,src.[CustomerUID]
				,src.[VersionUID]
				,src.[ConnectionString]
				,src.[ConnectionTypeId]
				,src.[SystemTypeId]
				,src.[IntegrationStageId]
				,src.[IsCloudEnabled]
				,src.[LS_Type]
				,src.[LS_Url]);

	UPDATE	 cn
	SET		 [LS_Type] = ls.[LinkedServiceType]
			,[LS_ConnectionString] = ls.[ConnectionString]
			,[LS_ConnectVia_IntegrationRuntime] = ls.[ConnectVia_IntegrationRuntime]
			,[LS_ConnectionStringKVS_UID] = ls.[ConnectionStringKVS_UID]
			,[LS_ConnectionStringKVS_SecretName] = ls.[ConnectionStringKVS_SecretName]
			,[LS_ConnectionStringKVS_SecretVersion] = ls.[ConnectionStringKVS_SecretVersion]
			,[LS_PasswordKVS_UID] = ls.[PasswordKVS_UID]
			,[LS_PasswordKVS_SecretName] = ls.[PasswordKVS_SecretName]
			,[LS_PasswordKVS_SecretVersion] = ls.[PasswordKVS_SecretVersion]
			,[LS_ServicePrincipalId] = ls.[ServicePrincipalId]
			,[LS_ServicePrincipalKey] = ls.[ServicePrincipalKey]
			,[LS_ServicePrincipalKeyKVS_UID] = ls.[ServicePrincipalKeyKVS_UID]
			,[LS_ServicePrincipalKeyKVS_SecretName] = ls.[ServicePrincipalKeyKVS_SecretName]
			,[LS_ServicePrincipalKeyKVS_SecretVersion] = ls.[ServicePrincipalKeyKVS_SecretVersion]
			,[LS_Tenant] = ls.[Tenant]
			,[LS_Username] = ls.[Username]
			,[LS_Password] = ls.[Password]
			,[LS_AuthenticationMethod] = ls.[AuthenticationMethod]
			,[LS_AccountKeyKVS_UID] = ls.[AccountKeyKVS_UID]
			,[LS_AccountKeyKVS_SecretName] = ls.[AccountKeyKVS_SecretName]
			,[LS_AccountKeyKVS_SecretVersion] = ls.[AccountKeyKVS_SecretVersion]
			,[LS_Db] = ls.[Db]
			,[LS_Credential] = ls.[Credential]
			,[LS_CredentialKVS_UID] = ls.[CredentialKVS_UID]
			,[LS_CredentialKVS_SecretName] = ls.[CredentialKVS_SecretName]
			,[LS_CredentialKVS_SecretVersion] = ls.[CredentialKVS_SecretVersion]
			,[LS_AuthenticationType] = ls.[AdfLinkedServiceAuthenticationType]
			,[LS_Server] = ls.[Server]
			,[LS_CredStringKVS_UID] = ls.[CredStringKVS_UID]
			,[LS_CredStringKVS_SecretName] = ls.[CredStringKVS_SecretName]
			,[LS_CredStringKVS_SecretVersion] = ls.[CredStringKVS_SecretVersion]
			,[LS_EncryptedCredential] = ls.[EncryptedCredential]
			,[LS_SasUri] = ls.[SasUri]
			,[LS_SasUriKVS_UID] = ls.[SasUriKVS_UID]
			,[LS_SasUriKVS_SecretName] = ls.[SasUriKVS_SecretName]
			,[LS_SasUriKVS_SecretVersion] = ls.[SasUriKVS_SecretVersion]
			,[LS_SasTokenKVS_UID] = ls.[SasTokenKVS_UID]
			,[LS_SasTokenKVS_SecretName] = ls.[SasTokenKVS_SecretName]
			,[LS_SasTokenKVS_SecretVersion] = ls.[SasTokenKVS_SecretVersion]
			,[LS_Endpoint] = ls.[Endpoint]
			,[LS_Url] = ls.[Url]
			,[LS_DeploymentType] = ls.[DeploymentType]
			,[LS_Organization] = ls.[Organization]
			,[LS_Host] = ls.[Host]
			,[LS_ServiceUri] = ls.[ServiceUri]
			,[LS_Port] = ls.[Port]
	FROM	[app].[AdfLinkedServices] ls
	INNER JOIN [app].[Connections] cn
		ON	ls.[ConnectionUID] = cn.[UID]'
	EXEC sp_executesql @sqlConn
END

-- Add column as part of migration so that no null errors occur on new index creation
IF NOT EXISTS (SELECT 1 FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND name = 'Name') 
	AND EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[vsto].[AdfLinkedServices]') AND type IN(N'U'))
BEGIN
	EXEC ('ALTER TABLE [vsto].[AdfLinkedServices] ADD [Name] VARCHAR(512) NULL')
	DECLARE @vstoAdfLinkedServicesName NVARCHAR(MAX)
	SET @vstoAdfLinkedServicesName = 'UPDATE [vsto].[AdfLinkedServices] SET [Name] = [Connection] WHERE [Name] IS NULL'
	EXEC sp_executesql @vstoAdfLinkedServicesName
END

IF EXISTS (SELECT 1 FROM [meta].[ConnectionType] WHERE [Code] IN ('OLESQL', 'ADOSQL', 'ODBCSQL') AND [IsEnabled] = 1)
BEGIN
	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'PushdownProcessing') BEGIN ALTER TABLE [app].[Projects] ADD [PushdownProcessing] BIT NULL END

	DECLARE @sqlPushdown NVARCHAR(MAX)
	SET @sqlPushdown = '
	UPDATE	p
	SET		[PushdownProcessing] = 
				CASE 
					WHEN it.[Code] NOT IN (''SSIS'',''DFL'') THEN 1
					WHEN it.[Code] = ''SSIS'' AND sc.[ConnectionTypeId] IN (8,24) THEN 1	-- Azure Blob Storage or Azure Data Lake Store
					WHEN it.[Code] = ''SSIS'' AND sc.[SystemTypeId] IN (10,17) THEN 1		-- Snowflake or Synapse
					WHEN it.[Code] = ''SSIS'' AND psc.[SystemTypeId] IN (10,17) THEN 1		-- Snowflake or Synapse
					WHEN it.[Code] = ''SSIS'' AND tc.[SystemTypeId] IN (10,17) THEN 1		-- Snowflake or Synapse
					WHEN it.[Code] = ''SSIS'' AND sc.[ConnectionTypeId] IN (17,18,23) THEN 1	-- ''OLESQL'', ''ADOSQL'', ''ODBCSQL''
					WHEN it.[Code] = ''SSIS'' AND psc.[ConnectionTypeId] IN (17,18,23) THEN 1	-- ''OLESQL'', ''ADOSQL'', ''ODBCSQL''
					WHEN it.[Code] = ''SSIS'' AND tc.[ConnectionTypeId] IN (17,18,23) THEN 1	-- ''OLESQL'', ''ADOSQL'', ''ODBCSQL''
					WHEN ISNULL(p.[PushdownProcessing], 0) = 1 THEN 1 
					ELSE 0 
				END
	FROM [app].[Projects] p
	INNER JOIN [app].[Connections] c
		ON	p.[SourceConnectionUID] = c.[UID]
	LEFT OUTER JOIN [app].[Connections] sc
		ON	p.[StageConnectionUID] = sc.[UID]
		AND	c.[IntegrationStageId] = 1
	LEFT OUTER JOIN [app].[Connections] psc
		ON	p.[PersistentStageConnectionUID] = psc.[UID]
		AND	c.[IntegrationStageId] = 1
	INNER JOIN[app].[Connections] tc
		ON	p.[TargetConnectionUID] = tc.[UID]
	INNER JOIN [meta].[IntegrationStage] tis
		ON	tc.[IntegrationStageId] = tis.[Id]
	LEFT OUTER JOIN [meta].[IntegrationTemplate] it
		ON	p.[IntegrationTemplateId] = it.[Id]
	WHERE	it.[Code] = ''SSIS'''

	EXEC sp_executesql @sqlPushdown

	UPDATE	c
	SET		[ConnectionTypeId] = 
				CASE
					WHEN [ConnectionTypeId] = 17 THEN 1
					WHEN [ConnectionTypeId] = 18 THEN 2
					WHEN [ConnectionTypeId] = 23 THEN 4
				END
	FROM	[app].[Connections] c
	WHERE	c.[ConnectionTypeId] IN (17,18,23)
END

IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Settings]') AND name = 'SettingDisplayRow') BEGIN ALTER TABLE [app].[Settings] ADD [SettingDisplayRow] SMALLINT NULL END
IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Settings]') AND name = 'ToolTipDescription') BEGIN ALTER TABLE [app].[Settings] ADD [ToolTipDescription] NVARCHAR(2000) NULL END
IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Settings]') AND name = 'IsToolTipEnabled') BEGIN ALTER TABLE [app].[Settings] ADD [IsToolTipEnabled] BIT NULL END

IF EXISTS(SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[UpgradeLog]') AND type IN(N'U'))
BEGIN
	TRUNCATE TABLE [dbo].[UpgradeLog]
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LS_Type')
BEGIN
	DECLARE @sqlAccountKey NVARCHAR(MAX)
	SET @sqlAccountKey = '
	UPDATE	c
	SET		 [LS_AccountKey] = REPLACE(REPLACE(REPLACE(REPLACE([LS_ConnectionString], ''AccountKey='', ''''), ''=;'', ''=''), ''=;'', ''=''), ''=;'', ''='')
			,[LS_ConnectionString] = NULL
	FROM	[app].[Connections] c
	WHERE	[LS_AuthenticationMethod] = ''Account Key''
		AND	[LS_Type] = ''AstAdfAzureDataLakeStoreGen2LinkedServiceNode''
		AND	ISNULL([LS_ConnectionString], '''') <> '''''
	EXEC sp_executesql @sqlAccountKey
END

IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'ProjectFolder') BEGIN EXEC dbo.sp_rename @objname=N'[app].[Projects].[ProjectFolder]', @newname=N'DisplayFolder', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Projects]') AND name = 'ProjectFolder') BEGIN EXEC dbo.sp_rename @objname=N'[vsto].[Projects].[ProjectFolder]', @newname=N'DisplayFolder', @objtype= 'COLUMN' END
IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Projects]') AND name = 'ProjectFolder') BEGIN EXEC dbo.sp_rename @objname=N'[archive].[Projects].[ProjectFolder]', @newname=N'DisplayFolder', @objtype= 'COLUMN' END

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_DataTypeMappings') EXEC ('DISABLE TRIGGER [app].[tdel_DataTypeMappings] ON [app].[DataTypeMappings]');

IF OBJECT_ID('tempdb..#DataTypeMappings') IS NOT NULL DROP TABLE #DataTypeMappings
SELECT	 [CustomerUID]
		,[VersionUID]
		,[Name] AS [OldName]
		,'AString' + SUBSTRING([Name], 3, 50) AS [NewName]
INTO	#DataTypeMappings
FROM	[app].[DataTypeMappings] dtm
WHERE	LEFT([Name], 2) = 'AS'
	AND [Name] NOT LIKE 'AString%'
	AND	[DataTypeId] = 1
	AND	[IsMaster] = 0
	AND	(ISNUMERIC(SUBSTRING([Name], 3, 1)) = 1 OR [Name] = 'ASMAX')
UNION
SELECT	 [CustomerUID]
		,[VersionUID]
		,[Name] AS [OldName]
		,'AStringFixed' + SUBSTRING([Name], 4, 50) AS [NewName]
FROM	[app].[DataTypeMappings] dtm
WHERE	LEFT([Name], 3) = 'ASF'
	AND [Name] NOT LIKE 'AString%'
	AND	[DataTypeId] = 2
	AND	[IsMaster] = 0
	AND	(ISNUMERIC(SUBSTRING([Name], 4, 1)) = 1 OR [Name] = 'ASFMAX')
UNION
SELECT	 [CustomerUID]
		,[VersionUID]
		,[Name] AS [OldName]
		,'String' + SUBSTRING([Name], 2, 50) AS [NewName]
FROM	[app].[DataTypeMappings] dtm
WHERE	LEFT([Name], 1) = 'S'
	AND [Name] NOT LIKE 'String%'
	AND	[DataTypeId] = 20
	AND	[IsMaster] = 0
	AND	(ISNUMERIC(SUBSTRING([Name], 2, 1)) = 1 OR [Name] = 'SMAX')
UNION
SELECT	 [CustomerUID]
		,[VersionUID]
		,[Name] AS [OldName]
		,'StringFixed' + SUBSTRING([Name], 3, 50) AS [NewName]
FROM	[app].[DataTypeMappings] dtm
WHERE	LEFT([Name], 2) = 'SF'
	AND [Name] NOT LIKE 'String%'
	AND	[DataTypeId] = 21
	AND	[IsMaster] = 0
	AND	(ISNUMERIC(SUBSTRING([Name], 3, 1)) = 1 OR [Name] = 'SFMAX')


DELETE	dtmr3
FROM	[app].[DataTypeMappings] dtm
INNER JOIN #DataTypeMappings AS dtmr2
	ON	dtm.[Name] = dtmr2.[NewName]
	AND	dtm.[CustomerUID] = dtmr2.[CustomerUID]
	AND	dtm.[VersionUID] = dtmr2.[VersionUID]
INNER JOIN [app].[DataTypeMappings] dtmr3
	ON	dtmr2.[OldName] = dtmr3.[Name]
	AND	dtmr2.[CustomerUID] = dtmr3.[CustomerUID]
	AND	dtmr2.[VersionUID] = dtmr3.[VersionUID]

UPDATE	dtm
SET		[Name] = dtmr2.[NewName]
FROM	[app].[DataTypeMappings] dtm
INNER JOIN #DataTypeMappings AS dtmr2
	ON	dtm.[Name] = dtmr2.[OldName]
	AND	dtm.[CustomerUID] = dtmr2.[CustomerUID]
	AND	dtm.[VersionUID] = dtmr2.[VersionUID]

IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_DataTypeMappings') EXEC ('ENABLE TRIGGER [app].[tdel_DataTypeMappings] ON [app].[DataTypeMappings]');


-- =========================================================================
-- Migrate DvAppendLinkSatelliteRecordSource
-- =========================================================================
IF (ISNUMERIC(@majorVersion) = 1 AND CONVERT(INT, @majorVersion) >= 22 
	AND ISNUMERIC(@minorVersion) = 1 AND CONVERT(INT, @minorVersion) >= 3 
	AND ISNUMERIC(@buildNumber) = 1 AND CONVERT(INT, @buildNumber) >= 166)
BEGIN
	UPDATE	rcol
	SET		[BusinessSubject] = 
				CASE 
					WHEN ISNULL(ca.[AttributeProperty], 'Y') = 'N' OR (ISNULL(ca.[AttributeProperty], '') <> 'Y' AND ISNULL(s.[SettingValue], 'Y') = 'N')
						THEN CASE WHEN ISNULL(rcol.[BusinessSubject], '') = '' THEN 'SRS' ELSE 'SRS_' + LTRIM(RTRIM(rcol.[BusinessSubject])) END
					ELSE LTRIM(RTRIM(rcol.[BusinessSubject]))
				END
	FROM	[app].[Columns] col
	INNER JOIN [app].[Objects] obj
		ON	col.[ObjectUID] = obj.[UID]
	INNER JOIN [app].[Columns] scol
		ON	col.[UID] = scol.[TargetColumnUID]
	INNER JOIN [app].[Columns] rcol
		ON	scol.[ObjectUID] = rcol.[ObjectUID]
		AND	rcol.[ReferenceColumnUID] IS NOT NULL
	INNER JOIN [app].[Objects] sobj
		ON	scol.[ObjectUID] = sobj.[UID]
	INNER JOIN [app].[Projects] sprj
		ON	sobj.[ProjectUID] = sprj.[UID]
	INNER JOIN [app].[Versions] v
		ON	col.[VersionUID] = v.[UID]
	LEFT OUTER JOIN 
	(
		SELECT	 o.[UID] AS [ObjectUID]
				,ca.[AttributeProperty]
		FROM	[app].[CustomAttributes] ca
		LEFT OUTER JOIN [app].[Projects] pp
			ON	ca.[ProjectUID] = pp.[ParentProjectUID]
		LEFT OUTER JOIN [app].[Objects] o
			ON	ISNULL(ca.[ObjectUID], o.[UID]) = o.[UID]
			AND	ISNULL(ca.[ConnectionUID], o.[ConnectionUID]) = o.[ConnectionUID]
			AND	ISNULL(ca.[ProjectUID], o.[ProjectUID]) = o.[ProjectUID]
		WHERE	ca.[AttributeValue] = 'DvAppendLinkSatelliteRecordSource'
	) ca
		ON	scol.[ObjectUID] = ca.[ObjectUID]
	LEFT OUTER JOIN [app].[Settings] s
		ON	scol.[CustomerUID] = s.[CustomerUID]
		AND	scol.[VersionUID] = s.[VersionUID] 
		AND	s.[SettingKey] = 'DvAppendLinkSatelliteRecordSource'
	WHERE	scol.[CustomerUID] <> '00000000-0000-0000-0000-000000000000'
		AND	obj.[ObjectTypeId] = 10
		AND	sobj.[AcceleratorTypeId] = 9
		AND	(ISNULL(ca.[AttributeProperty], 'Y') = 'N' OR (ISNULL(ca.[AttributeProperty], '') <> 'Y' AND ISNULL(s.[SettingValue], 'Y') = 'N'))

	IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_Settings') EXEC ('DISABLE TRIGGER [app].[tdel_Settings] ON [app].[Settings]');
	IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_CustomAttributes') EXEC ('DISABLE TRIGGER [app].[tdel_CustomAttributes] ON [app].[CustomAttributes]');

	DELETE [app].[Settings] WHERE [SettingKey] = 'DvAppendLinkSatelliteRecordSource'
	DELETE [app].[CustomAttributes] WHERE [AttributeValue] = 'DvAppendLinkSatelliteRecordSource'

	IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_Settings') EXEC ('ENABLE TRIGGER [app].[tdel_Settings] ON [app].[Settings]');
	IF EXISTS (SELECT * FROM sys.objects WHERE type = 'TR' AND name = 'tdel_CustomAttributes') EXEC ('ENABLE TRIGGER [app].[tdel_CustomAttributes] ON [app].[CustomAttributes]');
END

--IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LandingConnectionUID') 
--	AND NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'LandingConnectionUID')
--BEGIN
--	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'LandingConnectionUID') BEGIN ALTER TABLE [app].[Projects] ADD [LandingConnectionUID] UNIQUEIDENTIFIER NULL END
--	IF NOT EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Projects]') AND name = 'PolybaseConnectionUID') BEGIN ALTER TABLE [app].[Projects] ADD [PolybaseConnectionUID] UNIQUEIDENTIFIER NULL END

--	DECLARE @sqlProject NVARCHAR(MAX)
--	SET @sqlProject = '
--	UPDATE	p
--	SET		 [LandingConnectionUID] = lc.[UID]
--			,[PolybaseConnectionUID] = pc.[UID]
--	FROM	[app].[Connections] c
--	INNER JOIN [app].[Connections] lc
--		ON	c.[LandingConnectionUID] = lc.[UID]
--	INNER JOIN [app].[Projects] p
--		ON	c.[UID] = p.[SourceConnectionUID]
--	WHERE	c.[IntegrationStageId] = 1
--		AND	lc.[IntegrationStageId] = 13
--		AND	lc.[LandingConnectionUID] IS NULL'
--	EXEC sp_executesql @sqlProject

--	IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[vsto].[Connections]') AND name = 'LandingConnectionUID') BEGIN ALTER TABLE [vsto].[Connections] DROP COLUMN [LandingConnectionUID] END
--	IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[app].[Connections]') AND name = 'LandingConnectionUID')
--	BEGIN 
--		IF (OBJECT_ID('app.FK_appConnection_REF_appLandingConnection', 'F') IS NOT NULL) ALTER TABLE [app].[Connections] DROP CONSTRAINT [FK_appConnection_REF_appLandingConnection]
--		ALTER TABLE [app].[Connections] DROP COLUMN [LandingConnectionUID] 
--	END
--	IF EXISTS(SELECT * FROM sys.columns WHERE object_id = OBJECT_ID(N'[archive].[Connections]') AND name = 'LandingConnectionUID') BEGIN ALTER TABLE [archive].[Connections] DROP COLUMN [LandingConnectionUID] END
--END

--UPDATE	cd
--SET		[Ordinal] = ucd.[Ordinal]
--FROM	[BimlFlex_Clean].[app].[Columns] cd
--INNER JOIN
--(
--	SELECT	cd.[UID]
--			,ROW_NUMBER() OVER (PARTITION BY cd.[ObjectUID], cd.[Ordinal] / 1000 ORDER BY cd.[Ordinal], cd.[IsIntegrationKey] DESC, cd.[IsPrimaryKey] DESC, cd.[IsSourceKey] DESC, cd.[Name]) AS [Ordinal]
--	FROM	[BimlFlex_Clean].[app].[Columns] cd
--	WHERE	cd.[Ordinal] < 7000
--) AS ucd
--	ON	cd.[UID] = ucd.[UID]

--UPDATE	cd
--SET		[Ordinal] = ucd.[Ordinal]
--FROM	[BimlFlex_Clean].[app].[Columns] cd
--INNER JOIN
--(
--	SELECT	 cd.[UID]
--			,(cd.[Ordinal] / 1000 * 1000) + ROW_NUMBER() OVER (PARTITION BY cd.[ObjectUID], cd.[Ordinal] / 1000 ORDER BY cd.[Ordinal], cd.[IsIntegrationKey] DESC, cd.[IsPrimaryKey] DESC, cd.[IsSourceKey] DESC, cd.[Name]) AS [Ordinal]
--	FROM	[BimlFlex_Clean].[app].[Columns] cd
--	WHERE	cd.[Ordinal] >= 7000 
--) AS ucd
--	ON	cd.[UID] = ucd.[UID]
