macro_rules! include_migrations {
($dir:tt) => { ... };
}
Expand description
Embed migrations from the given directory into the current binary for PostgreSQL or SQLite.
The macro invocation include_migrations!(path)
evaluates to an expression of type impl Iterator<Item = Migration>
. Each migration must be a text file which is an immediate child of
path
, and there must be no non-migration files in path
. The migration files must have names
of the form V${version}__${name}.sql
, where version
is a positive integer indicating how the
migration is to be ordered relative to other migrations, and name
is a descriptive name for
the migration.
path
should be an absolute path. It is possible to give a path relative to the root of the
invoking crate by using environment variable expansions and the CARGO_MANIFEST_DIR
environment
variable.
As an example, this is the invocation used to load the default migrations from the
hotshot-query-service
crate. The migrations are located in a directory called migrations
at
- PostgreSQL migrations are in
/migrations/postgres
. - SQLite migrations are in
/migrations/sqlite
.
// For PostgreSQL
#[cfg(not(feature = "embedded-db"))]
let mut migrations: Vec<Migration> =
include_migrations!("$CARGO_MANIFEST_DIR/migrations/postgres").collect();
// For SQLite
#[cfg(feature = "embedded-db")]
let mut migrations: Vec<Migration> =
include_migrations!("$CARGO_MANIFEST_DIR/migrations/sqlite").collect();
migrations.sort();
assert_eq!(migrations[0].version(), 10);
assert_eq!(migrations[0].name(), "init_schema");
Note that a similar macro is available from Refinery:
embed_migrations. This
macro differs in that it evaluates to an iterator of migrations, making it an
expression macro, while embed_migrations
is a statement macro that defines a module which
provides access to the embedded migrations only indirectly via a
Runner
. The direct access to
migrations provided by include_migrations
makes this macro easier to use with
Config::migrations
, for combining custom migrations with default_migrations
.