Test for renaming + altering an index
The index should be recreated, not renamed.
#![allow(unused)]
mod test_harness;
use pretty_assertions::{assert_eq, assert_ne};
-use sql_migration_connector::{AlterIndex, SqlFamily, SqlMigrationStep};
+use sql_migration_connector::{AlterIndex, DropIndex, CreateIndex, SqlFamily, SqlMigrationStep};
use sql_schema_describer::*;
use test_harness::*;
@@ -1112,6 +1112,67 @@ fn index_renaming_must_work_when_renaming_to_custom() {
});
}
+#[test]
+fn index_updates_with_rename_must_work() {
+ test_each_connector(|sql_family, api| {
+ let dm1 = r#"
+ model A {
+ id Int @id
+ field String
+ secondField Int
+
+ @@unique([field, secondField], name: "customName")
+ }
+ "#;
+ let result = infer_and_apply(api, &dm1).sql_schema;
+ let index = result
+ .table_bang("A")
+ .indices
+ .iter()
+ .find(|i| i.name == "customName" && i.columns == &["field", "secondField"]);
+ assert!(index.is_some());
+ assert_eq!(index.unwrap().tpe, IndexType::Unique);
+
+ let dm2 = r#"
+ model A {
+ id Int @id
+ field String
+ secondField Int
+
+ @@unique([field, id], name: "customNameA")
+ }
+ "#;
+ let result = infer_and_apply(api, &dm2);
+ let indexes = result
+ .sql_schema
+ .table_bang("A")
+ .indices
+ .iter()
+ .filter(|i| i.columns == &["field", "id"] && i.name == "customNameA");
+ assert_eq!(indexes.count(), 1);
+
+ // Test that we are not dropping and recreating the index. Except in SQLite, because there we are.
+ if sql_family != SqlFamily::Sqlite {
+ let expected_steps = vec![
+ SqlMigrationStep::DropIndex(DropIndex {
+ table: "A".into(),
+ name: "customName".into(),
+ }),
+ SqlMigrationStep::CreateIndex(CreateIndex {
+ table: "A".into(),
+ index: Index {
+ name: "customNameA".into(),
+ columns: vec!["field".into(), "id".into()],
+ tpe: IndexType::Unique,
+ },
+ })
+ ];
+ let actual_steps = result.sql_migration();
+ assert_eq!(actual_steps, expected_steps);
+ }
+ });
+}
+
#[test]
fn adding_a_scalar_list_for_a_modelwith_id_type_int_must_work() {
test_each_connector(|sql_family, api| {
