Skip to content

Commit

Permalink
adds integration test to handle index expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
nickzelei committed Jan 27, 2025
1 parent 3d57e54 commit d372bdd
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
24 changes: 18 additions & 6 deletions backend/pkg/sqlmanager/mysql/mysql-manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,19 @@ func (m *MysqlManager) GetTableInitStatements(ctx context.Context, tables []*sql
if _, exists := indexmap[key.String()]; !exists {
indexmap[key.String()] = make(map[string][]string)
}
// Group columns by index name
indexmap[key.String()][record.IndexName] = append(
indexmap[key.String()][record.IndexName],
record.ColumnName,
)
// Group columns/expressions by index name
if record.ColumnName.Valid {
indexmap[key.String()][record.IndexName] = append(
indexmap[key.String()][record.IndexName],
record.ColumnName.String,
)
} else if record.Expression.Valid {
indexmap[key.String()][record.IndexName] = append(
indexmap[key.String()][record.IndexName],
// expressions must be wrapped in parentheses on creation, but don't come out of the DB in that format /shrug
fmt.Sprintf("(%s)", record.Expression.String),
)
}
}
return nil
})
Expand Down Expand Up @@ -793,7 +801,11 @@ func wrapIdempotentIndex(

columnInput := []string{}
for _, col := range cols {
columnInput = append(columnInput, EscapeMysqlColumn(col))
if strings.HasPrefix(col, "(") {
columnInput = append(columnInput, col)
} else {
columnInput = append(columnInput, EscapeMysqlColumn(col))
}
}
procedureName := fmt.Sprintf("NeosyncAddIndex_%s", hashInput(hashParams...))[:64]
stmt := fmt.Sprintf(`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ func Test_MysqlManager(t *testing.T) {
{Schema: schema, Table: "parent1"},
{Schema: schema, Table: "child1"},
{Schema: schema, Table: "order"},
{Schema: schema, Table: "test_mixed_index"},
},
)

Expand Down
12 changes: 12 additions & 0 deletions backend/pkg/sqlmanager/mysql/testdata/setup.sql
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,15 @@ CREATE TABLE `order` (

-- Creates an index that uses reserved MySQL words
CREATE INDEX `order_index_on_reserved_words` ON `order` (`select`, `from`, `where`);

-- Create a table with some columns
CREATE TABLE test_mixed_index (
id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
birth_date DATE
);

-- Create a composite index that uses both regular columns and expressions
CREATE INDEX idx_mixed ON test_mixed_index
(first_name, (UPPER(last_name)), birth_date, (YEAR(birth_date)));

0 comments on commit d372bdd

Please sign in to comment.