Skip to main content

migratio/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! `migratio` is a library for managing database migrations.
3//!
4//! Core concepts:
5//! - Live DB Connections: `migratio` supplies migration definitions with a live connection to the database, allowing more expressive migration logic than just preparing SQL statements.
6//! - Code-First: `migratio` is a code-first library, making embedding it in your application easier than other CLI-first libraries.
7//!
8//! # Motivation
9//!
10//! ## Using a Live Database Connection
11//!
12//! Most Rust-based migration solutions focus only on using SQL to define migration logic.
13//! Even the ones that say they support writing migrations in Rust use Rust to simply construct SQL instructions, like [Refinery](https://github.com/rust-db/refinery/blob/main/examples/migrations/V3__add_brand_to_cars_table.rs).
14//!
15//! Taking a hint from [Alembic](https://alembic.sqlalchemy.org/en/latest/), this library provides the user with a live connection to the database with which to define their migrations.
16//! With a live connection, a migration can query the data, transform it in Rust, and write it back.
17//! Migrations defined as pure SQL statements can only accomplish this with the toolkit provided by SQL, which is much more limited.
18//!
19//! Note: [SeaORM](https://www.sea-ql.org/SeaORM/) allows this, but `migratio` aims to provide an alternative for developers that don't want to adopt a full ORM solution.
20//!
21//! ## Code-first approach
22//!
23//! A central use case for `migratio` is embedding migration logic within an application, usually in the startup procedure.
24//! This way, when the application updates, the next time it starts the database will automatically be migrated to the latest version without any manual intervention.
25//!
26//! # Benefits
27//! - Easy adoption from other migration tools or no migration tool.
28//! - Robust error handling and rollback support (when available).
29//! - Preview / dry-run support.
30//! - Migration history querying.
31//! - Observability hooks.
32//! - Tracing integration = available with the `tracing` feature flag.
33//! - Testing utilities - available with the `testing` feature flag.
34//!
35//! # Database support
36//!
37//! - [`SQLite`](sqlite) - available with the `sqlite` feature flag.
38//! - [`MySQL`](mysql) - available with the `mysql` feature flag.
39//! - [`PostgreSQL`](postgres) - available with the `postgres` feature flag.
40
41mod core;
42pub use core::{
43    AppliedMigration, Migration, MigrationFailure, MigrationReport, MigrationType, Precondition,
44};
45
46mod error;
47pub use error::Error;
48
49#[macro_use]
50mod macros;
51
52#[cfg(feature = "sqlite")]
53#[cfg_attr(docsrs, doc(cfg(feature = "sqlite")))]
54pub mod sqlite;
55
56#[cfg(feature = "mysql")]
57pub mod mysql;
58
59#[cfg(feature = "postgres")]
60#[cfg_attr(docsrs, doc(cfg(feature = "postgres")))]
61pub mod postgres;
62
63#[cfg(feature = "testing")]
64pub mod testing;
65
66#[cfg(all(test, feature = "mysql"))]
67pub(crate) mod test_mysql;
68
69#[cfg(all(test, feature = "postgres"))]
70pub(crate) mod test_postgres;