Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

feat(sqlschema): add support for partial unique indexes (#10604)

Pandey12b02a10022e
diff --git a/ee/sqlschema/postgressqlschema/provider.go b/ee/sqlschema/postgressqlschema/provider.go
index 83bab7ca55..c063983ef8 100644
--- a/ee/sqlschema/postgressqlschema/provider.go
+++ b/ee/sqlschema/postgressqlschema/provider.go
@@ -223,7 +223,8 @@ SELECT
i.indisunique AS unique,
i.indisprimary AS primary,
a.attname AS column_name,
- array_position(i.indkey, a.attnum) AS column_position
+ array_position(i.indkey, a.attnum) AS column_position,
+ pg_get_expr(i.indpred, i.indrelid) AS predicate
FROM
pg_index i
LEFT JOIN pg_class ct ON ct.oid = i.indrelid
@@ -246,7 +247,12 @@ ORDER BY index_name, column_position`, string(name))
}
}()
- uniqueIndicesMap := make(map[string]*sqlschema.UniqueIndex)
+ type indexEntry struct {
+ columns []sqlschema.ColumnName
+ predicate *string
+ }
+
+ uniqueIndicesMap := make(map[string]*indexEntry)
for rows.Next() {
var (
tableName string
@@ -256,30 +262,50 @@ ORDER BY index_name, column_position`, string(name))
columnName string
// starts from 0 and is unused in this function, this is to ensure that the column names are in the correct order
columnPosition int
+ predicate *string
)
- if err := rows.Scan(&tableName, &indexName, &unique, &primary, &columnName, &columnPosition); err != nil {
+ if err := rows.Scan(&tableName, &indexName, &unique, &primary, &columnName, &columnPosition, &predicate); err != nil {
return nil, err
}
if unique {
if _, ok := uniqueIndicesMap[indexName]; !ok {
- uniqueIndicesMap[indexName] = &sqlschema.UniqueIndex{
- TableName: name,
- ColumnNames: []sqlschema.ColumnName{sqlschema.ColumnName(columnName)},
+ uniqueIndicesMap[indexName] = &indexEntry{
+ columns: []sqlschema.ColumnName{sqlschema.ColumnName(columnName)},
+ predicate: predicate,
}
} else {
- uniqueIndicesMap[indexName].ColumnNames = append(uniqueIndicesMap[indexName].ColumnNames, sqlschema.ColumnName(columnName))
+ uniqueIndicesMap[indexName].columns = append(uniqueIndicesMap[indexName].columns, sqlschema.ColumnName(columnName))
}
}
}
indices := make([]sqlschema.Index, 0)
- for indexName, index := range uniqueIndicesMap {
- if index.Name() == indexName {
- indices = append(indices, index)
+ for indexName, entry := range uniqueIndicesMap {
+ if entry.predicate != nil {
+ index := &sqlschema.PartialUniqueIndex{
+ TableName: name,
+ ColumnNames: entry.columns,
+ Where: *entry.predicate,
+ }
+
+ if index.Name() == indexName {
+ indices = append(indices, index)
+ } else {
+ indices = append(indices, index.Named(indexName))
+ }
} else {
- indices = append(indices, index.Named(indexName))
+ index := &sqlschema.UniqueIndex{
+ TableName: name,
+ ColumnNames: entry.columns,
+ }
+
+ if index.Name() == indexName {
+ indices = append(indices, index)
+ } else {
+ indices = append(indices, index.Named(indexName))
+ }
}
}
diff --git a/pkg/sqlschema/index.go b/pkg/sqlschema/index.go
index 2c846c1724..4821e07ce7 100644
--- a/pkg/sqlschema/index.go
+++ b/pkg/sqlschema/index.go
@@ -8,8 +8,9 @@ import (
)
var (
- IndexTypeUnique = IndexType{s: valuer.NewString("uq")}
- IndexTypeIndex = IndexType{s: valuer.NewString("ix")}
+ IndexTypeUnique = IndexType{s: valuer.NewString("uq")}
+ IndexTypeIndex = IndexType{s: valuer.NewString("ix")}
+ IndexTypePartialUnique = IndexType{s: valuer.NewString("puq")}
)
type IndexType struct{ s valuer.String }
@@ -22,6 +23,7 @@ type Index interface {
// The name of the index.
// - Indexes are named as `ix_<table_name>_<column_names>`. The column names are separated by underscores.
// - Unique constraints are named as `uq_<table_name>_<column_names>`. The column names are separated by underscores.
+ // - Partial unique indexes are named as `puq_<table_name>_<column_names>_<predicate_hash>`.
// The name is autogenerated and should not be set by the user.
Name() string
@@ -133,3 +135,101 @@ func (index *UniqueIndex) ToDropSQL(fmter SQLFormatter) []byte {
return sql
}
+
+type PartialUniqueIndex struct {
+ TableName TableName
+ ColumnNames []ColumnName
+ Where string
+ name string
+}
+
+func (index *PartialUniqueIndex) Name() string {
+ if index.name != "" {
+ return index.name
+ }
+
+ var b strings.Builder
+ b.WriteString(IndexTypePartialUnique.String())
+ b.WriteString("_")
+ b.WriteString(string(index.TableName))
+ b.WriteString("_")
+ for i, column := range index.ColumnNames {
+ if i > 0 {
+ b.WriteString("_")
+ }
+ b.WriteString(string(column))
+ }
+ b.WriteString("_")
+ b.WriteString((&whereNormalizer{input: index.Where}).hash())
+ return b.String()
+}
+
+func (index *PartialUniqueIndex) Named(name string) Index {
+ copyOfColumnNames := make([]ColumnName, len(index.ColumnNames))
+ copy(copyOfColumnNames, index.ColumnNames)
+
+ return &PartialUniqueIndex{
+ TableName: index.TableName,
+ ColumnNames: copyOfColumnNames,
+ Where: index.Where,
+ name: name,
+ }
+}
+
+func (index *PartialUniqueIndex) IsNamed() bool {
+ return index.name != ""
+}
+
+func (*PartialUniqueIndex) Type() IndexType {
+ return IndexTypePartialUnique
+}
+
+func (index *PartialUniqueIndex) Columns() []ColumnName {
+ return index.ColumnNames
+}
+
+func (index *PartialUniqueIndex) Equals(other Index) bool {
+ if other.Type() != IndexTypePartialUnique {
+ return false
+ }
+
+ otherPartial, ok := other.(*PartialUniqueIndex)
+ if !ok {
+ return false
+ }
+
+ return index.Name() == other.Name() && slices.Equal(index.Columns(), other.Columns()) && (&whereNormalizer{input: index.Where}).normalize() == (&whereNormalizer{input: otherPartial.Where}).normalize()
+}
+
+func (index *PartialUniqueIndex) ToCreateSQL(fmter SQLFormatter) []byte {
+ sql := []byte{}
+
+ sql = append(sql, "CREATE UNIQUE INDEX IF NOT EXISTS "...)
+ sql = fmter.AppendIdent(sql, index.Name())
+ sql = append(sql, " ON "...)
+ sql = fmter.AppendIdent(sql, string(index.TableName))
+ sql = append(sql, " ("...)
+
+ for i, column := range index.ColumnNames {
+ if i > 0 {
+ sql = append(sql, ", "...)
+ }
+
+ sql = fmter.AppendIdent(sql, string(column))
+ }
+
+ sql = append(sql, ") WHERE "...)
+ sql = append(sql, index.Where...)
+
+ return sql
+}
+
+func (index *PartialUniqueIndex) ToDropSQL(fmter SQLFormatter) []byte {
+ sql := []byte{}
+
+ sql = append(sql, "DROP INDEX IF EXISTS "...)
+ sql = fmter.AppendIdent(sql, index.Name())
+
+ return sql
+}
+
diff --git a/pkg/sqlschema/index_test.go b/pkg/sqlschema/index_test.go
index ae554cb888..e6d1f870a1 100644
--- a/pkg/sqlschema/index_test.go
+++ b/pkg/sqlschema/index_test.go
@@ -38,6 +38,110 @@ func TestIndexToCreateSQL(t *testing.T) {
},
sql: `CREATE UNIQUE INDEX IF NOT EXISTS "my_index" ON "users" ("id", "name", "email")`,
},
+ {
+ name: "PartialUnique_1Column",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_94610c77" ON "users" ("email") WHERE "deleted_at" IS NULL`,
+ },
+ {
+ name: "PartialUnique_2Columns",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"org_id", "email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_org_id_email_94610c77" ON "users" ("org_id", "email") WHERE "deleted_at" IS NULL`,
+ },
+ {
+ name: "PartialUnique_Named",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ name: "my_partial_index",
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "my_partial_index" ON "users" ("email") WHERE "deleted_at" IS NULL`,
+ },
+ {
+ name: "PartialUnique_WhereWithParentheses",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `("deleted_at" IS NULL)`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_94610c77" ON "users" ("email") WHERE ("deleted_at" IS NULL)`,
+ },
+ {
+ name: "PartialUnique_WhereWithQuotedIdentifier",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"order" IS NULL`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_14c5f5f2" ON "users" ("email") WHERE "order" IS NULL`,
+ },
+ {
+ name: "PartialUnique_WhereWithQuotedLiteral",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `status = 'somewhere'`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_9817c709" ON "users" ("email") WHERE status = 'somewhere'`,
+ },
+ {
+ name: "PartialUnique_WhereWith2Columns",
+ index: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email", "status"},
+ Where: `email = '[email protected]' AND status = 'active'`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_status_e70e78c3" ON "users" ("email", "status") WHERE email = '[email protected]' AND status = 'active'`,
+ },
+ // postgres docs example
+ {
+ name: "PartialUnique_WhereWithPostgresDocsExample_1",
+ index: &PartialUniqueIndex{
+ TableName: "access_log",
+ ColumnNames: []ColumnName{"client_ip"},
+ Where: `NOT (client_ip > inet '192.168.100.0' AND client_ip < inet '192.168.100.255')`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_access_log_client_ip_5a596410" ON "access_log" ("client_ip") WHERE NOT (client_ip > inet '192.168.100.0' AND client_ip < inet '192.168.100.255')`,
+ },
+ // postgres docs example
+ {
+ name: "PartialUnique_WhereWithPostgresDocsExample_2",
+ index: &PartialUniqueIndex{
+ TableName: "orders",
+ ColumnNames: []ColumnName{"order_nr"},
+ Where: `billed is not true`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_orders_order_nr_6d31bb0e" ON "orders" ("order_nr") WHERE billed is not true`,
+ },
+ // sqlite docs example
+ {
+ name: "PartialUnique_WhereWithSqliteDocsExample_1",
+ index: &PartialUniqueIndex{
+ TableName: "person",
+ ColumnNames: []ColumnName{"team_id"},
+ Where: `is_team_leader`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_person_team_id_c8604a29" ON "person" ("team_id") WHERE is_team_leader`,
+ },
+ // sqlite docs example
+ {
+ name: "PartialUnique_WhereWithSqliteDocsExample_2",
+ index: &PartialUniqueIndex{
+ TableName: "purchaseorder",
+ ColumnNames: []ColumnName{"parent_po"},
+ Where: `parent_po IS NOT NULL`,
+ },
+ sql: `CREATE UNIQUE INDEX IF NOT EXISTS "puq_purchaseorder_parent_po_dbe2929d" ON "purchaseorder" ("parent_po") WHERE parent_po IS NOT NULL`,
+ },
}
for _, testCase := range testCases {
@@ -49,3 +153,109 @@ func TestIndexToCreateSQL(t *testing.T) {
})
}
}
+
+func TestIndexEquals(t *testing.T) {
+ testCases := []struct {
+ name string
+ a Index
+ b Index
+ equals bool
+ }{
+ {
+ name: "PartialUnique_Same",
+ a: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ b: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ equals: true,
+ },
+ {
+ name: "PartialUnique_NormalizedPostgresWhere",
+ a: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ b: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `(deleted_at IS NULL)`,
+ },
+ equals: true,
+ },
+ {
+ name: "PartialUnique_DifferentWhere",
+ a: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ b: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"active" = true`,
+ },
+ equals: false,
+ },
+ {
+ name: "PartialUnique_NotEqual_Unique",
+ a: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ b: &UniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ },
+ equals: false,
+ },
+ {
+ name: "Unique_NotEqual_PartialUnique",
+ a: &UniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ },
+ b: &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ equals: false,
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.equals, testCase.a.Equals(testCase.b))
+ })
+ }
+}
+
+func TestPartialUniqueIndexName(t *testing.T) {
+ a := &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ }
+ b := &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `(deleted_at IS NULL)`,
+ }
+ c := &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"active" = true`,
+ }
+
+ assert.Equal(t, "puq_users_email_94610c77", a.Name())
+ assert.Equal(t, a.Name(), b.Name())
+ assert.NotEqual(t, a.Name(), c.Name())
+}
diff --git a/pkg/sqlschema/normalizer.go b/pkg/sqlschema/normalizer.go
new file mode 100644
index 0000000000..e52ed23b21
--- /dev/null
+++ b/pkg/sqlschema/normalizer.go
@@ -0,0 +1,162 @@
+package sqlschema
+
+import (
+ "fmt"
+ "hash/fnv"
+ "strings"
+)
+
+type whereNormalizer struct {
+ input string
+}
+
+func (n *whereNormalizer) hash() string {
+ hasher := fnv.New32a()
+ _, _ = hasher.Write([]byte(n.normalize()))
+ return fmt.Sprintf("%08x", hasher.Sum32())
+}
+
+func (n *whereNormalizer) normalize() string {
+ where := strings.TrimSpace(n.input)
+ where = n.stripOuterParentheses(where)
+
+ var output strings.Builder
+ output.Grow(len(where))
+
+ for i := 0; i < len(where); i++ {
+ switch where[i] {
+ case ' ', '\t', '\n', '\r':
+ if output.Len() > 0 {
+ last := output.String()[output.Len()-1]
+ if last != ' ' {
+ output.WriteByte(' ')
+ }
+ }
+ case '\'':
+ end := n.consumeSingleQuotedLiteral(where, i, &output)
+ i = end
+ case '"':
+ token, end := n.consumeDoubleQuotedToken(where, i)
+ output.WriteString(token)
+ i = end
+ default:
+ output.WriteByte(where[i])
+ }
+ }
+
+ return strings.TrimSpace(output.String())
+}
+
+func (n *whereNormalizer) stripOuterParentheses(s string) string {
+ for {
+ s = strings.TrimSpace(s)
+ if len(s) < 2 || s[0] != '(' || s[len(s)-1] != ')' || !n.hasWrappingParentheses(s) {
+ return s
+ }
+ s = s[1 : len(s)-1]
+ }
+}
+
+func (n *whereNormalizer) hasWrappingParentheses(s string) bool {
+ depth := 0
+ inSingleQuotedLiteral := false
+ inDoubleQuotedToken := false
+
+ for i := 0; i < len(s); i++ {
+ switch s[i] {
+ case '\'':
+ if inDoubleQuotedToken {
+ continue
+ }
+ if inSingleQuotedLiteral && i+1 < len(s) && s[i+1] == '\'' {
+ i++
+ continue
+ }
+ inSingleQuotedLiteral = !inSingleQuotedLiteral
+ case '"':
+ if inSingleQuotedLiteral {
+ continue
+ }
+ if inDoubleQuotedToken && i+1 < len(s) && s[i+1] == '"' {
+ i++
+ continue
+ }
+ inDoubleQuotedToken = !inDoubleQuotedToken
+ case '(':
+ if inSingleQuotedLiteral || inDoubleQuotedToken {
+ continue
+ }
+ depth++
+ case ')':
+ if inSingleQuotedLiteral || inDoubleQuotedToken {
+ continue
+ }
+ depth--
+ if depth == 0 && i != len(s)-1 {
+ return false
+ }
+ }
+ }
+
+ return depth == 0
+}
+
+func (n *whereNormalizer) consumeSingleQuotedLiteral(s string, start int, output *strings.Builder) int {
+ output.WriteByte(s[start])
+ for i := start + 1; i < len(s); i++ {
+ output.WriteByte(s[i])
+ if s[i] == '\'' {
+ if i+1 < len(s) && s[i+1] == '\'' {
+ i++
+ output.WriteByte(s[i])
+ continue
+ }
+ return i
+ }
+ }
+
+ return len(s) - 1
+}
+
+func (n *whereNormalizer) consumeDoubleQuotedToken(s string, start int) (string, int) {
+ var ident strings.Builder
+
+ for i := start + 1; i < len(s); i++ {
+ if s[i] == '"' {
+ if i+1 < len(s) && s[i+1] == '"' {
+ ident.WriteByte('"')
+ i++
+ continue
+ }
+
+ if n.isSimpleUnquotedIdentifier(ident.String()) {
+ return ident.String(), i
+ }
+
+ return s[start : i+1], i
+ }
+
+ ident.WriteByte(s[i])
+ }
+
+ return s[start:], len(s) - 1
+}
+
+func (n *whereNormalizer) isSimpleUnquotedIdentifier(s string) bool {
+ if s == "" || strings.ToLower(s) != s {
+ return false
+ }
+
+ for i := 0; i < len(s); i++ {
+ ch := s[i]
+ if (ch >= 'a' && ch <= 'z') || ch == '_' {
+ continue
+ }
+ if i > 0 && ch >= '0' && ch <= '9' {
+ continue
+ }
+ return false
+ }
+
+ return true
+}
diff --git a/pkg/sqlschema/normalizer_test.go b/pkg/sqlschema/normalizer_test.go
new file mode 100644
index 0000000000..d35cb7fa4f
--- /dev/null
+++ b/pkg/sqlschema/normalizer_test.go
@@ -0,0 +1,57 @@
+package sqlschema
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestWhereNormalizerNormalize(t *testing.T) {
+ testCases := []struct {
+ name string
+ input string
+ output string
+ }{
+ {
+ name: "BooleanComparison",
+ input: `"active" = true`,
+ output: `active = true`,
+ },
+ {
+ name: "QuotedStringLiteralPreserved",
+ input: `status = 'somewhere'`,
+ output: `status = 'somewhere'`,
+ },
+ {
+ name: "EscapedStringLiteralPreserved",
+ input: `status = 'it''s active'`,
+ output: `status = 'it''s active'`,
+ },
+ {
+ name: "OuterParenthesesRemoved",
+ input: `(("deleted_at" IS NULL))`,
+ output: `deleted_at IS NULL`,
+ },
+ {
+ name: "InnerParenthesesPreserved",
+ input: `("deleted_at" IS NULL OR ("active" = true AND "status" = 'open'))`,
+ output: `deleted_at IS NULL OR (active = true AND status = 'open')`,
+ },
+ {
+ name: "MultipleClausesWhitespaceCollapsed",
+ input: " ( \"deleted_at\" IS NULL \n AND\t\"active\" = true AND status = 'open' ) ",
+ output: `deleted_at IS NULL AND active = true AND status = 'open'`,
+ },
+ {
+ name: "ComplexBooleanClauses",
+ input: `NOT ("deleted_at" IS NOT NULL AND ("active" = false OR "status" = 'archived'))`,
+ output: `NOT (deleted_at IS NOT NULL AND (active = false OR status = 'archived'))`,
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.output, (&whereNormalizer{input: testCase.input}).normalize())
+ })
+ }
+}
diff --git a/pkg/sqlschema/operator_test.go b/pkg/sqlschema/operator_test.go
index f67b27226b..b051bbd318 100644
--- a/pkg/sqlschema/operator_test.go
+++ b/pkg/sqlschema/operator_test.go
@@ -1146,3 +1146,100 @@ func TestOperatorAlterTable(t *testing.T) {
})
}
}
+
+func TestOperatorDiffIndices(t *testing.T) {
+ testCases := []struct {
+ name string
+ oldIndices []Index
+ newIndices []Index
+ expectedSQLs [][]byte
+ }{
+ {
+ name: "UniqueToPartialUnique_DropAndCreate",
+ oldIndices: []Index{
+ &UniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ },
+ },
+ newIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ },
+ expectedSQLs: [][]byte{
+ []byte(`DROP INDEX IF EXISTS "uq_users_email"`),
+ []byte(`CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_94610c77" ON "users" ("email") WHERE "deleted_at" IS NULL`),
+ },
+ },
+ {
+ name: "PartialUnique_SameWhere_NoOp",
+ oldIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ },
+ newIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ },
+ expectedSQLs: [][]byte{},
+ },
+ {
+ name: "PartialUnique_NormalizedWhere_NoOp",
+ oldIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `(deleted_at IS NULL)`,
+ },
+ },
+ newIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ },
+ expectedSQLs: [][]byte{},
+ },
+ {
+ name: "PartialUnique_DifferentWhere_DropAndCreate",
+ oldIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"deleted_at" IS NULL`,
+ },
+ },
+ newIndices: []Index{
+ &PartialUniqueIndex{
+ TableName: "users",
+ ColumnNames: []ColumnName{"email"},
+ Where: `"active" = true`,
+ },
+ },
+ expectedSQLs: [][]byte{
+ []byte(`DROP INDEX IF EXISTS "puq_users_email_94610c77"`),
+ []byte(`CREATE UNIQUE INDEX IF NOT EXISTS "puq_users_email_202121f8" ON "users" ("email") WHERE "active" = true`),
+ },
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ fmter := NewFormatter(schema.NewNopFormatter().Dialect())
+ operator := NewOperator(fmter, OperatorSupport{})
+
+ actuals := operator.DiffIndices(testCase.oldIndices, testCase.newIndices)
+ assert.Equal(t, testCase.expectedSQLs, actuals)
+ })
+ }
+}
diff --git a/pkg/sqlschema/sqlitesqlschema/provider.go b/pkg/sqlschema/sqlitesqlschema/provider.go
index 0ef3510f01..1b2b01f265 100644
--- a/pkg/sqlschema/sqlitesqlschema/provider.go
+++ b/pkg/sqlschema/sqlitesqlschema/provider.go
@@ -3,6 +3,7 @@ package sqlitesqlschema
import (
"context"
"strconv"
+ "strings"
"github.com/SigNoz/signoz/pkg/errors"
"github.com/SigNoz/signoz/pkg/factory"
@@ -114,7 +115,29 @@ func (provider *provider) GetIndices(ctx context.Context, tableName sqlschema.Ta
return nil, err
}
- if unique {
+ if unique && partial {
+ var indexSQL string
+ if err := provider.
+ sqlstore.
+ BunDB().
+ NewRaw("SELECT sql FROM sqlite_master WHERE type = 'index' AND name = ?", name).
+ Scan(ctx, &indexSQL); err != nil {
+ return nil, err
+ }
+
+ where := extractWhereClause(indexSQL)
+ index := &sqlschema.PartialUniqueIndex{
+ TableName: tableName,
+ ColumnNames: columns,
+ Where: where,
+ }
+
+ if index.Name() == name {
+ indices = append(indices, index)
+ } else {
+ indices = append(indices, index.Named(name))
+ }
+ } else if unique {
index := &sqlschema.UniqueIndex{
TableName: tableName,
ColumnNames: columns,
@@ -148,3 +171,73 @@ func (provider *provider) ToggleFKEnforcement(ctx context.Context, db bun.IDB, o
return errors.NewInternalf(errors.CodeInternal, "foreign_keys(actual: %s, expected: %s), maybe a transaction is in progress?", strconv.FormatBool(val), strconv.FormatBool(on))
}
+
+func extractWhereClause(sql string) string {
+ lastWhere := -1
+ inSingleQuotedLiteral := false
+ inDoubleQuotedIdentifier := false
+ inBacktickQuotedIdentifier := false
+ inBracketQuotedIdentifier := false
+
+ for i := 0; i < len(sql); i++ {
+ switch sql[i] {
+ case '\'':
+ if inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+ if inSingleQuotedLiteral && i+1 < len(sql) && sql[i+1] == '\'' {
+ i++
+ continue
+ }
+ inSingleQuotedLiteral = !inSingleQuotedLiteral
+ case '"':
+ if inSingleQuotedLiteral || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+ if inDoubleQuotedIdentifier && i+1 < len(sql) && sql[i+1] == '"' {
+ i++
+ continue
+ }
+ inDoubleQuotedIdentifier = !inDoubleQuotedIdentifier
+ case '`':
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+ inBacktickQuotedIdentifier = !inBacktickQuotedIdentifier
+ case '[':
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+ inBracketQuotedIdentifier = true
+ case ']':
+ if inBracketQuotedIdentifier {
+ inBracketQuotedIdentifier = false
+ }
+ }
+
+ if inSingleQuotedLiteral || inDoubleQuotedIdentifier || inBacktickQuotedIdentifier || inBracketQuotedIdentifier {
+ continue
+ }
+
+ if strings.EqualFold(sql[i:min(i+5, len(sql))], "WHERE") &&
+ (i == 0 || !isSQLiteIdentifierChar(sql[i-1])) &&
+ (i+5 == len(sql) || !isSQLiteIdentifierChar(sql[i+5])) {
+ lastWhere = i
+ i += 4
+ }
+ }
+
+ if lastWhere == -1 {
+ return ""
+ }
+
+ return strings.TrimSpace(sql[lastWhere+len("WHERE"):])
+}
+
+func isSQLiteIdentifierChar(ch byte) bool {
+ return (ch >= 'a' && ch <= 'z') ||
+ (ch >= 'A' && ch <= 'Z') ||
+ (ch >= '0' && ch <= '9') ||
+ ch == '_'
+}
+
diff --git a/pkg/sqlschema/sqlitesqlschema/provider_test.go b/pkg/sqlschema/sqlitesqlschema/provider_test.go
new file mode 100644
index 0000000000..d95df855a5
--- /dev/null
+++ b/pkg/sqlschema/sqlitesqlschema/provider_test.go
@@ -0,0 +1,52 @@
+package sqlitesqlschema
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/assert"
+)
+
+func TestExtractWhereClause(t *testing.T) {
+ testCases := []struct {
+ name string
+ sql string
+ where string
+ }{
+ {
+ name: "UppercaseWhere",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" ("email") WHERE "deleted_at" IS NULL`,
+ where: `"deleted_at" IS NULL`,
+ },
+ {
+ name: "LowercaseWhere",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" ("email") where "deleted_at" IS NULL`,
+ where: `"deleted_at" IS NULL`,
+ },
+ {
+ name: "NewlineBeforeWhere",
+ sql: "CREATE UNIQUE INDEX \"idx\" ON \"users\" (\"email\")\nWHERE \"deleted_at\" IS NULL",
+ where: `"deleted_at" IS NULL`,
+ },
+ {
+ name: "ExtraWhitespace",
+ sql: "CREATE UNIQUE INDEX \"idx\" ON \"users\" (\"email\") \n \t where \"deleted_at\" IS NULL ",
+ where: `"deleted_at" IS NULL`,
+ },
+ {
+ name: "WhereInStringLiteral",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" ("email") WHERE status = 'somewhere'`,
+ where: `status = 'somewhere'`,
+ },
+ {
+ name: "BooleanLiteral",
+ sql: `CREATE UNIQUE INDEX "idx" ON "users" ("email") WHERE active = true`,
+ where: `active = true`,
+ },
+ }
+
+ for _, testCase := range testCases {
+ t.Run(testCase.name, func(t *testing.T) {
+ assert.Equal(t, testCase.where, extractWhereClause(testCase.sql))
+ })
+ }
+}