diff --git a/butane/tests/migration-tests.rs b/butane/tests/migration-tests.rs index e4f63a3..db87081 100644 --- a/butane/tests/migration-tests.rs +++ b/butane/tests/migration-tests.rs @@ -344,7 +344,7 @@ fn test_migrate( let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 2); - migrations::migrate(conn, &ms).unwrap(); + ms.migrate(conn).unwrap(); let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 0); @@ -352,7 +352,7 @@ fn test_migrate( verify_sql(conn, &ms, expected_up_sql, expected_down_sql); // Now downgrade, just to make sure we can - migrations::rollback(conn, &ms).unwrap(); + ms.unmigrate(conn).unwrap(); let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 2); @@ -563,7 +563,7 @@ fn migration_delete_table(conn: &mut Connection, expected_up_sql: &str, expected let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 2); - migrations::migrate(conn, &ms).unwrap(); + ms.migrate(conn).unwrap(); let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 0); @@ -571,7 +571,7 @@ fn migration_delete_table(conn: &mut Connection, expected_up_sql: &str, expected verify_sql(conn, &ms, expected_up_sql, expected_down_sql); // Now downgrade, just to make sure we can - migrations::rollback(conn, &ms).unwrap(); + ms.unmigrate(conn).unwrap(); let to_apply = ms.unapplied_migrations(conn).unwrap(); assert_eq!(to_apply.len(), 2); diff --git a/butane_core/src/migrations/mod.rs b/butane_core/src/migrations/mod.rs index 831bc54..299c43d 100644 --- a/butane_core/src/migrations/mod.rs +++ b/butane_core/src/migrations/mod.rs @@ -8,7 +8,7 @@ use std::path::Path; use fallible_iterator::FallibleIterator; use nonempty::NonEmpty; -use crate::db::BackendRows; +use crate::db::{BackendConnection, BackendRows}; use crate::db::{Column, ConnectionMethods}; use crate::sqlval::{FromSql, SqlValRef, ToSql}; use crate::{db, query, DataObject, DataResult, Error, PrimaryKeyType, Result, SqlType}; @@ -109,6 +109,34 @@ pub trait Migrations { } Ok(None) } + + /// Migrate connection forward. + fn migrate(&self, connection: &mut impl BackendConnection) -> Result<()> { + let to_apply = self.unapplied_migrations(connection)?; + for migration in &to_apply { + crate::info!("Applying migration {}", migration.name()); + migration.apply(connection)?; + } + Ok(()) + } + + /// Remove all applied migrations. + fn unmigrate(&self, connection: &mut impl BackendConnection) -> Result<()> { + let mut migration = match self.last_applied_migration(connection)? { + Some(migration) => migration, + None => return Ok(()), + }; + migration.downgrade(connection)?; + + while let Ok(Some(migration_name)) = migration.migration_from() { + migration = self + .get_migration(&migration_name) + .ok_or(Error::MigrationError("Migration not in chain".to_string()))?; + crate::info!("Rolling back migration {}", migration.name()); + migration.downgrade(connection)?; + } + Ok(()) + } } /// Extension of [`Migrations`] to modify the series of migrations. @@ -326,37 +354,3 @@ impl crate::internal::DataObjectInternal for ButaneMigration { Ok(()) // no-op } } - -/// Migrate connection forward. -pub fn migrate( - connection: &mut impl crate::db::BackendConnection, - migrations: &impl Migrations, -) -> Result<()> { - let to_apply = migrations.unapplied_migrations(connection)?; - for migration in &to_apply { - crate::info!("Applying migration {}", migration.name()); - migration.apply(connection)?; - } - Ok(()) -} - -/// Rollback all applied migrations. -pub fn rollback( - connection: &mut impl crate::db::BackendConnection, - migrations: &impl Migrations, -) -> Result<()> { - let mut migration = match migrations.last_applied_migration(connection)? { - Some(migration) => migration, - None => return Ok(()), - }; - migration.downgrade(connection)?; - - while let Ok(Some(migration_name)) = migration.migration_from() { - migration = migrations - .get_migration(&migration_name) - .ok_or(Error::MigrationError("Migration not in chain".to_string()))?; - crate::info!("Rolling back migration {}", migration.name()); - migration.downgrade(connection)?; - } - Ok(()) -} diff --git a/butane_test_helper/src/lib.rs b/butane_test_helper/src/lib.rs index d206460..9bb1e8c 100644 --- a/butane_test_helper/src/lib.rs +++ b/butane_test_helper/src/lib.rs @@ -10,7 +10,7 @@ use std::sync::Mutex; use block_id::{Alphabet, BlockId}; use butane_core::db::{connect, get_backend, pg, sqlite, Backend, Connection, ConnectionSpec}; -use butane_core::migrations::{self, MemMigrations, Migration, MigrationsMut}; +use butane_core::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut}; use once_cell::sync::Lazy; use uuid::Uuid; @@ -209,7 +209,7 @@ pub fn setup_db(backend: Box, conn: &mut Connection, migrate: bool) "expected to create migration" ); log::info!("created current migration"); - migrations::migrate(conn, &mem_migrations).unwrap(); + mem_migrations.migrate(conn).unwrap(); } /// Create a sqlite [`Connection`]. diff --git a/docs/getting-started.md b/docs/getting-started.md index 969b401..a288d46 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -505,11 +505,11 @@ to use these migrations: ``` rust pub fn establish_connection() -> Connection { - use butane::migrations; + use butane::migrations::Migrations; let mut connection = butane::db::connect(&ConnectionSpec::load(".butane/connection.json").unwrap()).unwrap(); let migrations = butane_migrations::get_migrations().unwrap(); - migrations::migrate(&mut connection, &migrations).unwrap(); + migrations.migrate(&mut connection).unwrap(); connection } ``` diff --git a/examples/getting_started/src/lib.rs b/examples/getting_started/src/lib.rs index 3dd974c..ab2b727 100644 --- a/examples/getting_started/src/lib.rs +++ b/examples/getting_started/src/lib.rs @@ -6,7 +6,7 @@ pub mod butane_migrations; pub mod models; use butane::db::{Connection, ConnectionSpec}; -use butane::migrations; +use butane::migrations::Migrations; use butane::prelude::*; use models::{Blog, Post}; @@ -15,7 +15,7 @@ pub fn establish_connection() -> Connection { let mut connection = butane::db::connect(&ConnectionSpec::load(".butane/connection.json").unwrap()).unwrap(); let migrations = butane_migrations::get_migrations().unwrap(); - migrations::migrate(&mut connection, &migrations).unwrap(); + migrations.migrate(&mut connection).unwrap(); connection } diff --git a/examples/getting_started/tests/rollback.rs b/examples/getting_started/tests/rollback.rs index d9b5904..0b14a67 100644 --- a/examples/getting_started/tests/rollback.rs +++ b/examples/getting_started/tests/rollback.rs @@ -1,5 +1,5 @@ use butane::db::{BackendConnection, Connection}; -use butane::migrations; +use butane::migrations::Migrations; use butane::DataObject; use butane_test_helper::*; @@ -39,11 +39,11 @@ fn migrate_and_rollback(mut connection: Connection) { let base_dir = std::path::PathBuf::from(".butane"); let migrations = butane_cli::get_migrations(&base_dir).unwrap(); - migrations::migrate(&mut connection, &migrations).unwrap(); + migrations.migrate(&mut connection).unwrap(); insert_data(&connection); // Rollback migrations. - migrations::rollback(&mut connection, &migrations).unwrap(); + migrations.unmigrate(&mut connection).unwrap(); } testall_no_migrate!(migrate_and_rollback); diff --git a/examples/newtype/src/lib.rs b/examples/newtype/src/lib.rs index 261f5d1..fdbf5c4 100644 --- a/examples/newtype/src/lib.rs +++ b/examples/newtype/src/lib.rs @@ -6,7 +6,7 @@ pub mod butane_migrations; pub mod models; use butane::db::{Connection, ConnectionSpec}; -use butane::migrations; +use butane::migrations::Migrations; use butane::prelude::*; use models::{Blog, Post}; @@ -15,7 +15,7 @@ pub fn establish_connection() -> Connection { let mut connection = butane::db::connect(&ConnectionSpec::load(".butane/connection.json").unwrap()).unwrap(); let migrations = butane_migrations::get_migrations().unwrap(); - migrations::migrate(&mut connection, &migrations).unwrap(); + migrations.migrate(&mut connection).unwrap(); connection } diff --git a/examples/newtype/tests/rollback.rs b/examples/newtype/tests/rollback.rs index e77beb0..fdc9efd 100644 --- a/examples/newtype/tests/rollback.rs +++ b/examples/newtype/tests/rollback.rs @@ -33,7 +33,7 @@ fn migrate_and_rollback(mut connection: Connection) { let migrations = butane_cli::get_migrations(&base_dir).unwrap(); let to_apply = migrations.unapplied_migrations(&connection).unwrap(); - migrations::migrate(&mut connection, &migrations).unwrap(); + migrations.migrate(&mut connection).unwrap(); insert_data(&connection);