Flyway

Using Repeatable Migrations to manage data

Flyway also supports Repeatable Migrations that will execute on the target environment after all the pending migration scripts run every time the contents in the Repeatable Migrations change.  You can write an idempotent script to manage data or use MERGE if your database supports that. 

SQL Server Merge Example:

R__Data-MarketingReasons.sql

/*  Manage Marketing Data  */
 
SET XACT_ABORT, NOCOUNT ON;
GO
 
BEGIN TRY 
    CREATE TABLE #MarketingReasons (ID INT, ReasonType VARCHAR(MAX));
    INSERT INTO #MarketingReasons
    VALUES(1, 'Recommendation'),
        (2, 'Review'),
        (3, 'Internet');

    MERGE INTO dbo.MarketingReasons AS target
    USING #MarketingReasons AS source
    ON target.ID=source.ID
    WHEN MATCHED THEN UPDATE SET ReasonType=source.ReasonType
    WHEN NOT MATCHED BY TARGET THEN INSERT(ID, ReasonType)
                                    VALUES(ID, ReasonType)
    WHEN NOT MATCHED BY SOURCE THEN DELETE; 
END TRY

BEGIN CATCH
    DECLARE @msg NVARCHAR(2048) =N'';
    IF @@trancount>0 ROLLBACK TRANSACTION;
    SET @msg=ERROR_MESSAGE();
    RAISERROR(@msg, 16, 1);
END CATCH;
GO

If you change this file to add an additional value into the temporary table, then the next time flyway migrate is executed, this script will be included in the migration.

Learn more about Repeatable Migrations.

Read an example of using PowerShell to Bulk Load Data in SQL Server.


Didn't find what you were looking for?