2022-01-12 03:12:08 +03:00
|
|
|
mod common;
|
2022-01-15 18:15:31 +03:00
|
|
|
use common::Test;
|
2022-01-12 03:12:08 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_index() {
|
2022-01-15 18:15:31 +03:00
|
|
|
let mut test = Test::new("Remove index");
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.first_migration(
|
|
|
|
r#"
|
|
|
|
name = "create_users_table"
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions]]
|
|
|
|
type = "create_table"
|
|
|
|
name = "users"
|
|
|
|
primary_key = ["id"]
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions.columns]]
|
|
|
|
name = "id"
|
|
|
|
type = "INTEGER"
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions.columns]]
|
|
|
|
name = "name"
|
|
|
|
type = "TEXT"
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions]]
|
|
|
|
type = "add_index"
|
|
|
|
table = "users"
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[actions.index]
|
|
|
|
name = "name_idx"
|
|
|
|
columns = ["name"]
|
|
|
|
"#,
|
|
|
|
);
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.second_migration(
|
|
|
|
r#"
|
|
|
|
name = "remove_name_index"
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
[[actions]]
|
|
|
|
type = "remove_index"
|
|
|
|
index = "name_idx"
|
|
|
|
"#,
|
|
|
|
);
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.intermediate(|db, _| {
|
|
|
|
// Ensure index is still valid and ready during the migration
|
|
|
|
let result: Vec<(bool, bool)> = db
|
|
|
|
.query(
|
|
|
|
"
|
|
|
|
SELECT pg_index.indisready, pg_index.indisvalid
|
|
|
|
FROM pg_catalog.pg_index
|
|
|
|
JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
|
|
|
|
WHERE pg_class.relname = 'name_idx'
|
|
|
|
",
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.iter()
|
|
|
|
.map(|row| (row.get("indisready"), row.get("indisvalid")))
|
|
|
|
.collect();
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
assert_eq!(vec![(true, true)], result);
|
|
|
|
});
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.after_completion(|db| {
|
|
|
|
// Ensure index has been removed after the migration is complete
|
|
|
|
let count: i64 = db
|
|
|
|
.query(
|
|
|
|
"
|
|
|
|
SELECT COUNT(*)
|
|
|
|
FROM pg_catalog.pg_index
|
|
|
|
JOIN pg_catalog.pg_class ON pg_index.indexrelid = pg_class.oid
|
|
|
|
WHERE pg_class.relname = 'name_idx'
|
|
|
|
",
|
|
|
|
&[],
|
|
|
|
)
|
|
|
|
.unwrap()
|
|
|
|
.first()
|
|
|
|
.map(|row| row.get(0))
|
|
|
|
.unwrap();
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
assert_eq!(0, count, "expected index to not exist");
|
|
|
|
});
|
2022-01-12 03:12:08 +03:00
|
|
|
|
2022-01-15 18:15:31 +03:00
|
|
|
test.run();
|
2022-01-12 03:12:08 +03:00
|
|
|
}
|