-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
133f9d8
commit 868a1df
Showing
9 changed files
with
209 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
use crate::{migration::migration::Migration, Error}; | ||
use indicatif::ProgressBar; | ||
use rocksdb::DB; | ||
use std::sync::Arc; | ||
|
||
const INIT_DB_VERSION: &str = "20351116135521"; | ||
|
||
pub struct SampleMigration { | ||
version: String, | ||
} | ||
|
||
impl SampleMigration { | ||
pub fn new() -> Self { | ||
Self { | ||
version: INIT_DB_VERSION.to_string(), | ||
} | ||
} | ||
} | ||
|
||
impl Migration for SampleMigration { | ||
fn migrate( | ||
&self, | ||
db: Arc<DB>, | ||
_pb: Arc<dyn Fn(u64) -> ProgressBar + Send + Sync>, | ||
) -> Result<Arc<DB>, Error> { | ||
eprintln!("SampleMigration::migrate ..........."); | ||
Ok(db) | ||
} | ||
|
||
fn version(&self) -> &str { | ||
&self.version | ||
} | ||
|
||
fn expensive(&self) -> bool { | ||
false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
pub const MIGRATION_VERSION_KEY: &[u8] = b"db-version"; | ||
|
||
pub mod migrate; | ||
pub mod migration; | ||
pub mod migrations; | ||
pub mod db_migrate; | ||
pub(crate) mod migration; | ||
pub(crate) mod migrations; | ||
#[cfg(test)] | ||
mod tests; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use crate::migration::db_migrate::DbMigrate; | ||
use crate::migration::migration::Migration; | ||
use crate::migration::migration::Migrations; | ||
use crate::Error; | ||
use indicatif::ProgressBar; | ||
use rocksdb::DB; | ||
use std::cmp::Ordering; | ||
use std::sync::{Arc, RwLock}; | ||
|
||
fn gen_path() -> std::path::PathBuf { | ||
let tmp_dir = tempfile::Builder::new() | ||
.prefix("test_fiber_") | ||
.tempdir() | ||
.unwrap(); | ||
tmp_dir.as_ref().to_path_buf() | ||
} | ||
|
||
#[test] | ||
fn test_default_migration() { | ||
let migrate = DbMigrate::new(gen_path()); | ||
assert!(migrate.need_init()); | ||
assert_eq!(migrate.check(), Ordering::Less); | ||
migrate.init_db_version().unwrap(); | ||
assert!(!migrate.need_init()); | ||
assert_eq!(migrate.check(), Ordering::Equal); | ||
} | ||
|
||
#[test] | ||
fn test_run_migration() { | ||
let run_count = Arc::new(RwLock::new(0)); | ||
|
||
pub struct DummyMigration { | ||
version: String, | ||
run_count: Arc<RwLock<usize>>, | ||
} | ||
|
||
impl DummyMigration { | ||
pub fn new(version: &str, run_count: Arc<RwLock<usize>>) -> Self { | ||
Self { | ||
version: version.to_string(), | ||
run_count, | ||
} | ||
} | ||
} | ||
|
||
impl Migration for DummyMigration { | ||
fn migrate( | ||
&self, | ||
db: Arc<DB>, | ||
_pb: Arc<dyn Fn(u64) -> ProgressBar + Send + Sync>, | ||
) -> Result<Arc<DB>, Error> { | ||
eprintln!("DummyMigration::migrate {} ... ", self.version); | ||
let mut count = self.run_count.write().unwrap(); | ||
*count += 1; | ||
Ok(db) | ||
} | ||
|
||
fn version(&self) -> &str { | ||
&self.version | ||
} | ||
|
||
fn expensive(&self) -> bool { | ||
false | ||
} | ||
} | ||
|
||
let migrate = DbMigrate::new(gen_path()); | ||
migrate.init_db_version().unwrap(); | ||
let db = migrate.db(); | ||
|
||
let mut migrations = Migrations::default(); | ||
migrations.add_migration(Arc::new(DummyMigration::new( | ||
"20221116135521", | ||
run_count.clone(), | ||
))); | ||
|
||
migrations.add_migration(Arc::new(DummyMigration::new( | ||
"20251116135521", | ||
run_count.clone(), | ||
))); | ||
migrations.add_migration(Arc::new(DummyMigration::new( | ||
"20251116135522", | ||
run_count.clone(), | ||
))); | ||
migrations.add_migration(Arc::new(DummyMigration::new( | ||
"20251116135523", | ||
run_count.clone(), | ||
))); | ||
|
||
assert_eq!(migrations.check(db.clone()), Ordering::Less); | ||
migrations.migrate(db.clone()).unwrap(); | ||
assert_eq!(*run_count.read().unwrap(), 3); | ||
assert_eq!(migrations.check(db), Ordering::Equal); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
mod migrate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters