toasty_core/schema/db/
migration.rs

1/// A database migration generated from a [`SchemaDiff`](super::SchemaDiff) by a driver.
2///
3/// Currently only SQL migrations are supported. Multiple SQL statements
4/// within a single migration are separated by breakpoint markers
5/// (`-- #[toasty::breakpoint]`).
6///
7/// # Examples
8///
9/// ```ignore
10/// use toasty_core::schema::db::Migration;
11///
12/// let m = Migration::new_sql("CREATE TABLE users (id INTEGER PRIMARY KEY)".to_string());
13/// assert_eq!(m.statements(), vec!["CREATE TABLE users (id INTEGER PRIMARY KEY)"]);
14/// ```
15pub enum Migration {
16    /// A SQL migration containing one or more statements.
17    Sql(String),
18}
19
20impl Migration {
21    /// Creates a SQL migration from a single SQL string.
22    pub fn new_sql(sql: String) -> Self {
23        Migration::Sql(sql)
24    }
25
26    /// Creates a SQL migration from multiple SQL statements.
27    /// Statements are joined with `-- #[toasty::breakpoint]` markers.
28    pub fn new_sql_with_breakpoints<S: AsRef<str>>(statements: &[S]) -> Self {
29        let sql = statements
30            .iter()
31            .map(|s| s.as_ref())
32            .collect::<Vec<_>>()
33            .join("\n-- #[toasty::breakpoint]\n");
34        Migration::Sql(sql)
35    }
36
37    /// Returns individual SQL statements by splitting on breakpoint markers.
38    pub fn statements(&self) -> Vec<&str> {
39        match self {
40            Migration::Sql(sql) => sql.split("\n-- #[toasty::breakpoint]\n").collect(),
41        }
42    }
43}
44
45/// Metadata about a migration that has already been applied to a database.
46///
47/// Stores the unique migration ID assigned by the migration system.
48///
49/// # Examples
50///
51/// ```ignore
52/// use toasty_core::schema::db::AppliedMigration;
53///
54/// let applied = AppliedMigration::new(42);
55/// assert_eq!(applied.id(), 42);
56/// ```
57pub struct AppliedMigration {
58    id: u64,
59}
60
61impl AppliedMigration {
62    /// Creates a new `AppliedMigration` with the given ID.
63    pub fn new(id: u64) -> Self {
64        Self { id }
65    }
66
67    /// Returns the migration's unique ID.
68    pub fn id(&self) -> u64 {
69        self.id
70    }
71}