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//! You can also define migrations in your rust code by using the [`RustMigrationSource`].
37//! The [`CombinedMigrationSource`] allows you to combine migrations from different sources
38//! to execute them together
39//!
40//! ## Transactions
41//!
42//! By default, each migration runs inside its own transaction. This means a failing
43//! migration is automatically rolled back, leaving the database in the state it was
44//! in before the migration started.
45//!
46//! You can disable this behaviour on a per-migration basis. For file-based migrations,
47//! add a `metadata.toml` file to the migration folder with `run_in_transaction = false`.
48//! See [`FileBasedMigrations`] for a full example and an explanation of the
49//! "cannot run inside a transaction block" error that certain database operations produce.
50//!
51//! For Rust-based migrations, call [`RustMigration::without_transaction`] on the builder,
52//! or return `false` from [`TypedMigration::run_in_transaction`] when implementing the
53//! trait directly.
54
55mod combined_migrations;
56mod embedded_migrations;
57mod errors;
58mod file_based_migrations;
59mod migration_harness;
60mod rust_migrations;
61
62pub use self::embedded_migrations::EmbeddedMigrations;
63pub use self::file_based_migrations::FileBasedMigrations;
64pub use self::migration_harness::{HarnessWithOutput, MigrationHarness};
65pub use self::rust_migrations::{RustMigration, RustMigrationSource, TypedMigration};
66pub use combined_migrations::CombinedMigrationSource;
67pub use migrations_macros::embed_migrations;
68
69#[doc(hidden)]
70pub use crate::embedded_migrations::{EmbeddedMigration, EmbeddedName};
71#[doc(hidden)]
72pub use crate::errors::MigrationError;
73#[doc(hidden)]
74pub use crate::file_based_migrations::TomlMetadataWrapper;