A Julia package for managing database migrations
DBMigrations.jl tries to be simple, transparent, and flexible. It relies on minimal required structure to work, while allowing for more complex setups.
It aims to be compatible with all database packages that support the interfaces in DBInterface.jl. Currently that includes SQLite.jl, MySQL.jl, Postgres.jl, LibPQ.jl, and ODBC.jl.
The primary interface is calling DBMigrations.runmigrations(conn::DBInterface.Connection, dir::String).
DBMigrations.runmigrations(conn::DBInterface.Connection, dir::String)
Using an established database connection conn (which should have the appropriate schema already
selected), search the directory dir for migration files and apply them to the database. Migration
files should be named like V1__baseline.sql, V2__latlong.sql, etc. where they must start with
a capital V followed by a number, followed by two underscores, followed by a description of the
migration. The number must be unique across all migrations. The description can be anything, but
should be descriptive of the migration. The file extension currently must be .sql.
Migration files found in dir will be checked against a special flyway_schema_history table that
the DBMigrations.jl package manages in the database connection for tracking which migrations have
already been applied (the table name and layout are compatible with Flyway,
including its line-ending-independent CRC32 checksums, so a history table previously managed by
Flyway can be picked up by DBMigrations.jl). Flyway baseline rows also suppress all local versions
at or below the baseline, and Flyway DELETE repair markers are honored. If a migration file is found in dir that has not been
applied, it will be applied to the database. If a migration file is found in dir that has already
been applied, it will be skipped. If a migration file is found in dir that has been applied but
has changed since it was applied, a ChecksumMismatch will be thrown (migrations should be
immutable once applied).
Migration files may contain multiple SQL statements, separated by semicolons. Each statement will
be executed in order. Semicolons inside single-quoted strings, quoted identifiers, --//* */
comments, and Postgres dollar-quoted blocks are handled correctly. Direct MySQL connections also
use MySQL's # and whitespace-sensitive -- comment rules. Each migration file is applied
in a transaction: if any statement fails, the migration is rolled back and the error rethrown
(on MySQL, not once a DDL statement has run; see Limitations).
Supported keyword arguments to runmigrations:
silent::Bool=false: suppress informational logging while applying migrationssplitstatements::Bool=true: split each migration file on semicolons and execute each statement separately; passfalseto send each file's contents to the database driver as-is (useful for constructs the splitter doesn't understand, like SQLiteCREATE TRIGGERbodies with embedded semicolons — note some drivers, e.g. SQLite, only execute the first statement of a multi-statement string, so such constructs should live in their own file)allowoutoforder::Bool=false: by default anOutOfOrderMigrationErroris thrown if a pending migration has a version lower than the highest already-applied version (e.g.V2shows up afterV3was already applied); passtrueto apply such migrations anyway
Other error conditions: duplicate migration versions (in the directory or the history table) throw
a DuplicateMigrationError; duplicate history ranks throw a DuplicateInstalledRankError; a
migration recorded as failed in the history table (possible when
sharing the table with Flyway) throws a FailedMigrationError and requires manual repair. Renaming
an applied migration throws a DescriptionMismatch. A history row with a different migration type
throws a TypeMismatch. These checks are consistent with Flyway validation.
DBMigrations.clean!(conn; confirm=true) drops and recreates the history table, forgetting all
record of applied migrations (it does not undo the migrations themselves).
- Concurrent runs are serialized only through LibPQ and Postgres.jl (a PostgreSQL session
advisory lock) and MySQL.jl (
GET_LOCK). Other drivers, such as SQLite and ODBC (even when ODBC reaches PostgreSQL or MySQL), take no lock, so runners there may race; coordinate deployments externally. The history table's primary key oninstalled_rankmakes a losing runner fail instead of recording a migration twice. History tables created by DBMigrations before 2.2 have no primary key; on PostgreSQL or MySQL, add it withALTER TABLE flyway_schema_history ADD PRIMARY KEY (installed_rank). - Transactional rollback of failed migrations depends on the database supporting transactional DDL. SQLite and PostgreSQL do. MySQL commits implicitly at each DDL statement, so a failed migration that contains DDL can keep the statements that ran before the failure (data changes included), with no history row for the migration. Prefer one DDL statement per MySQL migration.
- Only versioned migrations are supported (no Flyway-style repeatable
R__migrations or undo migrations). - The statement splitter recognizes standard
''quote escaping but not non-standard backslash-escaped quotes (e.g. MySQL's default\'); use''doubling orsplitstatements=falsefor such files.
using DBMigrations, SQLite
db = SQLite.DB("test.db")
DBMigrations.runmigrations(db, "migrations")