Skip to main content

diesel_migrations/
lib.rs

1// Built-in Lints
2// Clippy lints
3#![allow(clippy::needless_pass_by_value, clippy::map_unwrap_or)]
4#![warn(
5    missing_docs,
6    clippy::mut_mut,
7    clippy::non_ascii_literal,
8    clippy::similar_names,
9    clippy::unicode_not_nfc,
10    clippy::if_not_else,
11    clippy::items_after_statements,
12    clippy::used_underscore_binding,
13    missing_copy_implementations
14)]
15//! Provides functions for maintaining database schema.
16//!
17//! A database migration always provides procedures to update the schema, as well as to revert
18//! itself. Diesel's migrations are versioned, and run in order. Diesel also takes care of tracking
19//! which migrations have already been run automatically. Your migrations don't need to be
20//! idempotent, as Diesel will ensure no migration is run twice unless it has been reverted.
21//!
22//! Migrations should be placed in a `/migrations` directory at the root of your project (the same
23//! directory as `Cargo.toml`). When any of these functions are run, Diesel will search for the
24//! migrations directory in the current directory and its parents, stopping when it finds the
25//! directory containing `Cargo.toml`.
26//!
27//! Individual migrations should be a folder containing two files, `up.sql` and `down.sql`.
28//! `up.sql` will be used to run the migration, while `down.sql` will be used for reverting it. The
29//! folder itself should have the structure `{version}_{migration_name}`. It is recommended that
30//! you use the timestamp of creation for the version.
31//!
32//! Migrations can either be run with the CLI or embedded into the compiled application
33//! and executed with code, for example right after establishing a database connection.
34//! For more information, consult the [`embed_migrations!`] macro.
35//!
36//! ## Transactions
37//!
38//! By default, each migration runs inside its own transaction. This means a failing
39//! migration is automatically rolled back, leaving the database in the state it was
40//! in before the migration started.
41//!
42//! You can disable this behaviour on a per-migration basis. For file-based migrations,
43//! add a `metadata.toml` file to the migration folder with `run_in_transaction = false`.
44//! See [`FileBasedMigrations`] for a full example and an explanation of the
45//! "cannot run inside a transaction block" error that certain database operations produce.
46
47mod embedded_migrations;
48mod errors;
49mod file_based_migrations;
50mod migration_harness;
51
52pub use crate::embedded_migrations::EmbeddedMigrations;
53pub use crate::file_based_migrations::FileBasedMigrations;
54pub use crate::migration_harness::{HarnessWithOutput, MigrationHarness};
55pub use migrations_macros::embed_migrations;
56
57#[doc(hidden)]
58pub use crate::embedded_migrations::{EmbeddedMigration, EmbeddedName};
59#[doc(hidden)]
60pub use crate::errors::MigrationError;
61#[doc(hidden)]
62pub use crate::file_based_migrations::TomlMetadataWrapper;