Documentation

fsds/db-manager-add-data-seeding-plan.md

Plan: Migrate Reference Data Seeding to db-manager

Problem Summary

BBU component fails on startup because the default tenant (00000000-0000-0000-0000-000000000001) doesn't exist. The root cause is that db-manager runs migrations but skips seeding, and the separate Identity DbMigrator (which had the seeding logic) is no longer deployed.

Solution

Migrate all reference data seeders from individual DbMigrator projects to Database projects, and have db-manager invoke them after migrations complete.


Implementation Stages

Stage 1: Create IReferenceDataSeeder Interface

Create strata/core/src/Acsis.Dynaplex/IReferenceDataSeeder.cs:

namespace Acsis.Dynaplex;

public interface IReferenceDataSeeder
{
    Task SeedAsync(string environment, ILogger logger, CancellationToken cancellationToken = default);
}

Simple interface - environment passed for conditional seeding (dev users), logger for consistent output.


Stage 2: Update db-manager to Invoke Seeders

Modify engines/db-manager/src/Acsis.Dynaplex.Engines.DbManager/MigrationWorker.cs:

  1. Add IHostEnvironment to constructor
  2. Replace the RunSeederAsync stub that returns early with actual seeder invocation:
    • After migrating each component, call its registered seeder (if any)
    • Use component name switch to get the right seeder from DI

Modify engines/db-manager/src/Acsis.Dynaplex.Engines.DbManager/Program.cs:

  1. Register seeders with DI (scoped lifetime):
builder.Services.AddScoped<IdentityReferenceDataSeeder>();
builder.Services.AddScoped<CoreDataReferenceDataSeeder>();
// etc.

Stage 3: Migrate Identity Seeder (Critical - Fixes the Issue)

This is the most critical seeder - creates the default tenant that BBU needs.

Create folder structure:

engines/identity/src/Acsis.Dynaplex.Engines.Identity.Database/
  Seeding/
    IdentityReferenceDataSeeder.cs
    RoleReferenceData.cs
    ReferenceData/
      DefaultTenant.sql
      UserStatuses.sql

Move files from Identity.DbMigrator/ to Identity.Database/Seeding/:

  • IdentityReferenceDataSeeder.cs - update to implement IReferenceDataSeeder
  • ReferenceData/*.sql files
  • RoleReferenceData.cs (static helper)

Update Identity.Database.csproj:

<ItemGroup>
  <None Update="Seeding\ReferenceData\*.sql">
    <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
  </None>
</ItemGroup>

Update seeder to:

  • Implement IReferenceDataSeeder
  • Update SQL file paths to Seeding/ReferenceData/
  • Keep ISystemDbContextUser for cross-tenant operations

Stage 4: Migrate CoreData Seeder

Create engines/core-data/src/Acsis.Dynaplex.Engines.CoreData.Database/Seeding/:

  • CoreDataReferenceDataSeeder.cs
  • ReferenceData/*.sql (move from DbMigrator)

Seeds: Identifier types, UoM quantities, categories, units.


Stage 5: Migrate Prism Seeder

Create engines/prism/src/Acsis.Dynaplex.Engines.Prism.Database/Seeding/:

  • PrismReferenceDataSeeder.cs
  • PlatformTypeReferenceData.cs (move from DbMigrator)

Seeds: Platform type collections, platform types (uses C# objects, no SQL files).


Stage 6: Migrate Spatial Seeder

Create engines/spatial/src/Acsis.Dynaplex.Engines.Spatial.Database/Seeding/:

  • SpatialReferenceDataSeeder.cs
  • ReferenceData/*.sql (move from DbMigrator)

Seeds: Countries, default location, movement reference data. Implements ISystemDbContextUser.


Stage 7: Migrate BBU Seeder

Create engines/bbu/src/Acsis.Dynaplex.Engines.Bbu.Database/Seeding/:

  • BbuReferenceDataSeeder.cs

Seeds: Superset analytics views (uses existing SupersetAnalyticsViewsSql class).


Seeder Execution Order

db-manager already processes components in topological order via ComponentIndex.DatabaseComponentsInOrder. Seeders will run in this order:

  1. core-data - UoM, identifier types (no dependencies)
  2. prism - Platform types (depends on core-data)
  3. identity - Default tenant, roles, users (depends on prism for passport)
  4. spatial - Countries, locations (depends on identity for tenant)
  5. bbu - Analytics views (depends on catalog)

Critical Files to Modify

File Action
Acsis.Dynaplex/IReferenceDataSeeder.cs Create
DbManager/MigrationWorker.cs Modify - add seeder invocation
DbManager/Program.cs Modify - register seeders
Identity.Database/Seeding/* Create - move from DbMigrator
Identity.Database.csproj Modify - add SQL copy
CoreData.Database/Seeding/* Create - move from DbMigrator
Prism.Database/Seeding/* Create - move from DbMigrator
Spatial.Database/Seeding/* Create - move from DbMigrator
Bbu.Database/Seeding/* Create - move from DbMigrator

Key Design Decisions

  1. Copy to output for SQL files (simpler than embedded resources, matches CoreData pattern)
  2. Interface in Acsis.Dynaplex (already referenced by all Database projects)
  3. Explicit DI registration (clearer than assembly scanning)
  4. Environment as parameter (seeders get environment via method, not constructor)
  5. Keep ISystemDbContextUser pattern for cross-tenant operations

Post-Implementation

After all seeders are migrated:

  1. Remove seeding code from DbMigrator projects (keep projects for EF Core CLI tooling)
  2. Deploy updated db-manager
  3. BBU will find the default tenant and initialize successfully