|
@@ -85,14 +85,14 @@ impl MigratableItem<Schema1> for Schema2b {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-type Schemas = (Schema1, Schema2);
|
|
|
+type Schemas12 = (Schema1, Schema2);
|
|
|
|
|
|
#[test]
|
|
|
fn run_empty_migration() {
|
|
|
let (pool, _db): (_, Schema2) = common::open_test_db!();
|
|
|
|
|
|
let mut lease = pool.acquire().unwrap();
|
|
|
- run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
|
+ run_migration::<Schemas12>(&mut lease).expect("migration result");
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -100,7 +100,7 @@ fn run_simple_migration() {
|
|
|
let (pool, _db): (_, Schema1) = common::open_test_db!();
|
|
|
|
|
|
let mut lease = pool.acquire().unwrap();
|
|
|
- run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
|
+ run_migration::<Schemas12>(&mut lease).expect("migration result");
|
|
|
}
|
|
|
|
|
|
#[test]
|
|
@@ -118,7 +118,7 @@ fn check_simple_migration() {
|
|
|
)
|
|
|
.expect("insert Schema1 kv");
|
|
|
|
|
|
- run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
|
+ run_migration::<Schemas12>(&mut lease).expect("migration result");
|
|
|
|
|
|
let db2 = Schema2::default();
|
|
|
let migrated = db2
|
|
@@ -153,3 +153,179 @@ fn check_incorrect_migration() {
|
|
|
panic!("incorrect migration succeeded");
|
|
|
};
|
|
|
}
|
|
|
+
|
|
|
+mod schema4 {
|
|
|
+ use microrm::prelude::*;
|
|
|
+
|
|
|
+ pub struct BookCategory;
|
|
|
+ impl microrm::Relation for BookCategory {
|
|
|
+ type Domain = Category;
|
|
|
+ type Range = Book;
|
|
|
+ const NAME: &'static str = "book_category";
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ pub struct Category {
|
|
|
+ #[key]
|
|
|
+ pub name: String,
|
|
|
+
|
|
|
+ pub books: microrm::RelationDomain<BookCategory>,
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ pub struct Book {
|
|
|
+ pub title: String,
|
|
|
+ pub author: String,
|
|
|
+
|
|
|
+ pub category: microrm::RelationRange<BookCategory>,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+mod schema5 {
|
|
|
+ use microrm::prelude::*;
|
|
|
+
|
|
|
+ pub struct BookCategory;
|
|
|
+ impl microrm::Relation for BookCategory {
|
|
|
+ type Domain = Category;
|
|
|
+ type Range = Book;
|
|
|
+ const NAME: &'static str = "book_category";
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ #[migratable(super::schema4::Category)]
|
|
|
+ pub struct Category {
|
|
|
+ #[key]
|
|
|
+ pub name: String,
|
|
|
+
|
|
|
+ pub books: microrm::RelationDomain<BookCategory>,
|
|
|
+ }
|
|
|
+
|
|
|
+ #[derive(Entity)]
|
|
|
+ pub struct Book {
|
|
|
+ pub title: String,
|
|
|
+ pub author: String,
|
|
|
+ pub isbn: Option<String>,
|
|
|
+
|
|
|
+ pub category: microrm::RelationRange<super::schema4::BookCategory>,
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Default, Schema)]
|
|
|
+pub struct Schema4 {
|
|
|
+ pub category: microrm::IDMap<schema4::Category>,
|
|
|
+ pub book: microrm::IDMap<schema4::Book>,
|
|
|
+}
|
|
|
+
|
|
|
+#[derive(Default, Schema)]
|
|
|
+pub struct Schema5 {
|
|
|
+ pub category: microrm::IDMap<schema5::Category>,
|
|
|
+ pub book: microrm::IDMap<schema5::Book>,
|
|
|
+}
|
|
|
+
|
|
|
+impl MigratableEntity<schema4::Book> for schema5::Book {
|
|
|
+ fn migrate(from: &schema4::Book) -> microrm::DBResult<Option<schema5::Book>> {
|
|
|
+ Ok(Some(schema5::Book {
|
|
|
+ title: from.title.clone(),
|
|
|
+ author: from.author.clone(),
|
|
|
+ isbn: None,
|
|
|
+ category: Default::default(),
|
|
|
+ }))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+impl MigratableItem<Schema4> for Schema5 {
|
|
|
+ fn run_migration(ctx: &mut MigrationContext) -> microrm::DBResult<()> {
|
|
|
+ ctx.migrate_entity::<schema4::Category, schema5::Category>()?;
|
|
|
+ ctx.migrate_entity::<schema4::Book, schema5::Book>()?;
|
|
|
+
|
|
|
+ Ok(())
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+#[test]
|
|
|
+fn check_relation_preservation() {
|
|
|
+ let (pool, db): (_, Schema4) = common::open_test_db!();
|
|
|
+ let mut lease = pool.acquire().unwrap();
|
|
|
+
|
|
|
+ // add some very simple test data
|
|
|
+ let cat_a = db
|
|
|
+ .category
|
|
|
+ .insert_and_return(
|
|
|
+ &mut lease,
|
|
|
+ schema4::Category {
|
|
|
+ name: "A".to_string(),
|
|
|
+ books: Default::default(),
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .expect("category A");
|
|
|
+ let cat_b = db
|
|
|
+ .category
|
|
|
+ .insert_and_return(
|
|
|
+ &mut lease,
|
|
|
+ schema4::Category {
|
|
|
+ name: "B".to_string(),
|
|
|
+ books: Default::default(),
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .expect("category B");
|
|
|
+
|
|
|
+ cat_a
|
|
|
+ .books
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ schema4::Book {
|
|
|
+ title: "0".into(),
|
|
|
+ author: "a".into(),
|
|
|
+ category: Default::default(),
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .expect("book 0");
|
|
|
+
|
|
|
+ cat_a
|
|
|
+ .books
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ schema4::Book {
|
|
|
+ title: "2".into(),
|
|
|
+ author: "b".into(),
|
|
|
+ category: Default::default(),
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .expect("book 2");
|
|
|
+
|
|
|
+ cat_b
|
|
|
+ .books
|
|
|
+ .insert(
|
|
|
+ &mut lease,
|
|
|
+ schema4::Book {
|
|
|
+ title: "1".into(),
|
|
|
+ author: "c".into(),
|
|
|
+ category: Default::default(),
|
|
|
+ },
|
|
|
+ )
|
|
|
+ .expect("book 1");
|
|
|
+
|
|
|
+ // now perform the migration
|
|
|
+ run_migration::<(Schema4, Schema5)>(&mut lease).expect("migration");
|
|
|
+
|
|
|
+ // do some quick integrity checks
|
|
|
+ let ndb = Schema5::default();
|
|
|
+
|
|
|
+ let ncat_a = ndb
|
|
|
+ .category
|
|
|
+ .with(schema5::Category::Name, "A")
|
|
|
+ .first()
|
|
|
+ .get(&mut lease)
|
|
|
+ .expect("ndb get")
|
|
|
+ .expect("ndb get");
|
|
|
+ let ncat_b = ndb
|
|
|
+ .category
|
|
|
+ .with(schema5::Category::Name, "B")
|
|
|
+ .first()
|
|
|
+ .get(&mut lease)
|
|
|
+ .expect("ndb get")
|
|
|
+ .expect("ndb get");
|
|
|
+
|
|
|
+ assert_eq!(ncat_a.books.count(&mut lease).ok(), Some(2));
|
|
|
+ assert_eq!(ncat_b.books.count(&mut lease).ok(), Some(1));
|
|
|
+}
|