iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
💨

The Dangers of Managing Classification Values Solely in the Database and the Pros/Cons of Managing Them in Code

に公開

Introduction

In many systems, the question of where to manage "category values (enum values/master values)" is often a subject of debate. Particularly in cases where these values are held only in staging (QA) environments, an operational pattern of "it's already in the environment, so we don't need it in the code" tends to emerge.

In practice, however, category values are often treated similarly to "specifications," and in many cases, it is safer to manage them on the code side as well. Here, "code side" refers not only to enums but also to managing them within the repository using SQL seeds (INSERT statements) or migrations.

This article covers the following:

  • Problems that occur when managing category values solely within environments
  • The pros and cons of managing them on the code side (including enums and SQL seeds)
  • Guidelines for a realistic separation of concerns

1. Issues with Managing Category Values Solely in Environments (QA/Staging)

1-1. Differences between environments are likely to occur

When managing category values depending on the environment, the following inconsistencies inevitably arise:

  • Category values present in QA but not in production
  • Production has received updates, but the development environment remains on older versions
  • Someone added a new category value only to their local environment, and it is not shared

As a result, this often leads to:

  • Production failures due to missing category values that worked in QA
  • Unstable behavior caused by "environment-dependent code"

1-2. Specifications cannot be verified through code reviews

If category values are not declared on the code side:

  • It is impossible to tell which category values are assumed
  • The "prerequisites for existence" become a black box
  • Review omissions occur even if additions or changes to category values affect the logic

In other words, the development process relies on implicit knowledge.

1-3. Reproducibility cannot be guaranteed in testing

When initializing a test DB, if category values are DB-dependent, the following issues arise:

  • Maintenance of initial data loading scripts becomes necessary
  • Discrepancies in category values between local, CI, and staging occur
  • Tests for "unknown category values" cannot be written

Especially when performing tests using an actual DB, the lack of category values makes it difficult to stabilize the test environment.

1-4. Category value discrepancies are frequent causes of production incidents

Typical troubles include:

  • "The category value was not registered in production"
  • "An old value remained only in staging"

Unless master data management is systematized, environmental differences easily surface as "unexplained bugs," which is the most painful aspect.

2. Benefits of Holding Category Values on the Product Code Side

2-1. Specifications are unified

Category values are essentially information belonging to "specifications."

  • Storing them as enums/consts in the code
  • Loading them via migrations or SQL seeds (INSERT statements)

It is important that both are "managed in the repository." This ensures that specifications and implementation remain consistent and avoids dependency on individuals.

Significant benefits in AI development

In recent years, the use of generative AI and Copilot tools in code development has increased. When leveraging such AI, having category values included as code within the project significantly affects accuracy.

Reasons include the following:

  • Significantly increased context available to AI

    • In environments where AI can read IDE files, it can understand specifications directly from enum/const definitions
    • If the database is the only source of information, the AI is forced to guess the "meaning of the category value"
  • Improvement in prompt accuracy

    • If "what status is" is clearly defined, the AI can generate accurate code
    • If a value exists only in the DB, the inquirer must first explain it to the AI
  • Safety during automated corrections and refactoring

    • If category values exist as enums in the code, the IDE can perform type checking
    • When AI suggests code fixes, type-safe changes are easier to accept
    • The IDE can point out bugs (typos, forgotten deletions) that reference non-existent category values

To explain with a concrete example, if the following enum definition exists in the project:

public enum OrderStatus {
    PENDING("Pending"),
    CONFIRMED("Confirmed"),
    SHIPPED("Shipped"),
    DELIVERED("Delivered"),
    CANCELLED("Cancelled");
    
    private final String label;
    // ...
}

If the same content is managed as a SQL seed (INSERT statement), it can be written like this:

INSERT INTO order_statuses (status_code, status_name, sort_order, is_active) VALUES
('PENDING',   'Pending',   10, 1),
('CONFIRMED', 'Confirmed', 20, 1),
('SHIPPED',   'Shipped',   30, 1),
('DELIVERED', 'Delivered', 40, 1),
('CANCELLED', 'Cancelled', 50, 1);

If such SQL is managed in the repository as a migration or seed file, it can be treated as a reviewable specification rather than "values only in the DB."

