[#6337] added support for case-insensitive password auth

This commit is contained in:
Gani Georgiev
2025-01-26 12:24:16 +02:00
parent c101798516
commit 33340a6977
13 changed files with 412 additions and 39 deletions
+20 -10
View File
@@ -21,13 +21,13 @@ type IndexColumn struct {
// Index represents a single parsed SQL CREATE INDEX expression.
type Index struct {
Unique bool `json:"unique"`
Optional bool `json:"optional"`
SchemaName string `json:"schemaName"`
IndexName string `json:"indexName"`
TableName string `json:"tableName"`
Columns []IndexColumn `json:"columns"`
Where string `json:"where"`
Columns []IndexColumn `json:"columns"`
Unique bool `json:"unique"`
Optional bool `json:"optional"`
}
// IsValid checks if the current Index contains the minimum required fields to be considered valid.
@@ -193,15 +193,25 @@ func ParseIndex(createIndexExpr string) Index {
return result
}
// HasColumnUniqueIndex loosely checks whether the specified column has
// a single column unique index (WHERE statements are ignored).
func HasSingleColumnUniqueIndex(column string, indexes []string) bool {
// FindSingleColumnUniqueIndex returns the first matching single column unique index.
func FindSingleColumnUniqueIndex(indexes []string, column string) (Index, bool) {
var index Index
for _, idx := range indexes {
parsed := ParseIndex(idx)
if parsed.Unique && len(parsed.Columns) == 1 && strings.EqualFold(parsed.Columns[0].Name, column) {
return true
index := ParseIndex(idx)
if index.Unique && len(index.Columns) == 1 && strings.EqualFold(index.Columns[0].Name, column) {
return index, true
}
}
return false
return index, false
}
// Deprecated: Use `_, ok := FindSingleColumnUniqueIndex(indexes, column)` instead.
//
// HasColumnUniqueIndex loosely checks whether the specified column has
// a single column unique index (WHERE statements are ignored).
func HasSingleColumnUniqueIndex(column string, indexes []string) bool {
_, ok := FindSingleColumnUniqueIndex(indexes, column)
return ok
}
+91
View File
@@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"strings"
"testing"
"github.com/pocketbase/pocketbase/tools/dbutils"
@@ -312,3 +313,93 @@ func TestHasSingleColumnUniqueIndex(t *testing.T) {
})
}
}
func TestFindSingleColumnUniqueIndex(t *testing.T) {
scenarios := []struct {
name string
column string
indexes []string
expected bool
}{
{
"empty indexes",
"test",
nil,
false,
},
{
"empty column",
"",
[]string{
"CREATE UNIQUE INDEX `index1` ON `example` (`test`)",
},
false,
},
{
"mismatched column",
"test",
[]string{
"CREATE UNIQUE INDEX `index1` ON `example` (`test2`)",
},
false,
},
{
"non unique index",
"test",
[]string{
"CREATE INDEX `index1` ON `example` (`test`)",
},
false,
},
{
"matching columnd and unique index",
"test",
[]string{
"CREATE UNIQUE INDEX `index1` ON `example` (`test`)",
},
true,
},
{
"multiple columns",
"test",
[]string{
"CREATE UNIQUE INDEX `index1` ON `example` (`test`, `test2`)",
},
false,
},
{
"multiple indexes",
"test",
[]string{
"CREATE UNIQUE INDEX `index1` ON `example` (`test`, `test2`)",
"CREATE UNIQUE INDEX `index2` ON `example` (`test`)",
},
true,
},
{
"partial unique index",
"test",
[]string{
"CREATE UNIQUE INDEX `index` ON `example` (`test`) where test != ''",
},
true,
},
}
for _, s := range scenarios {
t.Run(s.name, func(t *testing.T) {
index, exists := dbutils.FindSingleColumnUniqueIndex(s.indexes, s.column)
if exists != s.expected {
t.Fatalf("Expected exists %v got %v", s.expected, exists)
}
if !exists && len(index.Columns) > 0 {
t.Fatal("Expected index.Columns to be empty")
}
if exists && !strings.EqualFold(index.Columns[0].Name, s.column) {
t.Fatalf("Expected to find column %q in %v", s.column, index)
}
})
}
}