mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-18 05:52:51 +00:00
<!-- CURSOR_AGENT_PR_BODY_BEGIN --> # Which Problems Are Solved Login V2 `ListUsers` lookups of the form username OR email (and email OR phone) hash-join the full `users14` / `users14_humans` tables, then filter. That cost grows with instance size (~40ms at 10k users, ~650ms at 200k). Top-level login-name equals already used an indexed join, but the same login-name filter inside an `OrQuery` fell back to the slow view-based exists query. # How the Problems Are Solved **TL;DR:** equality lookups on username, email, phone, or login name become `INNER JOIN (UNION of indexed ID seeks) AS matches`. Mixed / contains / name searches keep the old WHERE shape. ### Reviewer map | Read first | Why | | --- | --- | | `internal/query/user_login_equality.go` | Typed filters + extractor + UNION join | | `internal/query/user.go` (`prepareUsersQuery`, constructors) | Wiring; username/email/phone equals return `lowerEqualsFilter` | | `cmd/setup/76.go` + `cmd/setup/76/*.sql` | Concurrent indexes for existing deployments; drop invalid leftovers | | `internal/query/projection/user.go` | Same indexes in `Init()` for new databases | | `internal/query/user_login_name_exists_test.go` | SQL-shape coverage | | `internal/api/grpc/user/v2/integration_test/query_test.go` | Nested username AND/OR user results | ### Query rewrite - Extract one equals leaf, a flat `OrQuery` of those leaves, or the same nested under `AndQuery`. Remaining AND conjuncts stay as WHERE. Equality nested under `OrQuery` / `NotQuery` is left as WHERE. - Login-name equals is a UNION arm that reuses `login_names3_users` SQL, so it works inside OR as well as at the top level. - EQUALS seeks use `LOWER(col)` for the index, then recheck `col = value` so case-sensitive searches do not match extra rows. EQUALS_IGNORE_CASE is unchanged. - Empty OR arms are not skipped (that would change OR meaning); mixed empty-OR falls back to WHERE. Login UI already omits empty arms. ### Indexes - Setup 76: `CREATE INDEX CONCURRENTLY IF NOT EXISTS` on `users14_username_lower_idx` `(instance_id, LOWER(username))` and `users14_humans_phone_lower_idx` `(instance_id, LOWER(phone))`. Not wrapped in a transaction. - Before create, drop any leftover `pg_index.indisvalid = false` indexes of those names (failed/cancelled `CREATE INDEX CONCURRENTLY`). `IF NOT EXISTS` would otherwise skip rebuild. - `Init()` creates the same indexes without `CONCURRENTLY` (empty tables). Existing `users14_username` and email lower indexes are unchanged. # Additional Changes - Unit tests assert UNION SQL for username OR email, email OR phone, login name OR email, username OR username, nested `AndQuery(loginName, org)`, and nested `AndQuery(Or(...), org)`; that contains / display-name / mixed OR / nested `OrQuery(AndQuery(...), ...)` do not UNION; that email EQUALS rechecks the raw column; and that empty-OR is not extracted. - Setup 76 sqlmock coverage: missing `users14` skips work; valid/absent indexes only create; invalid leftover username index is dropped then both creates run. - `TestServer_ListUsers_UsernameAndOr` checks live results: org AND (alice OR bob) returns both users, sibling username AND is empty, nested `AndQuery(Or(...), org)` returns both. # Additional Context - Base branch: `main` - Extends the login-name equals rewrite from #12460 to username/email/phone OR trees used by Login V2 search. <!-- CURSOR_AGENT_PR_BODY_END --> <div><a href="https://cursor.com/agents/bc-1f043b1c-6168-45ca-9e9e-c18451994207?cursor_ref=pr_footer&cursor_cta=open_in_web"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-web-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-web-light.png"><img alt="Open in Web" width="114" height="28" src="https://cursor.com/assets/images/open-in-web-dark.png"></picture></a> <a href="https://cursor.com/background-agent?bcId=bc-1f043b1c-6168-45ca-9e9e-c18451994207&cursor_ref=pr_footer&cursor_cta=open_in_cursor"><picture><source media="(prefers-color-scheme: dark)" srcset="https://cursor.com/assets/images/open-in-cursor-dark.png"><source media="(prefers-color-scheme: light)" srcset="https://cursor.com/assets/images/open-in-cursor-light.png"><img alt="Open in Cursor" width="131" height="28" src="https://cursor.com/assets/images/open-in-cursor-dark.png"></picture></a> </div> --------- Co-authored-by: Cursor Agent <[email protected]> Co-authored-by: Silvan <[email protected]>
93 lines
2.9 KiB
Go
93 lines
2.9 KiB
Go
package setup
|
|
|
|
import (
|
|
"context"
|
|
"regexp"
|
|
"testing"
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
)
|
|
|
|
const (
|
|
users14UsernameLowerCreate = "CREATE INDEX CONCURRENTLY IF NOT EXISTS users14_username_lower_idx ON projections.users14 (instance_id, LOWER(username));\n"
|
|
users14PhoneLowerCreate = "CREATE INDEX CONCURRENTLY IF NOT EXISTS users14_humans_phone_lower_idx ON projections.users14_humans (instance_id, LOWER(phone));\n"
|
|
)
|
|
|
|
func TestUsers14LoginEqualityIndexes_Execute(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
expects func(sqlmock.Sqlmock)
|
|
wantErr bool
|
|
}{
|
|
{
|
|
name: "table missing, no drop or create",
|
|
expects: func(mock sqlmock.Sqlmock) {
|
|
mock.ExpectQuery(regexp.QuoteMeta(users14TableExistsQuery)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(false))
|
|
},
|
|
},
|
|
{
|
|
name: "table exists, indexes valid, creates both concurrently",
|
|
expects: func(mock sqlmock.Sqlmock) {
|
|
expectUsers14TableExists(mock, true)
|
|
expectInvalidIndex(mock, users14UsernameLowerIdx, false)
|
|
expectInvalidIndex(mock, users14HumansPhoneLowerIdx, false)
|
|
mock.ExpectExec(regexp.QuoteMeta(users14UsernameLowerCreate)).
|
|
WillReturnResult(sqlmock.NewResult(0, 0))
|
|
mock.ExpectExec(regexp.QuoteMeta(users14PhoneLowerCreate)).
|
|
WillReturnResult(sqlmock.NewResult(0, 0))
|
|
},
|
|
},
|
|
{
|
|
name: "username index invalid, drop then create both",
|
|
expects: func(mock sqlmock.Sqlmock) {
|
|
expectUsers14TableExists(mock, true)
|
|
expectInvalidIndex(mock, users14UsernameLowerIdx, true)
|
|
mock.ExpectExec(regexp.QuoteMeta(dropInvalidIndexConcurrently + users14UsernameLowerIdx)).
|
|
WillReturnResult(sqlmock.NewResult(0, 0))
|
|
expectInvalidIndex(mock, users14HumansPhoneLowerIdx, false)
|
|
mock.ExpectExec(regexp.QuoteMeta(users14UsernameLowerCreate)).
|
|
WillReturnResult(sqlmock.NewResult(0, 0))
|
|
mock.ExpectExec(regexp.QuoteMeta(users14PhoneLowerCreate)).
|
|
WillReturnResult(sqlmock.NewResult(0, 0))
|
|
},
|
|
},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
db, mock, err := sqlmock.New()
|
|
require.NoError(t, err)
|
|
defer func() {
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
}()
|
|
defer db.Close()
|
|
tt.expects(mock)
|
|
|
|
mig := &Users14LoginEqualityIndexes{
|
|
dbClient: &database.DB{DB: db},
|
|
}
|
|
err = mig.Execute(context.Background(), nil)
|
|
if tt.wantErr {
|
|
assert.Error(t, err)
|
|
return
|
|
}
|
|
assert.NoError(t, err)
|
|
})
|
|
}
|
|
}
|
|
|
|
func expectUsers14TableExists(mock sqlmock.Sqlmock, exists bool) {
|
|
mock.ExpectQuery(regexp.QuoteMeta(users14TableExistsQuery)).
|
|
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(exists))
|
|
}
|
|
|
|
func expectInvalidIndex(mock sqlmock.Sqlmock, name string, invalid bool) {
|
|
mock.ExpectQuery(regexp.QuoteMeta(users14InvalidIndexQuery)).
|
|
WithArgs(name).
|
|
WillReturnRows(sqlmock.NewRows([]string{"exists"}).AddRow(invalid))
|
|
}
|