Migration Directory Import
Atlas supports the generation ofcustom migration file formatsfor a variety of existing migration management tools, e.g.Flyway orgolang-migrate/migrate. But Atlas has its own format as well and providesa convenient command to import existing migration directories of supported tools into the Atlas format.
Flags
When usingatlas migrate import
to import a migration directory, users must supply multiple parameters:
--from
theURL to the migration directory to import, theformat
query parameter controls themigration directory format, e.g.file://migrations?format=flyway
. Supported formats areatlas
(default),golang-migrate
,goose
,flyway
,liquibase
anddbmate
.--to
the URL of the migration directory to save imported migration files into, by default it isfile://migrations
.
Limitations
Importing an existing migration directory has some limitations:
Comments not directly preceding a SQL statement will get lost.
- source.sql
- imported.sql
-- This comment will get lost
-- This will be preserved
/*
This will be preservedas well
/*
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);-- This will get lost.
-- This will be preserved
/*
This will be preservedas well
/*
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);
Rollback migrations will not get imported.
Atlas does not have the concept of rollback migrations. Therefore migrations to undo an applied migration, often called"down" or "undo" migrations, will not be imported into the new migration directory. For migration formats having the rollbackmigration part of one file separated by some directive, the rollback parts are stripped away.
- source.sql
- imported.sql
-- +goose Up
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);
-- +goose Down
DROPTABLE`users`;
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);
Repeatable Migrations
Flyway has the concept of repeatable migrations, however, Atlas does not. In Flyway repeatable migrations are run last,if their contents did change. Atlas tries to reproduce this behavior by creating versioned migrations out of eachrepeatable migration file found and giving them the characterR
as version suffix.
- V1__users.sql
- R__users_view.sql
- 1_users.sql
- 1R_users_view.sql
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);
CREATEVIEW`users_over_30`ASSELECT*FROM`users`where`age`>30;
CREATETABLE`users`(
`id`bigint(20)NOTNULLAUTO_INCREMENT,
`age`bigint(20)NOTNULL,
`name`varchar(255)COLLATE utf8mb4_binNOTNULL,
PRIMARYKEY(`id`),
UNIQUEKEY`age`(`age`)
);
CREATEVIEW`users_over_30`ASSELECT*FROM`users`where`age`>30;
Examples
Import existinggolang-migrate/migrate
migration directory:
atlas migrateimport\
--from"file://migrations?format=golang-migrate"\
--to"file://atlas-migrations"
Import existing Flyway migration directory:
atlas migrateimport\
--from"file://migrations?format=flyway"\
--to"file://atlas-migrations"