AI automatically understands:

  • There are only five states
  • Constraints on state transitions (e.g., is a jump from PENDING to CANCELLED possible?)
  • Whether label display is multi-language compatible or fixed

Conversely, if this information exists only in the DB, the developer must explain the "OrderStatus table definition and transition rules" in the prompt before asking the AI, which increases effort and lowers accuracy.

In summary, in the AI development era, "holding category values on the code side" brings several times more efficiency than not doing so.

  • Simultaneous management of documentation and code
    • By placing it in the same repository as the code in Markdown, it is easier to synchronize specification changes and implementation
    • If the environment allows the AI to reference both documents and code, accuracy improves dramatically

2-2. Improves test reproducibility and automation

Defining category values on the code side allows for automatic generation of a DB with the same initial state, both locally and in CI, which provides benefits such as:

  • High test reproducibility
  • Automatic seeding in the CI/CD pipeline
  • Ability to test error behavior with unknown category values

2-3. Enables automation of master loading (DB seeding)

With code-side management, you can automate the following in CI/CD:

  • Initial loading of category values
  • Difference detection
  • Migrations when updating master data

This largely prevents environmental discrepancies.

3. Disadvantages of Holding Values on the Code Side

3-1. Master data change procedures become slightly heavier

When including these in product code, any change inevitably requires the following steps:

  • PR → Review → Deploy

3-2. Compatibility issues with frequently changing operational master data

Data that operational staff need to change on the fly, such as campaign settings or UI display labels, are not suitable for code-based management.

3-3. Tendency for bloat if everything is converted to code

Designing with a "put everything into code" approach can conversely increase complexity, so appropriate categorization based on the nature of the category values is necessary.

4. Guidelines for Realistic Separation of Concerns

Category values that should be held in code

  • Fixed as specifications
  • Logic would break if they changed
  • Shared with other teams or services
  • Required for testing
  • Intended for development/maintenance using AI

Examples: Transaction types, statuses, category codes, role definitions, etc.

Category values that can be managed in the DB environment

  • Values frequently changed by business operations
  • Simple display content
  • Master data that does not affect logic
  • Values that users need to add or change themselves

Examples: Display labels, campaign types, auxiliary master data information, dynamic category lists

Note: "Managed in the DB" does not mean "only placed in the DB"; if they are managed in the repository as SQL seeds or migrations, they are included in what this document calls "code-side management," making problems less likely to occur.

Checklist

If you are unsure whether to convert category values to code, judging based on the following perspectives makes it easier to organize:

  • Do value changes affect business logic or conditional criteria?
  • Is it necessary to guarantee the comprehensiveness of category values in tests?
  • Is it necessary to share them with the same meaning across other teams or services?
  • Is it necessary to retain change history in a reviewable format?

If multiple points apply, managing them as enums/consts or SQL seeds in the repository is safer.

5. Importance of Category Value Management in the AI Era

AI adoption in development workplaces has accelerated over the past few years. Within this context, the method of managing category values is no longer just about operational efficiency—it is directly linked to how much development productivity improves through collaboration with AI.

Key points:

  • Coded enums/consts → It is not uncommon for AI accuracy to improve by 3–5 times

    • No need for the effort of explaining "what is the Order status" in prompts
    • Correction costs also decrease because you receive type-safe suggestions
  • Integration of documentation + code + tests into the same repository

    • If AI can reference the entire project, time spent on knowledge reconstruction is close to zero
    • Everything is available in the IDE context window during local development
  • Reliable automated refactoring and version upgrades

    • IDE refactoring tools or AI code correction suggestions work accurately when deleting or adding category values
    • If values only exist in the DB, identifying "where they are referenced" becomes a manual task

6. Conclusion

  • Managing category values dependent on the environment makes environmental discrepancies, test inconsistencies, black-boxed reviews, and production incidents more likely to occur.
  • Category values are "specifications," and there are many areas that should be defined on the product code side.
  • Especially when utilizing AI for development, placing category values on the code side is almost essential.
    • Reduces prompt effort and dramatically improves accuracy.
    • Reduces correction costs because IDEs, tools, and AI can suggest "safe changes."
  • On the other hand, operational values that change frequently are better suited for DB management.
  • In reality, the separation of "category values that break logic are on the code side, others are on the DB side" is the safest, and it is recommended especially for new development environments.

Discussion