Using Flyway: record and apply database migrations
Flyway versions SQL and other migration files so database structure and data can be changed in the same order across environments. A new database can receive migrations from the beginning; an existing database usually needs a baseline recorded first, then only later changes are applied. A typical workflow is to create and review a file, inspect the target with `info`, check recorded history with `validate`, and then run `migrate`. Confirm the connection and baseline before pointing the tool at a live database.
Using Flyway: record and apply database migrations
Flyway versions SQL and other migration files so database structure and data can be changed in the same order across environments. A new database can receive migrations from the beginning; an existing database usually needs a baseline recorded first, then only later changes are applied. A typical workflow is to create and review a file, inspect the target with info, check recorded history with validate, and then run migrate. Confirm the connection and baseline before pointing the tool at a live database.
How Flyway tracks changes
A migration is an incremental script for changing a database. Flyway discovers files in configured locations and records applied migrations, versions, checksums and success state in flyway_schema_history. This lets it distinguish completed from pending changes and apply new files in order across environments. Flyway: Migrations Schema history table
A versioned migration might be named V001__create_account.sql. V marks a versioned migration; its version is unique and ordered numerically. Two underscores separate version from description. Naming conventions can be configured, so follow the project settings. Once a versioned file has been applied to a permanent downstream environment, do not edit it; add a new versioned migration for later corrections. Flyway applies each versioned migration once and tracks it. Flyway: Versioned migrations
A repeatable migration might be R__refresh_views.sql: it has a description but no version. Flyway reapplies it on migrate when its checksum changes, after pending versioned migrations. It suits definitions such as views or procedures maintained in one file. It must be safe to run repeatedly; use database-supported constructs such as CREATE OR REPLACE when appropriate. A repeatable script that changes data can have repeated effects, so choose it carefully. Flyway: Repeatable migrations
Example for a new database
This is an illustrative PostgreSQL project, not executed or verified against an application. Other database dialects differ in types, identity columns and SQL syntax.
sql/
V001__create_account.sql
V002__add_account_status.sql
R__account_views.sqlAssume the first file creates an account table, the second adds a status column, and the repeatable file defines an account view. Keep migrations under version control and configure locations to point to the folder. For the CLI, a filesystem location can be written as filesystem:./sql; relative paths are resolved from the working directory. Defaults differ by invocation type, so verify the actual setting instead of relying on a CLI default. Flyway: Locations setting
**Check the development target and configuration.** Confirm the connection, permissions, schema, migration location and naming conventions point to the intended development database. Supply credentials through an approved secret mechanism, not committed files.
**Write and review a migration.** Choose a unique version for a coherent change. Consider target-database semantics, locks, execution time, retry safety, data loss and application compatibility, especially for large tables or transformations.
**Run `flyway info`.** Check applied, pending and failed states. Confirm only the intended migrations are pending. This reports status; it does not apply changes.
**Run `flyway validate`.** Check for discrepancies between locally available migrations and recorded history. A successful validation means that comparison passed; it does not prove the SQL is correct for the business or that every live object matches the intended schema.
**After review, run `flyway migrate`.** Flyway applies pending versioned migrations in order, followed by changed repeatables. Then run `info` again and inspect the database for the expected tables, constraints and data. [Flyway: Migrate command](https://documentation.red-gate.com/flyway/reference/commands/migrate)
An empty database usually does not need a baseline. When first connecting Flyway to a database that already contains objects and data, a baseline may be needed. baseline records a chosen version as already applied; migrations at or below it are skipped by migrate. If the version does not match the actual database state, needed migrations may be skipped or an incorrect state accepted. Align downstream environments first or create an explicit baseline strategy if they differ. Flyway: Baselines Baseline command
Starting with an existing database
Before applying anything, verify the target database, confirm that its objects match the chosen baseline version, and check whether downstream environments are aligned. baselineOnMigrate automatically baselines a non-empty schema with no history table; its default is false. Convenience comes with less protection against a connection mistake that points to the wrong database, so do not enable it just to avoid a deliberate baseline step. Flyway: baselineOnMigrate
Failure recovery depends on what the database actually did. Determine whether the failed script rolled back transactionally or partially changed objects first. Running repair before understanding the cause may change history without repairing the live database. If a versioned migration already ran in a permanent environment, leave that file intact and make a reviewed forward correction in a new version.
Many migrations run in transactions, but not every database statement can be rolled back. Transaction support for operations such as index creation depends on the database and settings. Inspect the result after failure, test risky changes on a similar target, and prepare a recovery plan instead of assuming automatic rollback. Flyway: Migrations
Where Flyway fits
Flyway suits teams that want migration files reviewed in code and an explicit order for database changes. It executes and records the files; it does not automatically guarantee that a schema change is safe or decide whether every difference in a live database is acceptable. SQL review, testing on a representative database, deployment approval, drift checks, and backup/recovery planning remain separate responsibilities. Desktop comparison/model features and command-line capabilities vary by product tier; check the current command and edition list. Flyway: Commands and tiers
A sound starting routine is to verify the target, record small understandable changes as new versioned files, inspect status, validate history, review, apply, and confirm results. Existing databases also need a deliberate baseline and environment-alignment plan.
Sources
[Flyway: Migrations](https://documentation.red-gate.com/flyway/flyway-concepts/migrations)
[Flyway: Versioned migrations](https://documentation.red-gate.com/fd/versioned-migrations-273973333.html)
[Flyway: Repeatable migrations](https://documentation.red-gate.com/flyway/flyway-concepts/migrations/repeatable-migrations)
[Flyway: Baselines](https://documentation.red-gate.com/fd/baselines-273973441.html)
[Flyway: Migrate command](https://documentation.red-gate.com/flyway/reference/commands/migrate)