Skip to content

refactor: Centralise Postgres enum registration and fix petTrainer typo#316

Draft
hhvrc wants to merge 3 commits into
developfrom
feature/pg-enum-refactor
Draft

refactor: Centralise Postgres enum registration and fix petTrainer typo#316
hhvrc wants to merge 3 commits into
developfrom
feature/pg-enum-refactor

Conversation

@hhvrc

@hhvrc hhvrc commented Jun 13, 2026

Copy link
Copy Markdown
Contributor

No description provided.

@hhvrc hhvrc self-assigned this Jun 13, 2026
Copilot AI review requested due to automatic review settings June 13, 2026 19:03

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR centralizes Postgres enum mapping/registration for EF Core by introducing a [PgEnum] marker attribute and shared registration helpers, while also correcting the petTrainer enum label typo to petrainer via a migration.

Changes:

  • Added [PgEnum] + MapPgEnums()/RegisterPgEnums() to centralize Npgsql enum mapping and EF model enum registration.
  • Updated multiple enums to use explicit [PgName(...)] labels rather than relying on implicit enum member naming.
  • Added a migration to rename the Postgres enum value petTrainerpetrainer and updated snapshots accordingly.

Reviewed changes

Copilot reviewed 14 out of 15 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
Common/Utils/PgEnumAttribute.cs Adds a marker attribute to opt enums into centralized PG enum registration.
Common/OpenShockDb/UserNameBlacklist.cs Marks MatchTypeEnum as a PG enum and sets explicit PG labels.
Common/OpenShockDb/OpenShockContext.cs Replaces per-enum mapping/registration with centralized extension methods.
Common/OpenShockDb/NpgsqlEnumExtensions.cs Implements centralized mapping/registration of all PG enums.
Common/OpenShockDb/ConfigurationItem.cs Marks ConfigurationValueType as a PG enum and sets explicit PG labels.
Common/Models/ShockerModelType.cs Fixes PG label typo for PetTrainer (petTrainerpetrainer) and opts into centralized registration.
Common/Models/RoleType.cs Adds [PgEnum] and explicit PG labels.
Common/Models/PermissionType.cs Adds [PgEnum] to ensure it’s included in centralized registration.
Common/Models/PasswordHashingAlgorithm.cs Adds [PgEnum("password_encryption_type")] and explicit PG labels.
Common/Models/OtaUpdateStatus.cs Adds [PgEnum] and explicit PG labels.
Common/Models/ControlType.cs Adds [PgEnum] and explicit PG labels.
Common/Models/ControlLimitMode.cs Adds [PgEnum] and explicit PG labels.
Common/Migrations/OpenShockContextModelSnapshot.cs Updates model snapshot to reflect centralized enum definitions and renamed label.
Common/Migrations/20260612142230_RefactorPgEnums.Designer.cs Adds migration designer snapshot for the refactor migration.
Common/Migrations/20260612142230_RefactorPgEnums.cs Adds migration that renames the shocker_model_type value and updates enum annotations.
Files not reviewed (1)
  • Common/Migrations/20260612142230_RefactorPgEnums.Designer.cs: Generated file

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +4 to +6
/// Marks an enum as a Postgres native enum type so it is automatically registered
/// with the EF Core model via <see cref="NpgsqlEnumExtensions.RegisterPgEnums"/>.
/// </summary>
Comment on lines +38 to +40
return new PgEnumInfo(
Map: b => b.MapEnum<TEnum>(pgTypeName),
Register: m => m.HasPostgresEnum(attr.Schema, pgTypeName, members));
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "password_encryption_type", new[] { "bcrypt_enhanced", "pbkdf2" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "permission_type", new[] { "shockers.use", "shockers.edit", "shockers.pause", "devices.edit", "devices.auth" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "role_type", new[] { "support", "staff", "admin", "system" });
NpgsqlModelBuilderExtensions.HasPostgresEnum(modelBuilder, "shocker_model_type", new[] { "caiXianlin", "petTrainer", "petrainer998DR", "wellturnT330" });
Comment thread Common/Models/ShockerModelType.cs Outdated
Comment on lines 9 to 11
[PgName("caiXianlin")] CaiXianlin = 0,
[PgName("petTrainer")] PetTrainer = 1, // Misspelled, should be "petrainer",
[PgName("petrainer")] PetTrainer = 1,
[PgName("petrainer998DR")] Petrainer998DR = 2,
…factor

# Conflicts:
#	Common/Migrations/OpenShockContextModelSnapshot.cs
#	Common/OpenShockDb/OpenShockContext.cs
@stage-review

stage-review Bot commented Jun 30, 2026

Copy link
Copy Markdown

Ready to review this PR? Stage has broken it down into 5 individual chapters for you:

Title
1 Define PgEnumAttribute for automatic registration
2 Annotate enums with Postgres metadata
3 Implement centralized enum registration logic
4 Wire centralized registration into DbContext
5 Apply database migrations for enum changes
Open in Stage

Chapters generated by Stage for commit f57cda6 on Jul 1, 2026 12:05am UTC.

Restamp RefactorPgEnums to run after AddEmailOutbox (resolving the merge
ordering) and rewrite it to recreate each changed enum instead of relying
on EF's annotation diff, which can only ALTER TYPE ... ADD VALUE and so
silently no-ops reorders and leaves stale labels on renames.

For each affected type the migration detaches its columns to text, drops
the old type, remaps values, recreates the type, and reattaches:
- control_type / password_encryption_type: reorder labels
- shocker_model_type: rename labels to snake_case (cai_xianlin, petrainer,
  petrainer_998dr, wellturn_t330), fixing the petTrainer typo

Verified both Up and Down round-trip against Postgres 15 with seeded data.
@hhvrc hhvrc marked this pull request as draft July 1, 2026 00:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants