on main help sign in
reshape/tests/add_index.rs
223 lines 5.7 kB history raw
1mod common;
2use common::Test;
4#[test]
5fn add_index() {
6 let mut test = Test::new("Add 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"
24 "#,
25 );
27 test.second_migration(
28 r#"
29 name = "add_users_name_index"
31 [[actions]]
32 type = "add_index"
33 table = "users"
35 [actions.index]
36 name = "name_idx"
37 columns = ["name"]
38 "#,
39 );
41 test.intermediate(|db, _| {
42 // Ensure index is valid and ready
43 let (is_ready, is_valid): (bool, bool) = db
44 .query(
45 "
46 SELECT pg_index.indisready, pg_index.indisvalid
47 FROM pg_catalog.pg_index
48 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
49 WHERE pg_class.relname = 'name_idx'
50 ",
51 &[],
52 )
53 .unwrap()
54 .first()
55 .map(|row| (row.get("indisready"), row.get("indisvalid")))
56 .unwrap();
58 assert!(is_ready, "expected index to be ready");
59 assert!(is_valid, "expected index to be valid");
60 });
62 test.after_completion(|db| {
63 // Ensure index is valid and ready
64 let (is_ready, is_valid): (bool, bool) = db
65 .query(
66 "
67 SELECT pg_index.indisready, pg_index.indisvalid
68 FROM pg_catalog.pg_index
69 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
70 WHERE pg_class.relname = 'name_idx'
71 ",
72 &[],
73 )
74 .unwrap()
75 .first()
76 .map(|row| (row.get("indisready"), row.get("indisvalid")))
77 .unwrap();
79 assert!(is_ready, "expected index to be ready");
80 assert!(is_valid, "expected index to be valid");
81 });
83 test.run();
86#[test]
87fn add_index_unique() {
88 let mut test = Test::new("Add unique index");
90 test.first_migration(
91 r#"
92 name = "create_users_table"
94 [[actions]]
95 type = "create_table"
96 name = "users"
97 primary_key = ["id"]
99 [[actions.columns]]
100 name = "id"
101 type = "INTEGER"
103 [[actions.columns]]
104 name = "name"
105 type = "TEXT"
106 "#,
107 );
109 test.second_migration(
110 r#"
111 name = "add_name_index"
113 [[actions]]
114 type = "add_index"
115 table = "users"
117 [actions.index]
118 name = "name_idx"
119 columns = ["name"]
120 unique = true
121 "#,
122 );
124 test.intermediate(|db, _| {
125 // Ensure index is valid, ready and unique
126 let (is_ready, is_valid, is_unique): (bool, bool, bool) = db
127 .query(
128 "
129 SELECT pg_index.indisready, pg_index.indisvalid, pg_index.indisunique
130 FROM pg_catalog.pg_index
131 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
132 WHERE pg_class.relname = 'name_idx'
133 ",
134 &[],
135 )
136 .unwrap()
137 .first()
138 .map(|row| {
139 (
140 row.get("indisready"),
141 row.get("indisvalid"),
142 row.get("indisunique"),
143 )
144 })
145 .unwrap();
147 assert!(is_ready, "expected index to be ready");
148 assert!(is_valid, "expected index to be valid");
149 assert!(is_unique, "expected index to be unique");
150 });
152 test.run();
155#[test]
156fn add_index_with_type() {
157 let mut test = Test::new("Add GIN index");
159 test.first_migration(
160 r#"
161 name = "create_users_table"
163 [[actions]]
164 type = "create_table"
165 name = "users"
166 primary_key = ["id"]
168 [[actions.columns]]
169 name = "id"
170 type = "INTEGER"
172 [[actions.columns]]
173 name = "data"
174 type = "JSONB"
175 "#,
176 );
178 test.second_migration(
179 r#"
180 name = "add_data_index"
182 [[actions]]
183 type = "add_index"
184 table = "users"
186 [actions.index]
187 name = "data_idx"
188 columns = ["data"]
189 type = "gin"
190 "#,
191 );
193 test.intermediate(|db, _| {
194 // Ensure index is valid, ready and has the right type
195 let (is_ready, is_valid, index_type): (bool, bool, String) = db
196 .query(
197 "
198 SELECT pg_index.indisready, pg_index.indisvalid, pg_am.amname
199 FROM pg_catalog.pg_index
200 JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
201 JOIN pg_catalog.pg_am ON pg_class.relam = pg_am.oid
202 WHERE pg_class.relname = 'data_idx'
203 ",
204 &[],
205 )
206 .unwrap()
207 .first()
208 .map(|row| {
209 (
210 row.get("indisready"),
211 row.get("indisvalid"),
212 row.get("amname"),
213 )
214 })
215 .unwrap();
217 assert!(is_ready, "expected index to be ready");
218 assert!(is_valid, "expected index to be valid");
219 assert_eq!("gin", index_type, "expected index type to be GIN");
220 });
222 test.run();