
----------------------------------------------------
--
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn.Upgrade.v6_to_v61.GetPreviousTableName]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn.Upgrade.v6_to_v61.GetPreviousTableName]
GO
CREATE FUNCTION [dbo].[fn.Upgrade.v6_to_v61.GetPreviousTableName] (@table_name nvarchar(255))
RETURNS nvarchar(max)
AS
BEGIN
	declare @result nvarchar(max)
	set @result = [dbo].[fn.Upgrade.Common.GetRenamedTableName] (@table_name, '6')
	return @result
END
GO
--
----------------------------------------------------




----------------------------------------------------
--
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[fn.Upgrade.v6_to_v61.GetTablesToUpgrade]') AND type in (N'FN', N'IF', N'TF', N'FS', N'FT'))
DROP FUNCTION [dbo].[fn.Upgrade.v6_to_v61.GetTablesToUpgrade]
GO
CREATE FUNCTION [dbo].[fn.Upgrade.v6_to_v61.GetTablesToUpgrade] ()
RETURNS TABLE 
AS RETURN
(
	select * from (
	select       'Backup.Model.Backups', CAST(null as nvarchar(255))
	union select 'Backup.Model.Storages', null
	union select 'Backup.Model.Points', null
	union select 'Backup.Model.OIBs', null
	union select 'Backup.Model.BackupTaskSessions', null
	union select 'Backup.Model.SbSessions', null
	union select 'Backup.Model.BackupJobSessions', null
	union select 'Backup.Model.JobSessions', null
	union select 'Backup.Model.SbTaskSessions', null
	union select 'Folders', null
	union select 'HostsByJobs', null
	union select 'Folder_Host', null
	union select 'VirtualLabs', null
	union select 'ObjectsInJobs', null
	union select 'Soap_creds', null
	union select 'BJobs', null
	union select 'DRRoles', null
	union select 'BObjects', null
	union select 'Ssh_creds', null
	union select 'BSessions', null
	union select 'BSessionInfo', null
	union select 'LicensedHosts', null
	union select 'ObjectsInApplicationGroups', null
	union select 'SbVerificationRules', null
	union select 'LinkedJobs', null
	union select 'Hosts', null
	) o(name, custom_sync_proc)
)
GO
--
----------------------------------------------------


 
----------------------------------------------------
--
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp.Upgrade.v6_to_v61.InternalSync]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalSync]
GO
CREATE PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalSync]
	@dbinst uniqueidentifier,
	@tgtTableNamePrefix nvarchar(5)
AS
BEGIN
	SET NOCOUNT ON;				

	declare @table_name nvarchar(255)
	declare @custom_sync_proc nvarchar(255)
	declare @statement nvarchar(max)
	
	declare tables_cursor cursor for
	select name, custom_sync_proc from [dbo].[fn.Upgrade.v6_to_v61.GetTablesToUpgrade] ()
	
	open tables_cursor
	fetch next from tables_cursor into @table_name, @custom_sync_proc

	while @@FETCH_STATUS = 0
	BEGIN

		print @table_name + N'synchronization...'

		IF LEN( ISNULL(@custom_sync_proc, '') ) = 0
			exec [dbo].[usp.Upgrade.v6_to_v61.InternalStandardSyncTable] @dbinst, @tgtTableNamePrefix, @table_name
		ELSE
			BEGIN
				print 'Using custom synchronization...'
				set @statement = 'exec ' + [dbo].[fn.Upgrade.Common.GetWrappedTableName] (@custom_sync_proc) + ' ''' + CAST(@dbinst as nvarchar(255)) + ''', ''' + @tgtTableNamePrefix + ''''
				exec sp_executesql @statement 
			END
		
		fetch next from tables_cursor into @table_name, @custom_sync_proc
	END
	
	close tables_cursor
	deallocate tables_cursor	
END
GO
--
----------------------------------------------------



 
----------------------------------------------------
--
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp.Upgrade.v6_to_v61.InternalStandardSyncTable]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalStandardSyncTable]
GO
CREATE PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalStandardSyncTable]
	@dbinst uniqueidentifier,
	@tgtTableNamePrefix nvarchar(5),
	@table_name nvarchar(255)
AS
BEGIN
	SET NOCOUNT ON;				

	declare @previous_table nvarchar(255)		
	set @previous_table = [dbo].[fn.Upgrade.v6_to_v61.GetPreviousTableName] (@table_name)

	declare @tgt_table nvarchar(255)
	set @tgt_table = [dbo].[fn.Upgrade.Common.GetRenamedTableName] (@table_name, @tgtTableNamePrefix)
	
	declare @statement nvarchar(max) 
	set @statement = 'delete ' + [dbo].[fn.Upgrade.Common.GetWrappedTableName] (@tgt_table) + 
	' where db_instance_id = ''' + CAST(@dbinst as nvarchar(255)) + ''''

	exec sp_executesql @statement  
	
	set @statement = 'insert ' + [dbo].[fn.Upgrade.Common.GetWrappedTableName] (@tgt_table) + 
	' (' + [dbo].[fn.Upgrade.Common.JoinCommonTableColumnsToString] (@tgt_table, @previous_table) + ')' +
	' select * from ' + [dbo].[fn.Upgrade.Common.GetWrappedTableName] (@previous_table) +
	' where db_instance_id = ''' + CAST(@dbinst as nvarchar(255)) + ''''
	
	--print @statement

	exec sp_executesql @statement  
	
END
GO
--
----------------------------------------------------


----------------------------------------------------
--
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[usp.Upgrade.v6_to_v61.InternalCustomSync]') AND type in (N'P', N'PC'))
DROP PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalCustomSync]
GO
CREATE PROCEDURE [dbo].[usp.Upgrade.v6_to_v61.InternalCustomSync]
	@dbinst uniqueidentifier,
	@tgtTableNamePrefix nvarchar(5)
AS
BEGIN
	SET NOCOUNT ON;				
	
	--
	--TODO: Add custom internal sync handling
	--
	
END
GO
--
----------------------------------------------------

