Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
Created November 26, 2024 13:31
Show Gist options
  • Save pavelfomin/02989833ed9f834d1bf3319836125699 to your computer and use it in GitHub Desktop.
Save pavelfomin/02989833ed9f834d1bf3319836125699 to your computer and use it in GitHub Desktop.
Parameterizing flyway scripts

Occasionally it can be helpful to parameterize a migration depending on an environment. Flyway supports the concept of placeholders to help with that.

Flyway provides default placeholders, whose values are automatically populated:

  • ${flyway:defaultSchema} = The default schema for Flyway
  • ${flyway:user} = The user Flyway will use to connect to the database
  • ${flyway:database} = The name of the database from the connection url
  • ${flyway:timestamp} = The time that Flyway parsed the script, formatted as 'yyyy-MM-dd HH:mm:ss'
  • ${flyway:filename} = The filename of the current script
  • ${flyway:workingDirectory} = The user working directory as defined by the 'user.dir' System Property
  • ${flyway:table} = The name of the Flyway schema history table
  • ${flyway:environment} = The name of the environment configured for this script execution

Example of usage in a migration script for Postgres:

DO $$
BEGIN
    IF '${flyway:user}' = 'test-account' THEN
        EXECUTE 'ALTER TABLE foo.bar OWNER TO test-role';
    END IF;
END
$$

Custom placeholders can also be configured. Example for Spring Boot custom Flyway placeholder configuration:

spring:
  flyway:
    placeholders:
      test-placeholder: something
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment