java
36 lines · 6 steps
Customizing Flyway migrations in Spring
A Spring config class builds a fully customized Flyway instance and controls exactly how migrations run at startup.
Explained by
highlit
1package com.acme.billing.config;
2
3import org.flywaydb.core.Flyway;
4import org.flywaydb.core.api.MigrationVersion;
5import org.flywaydb.core.api.configuration.FluentConfiguration;
6import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
7import org.springframework.context.annotation.Bean;
8import org.springframework.context.annotation.Configuration;
9
10import javax.sql.DataSource;
11
12@Configuration
13public class FlywayConfig {
14
15 @Bean
16 public Flyway flyway(DataSource dataSource) {
17 FluentConfiguration configuration = Flyway.configure()
18 .dataSource(dataSource)
19 .locations("classpath:db/migration")
20 .baselineOnMigrate(true)
21 .baselineVersion(MigrationVersion.fromVersion("0"))
22 .table("schema_history")
23 .validateOnMigrate(true)
24 .cleanDisabled(true);
25
26 return configuration.load();
27 }
28
29 @Bean
30 public FlywayMigrationStrategy migrationStrategy() {
31 return flyway -> {
32 flyway.repair();
33 flyway.migrate();
34 };
35 }
36}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1Defining your own Flyway bean overrides Spring Boot's auto-configured defaults so you control every migration setting.
- 2baselineOnMigrate lets Flyway adopt an existing non-empty database instead of failing on first run.
- 3A FlywayMigrationStrategy replaces the default migrate-on-startup behavior with your own sequence of steps.
Related explainers
typescript
import { registerLocaleData } from '@angular/common'; import localeFr from '@angular/common/locales/fr'; import localeFrExtra from '@angular/common/locales/extra/fr'; import localeDe from '@angular/common/locales/de';
Locale-aware bootstrapping in Angular
i18n
localization
dependency-injection
Intermediate
8 steps
typescript
import { Module } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import * as Joi from 'joi';
Validating env config at boot in NestJS
configuration
schema-validation
environment-variables
Intermediate
8 steps
java
@Component @Converter public class EncryptedStringConverter implements AttributeConverter<String, String> {
Transparent column encryption in Spring & JPA
encryption
aes-gcm
jpa-converter
Advanced
10 steps
typescript
import { Inject, Injectable, Logger } from '@nestjs/common'; import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { InjectRepository } from '@nestjs/typeorm';
A cache-aside country lookup in NestJS
cache-aside
dependency-injection
batch-lookup
Intermediate
8 steps
java
package com.acme.billing.config; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.ConfigurationProperties;
Feature-flagged beans with Spring @ConditionalOnProperty
feature-flags
conditional-beans
strategy-pattern
Intermediate
5 steps
typescript
import { Injectable, effect, signal, computed } from '@angular/core'; interface Preferences { theme: 'light' | 'dark';
A signal-based preferences store in Angular
signals
state-management
persistence
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/customizing-flyway-migrations-in-spring-explained-java-7d93/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.