|
@@ -10,14 +10,10 @@ mod schema1 {
|
|
|
|
|
|
#[derive(Entity)]
|
|
#[derive(Entity)]
|
|
pub struct KVEntity {
|
|
pub struct KVEntity {
|
|
|
|
+ #[key]
|
|
pub k: usize,
|
|
pub k: usize,
|
|
pub v: String,
|
|
pub v: String,
|
|
}
|
|
}
|
|
-
|
|
|
|
- #[derive(Default, Schema)]
|
|
|
|
- pub struct Schema {
|
|
|
|
- pub kv: microrm::IDMap<KVEntity>,
|
|
|
|
- }
|
|
|
|
}
|
|
}
|
|
|
|
|
|
mod schema2 {
|
|
mod schema2 {
|
|
@@ -25,17 +21,28 @@ mod schema2 {
|
|
|
|
|
|
#[derive(Entity)]
|
|
#[derive(Entity)]
|
|
pub struct KVEntity {
|
|
pub struct KVEntity {
|
|
|
|
+ #[key]
|
|
pub k: String,
|
|
pub k: String,
|
|
pub v: String,
|
|
pub v: String,
|
|
}
|
|
}
|
|
|
|
+}
|
|
|
|
|
|
- #[derive(Default, Schema)]
|
|
|
|
- pub struct Schema {
|
|
|
|
- pub kv: microrm::IDMap<KVEntity>,
|
|
|
|
- }
|
|
|
|
|
|
+#[derive(Default, Schema)]
|
|
|
|
+pub struct Schema1 {
|
|
|
|
+ pub kv: microrm::IDMap<schema1::KVEntity>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Default, Schema)]
|
|
|
|
+pub struct Schema2 {
|
|
|
|
+ pub kv: microrm::IDMap<schema2::KVEntity>,
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+#[derive(Default, Schema)]
|
|
|
|
+pub struct Schema2b {
|
|
|
|
+ pub kv: microrm::IDMap<schema2::KVEntity>,
|
|
}
|
|
}
|
|
|
|
|
|
-impl MigratableItem<schema1::Schema> for schema2::Schema {
|
|
|
|
|
|
+impl MigratableItem<Schema1> for Schema2 {
|
|
fn run_migration(ctx: &mut MigrationContext) -> microrm::DBResult<()>
|
|
fn run_migration(ctx: &mut MigrationContext) -> microrm::DBResult<()>
|
|
where
|
|
where
|
|
Self: Sized,
|
|
Self: Sized,
|
|
@@ -44,7 +51,7 @@ impl MigratableItem<schema1::Schema> for schema2::Schema {
|
|
|
|
|
|
let ipkv2 = ctx.in_progress::<schema2::KVEntity>()?;
|
|
let ipkv2 = ctx.in_progress::<schema2::KVEntity>()?;
|
|
|
|
|
|
- for kv in schema1::Schema::default().kv.get(ctx.lease())? {
|
|
|
|
|
|
+ for kv in Schema1::default().kv.get(ctx.lease())? {
|
|
ipkv2.insert_matching(
|
|
ipkv2.insert_matching(
|
|
ctx.lease(),
|
|
ctx.lease(),
|
|
schema2::KVEntity {
|
|
schema2::KVEntity {
|
|
@@ -68,11 +75,21 @@ impl MigratableItem<schema1::Schema> for schema2::Schema {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-type Schemas = (schema1::Schema, schema2::Schema);
|
|
|
|
|
|
+impl MigratableItem<Schema1> for Schema2b {
|
|
|
|
+ fn run_migration(_ctx: &mut MigrationContext) -> microrm::DBResult<()>
|
|
|
|
+ where
|
|
|
|
+ Self: Sized,
|
|
|
|
+ {
|
|
|
|
+ // incorrectly, don't do anything. this should be caught.
|
|
|
|
+ Ok(())
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+type Schemas = (Schema1, Schema2);
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
fn run_empty_migration() {
|
|
fn run_empty_migration() {
|
|
- let (pool, _db): (_, schema2::Schema) = common::open_test_db!();
|
|
|
|
|
|
+ let (pool, _db): (_, Schema2) = common::open_test_db!();
|
|
|
|
|
|
let mut lease = pool.acquire().unwrap();
|
|
let mut lease = pool.acquire().unwrap();
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
@@ -80,7 +97,7 @@ fn run_empty_migration() {
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
fn run_simple_migration() {
|
|
fn run_simple_migration() {
|
|
- let (pool, _db): (_, schema1::Schema) = common::open_test_db!();
|
|
|
|
|
|
+ let (pool, _db): (_, Schema1) = common::open_test_db!();
|
|
|
|
|
|
let mut lease = pool.acquire().unwrap();
|
|
let mut lease = pool.acquire().unwrap();
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
@@ -88,7 +105,7 @@ fn run_simple_migration() {
|
|
|
|
|
|
#[test]
|
|
#[test]
|
|
fn check_simple_migration() {
|
|
fn check_simple_migration() {
|
|
- let (pool, db): (_, schema1::Schema) = common::open_test_db!();
|
|
|
|
|
|
+ let (pool, db): (_, Schema1) = common::open_test_db!();
|
|
let mut lease = pool.acquire().unwrap();
|
|
let mut lease = pool.acquire().unwrap();
|
|
|
|
|
|
db.kv
|
|
db.kv
|
|
@@ -103,7 +120,7 @@ fn check_simple_migration() {
|
|
|
|
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
run_migration::<Schemas>(&mut lease).expect("migration result");
|
|
|
|
|
|
- let db2 = schema2::Schema::default();
|
|
|
|
|
|
+ let db2 = Schema2::default();
|
|
let migrated = db2
|
|
let migrated = db2
|
|
.kv
|
|
.kv
|
|
.with(schema2::KVEntity::K, "42")
|
|
.with(schema2::KVEntity::K, "42")
|
|
@@ -124,3 +141,15 @@ fn check_simple_migration() {
|
|
assert_eq!(migrated.k, "s");
|
|
assert_eq!(migrated.k, "s");
|
|
assert_eq!(migrated.v, "v");
|
|
assert_eq!(migrated.v, "v");
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+#[test]
|
|
|
|
+fn check_incorrect_migration() {
|
|
|
|
+ let (pool, _db): (_, Schema1) = common::open_test_db!();
|
|
|
|
+ let mut lease = pool.acquire().unwrap();
|
|
|
|
+
|
|
|
|
+ let Err(microrm::Error::ConsistencyError(_msg)) =
|
|
|
|
+ run_migration::<(Schema1, Schema2b)>(&mut lease)
|
|
|
|
+ else {
|
|
|
|
+ panic!("incorrect migration succeeded");
|
|
|
|
+ };
|
|
|
|
+}
|