on iko/quotes@origin help sign in
reshape/tests/remove_index.rs
86 lines 2.1 kB history raw
1mod common;
2use common::Test;
4#[test]
5fn remove_index() {
6 let mut test = Test::new("Remove index");
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"
21 [[actions.columns]]
22 name = "name"
23 type = "TEXT"
25 [[actions]]
26 type = "add_index"
27 table = "users"
29 [actions.index]
30 name = "name_idx"
31 columns = ["name"]
32 "#,
33 );
35 test.second_migration(
36 r#"
37 name = "remove_name_index"
39 [[actions]]
40 type = "remove_index"
41 index = "name_idx"
42 "#,
43 );
45 test.intermediate(|db, _| {
46 // Ensure index is still valid and ready during the migration
47 let result: Vec<(bool, bool)> = db
48 .query(
49 "
50 SELECT pg_index.indisready, pg_index.indisvalid
51 FROM pg_catalog.pg_index
52 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
53 WHERE pg_class.relname = 'name_idx'
54 ",
55 &[],
56 )
57 .unwrap()
58 .iter()
59 .map(|row| (row.get("indisready"), row.get("indisvalid")))
60 .collect();
62 assert_eq!(vec![(true, true)], result);
63 });
65 test.after_completion(|db| {
66 // Ensure index has been removed after the migration is complete
67 let count: i64 = db
68 .query(
69 "
70 SELECT COUNT(*)
71 FROM pg_catalog.pg_index
72 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
73 WHERE pg_class.relname = 'name_idx'
74 ",
75 &[],
76 )
77 .unwrap()
78 .first()
79 .map(|row| row.get(0))
80 .unwrap();
82 assert_eq!(0, count, "expected index to not exist");
83 });
85 test.run();