mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-23 12:16:42 +00:00
This pull request introduces a significant refactoring of the database interaction layer, focusing on improving explicitness, transactional control, and error handling. The core change is the removal of the stateful `QueryExecutor` from repository instances. Instead, it is now passed as an argument to each method that interacts with the database. This change makes transaction management more explicit and flexible, as the same repository instance can be used with a database pool or a specific transaction without needing to be re-instantiated. ### Key Changes - **Explicit `QueryExecutor` Passing:** - All repository methods (`Get`, `List`, `Create`, `Update`, `Delete`, etc.) in `InstanceRepository`, `OrganizationRepository`, `UserRepository`, and their sub-repositories now require a `database.QueryExecutor` (e.g., a `*pgxpool.Pool` or `pgx.Tx`) as the first argument. - Repository constructors no longer accept a `QueryExecutor`. For example, `repository.InstanceRepository(pool)` is now `repository.InstanceRepository()`. - **Enhanced Error Handling:** - A new `database.MissingConditionError` is introduced to enforce required query conditions, such as ensuring an `instance_id` is always present in `UPDATE` and `DELETE` operations. - The database error wrapper in the `postgres` package now correctly identifies and wraps `pgx.ErrTooManyRows` and similar errors from the `scany` library into a `database.MultipleRowsFoundError`. - **Improved Database Conditions:** - The `database.Condition` interface now includes a `ContainsColumn(Column) bool` method. This allows for runtime checks to ensure that critical filters (like `instance_id`) are included in a query, preventing accidental cross-tenant data modification. - A new `database.Exists()` condition has been added to support `EXISTS` subqueries, enabling more complex filtering logic, such as finding an organization that has a specific domain. - **Repository and Interface Refactoring:** - The method for loading related entities (e.g., domains for an organization) has been changed from a boolean flag (`Domains(true)`) to a more explicit, chainable method (`LoadDomains()`). This returns a new repository instance configured to load the sub-resource, promoting immutability. - The custom `OrgIdentifierCondition` has been removed in favor of using the standard `database.Condition` interface, simplifying the API. - **Code Cleanup and Test Updates:** - Unnecessary struct embeddings and metadata have been removed. - All integration and repository tests have been updated to reflect the new method signatures, passing the database pool or transaction object explicitly. - New tests have been added to cover the new `ExistsDomain` functionality and other enhancements. These changes make the data access layer more robust, predictable, and easier to work with, especially in the context of database transactions.
249 lines
6.4 KiB
Go
249 lines
6.4 KiB
Go
package database
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestWrite(t *testing.T) {
|
|
type want struct {
|
|
stmt string
|
|
args []any
|
|
}
|
|
for _, test := range []struct {
|
|
name string
|
|
cond Condition
|
|
want want
|
|
}{
|
|
{
|
|
name: "no and condition",
|
|
cond: And(),
|
|
want: want{
|
|
stmt: "",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "one and condition",
|
|
cond: And(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
),
|
|
want: want{
|
|
stmt: "table.column1 = other_table.column2",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "multiple and condition",
|
|
cond: And(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column3"), NewColumn("other_table", "column4")),
|
|
),
|
|
want: want{
|
|
stmt: "(table.column1 = other_table.column2 AND table.column3 = other_table.column4)",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "no or condition",
|
|
cond: Or(),
|
|
want: want{
|
|
stmt: "",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "one or condition",
|
|
cond: Or(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
),
|
|
want: want{
|
|
stmt: "table.column1 = other_table.column2",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "multiple or condition",
|
|
cond: Or(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column3"), NewColumn("other_table", "column4")),
|
|
),
|
|
want: want{
|
|
stmt: "(table.column1 = other_table.column2 OR table.column3 = other_table.column4)",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "is null condition",
|
|
cond: IsNull(NewColumn("table", "column1")),
|
|
want: want{
|
|
stmt: "table.column1 IS NULL",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "is not null condition",
|
|
cond: IsNotNull(NewColumn("table", "column1")),
|
|
want: want{
|
|
stmt: "table.column1 IS NOT NULL",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "text condition",
|
|
cond: NewTextCondition(NewColumn("table", "column1"), TextOperationEqual, "some text"),
|
|
want: want{
|
|
stmt: "table.column1 = $1",
|
|
args: []any{"some text"},
|
|
},
|
|
},
|
|
{
|
|
name: "text ignore case condition",
|
|
cond: NewTextIgnoreCaseCondition(NewColumn("table", "column1"), TextOperationNotEqual, "some TEXT"),
|
|
want: want{
|
|
stmt: "LOWER(table.column1) <> LOWER($1)",
|
|
args: []any{"some TEXT"},
|
|
},
|
|
},
|
|
{
|
|
name: "number condition",
|
|
cond: NewNumberCondition(NewColumn("table", "column1"), NumberOperationEqual, 42),
|
|
want: want{
|
|
stmt: "table.column1 = $1",
|
|
args: []any{42},
|
|
},
|
|
},
|
|
{
|
|
name: "boolean condition",
|
|
cond: NewBooleanCondition(NewColumn("table", "column1"), true),
|
|
want: want{
|
|
stmt: "table.column1 = $1",
|
|
args: []any{true},
|
|
},
|
|
},
|
|
{
|
|
name: "bytes condition",
|
|
cond: NewBytesCondition[[]byte](NewColumn("table", "column1"), BytesOperationEqual, []byte{0x01, 0x02, 0x03}),
|
|
want: want{
|
|
stmt: "table.column1 = $1",
|
|
args: []any{[]byte{0x01, 0x02, 0x03}},
|
|
},
|
|
},
|
|
{
|
|
name: "column condition",
|
|
cond: NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
want: want{
|
|
stmt: "table.column1 = other_table.column2",
|
|
args: nil,
|
|
},
|
|
},
|
|
{
|
|
name: "exists condition",
|
|
cond: Exists("table", And(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column3"), NewColumn("other_table", "column4")),
|
|
)),
|
|
want: want{
|
|
stmt: " EXISTS (SELECT 1 FROM table WHERE (table.column1 = other_table.column2 AND table.column3 = other_table.column4))",
|
|
args: nil,
|
|
},
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
var builder StatementBuilder
|
|
test.cond.Write(&builder)
|
|
assert.Equal(t, test.want.stmt, builder.String())
|
|
require.Len(t, builder.Args(), len(test.want.args))
|
|
for i, arg := range test.want.args {
|
|
assert.Equal(t, arg, builder.Args()[i])
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIsRestrictingColumn(t *testing.T) {
|
|
for _, test := range []struct {
|
|
name string
|
|
col Column
|
|
cond Condition
|
|
want bool
|
|
}{
|
|
{
|
|
name: "and with restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: And(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column3")),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "and without restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: And(
|
|
NewColumnCondition(NewColumn("table", "column2"), NewColumn("other_table", "column3")),
|
|
IsNull(NewColumn("table", "column4")),
|
|
IsNotNull(NewColumn("table", "column5")),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "or with restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: Or(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column3")),
|
|
),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "or without restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: Or(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column3")),
|
|
IsNotNull(NewColumn("table", "column4")),
|
|
IsNull(NewColumn("table", "column5")),
|
|
),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "is null never restricts",
|
|
col: NewColumn("table", "column1"),
|
|
cond: IsNull(NewColumn("table", "column1")),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "is not null never restricts",
|
|
col: NewColumn("table", "column1"),
|
|
cond: IsNotNull(NewColumn("table", "column1")),
|
|
want: false,
|
|
},
|
|
{
|
|
name: "exists with restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: Exists("table", And(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column2")),
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column3")),
|
|
)),
|
|
want: true,
|
|
},
|
|
{
|
|
name: "exists without restricting column",
|
|
col: NewColumn("table", "column1"),
|
|
cond: Exists("table", Or(
|
|
NewColumnCondition(NewColumn("table", "column1"), NewColumn("other_table", "column3")),
|
|
IsNotNull(NewColumn("table", "column4")),
|
|
IsNull(NewColumn("table", "column5")),
|
|
)),
|
|
want: false,
|
|
},
|
|
} {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
isRestricting := test.cond.IsRestrictingColumn(test.col)
|
|
assert.Equal(t, test.want, isRestricting)
|
|
})
|
|
}
|
|
}
|