Skip to content

Commit f74f376

Browse files
committed
Fix naming issue
1 parent 3d6f508 commit f74f376

5 files changed

+19
-13
lines changed

crates/vespertide-query/src/sql/add_column.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ pub fn build_add_column(
121121
let mut index_queries = Vec::new();
122122
for constraint in &table_def.constraints {
123123
if let vespertide_core::TableConstraint::Index { name, columns } = constraint {
124-
let index_name = name
125-
.clone()
126-
.unwrap_or_else(|| format!("ix_{}_{}", table, columns.join("_")));
124+
let index_name = vespertide_naming::build_index_name(
125+
table,
126+
columns,
127+
name.as_deref(),
128+
);
127129
let mut idx_stmt = sea_query::Index::create();
128130
idx_stmt = idx_stmt.name(&index_name).to_owned();
129131
for col_name in columns {

crates/vespertide-query/src/sql/modify_column_type.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,11 @@ pub fn build_modify_column_type(
8383
let mut index_queries = Vec::new();
8484
for constraint in &table_def.constraints {
8585
if let vespertide_core::TableConstraint::Index { name, columns } = constraint {
86-
let index_name = name
87-
.clone()
88-
.unwrap_or_else(|| format!("ix_{}_{}", table, columns.join("_")));
86+
let index_name = vespertide_naming::build_index_name(
87+
table,
88+
columns,
89+
name.as_deref(),
90+
);
8991
let mut idx_stmt = sea_query::Index::create();
9092
idx_stmt = idx_stmt.name(&index_name).to_owned();
9193
for col_name in columns {

crates/vespertide-query/src/sql/remove_constraint.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,11 @@ pub fn build_remove_constraint(
7777
columns: idx_cols,
7878
} = constraint
7979
{
80-
let index_name = idx_name
81-
.clone()
82-
.unwrap_or_else(|| format!("ix_{}_{}", table, idx_cols.join("_")));
80+
let index_name = vespertide_naming::build_index_name(
81+
table,
82+
idx_cols,
83+
idx_name.as_deref(),
84+
);
8385
let mut idx_stmt = sea_query::Index::create();
8486
idx_stmt = idx_stmt.name(&index_name).to_owned();
8587
for col_name in idx_cols {
@@ -717,7 +719,7 @@ mod tests {
717719

718720
if matches!(backend, DatabaseBackend::Sqlite) {
719721
assert!(sql.contains("CREATE INDEX"));
720-
assert!(sql.contains("idx_id"));
722+
assert!(sql.contains("ix_users__idx_id"));
721723
}
722724

723725
with_settings!({ snapshot_suffix => format!("remove_primary_key_with_index_{:?}", backend) }, {
@@ -905,7 +907,7 @@ mod tests {
905907

906908
if matches!(backend, DatabaseBackend::Sqlite) {
907909
assert!(sql.contains("CREATE INDEX"));
908-
assert!(sql.contains("idx_id"));
910+
assert!(sql.contains("ix_users__idx_id"));
909911
}
910912

911913
with_settings!({ snapshot_suffix => format!("remove_unique_with_index_{:?}", backend) }, {

crates/vespertide-query/src/sql/snapshots/vespertide_query__sql__modify_column_type__tests__modify_column_type_with_index@modify_column_type_with_index_modify_column_type_with_index_sqlite.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ CREATE TABLE "users_temp" ( "id" integer NOT NULL, "age" bigint );
66
INSERT INTO "users_temp" ("id", "age") SELECT "id", "age" FROM "users";
77
DROP TABLE "users";
88
ALTER TABLE "users_temp" RENAME TO "users";
9-
CREATE INDEX "idx_age" ON "users" ("age")
9+
CREATE INDEX "ix_users__idx_age" ON "users" ("age")

crates/vespertide-query/src/sql/snapshots/vespertide_query__sql__remove_constraint__tests__remove_constraint_primary_key_with_index@remove_primary_key_with_index_Sqlite.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ CREATE TABLE "users_temp" ( "id" integer NOT NULL )
66
INSERT INTO "users_temp" ("id") SELECT "id" FROM "users"
77
DROP TABLE "users"
88
ALTER TABLE "users_temp" RENAME TO "users"
9-
CREATE INDEX "idx_id" ON "users" ("id")
9+
CREATE INDEX "ix_users__idx_id" ON "users" ("id")

0 commit comments

Comments
 (0)