2021-11-18 01:39:03 +03:00
|
|
|
mod common;
|
2022-01-15 18:15:31 +03:00
|
|
|
use common::Test;
|
2021-11-18 01:39:03 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn rename_table() {
|
2022-01-15 18:15:31 +03:00
|
|
|
let mut test = Test::new("Rename table");
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.first_migration(
|
|
|
|
r#"
|
|
|
|
name = "create_users_table"
|
|
|
|
|
|
|
|
[[actions]]
|
|
|
|
type = "create_table"
|
|
|
|
name = "users"
|
|
|
|
primary_key = ["id"]
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions.columns]]
|
|
|
|
name = "id"
|
|
|
|
type = "INTEGER"
|
|
|
|
"#,
|
|
|
|
);
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.second_migration(
|
|
|
|
r#"
|
|
|
|
name = "rename_users_table_to_customers"
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions]]
|
|
|
|
type = "rename_table"
|
|
|
|
table = "users"
|
|
|
|
new_name = "customers"
|
|
|
|
"#,
|
|
|
|
);
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.intermediate(|old_db, new_db| {
|
|
|
|
// Make sure inserts work using both the old and new name
|
2021-11-18 01:39:03 +03:00
|
|
|
old_db
|
2022-01-15 18:15:31 +03:00
|
|
|
.simple_query("INSERT INTO users(id) VALUES (1)")
|
|
|
|
.unwrap();
|
2021-11-18 01:39:03 +03:00
|
|
|
new_db
|
2022-01-15 18:15:31 +03:00
|
|
|
.simple_query("INSERT INTO customers(id) VALUES (2)")
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
// Ensure the table can be queried using both the old and new name
|
|
|
|
let expected: Vec<i32> = vec![1, 2];
|
|
|
|
assert_eq!(
|
|
|
|
expected,
|
|
|
|
old_db
|
|
|
|
.query("SELECT id FROM users ORDER BY id", &[])
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|row| row.get::<_, i32>("id"))
|
|
|
|
.collect::<Vec<i32>>()
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
expected,
|
|
|
|
new_db
|
|
|
|
.query("SELECT id FROM customers ORDER BY id", &[])
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|row| row.get::<_, i32>("id"))
|
|
|
|
.collect::<Vec<i32>>()
|
|
|
|
);
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
// Ensure the table can't be queried using the wrong name for the schema
|
|
|
|
assert!(old_db.simple_query("SELECT id FROM customers").is_err());
|
|
|
|
assert!(new_db.simple_query("SELECT id FROM users").is_err());
|
|
|
|
});
|
2021-11-18 01:39:03 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.run();
|
2021-11-18 01:39:03 +03:00
|
|
|
}
|