fix(repo): correct mapping for domains (#10653)

This pull request fixes an issue where the repository would fail to scan
organization or instance structs if the `domains` column was `NULL`.

## Which problems are solved

If the `domains` column of `orgs` or `instances` was `NULL`, the
repository failed scanning into the structs. This happened because the
scanning mechanism did not correctly handle `NULL` JSONB columns.

## How the problems are solved

A new generic type `JSONArray[T]` is introduced, which implements the
`sql.Scanner` interface. This type can correctly scan JSON arrays from
the database, including handling `NULL` values gracefully.

The repositories for instances and organizations have been updated to
use this new type for the domains field. The SQL queries have also been
improved to use `FILTER` with `jsonb_agg` for better readability and
performance when aggregating domains.

## Additional changes
* An unnecessary cleanup step in the organization domain tests for
already removed domains has been removed.
* The `pgxscan` library has been replaced with `sqlscan` for scanning
`database/sql`.Rows.
* Minor cleanups in integration tests.
This commit is contained in:
Silvan
2025-09-08 09:35:31 +02:00
committed by GitHub
parent 69be6b9416
commit 869282ca49
9 changed files with 128 additions and 128 deletions
@@ -35,11 +35,11 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Wait for instance to be created
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
_, err := instanceRepo.Get(CTX,
database.WithCondition(instanceRepo.IDCondition(instance.Instance.Id)),
database.WithCondition(instanceRepo.IDCondition(instance.ID())),
)
assert.NoError(ttt, err)
assert.NoError(t, err)
}, retryDuration, tick)
t.Run("test instance custom domain add reduces", func(t *testing.T) {
@@ -65,7 +65,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Test that domain add reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -75,13 +75,13 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
// event instance.domain.added
assert.Equal(ttt, domainName, domain.Domain)
assert.Equal(ttt, instance.Instance.Id, domain.InstanceID)
assert.False(ttt, *domain.IsPrimary)
assert.WithinRange(ttt, domain.CreatedAt, beforeAdd, afterAdd)
assert.WithinRange(ttt, domain.UpdatedAt, beforeAdd, afterAdd)
assert.Equal(t, domainName, domain.Domain)
assert.Equal(t, instance.Instance.Id, domain.InstanceID)
assert.False(t, *domain.IsPrimary)
assert.WithinRange(t, domain.CreatedAt, beforeAdd, afterAdd)
assert.WithinRange(t, domain.UpdatedAt, beforeAdd, afterAdd)
}, retryDuration, tick)
})
@@ -124,7 +124,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Wait for domain to be created
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -134,9 +134,9 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.False(ttt, *domain.IsPrimary)
assert.Equal(ttt, domainName, domain.Domain)
require.NoError(t, err)
require.False(t, *domain.IsPrimary)
assert.Equal(t, domainName, domain.Domain)
}, retryDuration, tick)
// Set domain as primary
@@ -150,7 +150,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Test that set primary reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -160,11 +160,11 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
// event instance.domain.primary.set
assert.Equal(ttt, domainName, domain.Domain)
assert.True(ttt, *domain.IsPrimary)
assert.WithinRange(ttt, domain.UpdatedAt, beforeSetPrimary, afterSetPrimary)
assert.Equal(t, domainName, domain.Domain)
assert.True(t, *domain.IsPrimary)
assert.WithinRange(t, domain.UpdatedAt, beforeSetPrimary, afterSetPrimary)
}, retryDuration, tick)
})
@@ -179,7 +179,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Wait for domain to be created and verify it exists
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
_, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -189,7 +189,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
}, retryDuration, tick)
// Remove the domain
@@ -201,7 +201,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Test that domain remove reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -212,8 +212,8 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
)
// event instance.domain.removed
assert.Nil(ttt, domain)
require.ErrorIs(ttt, err, new(database.NoRowFoundError))
assert.Nil(t, domain)
require.ErrorIs(t, err, new(database.NoRowFoundError))
}, retryDuration, tick)
})
@@ -240,7 +240,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Test that domain add reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -250,12 +250,12 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
// event instance.domain.added
assert.Equal(ttt, domainName, domain.Domain)
assert.Equal(ttt, instance.Instance.Id, domain.InstanceID)
assert.WithinRange(ttt, domain.CreatedAt, beforeAdd, afterAdd)
assert.WithinRange(ttt, domain.UpdatedAt, beforeAdd, afterAdd)
assert.Equal(t, domainName, domain.Domain)
assert.Equal(t, instance.Instance.Id, domain.InstanceID)
assert.WithinRange(t, domain.CreatedAt, beforeAdd, afterAdd)
assert.WithinRange(t, domain.UpdatedAt, beforeAdd, afterAdd)
}, retryDuration, tick)
})
@@ -270,7 +270,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Wait for domain to be created and verify it exists
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
_, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -280,7 +280,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
}, retryDuration, tick)
// Remove the domain
@@ -292,7 +292,7 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
// Test that domain remove reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := instanceDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -303,8 +303,8 @@ func TestServer_TestInstanceDomainReduces(t *testing.T) {
),
)
// event instance.domain.removed
assert.Nil(ttt, domain)
require.ErrorIs(ttt, err, new(database.NoRowFoundError))
assert.Nil(t, domain)
require.ErrorIs(t, err, new(database.NoRowFoundError))
}, retryDuration, tick)
})
}
@@ -45,13 +45,13 @@ func TestServer_TestInstanceReduces(t *testing.T) {
})
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
instance, err := instanceRepo.Get(CTX,
database.WithCondition(instanceRepo.IDCondition(instance.GetInstanceId())),
)
require.NoError(ttt, err)
require.NoError(t, err)
// event instance.added
assert.Equal(ttt, instanceName, instance.Name)
assert.Equal(t, instanceName, instance.Name)
// event instance.default.org.set
assert.NotNil(t, instance.DefaultOrgID)
// event instance.iam.project.set
@@ -36,11 +36,11 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
// Wait for org to be created
retryDuration, tick := integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
_, err := orgRepo.Get(CTX,
database.WithCondition(orgRepo.IDCondition(org.GetId())),
)
assert.NoError(ttt, err)
assert.NoError(t, err)
}, retryDuration, tick)
// The API call also sets the domain as primary, so we don't do a separate test for that.
@@ -67,7 +67,7 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
// Test that domain add reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
gottenDomain, err := orgDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -77,7 +77,7 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
),
),
)
require.NoError(ttt, err)
require.NoError(t, err)
// event org.domain.added
assert.Equal(t, domainName, gottenDomain.Domain)
assert.Equal(t, Instance.Instance.Id, gottenDomain.InstanceID)
@@ -97,16 +97,6 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
})
require.NoError(t, err)
t.Cleanup(func() {
_, err := OrgClient.DeleteOrganizationDomain(CTX, &v2beta.DeleteOrganizationDomainRequest{
OrganizationId: org.GetId(),
Domain: domainName,
})
if err != nil {
t.Logf("Failed to delete domain on cleanup: %v", err)
}
})
// Remove the domain
_, err = OrgClient.DeleteOrganizationDomain(CTX, &v2beta.DeleteOrganizationDomainRequest{
OrganizationId: org.GetId(),
@@ -116,7 +106,7 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
// Test that domain remove reduces
retryDuration, tick = integration.WaitForAndTickWithMaxDuration(CTX, time.Minute)
assert.EventuallyWithT(t, func(ttt *assert.CollectT) {
assert.EventuallyWithT(t, func(t *assert.CollectT) {
domain, err := orgDomainRepo.Get(CTX,
database.WithCondition(
database.And(
@@ -126,8 +116,8 @@ func TestServer_TestOrgDomainReduces(t *testing.T) {
),
)
// event instance.domain.removed
assert.Nil(ttt, domain)
require.ErrorIs(ttt, err, new(database.NoRowFoundError))
assert.Nil(t, domain)
require.ErrorIs(t, err, new(database.NoRowFoundError))
}, retryDuration, tick)
})
}