SQL Change Automation 3

Help for older versions available.

These pages cover SQL Change Automation 3, which is not the latest version. Help for other versions is also available.

Migrating projects with custom Migration Log name properties

Customizing the names of the Migration Log table and Migration Log view is now deprecated, and support for the following project settings has been removed:

<PropertyGroup>
<MigrationLogTableName>MyMigrationLog</MigrationLogTableName>
<MigrationLogViewName>MyMigrationLogCurrent</MigrationLogViewName>
</PropertyGroup>


If you have any projects that are currently using the above settings, you will need to adapt your project to use the MigrationLogSchemaName setting. This means that your migration log table and view objects will always be named __MigrationLog and __MigrationLogCurrent respectively, however the schema in which they are contained will still be customizable.

Once you have removed the MigrationLogTableName and MigrationLogViewName settings from your sqlproj file, and added the MigrationLogSchemaName setting, you will need to move your existing migration log table to the newly-defined schema.

Adapting existing projects can be accomplished by performing the following steps:

  1. Open your SQL Change Automation project in Visual Studio
  2. In the Solution Explorer, right-click the project and select Unload Project.
  3. In the document window, remove the <MigrationLogTableName> and <MigrationLogViewName> elements, and add the <MigrationLogSchemaName> property in their place:

    <PropertyGroup>
    <MigrationLogSchemaName>MySchema</MigrationLogSchemaName>
    </PropertyGroup>

  4. Save and right-click the project and select Reload Project.
  5. In the Solution Explorer, right-click the Pre-Deployment folder and select Add Script
  6. This script should run after 01_Create_Database.sql, so give it a name like 02_Move_MigrLog_to_Schema.sql
  7. Paste the below code into the script window
  8. Replace the values of the variables in the header of the script as appropriate. OldMigrationLogSchema should be left as "dbo" unless you had previously customised the schema name. OldMigrationLogTableName should use the value of the <MigrationLogTableName> (former) sqlproj setting, OldMigrationLogViewName should use the value of <MigrationLogViewName>, and NewMigrationLogSchemaName should use the value of <MigrationLogSchemaName>.
  9. Click Build... Deploy solution


In the Output window, look for the following message:

Successfully moved migration objects


If this message does not appear, check that the variables are set correctly and try again. Note that you may receive the following error after this message:

This script "Migrations\0001_<<scriptSuffix>>.sql" has already been executed within the "<<DatabaseName>>" database on this server. Halting deployment.


However, the migration object move should have succeeded in spite of this message, and the error should not appear during subsequent deployment attempts. If the error reappears, check that the NewMigrationLogSchemaName is set to the correct value.


Content of Pre-Deployment\02_Move_MigrLog_to_Schema.sql:


  1. :setvar OldMigrationLogSchema "dbo"
  2. :setvar OldMigrationLogTableName "__MigrationLog"
  3. :setvar OldMigrationLogViewName "__MigrationLogCurrent"
  4. :setvar NewMigrationLogSchemaName "MyMigrationObjSchema"
  5. GO
  6.  
  7. USE [$(DatabaseName)]
  8.  
  9. BEGIN TRANSACTION
  10.  
  11. IF OBJECT_ID('[$(NewMigrationLogSchemaName)].[__MigrationLog]', 'U') IS NULL AND OBJECT_ID('[$(OldMigrationLogSchema)].[$(OldMigrationLogTableName)]', 'U') IS NOT NULL
  12. BEGIN
  13. IF SCHEMA_ID(N'$(NewMigrationLogSchemaName)') IS NULL
  14. EXEC sp_executesql N'CREATE SCHEMA [$(NewMigrationLogSchemaName)] AUTHORIZATION [dbo]'
  15.  
  16. ALTER SCHEMA [$(NewMigrationLogSchemaName)] TRANSFER [$(OldMigrationLogSchema)].[$(OldMigrationLogTableName)]
  17. ALTER SCHEMA [$(NewMigrationLogSchemaName)] TRANSFER [$(OldMigrationLogSchema)].[$(OldMigrationLogViewName)]
  18.  
  19. IF OBJECT_ID(N'[$(OldMigrationLogSchema)].[__SchemaSnapshot]', 'U') IS NOT NULL
  20. ALTER SCHEMA [$(NewMigrationLogSchemaName)] TRANSFER [$(OldMigrationLogSchema)].[__SchemaSnapshot]
  21.  
  22. PRINT N'Dropping [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[IX_$(OldMigrationLogTableName)_CompleteDt]...';
  23. DROP INDEX [IX_$(OldMigrationLogTableName)_CompleteDt]
  24. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)];
  25.  
  26. PRINT N'Dropping [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[IX_$(OldMigrationLogTableName)_Version]...';
  27. DROP INDEX [IX_$(OldMigrationLogTableName)_Version]
  28. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)];
  29.  
  30. PRINT N'Dropping [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[UX_$(OldMigrationLogTableName)_SequenceNo]...';
  31. DROP INDEX [UX_$(OldMigrationLogTableName)_SequenceNo]
  32. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)];
  33.  
  34. PRINT N'Dropping [$(NewMigrationLogSchemaName)].[DF_$(OldMigrationLogTableName)_deployed]...';
  35. ALTER TABLE [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)] DROP CONSTRAINT [DF_$(OldMigrationLogTableName)_deployed];
  36.  
  37. PRINT N'Creating [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[IX___MigrationLog_CompleteDt]...';
  38. CREATE NONCLUSTERED INDEX [IX___MigrationLog_CompleteDt]
  39. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)]([complete_dt] ASC);
  40.  
  41. PRINT N'Creating [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[IX___MigrationLog_Version]...';
  42. CREATE NONCLUSTERED INDEX [IX___MigrationLog_Version]
  43. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)]([version] ASC);
  44.  
  45. PRINT N'Creating [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)].[UX___MigrationLog_SequenceNo]...';
  46. CREATE UNIQUE NONCLUSTERED INDEX [UX___MigrationLog_SequenceNo]
  47. ON [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)]([sequence_no] ASC);
  48.  
  49. PRINT N'Creating [$(NewMigrationLogSchemaName)].[DF___MigrationLog_deployed]...';
  50. ALTER TABLE [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)]
  51. ADD CONSTRAINT [DF___MigrationLog_deployed] DEFAULT ((1)) FOR [deployed];
  52.  
  53. PRINT N'Rename [$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)] to __MigrationLog';
  54. EXECUTE sp_rename @objname = N'[$(NewMigrationLogSchemaName)].[$(OldMigrationLogTableName)]', @newname = N'__MigrationLog', @objtype = N'OBJECT';
  55.  
  56. PRINT N'Rename [$(NewMigrationLogSchemaName)].[$(OldMigrationLogViewName)] to __MigrationLogCurrent';
  57. EXECUTE sp_rename @objname = N'[$(NewMigrationLogSchemaName)].[$(OldMigrationLogViewName)]', @newname = N'__MigrationLogCurrent', @objtype = N'OBJECT';
  58.  
  59. ALTER TABLE [$(NewMigrationLogSchemaName)].[__MigrationLog]
  60. DROP CONSTRAINT [PK_$(OldMigrationLogTableName)]
  61.  
  62. ALTER TABLE [$(NewMigrationLogSchemaName)].[__MigrationLog] ADD CONSTRAINT
  63. [PK___MigrationLog] PRIMARY KEY CLUSTERED
  64. (
  65. migration_id,
  66. complete_dt,
  67. script_checksum
  68. )
  69.  
  70. PRINT N'Altering [$(NewMigrationLogSchemaName)].[__MigrationLogCurrent]...';
  71. EXEC ('
  72. ALTER VIEW [$(NewMigrationLogSchemaName)].[__MigrationLogCurrent]
  73. AS
  74. WITH currentMigration AS
  75. (
  76. SELECT
  77. migration_id, script_checksum, script_filename, complete_dt, applied_by, deployed, ROW_NUMBER() OVER(PARTITION BY migration_id ORDER BY sequence_no DESC) AS RowNumber
  78. FROM [$(NewMigrationLogSchemaName)].[__MigrationLog]
  79. )
  80. SELECT migration_id, script_checksum, script_filename, complete_dt, applied_by, deployed
  81. FROM currentMigration
  82. WHERE RowNumber = 1
  83. ');
  84.  
  85. PRINT 'Successfully moved migration objects to the [$(NewMigrationLogSchemaName)] schema'
  86. END
  87.  
  88. COMMIT TRANSACTION




Didn't find what you were looking for?