on main help sign in
reshape/tests/rename_table.rs
70 lines 1.9 kB history raw
1mod common;
2use common::Test;
4#[test]
5fn rename_table() {
6 let mut test = Test::new("Rename table");
8 test.first_migration(
9 r#"
10 name = "create_users_table"
12 [[actions]]
13 type = "create_table"
14 name = "users"
15 primary_key = ["id"]
17 [[actions.columns]]
18 name = "id"
19 type = "INTEGER"
20 "#,
21 );
23 test.second_migration(
24 r#"
25 name = "rename_users_table_to_customers"
27 [[actions]]
28 type = "rename_table"
29 table = "users"
30 new_name = "customers"
31 "#,
32 );
34 test.intermediate(|old_db, new_db| {
35 // Make sure inserts work using both the old and new name
36 old_db
37 .simple_query("INSERT INTO users(id) VALUES (1)")
38 .unwrap();
39 new_db
40 .simple_query("INSERT INTO customers(id) VALUES (2)")
41 .unwrap();
43 // Ensure the table can be queried using both the old and new name
44 let expected: Vec<i32> = vec![1, 2];
45 assert_eq!(
46 expected,
47 old_db
48 .query("SELECT id FROM users ORDER BY id", &[])
49 .unwrap()
50 .iter()
51 .map(|row| row.get::<_, i32>("id"))
52 .collect::<Vec<i32>>()
53 );
54 assert_eq!(
55 expected,
56 new_db
57 .query("SELECT id FROM customers ORDER BY id", &[])
58 .unwrap()
59 .iter()
60 .map(|row| row.get::<_, i32>("id"))
61 .collect::<Vec<i32>>()
62 );
64 // Ensure the table can't be queried using the wrong name for the schema
65 assert!(old_db.simple_query("SELECT id FROM customers").is_err());
66 assert!(new_db.simple_query("SELECT id FROM users").is_err());
67 });
69 test.run();