예제 #1
0
        protected virtual void GenerateScriptForApplyingVersionScript(MigrationVersion version, ScriptGenerationOptions options, TextWriter output)
        {
            output.WriteLine("PRINT N'Applying version {0} to schema {1}'", Escape(version.Version), Escape(version.Schema));

            if (options.ProcessBatchedScripts)
            {
                string[] scriptParts = GetScriptParts(version.Script, options.BatchTerminator);

                foreach (string scriptPart in scriptParts)
                {
                    output.WriteLine("exec sp_executesql N'{0}'", Escape(scriptPart));
                }
            }
            else
            {
                output.WriteLine(version.Script);
            }
        }
예제 #2
0
 protected virtual void GenerateScriptToTrackVersion(MigrationVersion version, ScriptGenerationOptions options, TextWriter output)
 {
     output.WriteLine(@"INSERT INTO [{0}] ([Schema], [Version], [NumericVersion], [Checksum], [Description], [DateAdded])
         VALUES (N'{1}', N'{2}', {3}, '{4}', N'{5}', getutcdate())",
         options.VersionTableName, Escape(version.Schema), Escape(version.Version), version.NumericVersion, Escape(version.Checksum), Escape(version.Description));
 }
예제 #3
0
 protected virtual void GenerateScriptForSkipCondition(MigrationVersion version, ScriptGenerationOptions options, TextWriter output)
 {
     output.WriteLine(@"IF ({0})
     BEGIN
     PRINT N'Skipping version {1} on schema {2} due to a matched skip condition'
     END
     ELSE
     BEGIN
     ", version.SkipCondition, Escape(version.Version), Escape(version.Schema));
 }
예제 #4
0
        protected virtual void GenerateScriptForVersionContinuityChecks(MigrationVersion version, string tableName, bool rollbackTransactionOnError, TextWriter output)
        {
            output.WriteLine(@"IF EXISTS(SELECT [Version] FROM [{0}] WHERE [Schema] = N'{1}' AND [Version] = N'{2}')
            BEGIN
            IF NOT EXISTS(SELECT [Version] FROM [{0}] WHERE Version = N'{2}' AND Checksum = '{3}')
            BEGIN
            RAISERROR (N'Conflicting checksum while applying version %s to schema %s', 15, 0, N'{2}', N'{1}')
            ", tableName, Escape(version.Schema), Escape(version.Version), Escape(version.Checksum));

            if (rollbackTransactionOnError)
            {
                output.WriteLine("ROLLBACK TRANSACTION");
            }

            output.WriteLine(@"
            RETURN
            END
            END
            ELSE
            BEGIN
            IF EXISTS(SELECT [Version] FROM [{0}] WHERE [Schema] = '{1}' AND [NumericVersion] > {3})
            BEGIN
            SELECT TOP 1 @Tern_ConflictingVersion = [Version] FROM [{0}] WHERE [Schema] = '{1}' AND [NumericVersion] > {3}
            RAISERROR (N'Cannot apply older version %s to schema %s when newer version %s exists', 15, 0, N'{2}', N'{1}', @Tern_ConflictingVersion)
            RETURN
            END

            ", tableName, Escape(version.Schema), Escape(version.Version), version.NumericVersion);
        }
예제 #5
0
        protected virtual void GenerateScriptForDetectingFailedUpgrade(MigrationVersion version, ScriptGenerationOptions options, TextWriter output)
        {
            output.WriteLine(@"IF @@ERROR <> 0 BEGIN");

            output.Write("PRINT N'Error occured while applying version {0} to schema {1}", Escape(version.Version), Escape(version.Schema));

            if (options.UseTransaction)
            {
                output.WriteLine(", rolling back transaction'");
                output.WriteLine("ROLLBACK TRANSACTION");
            }
            else
            {
                output.WriteLine("'");
            }

            output.WriteLine("RETURN");
            output.WriteLine("END");
        }