How to disable flyway in a particular Spring profile?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Set spring.flyway.enabled=false in the profile-specific configuration file. For example, to disable Flyway in the test profile, add that property to application-test.properties or application-test.yml. When the application starts with that profile active, Spring Boot will skip Flyway's automatic migration execution entirely.
That single property is all it takes. The rest of this article covers the details: how profiles interact with Flyway, alternative approaches for more complex setups, and the traps that catch teams in production.
How Flyway Integration Works in Spring Boot
Spring Boot auto-configures Flyway when it detects org.flywaydb:flyway-core on the classpath. On startup, the FlywayAutoConfiguration class creates a Flyway bean, points it at the configured datasource, and calls flyway.migrate(). This happens before any JPA/Hibernate initialization so that the schema is ready when entity managers start.
The spring.flyway.enabled property controls whether this auto-configuration runs. When set to false, Spring Boot does not create the Flyway bean at all, and no migration SQL executes.
Profile-Specific Configuration Files
Spring Boot resolves configuration in a well-defined order. Profile-specific files override the base application.properties or application.yml:
Properties in a profile-specific file take precedence over the base file for the same key. This means you can keep spring.flyway.enabled=true (or omit it, since true is the default) in your base configuration and override it to false only in the profiles where you want Flyway disabled.
Full Configuration Example
Here is a typical multi-profile setup:
Activate a profile at startup through any of these methods:
Using YAML Multi-Document Syntax
If you prefer a single YAML file over multiple profile-specific files, use Spring Boot's multi-document syntax with the spring.config.activate.on-profile key:
The --- separator creates distinct configuration documents within a single file. The on-profile key replaces the older spring.profiles syntax that was deprecated in Spring Boot 2.4.
Disabling Flyway Programmatically
For advanced scenarios where a boolean property is not flexible enough, you can exclude Flyway's auto-configuration class:
Or conditionally exclude it using a profile-specific configuration class:
The FlywayMigrationStrategy approach is useful when you want the Flyway bean to exist (for example, to call flyway.clean() in test setup) but do not want automatic migration on startup.
Comparison of Approaches
| Approach | Scope | Flyway Bean Created | Reversible at Runtime |
spring.flyway.enabled=false | Profile-specific | No | No |
exclude = FlywayAutoConfiguration | Application-wide | No | No |
Custom FlywayMigrationStrategy | Profile-specific | Yes (no-op) | Yes |
@ConditionalOnProperty custom bean | Profile-specific | Conditional | No |
Testing Without Flyway
When Flyway is disabled in tests, you need an alternative way to set up the database schema. Common approaches include:
For Spring Boot integration tests using @SpringBootTest, annotate the test class with the profile:
Common Pitfalls
Environment variables overriding profile-specific properties. The environment variable SPRING_FLYWAY_ENABLED=true takes precedence over application-test.properties because environment variables have higher priority in Spring Boot's property resolution order. If Flyway runs despite your profile setting it to false, check for conflicting environment variables or command-line arguments.
Forgetting that Flyway creates its schema history table. Even on first run, Flyway creates the flyway_schema_history table. If Flyway is disabled in a profile, that table does not exist. Other code or scripts that query this table will fail. Design your tests to not depend on it.
Running tests against a database that expects migrations. When Flyway is disabled, the database starts empty. If your tests assume tables exist, use spring.jpa.hibernate.ddl-auto=create-drop or a test-specific schema.sql to bootstrap the schema.
Using the deprecated spring.profiles key in YAML. In Spring Boot 2.4+, spring.profiles inside a YAML document was replaced by spring.config.activate.on-profile. Using the old key may cause the profile activation to silently fail, leaving Flyway enabled when you expect it to be off.
Activating multiple profiles with conflicting Flyway settings. If both dev and test profiles are active and application-dev.properties enables Flyway while application-test.properties disables it, the last profile listed wins. Be explicit about profile ordering or avoid contradictory settings across profiles.
Summary
- Set
spring.flyway.enabled=falsein a profile-specific configuration file to disable Flyway for that profile. - Use profile-specific
.propertiesor.ymlfiles, or the multi-document YAML syntax withspring.config.activate.on-profile. - For tests, pair disabled Flyway with
spring.jpa.hibernate.ddl-auto=create-dropor aschema.sqlto ensure the database schema is available. - Watch out for environment variables and command-line arguments that override your property file settings.
- The
FlywayMigrationStrategybean offers a middle ground where the Flyway bean exists but does not run migrations automatically.

