Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resolving IndexCreateStatement not generating a valid query when no name is provided #638

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/backend/mysql/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 1,7 @@
use super::*;

const MYSQL_IDENTIFIER_NAME_MAX_LENGTH: usize = 64;

impl IndexBuilder for MysqlQueryBuilder {
fn prepare_table_index_expression(
&self,
Expand Down Expand Up @@ -37,16 39,16 @@ impl IndexBuilder for MysqlQueryBuilder {
self.prepare_index_prefix(create, sql);
write!(sql, "INDEX ").unwrap();

if let Some(name) = &create.index.name {
write!(
sql,
"{}{}{}",
self.quote().left(),
name,
self.quote().right()
)
.unwrap();
}
let mut name = create.get_name();
name.truncate(MYSQL_IDENTIFIER_NAME_MAX_LENGTH);
write!(
sql,
"{}{}{}",
self.quote().left(),
name,
self.quote().right()
)
.unwrap();

write!(sql, " ON ").unwrap();
if let Some(table) = &create.table {
Expand Down
14 changes: 14 additions & 0 deletions src/index/create.rs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 229,20 @@ impl IndexCreateStatement {
&self.index
}

pub(crate) fn get_name(&self) -> String {
if let Some(name) = self.index.name.clone() {
return name;
}
let prefix = if self.is_primary_key() {
"pri"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure it's a good solution to hard-code prefixes in the library... @billy1624 what do you think?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is ok in these cases because:
it was like that before at other locations you can already find these prefixes.
This is merely a fallback if nothing has been specified by the user.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

} else if self.is_unique_key() {
"uni"
} else {
"idx"
};
format!("{}-{}", prefix, self.index.get_column_names().join("-"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is similar to the preview comment...

}

pub fn take(&mut self) -> Self {
Self {
table: self.table.take(),
Expand Down
12 changes: 12 additions & 0 deletions tests/mysql/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 53,18 @@ fn create_4() {
);
}

#[test]
fn create_without_name() {
assert_eq!(
Index::create()
.index_type(IndexType::Hash)
.table(Glyph::Table)
.col(Glyph::Image)
.to_string(MysqlQueryBuilder),
"CREATE INDEX `idx-image` ON `glyph` (`image`) USING HASH"
);
}

#[test]
fn drop_1() {
assert_eq!(
Expand Down