-
Notifications
You must be signed in to change notification settings - Fork 245
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: correctly handle foreign keys when dropping mysql indexes (#5073)
* fix: correctly handle foreign keys when dropping mysql indexes * doc: clarify comments * doc: clarify comment * chore: add tests and clean up code
- Loading branch information
1 parent
f15691e
commit 57a00c8
Showing
8 changed files
with
269 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 20 additions & 8 deletions
28
schema-engine/connectors/sql-schema-connector/src/sql_schema_differ/index.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,27 @@ | ||
use sql_schema_describer::walkers::{IndexWalker, TableWalker}; | ||
use either::Either; | ||
use sql_schema_describer::{ | ||
walkers::{IndexWalker, TableWalker}, | ||
ForeignKeyWalker, IndexType, | ||
}; | ||
use std::iter; | ||
|
||
pub(super) fn index_covers_fk(table: TableWalker<'_>, index: IndexWalker<'_>) -> bool { | ||
// Only normal indexes can cover foreign keys. | ||
if index.index_type() != sql_schema_describer::IndexType::Normal { | ||
return false; | ||
pub(super) fn get_fks_covered_by_index<'a>( | ||
table: TableWalker<'a>, | ||
index: IndexWalker<'a>, | ||
) -> impl Iterator<Item = ForeignKeyWalker<'a>> { | ||
// Only normal, unique and primary key indexes can cover foreign keys. | ||
if !matches!( | ||
index.index_type(), | ||
IndexType::Normal | IndexType::Unique | IndexType::PrimaryKey | ||
) { | ||
return Either::Left(iter::empty()); | ||
} | ||
|
||
table.foreign_keys().any(|fk| { | ||
Either::Right(table.foreign_keys().filter(move |fk| { | ||
let fk_cols = fk.constrained_columns().map(|col| col.name()); | ||
let index_cols = index.column_names(); | ||
|
||
fk_cols.len() == index_cols.len() && fk_cols.zip(index_cols).all(|(a, b)| a == b) | ||
}) | ||
// It's sufficient that leftmost columns of the index match the FK columns. | ||
fk_cols.len() <= index_cols.len() && fk_cols.zip(index_cols).all(|(a, b)| a == b) | ||
})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters