Synchronizing Schema - Live Database, Scripts folder VB
From RedGateWiki
These examples show how you can synchronize one schema to another and how you can synchronize folders containing SQL scripts.
Assembly references:
- RedGate.SQLCompare.Engine.dll
- RedGate.Shared.Utils.dll
- RedGate.Shared.SQL.dll
[edit]
Synchronising Databases
Demonstration that synchronises all objects in WidgetProduction with those currently contained in WidgetStaging
- Imports System
- Imports RedGate.Shared.SQL
- Imports RedGate.Shared.SQL.ExecutionBlock
- Imports RedGate.SQLCompare.Engine
- Module Module1
- Sub Main(ByVal args As String())
- Using widgetStaging As New Database(), widgetProduction As New Database()
- ' Retrieve the schema information for the two databases
- widgetStaging.Register(New ConnectionProperties(".", "WidgetStaging"), Options.[Default])
- widgetProduction.Register(New ConnectionProperties(".", "WidgetProduction"), Options.[Default])
- ' Compare widgetStaging to widgetProduction. Comparing in this order makes WidgetProduction the second database
- Dim stagingVsProduction As Differences = widgetStaging.CompareWith(widgetProduction, Options.[Default])
- ' Select the differences to include in the synchronization. In this case, we're using all differences.
- For Each difference As Difference In stagingVsProduction
- difference.Selected = True
- Next
- Dim work As New Work()
- ' Calculate the work to do using sensible default options
- ' The script is to be run on WidgetProduction so the runOnTwo parameter is true
- work.BuildFromDifferences(stagingVsProduction, Options.[Default], True)
- ' We can now access the messages and warnings
- Console.WriteLine("Messages:")
- For Each message As Message In work.Messages
- Console.WriteLine(message.Text)
- Next
- Console.WriteLine("Warnings:")
- For Each message As Message In work.Warnings
- Console.WriteLine(message.Text)
- Next
- ' Disposing the execution block when it's not needed any more is important to ensure
- ' that all the temporary files are cleaned up
- Using block As ExecutionBlock.ExecutionBlock = work.ExecutionBlock
- ' Display the SQL used to synchronize
- Console.WriteLine("SQL to synchronize:")
- Console.WriteLine(block.GetString())
- ' Finally, use a BlockExecutor to run the SQL against the WidgetProduction database
- ' BlockExecutor executor = new BlockExecutor();
- ' executor.ExecuteBlock(block, ".", "WidgetLive");
- End Using
- End Using
- Console.WriteLine("Press [Enter]")
- Console.ReadLine()
- End Sub
- End Module
Synchronizing scripts folders
- Imports System
- Imports RedGate.Shared.SQL
- Imports RedGate.SQLCompare.Engine
- Imports RedGate.SQLCompare.Engine.ReadFromFolder
- ''' <summary>
- ''' Demonstration that synchronizes the C:\WidgetDevScripts scripts folder against the
- ''' C:\WidgetProductionScripts scripts folder.
- ''' </summary>
- ''' <remarks>
- ''' The scripts folder for this demonstration can be generated by running
- ''' the SaveScriptsFolder demo.
- ''' </remarks>
- Module Module1
- ''' <summary>
- ''' The folder that this demonstration will synchronize against
- ''' </summary>
- Private Const c_WidgetProductionFolder As String = "C:\WidgetProductionScripts"
- Private Const c_WidgetDevFolder As String = "C:\WidgetDevScripts"
- Sub Main(ByVal args As String())
- Using widgetDevScripts As New Database(), widgetProductionScripts As New Database()
- ' Read the schema for the WidgetStaging database
- widgetDevScripts.Register(c_WidgetDevFolder, Nothing, Options.[Default])
- ' Establish the schema from the scripts stored in the WidgetProduction scripts folder
- ' Passing in null for the database information parameter causes SQL Compare to read the
- ' XML file supplied in the folder.
- widgetProductionScripts.Register(c_WidgetProductionFolder, Nothing, Options.[Default])
- ' Compare the database against the scripts.
- ' Comparing in this order makes the WidgetProduction scripts folder the second database
- Dim stagingVsProduction As Differences = widgetDevScripts.CompareWith(widgetProductionScripts, Options.[Default])
- ' Select all of the differences for synchronization
- For Each difference As Difference In stagingVsProduction
- difference.Selected = True
- Next
- ' Use the default folder structure for any new files
- Dim folderOptions As New WriteToFileOptions()
- ' Calculate the work to do using sensible default options
- ' The WidgetProductionScripts folder is to be updated, so the runOnTwo parameter is true
- Dim work As New Work()
- work.BuildFromDifferences(stagingVsProduction, Options.[Default], True, folderOptions)
- ' We can now access the messages and warnings
- Console.WriteLine("Messages:")
- For Each message As Message In work.Messages
- Console.WriteLine(message.Text)
- Next
- Console.WriteLine("Warnings:")
- For Each message As Message In work.Warnings
- Console.WriteLine(message.Text)
- Next
- ' Disposing the execution block when it's not needed any more is important to ensure
- ' that all the temporary files are cleaned up
- Using block As ExecutionBlock.ExecutionBlock = work.ExecutionBlock
- ' Display the SQL used to synchronize
- Console.WriteLine("SQL to synchronize:")
- ' Finally, use a BlockExecutor to run the SQL against the WidgetProduction database
- ' BlockExecutor executor = new BlockExecutor();
- ' executor.ExecuteBlock(block, ".", "WidgetLive");
- Console.WriteLine(block.GetString())
- End Using
- ' and we can also perform the synchronization now
- ' Retrieve the list of actions to perform to synchronize the database against the scripts folder
- Dim scriptActions As ScriptSynchronizationActions = work.ScriptSynchronizationActions
- ' Display the files that will be modified during the synchronization
- Console.WriteLine("Script files that will be modified:")
- For Each action As ScriptSynchronizationAction In scriptActions.SynchronizationActions.Values
- Console.WriteLine("{0}", action.FileName)
- Next
- ' Check for any read-only files
- Try
- scriptActions.Check()
- Catch generatedExceptionName As SqlCompareException
- ' An exception here means that there are read-only files in the scripts folder.
- ' We can continue, but they will be forcibly overwritten. For the purposes of
- ' this demonstration we choose to replace any read-only files, so we ignore
- ' the exception.
- End Try
- ' Synchronize the scripts folder, overwriting any read-only files
- scriptActions.Execute()
- End Using
- Console.WriteLine("Press [Enter]")
- Console.ReadLine()
- End Sub
- End Module
