Skip to content

Commit bc544fa

Browse files
authored
fix: Skip missing tenant schemas in demo user seeding (#2146)
* fix: Skip missing tenant schemas in demo user seeding SeedDemoUsers iterates all tenants from DEMO_OPERATOR_TENANT (comma- separated). When a listed tenant has not been provisioned in the current environment (e.g. payg_energy on demo), the schema lookup fails with ErrTenantSchemaNotProvisioned and aborts the entire seeding operation - even though earlier tenants (volterra_energy) were seeded successfully. Skip tenants with missing schemas instead of failing. Log a warning so the skip is visible in deploy logs without blocking the pipeline. * test: Add coverage for SeedDemoUsers skipping missing tenant schemas Add TestSeedDemoUsers_SkipsMissingTenantSchema to cover the new ErrTenantSchemaNotProvisioned skip path. Uses schemaNotProvisioned flag on fakeRepo to simulate the missing-schema condition without requiring a real database. --------- Co-authored-by: Ben Coombs <bjcoombs@users.noreply.github.com>
1 parent fd52378 commit bc544fa

2 files changed

Lines changed: 31 additions & 0 deletions

File tree

services/identity/bootstrap/bootstrap.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"github.com/meridianhub/meridian/services/identity/domain"
1919
"github.com/meridianhub/meridian/shared/pkg/credentials"
2020
"github.com/meridianhub/meridian/shared/platform/auth"
21+
"github.com/meridianhub/meridian/shared/platform/db"
2122
"github.com/meridianhub/meridian/shared/platform/tenant"
2223
)
2324

@@ -213,6 +214,16 @@ func SeedDemoUsers(ctx context.Context, repo domain.Repository) error {
213214

214215
for _, u := range users {
215216
if err := seedDemoUser(ctx, repo, u); err != nil {
217+
// Skip tenants whose schemas have not been provisioned.
218+
// DEMO_OPERATOR_TENANT may list multiple tenants but not all
219+
// of them exist in every environment (e.g. payg_energy is
220+
// configured but only volterra_energy is provisioned on demo).
221+
if errors.Is(err, db.ErrTenantSchemaNotProvisioned) {
222+
slog.WarnContext(ctx, "demo user seeding skipped: tenant schema not provisioned",
223+
"email", u.Email,
224+
"tenant", u.TenantID)
225+
continue
226+
}
216227
return fmt.Errorf("seeding demo user %s: %w", u.Email, err)
217228
}
218229
}

services/identity/bootstrap/demo_users_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"github.com/google/uuid"
1010
"github.com/meridianhub/meridian/services/identity/domain"
11+
"github.com/meridianhub/meridian/shared/platform/db"
1112
"github.com/meridianhub/meridian/shared/platform/tenant"
1213
"github.com/stretchr/testify/assert"
1314
"github.com/stretchr/testify/require"
@@ -19,6 +20,9 @@ type fakeRepo struct {
1920
roles map[uuid.UUID][]*domain.RoleAssignment // keyed by identity ID
2021

2122
saveWithRolesCalled bool
23+
// schemaNotProvisioned makes FindByEmail return ErrTenantSchemaNotProvisioned
24+
// for any email lookup, simulating a missing tenant schema.
25+
schemaNotProvisioned bool
2226
}
2327

2428
func newFakeRepo() *fakeRepo {
@@ -43,6 +47,9 @@ func (f *fakeRepo) FindByID(_ context.Context, id uuid.UUID) (*domain.Identity,
4347
}
4448

4549
func (f *fakeRepo) FindByEmail(_ context.Context, email string) (*domain.Identity, error) {
50+
if f.schemaNotProvisioned {
51+
return nil, db.ErrTenantSchemaNotProvisioned
52+
}
4653
if ident, ok := f.identities[email]; ok {
4754
return ident, nil
4855
}
@@ -240,3 +247,16 @@ func TestSeedDemoUsers_ReconcilesRole_ExistingUserMissingRole(t *testing.T) {
240247
require.Len(t, roles, 1)
241248
assert.Equal(t, domain.RoleOperator, roles[0].Role())
242249
}
250+
251+
func TestSeedDemoUsers_SkipsMissingTenantSchema(t *testing.T) {
252+
t.Setenv("DEMO_OPERATOR_EMAIL", "operator@volterra.energy")
253+
t.Setenv("DEMO_OPERATOR_PASSWORD", "demo2026")
254+
t.Setenv("DEMO_OPERATOR_TENANT", "missing_tenant")
255+
256+
repo := newFakeRepo()
257+
repo.schemaNotProvisioned = true
258+
259+
err := SeedDemoUsers(context.Background(), repo)
260+
require.NoError(t, err, "should skip tenants with missing schemas instead of failing")
261+
assert.False(t, repo.saveWithRolesCalled, "should not have attempted to save identity")
262+
}

0 commit comments

Comments
 (0)