Skip to content

Commit c20e9e3

Browse files
committed
docs: design normalized database seed update
1 parent fc9207a commit c20e9e3

1 file changed

Lines changed: 64 additions & 0 deletions

File tree

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# Normalized Database Seed Design
2+
3+
## Goal
4+
5+
Update user seeding to work with the normalized user database schema while preserving the existing seed configuration and idempotent behavior. Update the README so operators can understand which records are created and how `SEED_ADMIN_FULLNAME` maps to the current schema.
6+
7+
## Current Problem
8+
9+
The user seeder passes `fullname` to `User.create()`. The normalized `users` table and current domain factory no longer accept that field. Personal names now belong in `user_profiles`, specifically `display_name` for the existing seed value. As a result, configured user seeding fails before a user can be persisted.
10+
11+
## Design
12+
13+
### User creation
14+
15+
The seeder will create each user with the current identity fields only: email, password hash, and username. It will continue using `SQLAlchemyUserRepository.save()`, which creates the required default `user_profiles`, `user_settings`, and `user_security` records alongside a new user.
16+
17+
### Profile population
18+
19+
The seed repository contract will expose profile persistence. After saving a new user, the seeder will save a `UserProfile` whose `display_name` contains the configured full name. `SEED_ADMIN_FULLNAME` remains unchanged for backward compatibility. Development-user full names follow the same mapping.
20+
21+
The profile is saved before assigning the role. All operations remain within the seed runner's existing transaction, so a failure rolls back the user, normalized related records, profile update, and role assignment together.
22+
23+
### Idempotency
24+
25+
Email remains the identity used to detect existing seed users. If a user already exists, the seeder will not update identity data, profile data, password hashes, settings, security state, or role assignments. Authorization resource, role, permission, role-permission, and Casbin policy seeding retains its existing idempotency checks.
26+
27+
### Configuration
28+
29+
No environment-variable names change. The relevant variables remain:
30+
31+
- `SEED_ADMIN_EMAIL`
32+
- `SEED_ADMIN_PASSWORD`
33+
- `SEED_ADMIN_USERNAME`
34+
- `SEED_ADMIN_FULLNAME`
35+
- `SEED_DEVELOPMENT_USERS_PASSWORD`
36+
37+
`SEED_ADMIN_FULLNAME` is documented as the value stored in `user_profiles.display_name`, not a column in `users`.
38+
39+
## Testing
40+
41+
Focused unit tests will cover:
42+
43+
- Creating an admin user with current `User.create()` arguments.
44+
- Saving the admin display name to a normalized profile.
45+
- Creating development users and their profiles only in development mode.
46+
- Skipping user and profile writes when seed credentials are missing or the email exists.
47+
- Assigning the configured role only after a new user and profile are saved.
48+
- Returning accurate user and role counters.
49+
50+
Existing project tests and Ruff checks will be run after implementation. Database-backed execution will be used only if the configured local services are available; otherwise unit tests will verify the seeder's behavior through its repository and authorization contracts.
51+
52+
## README Changes
53+
54+
The database-seeding section will explain:
55+
56+
- The authorization records created by the baseline seed.
57+
- The normalized user records created for each new seeded user: `users`, `user_profiles`, `user_settings`, and `user_security`.
58+
- The mapping from `SEED_ADMIN_FULLNAME` to `user_profiles.display_name`.
59+
- Development-only demo accounts and their roles.
60+
- Existing-user behavior and transactional/idempotent guarantees.
61+
62+
## Scope
63+
64+
This change does not alter migrations, database models, seed account credentials, authorization definitions, or existing-user reconciliation. It fixes user seed compatibility with the current schema and documents actual behavior.

0 commit comments

Comments
 (0)