Files
zitadel/backend/v3/storage/database/statement.go
T
5df8a97f9d feat(repository): implement user repository (#10967)
## Summary

This PR introduces the relational user repository for backend v3,
including schema, repository APIs, reducers/projections, and tests for
both human and machine users.

## What changed

* Added user-focused relational migrations (users, verifications,
metadata, PATs, machine keys, passkeys, identity provider links) and
related FK wiring.
* Implemented/expanded repository domain contracts and storage
implementations for human and machine user flows.
* Added support for machine secret handling and aligned access token
type modeling.
* Extended user domain models (verifications, password/email/phone/TOTP
state, passkeys, metadata, invite handling, identity provider links).
* Updated projection/reduction coverage and integration tests for key
user lifecycle events.
* Regenerated and updated mocks/enums plus supporting database
change/column utilities.

## Notes for reviewers

* This PR is large because it establishes the foundational relational
user storage layer and related test scaffolding. Sorry for that.

## Closes

Closes #10218
Closes #10224
Closes #10225
Closes #10209
Closes #10802

---------

Co-authored-by: Iraq Jaber <[email protected]>
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Marco A. <[email protected]>
Co-authored-by: Tim Möhlmann <[email protected]>
2026-02-23 16:03:47 +01:00

76 lines
1.8 KiB
Go

package database
import (
"strconv"
"strings"
)
type Instruction string
const (
DefaultInstruction Instruction = "DEFAULT"
NowInstruction Instruction = "NOW()"
NullInstruction Instruction = "NULL"
GenRandomUUIDInstruction Instruction = "gen_random_uuid()"
)
// StatementBuilder is a helper to build SQL statement.
type StatementBuilder struct {
strings.Builder
args []any
}
type argWriter interface {
WriteArg(builder *StatementBuilder)
}
func NewStatementBuilder(baseQuery string, baseArgs ...any) *StatementBuilder {
b := &StatementBuilder{}
b.WriteString(baseQuery)
b.AppendArgs(baseArgs...)
return b
}
// WriteArgs adds the argument to the statement and writes the placeholder to the query.
func (b *StatementBuilder) WriteArg(arg any) {
if writer, ok := arg.(argWriter); ok {
writer.WriteArg(b)
return
}
b.WriteString(b.AppendArg(arg))
}
// WriteArgs adds the arguments to the statement and writes the placeholders to the query.
// The placeholders are comma separated.
func (b *StatementBuilder) WriteArgs(args ...any) {
for i, arg := range args {
if i > 0 {
b.WriteString(", ")
}
b.WriteArg(arg)
}
}
// AppendArg adds the argument to the statement and returns the placeholder.
func (b *StatementBuilder) AppendArg(arg any) (placeholder string) {
if instruction, ok := arg.(Instruction); ok {
return string(instruction)
}
b.args = append(b.args, arg)
placeholder = "$" + strconv.Itoa(len(b.args))
return placeholder
}
// AppendArgs adds the arguments to the statement and doesn't return the placeholders.
func (b *StatementBuilder) AppendArgs(args ...any) {
for _, arg := range args {
b.AppendArg(arg)
}
}
// Args returns the arguments added to the statement.
func (b *StatementBuilder) Args() []any {
return b.args
}