on iko/quotes@origin help sign in
reshape/tests/remove_column.rs
131 lines 3.2 kB history raw
1mod common;
2use common::Test;
4#[test]
5fn remove_column() {
6 let mut test = Test::new("Remove column");
8 test.first_migration(
9 r#"
10 name = "create_user_table"
12 [[actions]]
13 type = "create_table"
14 name = "users"
15 primary_key = ["id"]
17 [[actions.columns]]
18 name = "id"
19 type = "INTEGER"
21 [[actions.columns]]
22 name = "name"
23 type = "TEXT"
24 "#,
25 );
27 test.second_migration(
28 r#"
29 name = "remove_name_column"
31 [[actions]]
32 type = "remove_column"
33 table = "users"
34 column = "name"
35 down = "'TEST_DOWN_VALUE'"
36 "#,
37 );
39 test.intermediate(|old_db, new_db| {
40 // Insert using old schema and ensure it can be retrieved through new schema
41 old_db
42 .simple_query("INSERT INTO users(id, name) VALUES (1, 'John Doe')")
43 .unwrap();
44 let results = new_db
45 .query("SELECT id FROM users WHERE id = 1", &[])
46 .unwrap();
47 assert_eq!(1, results.len());
48 assert_eq!(1, results[0].get::<_, i32>("id"));
50 // Ensure the name column is not accesible through the new schema
51 assert!(new_db.query("SELECT id, name FROM users", &[]).is_err());
53 // Insert using new schema and ensure the down function is correctly applied
54 new_db
55 .simple_query("INSERT INTO users(id) VALUES (2)")
56 .unwrap();
57 let result = old_db
58 .query_opt("SELECT name FROM users WHERE id = 2", &[])
59 .unwrap();
60 assert_eq!(
61 Some("TEST_DOWN_VALUE"),
62 result.as_ref().map(|row| row.get("name"))
63 );
64 });
66 test.run();
69#[test]
70fn remove_column_with_index() {
71 let mut test = Test::new("Remove column");
73 test.first_migration(
74 r#"
75 name = "create_user_table"
77 [[actions]]
78 type = "create_table"
79 name = "users"
80 primary_key = ["id"]
82 [[actions.columns]]
83 name = "id"
84 type = "INTEGER"
86 [[actions.columns]]
87 name = "name"
88 type = "TEXT"
90 [[actions]]
91 type = "add_index"
92 table = "users"
94 [actions.index]
95 name = "name_idx"
96 columns = ["name"]
97 "#,
98 );
100 test.second_migration(
101 r#"
102 name = "remove_name_column"
104 [[actions]]
105 type = "remove_column"
106 table = "users"
107 column = "name"
108 down = "'TEST_DOWN_VALUE'"
109 "#,
110 );
112 test.after_completion(|db| {
113 // Ensure index has been removed after the migration is complete
114 let count: i64 = db
115 .query(
116 "
117 SELECT COUNT(*)
118 FROM pg_catalog.pg_index
119 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
120 WHERE pg_class.relname = 'name_idx'
121 ",
122 &[],
123 )
124 .unwrap()
125 .first()
126 .map(|row| row.get(0))
127 .unwrap();
129 assert_eq!(0, count, "expected index to not exist");
130 });