Redgate Flyway

Different data for different environments or clients

Some teams want to capture some data scripts that are only executed in Test environments.  Some teams need to have certain data for Client 1 and other data for Client 2.  This is possible in Flyway.


Please get in touch with our development team if you have any questions about this or let us know how you are doing this so we can improve our documentation. 


There are options:


Note: If different scripts or different parts of scripts are ran on different environments, then you are not testing the exact deployment script that will be run on Production as part of your pipeline.  If possible, create a Staging/Pre-Production environment that matches Production as close as possible and setup your pipeline to deploy to this environment first to try and catch issues before going to Production. 

Use different folders for your different targets

  1. Create additional folders (e.g., data-for-test and data-for-prod at the root of your project folder).  Note - Do not put the folders under the migrations folder since Flyway will execute the migrations and any sub-folders in the migrations folder.
  2. Learn about adding a new versioned migration script to the project folder.  Create a V script for test, then copy and paste this into the prod folder.  Change the values for the script in prod as needed.

  3. For deploying, make sure you update your pipeline so that when you deploy to your test environments it includes all the locations for the migration scripts that you want to go to this environment.  Do the same for production.

    # When using the flyway commands on Test environment
    flyway info -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-test" 
    flyway migrate -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-test" 
    flyway info -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-test" 
    # When using the flyway commands on Prod environment
    flyway info -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-prod" 
    flyway migrate -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-prod" 
    flyway info -locations="filesystem:C/project/migrations", "filesystem:C/project/data-for-prod"


    Learn more about the flyway -locations parameter.
    See more examples of using locations to organize your migrations

Use placeholders within your migration scripts

  1. Learn about adding a new versioned migration script to the project folder.  Create a new Versioned migration script with placeholders and save it to your project.
    Example:

    # If your target databases have different names, you can use the default flyway:database placeholder
    IF (${flyway:database} == "HR_DEV")
    	INSERT INTO TBL_ENVIRONMENT VALUES ("DEV");
    ELSE IF (${flyway:database} == "HR_TEST")
    	INSERT INTO TBL_ENVIRONMENT VALUES ("TEST");
    ELSE IF (${flyway:database} == "HR_STAGE" || ${flyway:database} == "HR_PROD" )
    	INSERT INTO TBL_ENVIRONMENT VALUES ("PROD");
    END IF
    
    

    Learn more about placeholders.

  2. When this versioned migration script is deployed to a target environment, it will replace the placeholder with it's corresponding value and run the correct insert statement.


Use placeholders and script configuration files

See this tutorial

Create multiple projects 

  1. Have a project for your core scripts.

  2. Have a different project for your test scripts.

  3. Have a different project for your prod scripts.

  4. For deployment, deploy core and test projects in your test environments.  Then deploy core and prod in your staging and production environments.

Didn't find what you were looking for?