mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 11:17:32 +00:00
feat(db): adding relational instance table (#10007)
<!-- Please inform yourself about the contribution guidelines on submitting a PR here: https://github.com/zitadel/zitadel/blob/main/CONTRIBUTING.md#submit-a-pull-request-pr. Take note of how PR/commit titles should be written and replace the template texts in the sections below. Don't remove any of the sections. It is important that the commit history clearly shows what is changed and why. Important: By submitting a contribution you agree to the terms from our Licensing Policy as described here: https://github.com/zitadel/zitadel/blob/main/LICENSING.md#community-contributions. --> # Which Problems Are Solved Implementing Instance table to new relational database schema # How the Problems Are Solved The following fields must be managed in this table: - `id` - `name` - `default_org_id` - `zitadel_project_id` - `console_client_id` - `console_app_id` - `default_language` - `created_at` - `updated_at` - `deleted_at` The repository must provide the following functions: Manipulations: - create - `name` - `default_org_id` - `zitadel_project_id` - `console_client_id` - `console_app_id` - `default_language` - update - `name` - `default_language` - delete Queries: - get returns single instance matching the criteria and pagination, should return error if multiple instances were found - list returns list of instances matching the criteria, pagination Criteria are the following: - by id pagination: - by created_at - by updated_at - by name ### instance events The following events must be applied on the table using a projection (`internal/query/projection`) - `instance.added` results in create - `instance.changed` changes the `name` field - `instance.removed` sets the the `deleted_at` field - `instance.default.org.set` sets the `default_org_id` field - `instance.iam.project.set` sets the `zitadel_project_id` field - `instance.iam.console.set` sets the `console_client_id` and `console_app_id` fields - `instance.default.language.set` sets the `default_language` field - if answer is yes to discussion: `instance.domain.primary.set` sets the `primary_domain` field ### acceptance criteria - [x] migration is implemented and gets executed - [x] domain interfaces are implemented and documented for service layer - [x] repository is implemented and implements domain interface - [x] testing - [x] the repository methods - [x] events get reduced correctly - [x] unique constraints # Additional Context - Closes https://github.com/zitadel/zitadel/issues/9935
This commit is contained in:
171
backend/v3/storage/database/events_testing/instance_test.go
Normal file
171
backend/v3/storage/database/events_testing/instance_test.go
Normal file
@@ -0,0 +1,171 @@
|
||||
//go:build integration
|
||||
|
||||
package instance_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/brianvoe/gofakeit/v6"
|
||||
"github.com/jackc/pgx/v5/pgxpool"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres"
|
||||
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
||||
"github.com/zitadel/zitadel/internal/integration"
|
||||
"github.com/zitadel/zitadel/pkg/grpc/system"
|
||||
)
|
||||
|
||||
const ConnString = "host=localhost port=5432 user=zitadel dbname=zitadel sslmode=disable"
|
||||
|
||||
var (
|
||||
dbPool *pgxpool.Pool
|
||||
CTX context.Context
|
||||
Instance *integration.Instance
|
||||
SystemClient system.SystemServiceClient
|
||||
)
|
||||
|
||||
var pool database.Pool
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
os.Exit(func() int {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
Instance = integration.NewInstance(ctx)
|
||||
|
||||
CTX = Instance.WithAuthorization(ctx, integration.UserTypeIAMOwner)
|
||||
SystemClient = integration.SystemClient()
|
||||
|
||||
var err error
|
||||
dbPool, err = pgxpool.New(context.Background(), ConnString)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
pool = postgres.PGxPool(dbPool)
|
||||
|
||||
return m.Run()
|
||||
}())
|
||||
}
|
||||
|
||||
func TestServer_TestInstanceAddReduces(t *testing.T) {
|
||||
instanceName := gofakeit.Name()
|
||||
beforeCreate := time.Now()
|
||||
_, err := SystemClient.CreateInstance(CTX, &system.CreateInstanceRequest{
|
||||
InstanceName: instanceName,
|
||||
Owner: &system.CreateInstanceRequest_Machine_{
|
||||
Machine: &system.CreateInstanceRequest_Machine{
|
||||
UserName: "owner",
|
||||
Name: "owner",
|
||||
PersonalAccessToken: &system.CreateInstanceRequest_PersonalAccessToken{},
|
||||
},
|
||||
},
|
||||
})
|
||||
afterCreate := time.Now()
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||
instance, err := instanceRepo.Get(CTX,
|
||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||
)
|
||||
require.NoError(ttt, err)
|
||||
// event instance.added
|
||||
require.Equal(ttt, instanceName, instance.Name)
|
||||
// event instance.default.org.set
|
||||
require.NotNil(t, instance.DefaultOrgID)
|
||||
// event instance.iam.project.set
|
||||
require.NotNil(t, instance.IAMProjectID)
|
||||
// event instance.iam.console.set
|
||||
require.NotNil(t, instance.ConsoleAppID)
|
||||
// event instance.default.language.set
|
||||
require.NotNil(t, instance.DefaultLanguage)
|
||||
// event instance.added
|
||||
assert.WithinRange(t, instance.CreatedAt, beforeCreate, afterCreate)
|
||||
// event instance.added
|
||||
assert.WithinRange(t, instance.UpdatedAt, beforeCreate, afterCreate)
|
||||
require.Nil(t, instance.DeletedAt)
|
||||
}, retryDuration, tick)
|
||||
}
|
||||
|
||||
func TestServer_TestInstanceUpdateNameReduces(t *testing.T) {
|
||||
instanceName := gofakeit.Name()
|
||||
res, err := SystemClient.CreateInstance(CTX, &system.CreateInstanceRequest{
|
||||
InstanceName: instanceName,
|
||||
Owner: &system.CreateInstanceRequest_Machine_{
|
||||
Machine: &system.CreateInstanceRequest_Machine{
|
||||
UserName: "owner",
|
||||
Name: "owner",
|
||||
PersonalAccessToken: &system.CreateInstanceRequest_PersonalAccessToken{},
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
instanceName += "new"
|
||||
_, err = SystemClient.UpdateInstance(CTX, &system.UpdateInstanceRequest{
|
||||
InstanceId: res.InstanceId,
|
||||
InstanceName: instanceName,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||
instance, err := instanceRepo.Get(CTX,
|
||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||
)
|
||||
require.NoError(ttt, err)
|
||||
// event instance.changed
|
||||
require.Equal(ttt, instanceName, instance.Name)
|
||||
}, retryDuration, tick)
|
||||
}
|
||||
|
||||
func TestServer_TestInstanceDeleteReduces(t *testing.T) {
|
||||
instanceName := gofakeit.Name()
|
||||
res, err := SystemClient.CreateInstance(CTX, &system.CreateInstanceRequest{
|
||||
InstanceName: instanceName,
|
||||
Owner: &system.CreateInstanceRequest_Machine_{
|
||||
Machine: &system.CreateInstanceRequest_Machine{
|
||||
UserName: "owner",
|
||||
Name: "owner",
|
||||
PersonalAccessToken: &system.CreateInstanceRequest_PersonalAccessToken{},
|
||||
},
|
||||
},
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
instanceRepo := repository.InstanceRepository(pool)
|
||||
|
||||
// check instance exists
|
||||
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||
instance, err := instanceRepo.Get(CTX,
|
||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||
)
|
||||
require.NoError(ttt, err)
|
||||
require.Equal(ttt, instanceName, instance.Name)
|
||||
}, retryDuration, tick)
|
||||
|
||||
_, err = SystemClient.RemoveInstance(CTX, &system.RemoveInstanceRequest{
|
||||
InstanceId: res.InstanceId,
|
||||
})
|
||||
require.NoError(t, err)
|
||||
|
||||
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
|
||||
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
|
||||
instance, err := instanceRepo.Get(CTX,
|
||||
instanceRepo.NameCondition(database.TextOperationEqual, instanceName),
|
||||
)
|
||||
// event instance.removed
|
||||
require.Nil(t, instance)
|
||||
require.NoError(ttt, err)
|
||||
}, retryDuration, tick)
|
||||
}
|
Reference in New Issue
Block a user