Skip to content

Fix Raw SQL Issues and Refactor DatabaseDataCreator (closes #21607)#21590

Open
idseefeld wants to merge 30 commits intoumbraco:mainfrom
idseefeld:v173/21516-DatabaseDataCreator-refactor
Open

Fix Raw SQL Issues and Refactor DatabaseDataCreator (closes #21607)#21590
idseefeld wants to merge 30 commits intoumbraco:mainfrom
idseefeld:v173/21516-DatabaseDataCreator-refactor

Conversation

@idseefeld
Copy link
Contributor

@idseefeld idseefeld commented Jan 30, 2026

Refactoring Hint

This looks like another case of working just for CodeScene, but actually I did this before I was aware of CodeScene.
And I think these would be acceptable for code review 😥

Details to Help in Code Review

There seem to be many changes, but actually there are only 4 principals in many places.
So, what I basically did:

  1. Updated method private void ConditionalInsert<TDto>(string configKey, string id, TDto dto) by reducing input parameters).

  2. Added method private object Insert<T>(T poco).
    That reduces constant input parameter e.g.:

    from
    
    _database.Insert(
          Constants.DatabaseSchema.Tables.KeyValue, 
          "key", 
          false, 
          new KeyValueDto(...
    
    to 
    
    `Insert(new KeyValueDto(...
    
  3. Replace constants by table properties e.g.: Constants.DatabaseSchema.Tables.Node becomes NodeDto.TableName. I think this makes the code cleaner, more OO 😉

  4. _database.Insert(Constants.DatabaseSchema.Tables.Lock, "id", false, new LockDto { Id = Constants.Locks.Servers, Name = "Servers" }); becomes
    Insert(new LockDto { Id = Constants.Locks.Servers, Name = "Servers" });
    all in all 19 lines changed like this in method private void CreateLockData().

The refactoring decreases the total number of lines from 2758 to 2468 and removes a lot of repetitive code.
And it solves another issue for auto increment columns in PostgreSqlProvider and therefor it is another part of issue #20453.

I hope this summary makes it much easier to review the changes.

This is another PR for issue #20453.

@github-actions
Copy link

Hi there @idseefeld, thank you for this contribution! 👍

While we wait for one of the Core Collaborators team to have a look at your work, we wanted to let you know about that we have a checklist for some of the things we will consider during review:

  • It's clear what problem this is solving, there's a connected issue or a description of what the changes do and how to test them
  • The automated tests all pass (see "Checks" tab on this PR)
  • The level of security for this contribution is the same or improved
  • The level of performance for this contribution is the same or improved
  • Avoids creating breaking changes; note that behavioral changes might also be perceived as breaking
  • If this is a new feature, Umbraco HQ provided guidance on the implementation beforehand
  • 💡 The contribution looks original and the contributor is presumably allowed to share it

Don't worry if you got something wrong. We like to think of a pull request as the start of a conversation, we're happy to provide guidance on improving your contribution.

If you realize that you might want to make some changes then you can do that by adding new commits to the branch you created for this work and pushing new commits. They should then automatically show up as updates to this pull request.

Thanks, from your friendly Umbraco GitHub bot 🤖 🙂

@idseefeld idseefeld changed the title Fix Raw SQL Issues and Refactor DatabaseDataCreator for Code Health Fix Raw SQL Issues and Refactor DatabaseDataCreator Feb 2, 2026
@idseefeld idseefeld changed the title Fix Raw SQL Issues and Refactor DatabaseDataCreator Fix Raw SQL Issues and Refactor DatabaseDataCreator (closes #21607) Feb 3, 2026
@idseefeld idseefeld marked this pull request as ready for review February 3, 2026 20:38
Copilot AI review requested due to automatic review settings February 3, 2026 20:38
Copy link
Contributor

Copilot AI left a comment

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 refactors database seeding and database factory behavior to reduce raw NPoco insert boilerplate, align table-name usage with DTOs, and address PostgreSQL-specific issues around auto-increment/identity columns.

Changes:

  • Makes UmbracoDatabaseFactory.DbProviderFactory protected and CreateDatabaseInstance() protected virtual to allow extension and test customization of database creation.
  • Refactors DatabaseDataCreator to use DTO TableName constants, centralizes insert logic via a new generic Insert<T> helper using PocoData, and simplifies ConditionalInsert<TDto> to depend only on configuration and the DTO instance.
  • Fixes the content-type-allowed-content-type initialization to use ContentTypeAllowedContentTypeDto.TableName and updates the seed methods to rely on DTO-based inserts instead of hard-coded table/PK names, with additional logic to toggle auto-increment behavior based on database type.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/Umbraco.Infrastructure/Persistence/UmbracoDatabaseFactory.cs Exposes DbProviderFactory as protected and CreateDatabaseInstance as protected virtual so subclasses or tests can customize how UmbracoDatabase instances are created.
src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs Refactors install-time seed data creation to use DTO table constants and a shared Insert<T> based on PocoData, updates ConditionalInsert to no longer require table/PK names, corrects the content child-type table mapping to ContentTypeAllowedContentTypeDto.TableName, and introduces PostgreSQL-aware auto-increment handling for inserts.
Comments suppressed due to low confidence (3)

src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs:2425

        if (!alwaysInsert && installDefaultDataSettings?.InstallData == InstallDefaultDataOption.None)

src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs:2430

        if (!alwaysInsert && installDefaultDataSettings?.InstallData == InstallDefaultDataOption.Values &&

src/Umbraco.Infrastructure/Migrations/Install/DatabaseDataCreator.cs:2436

        if (!alwaysInsert && installDefaultDataSettings?.InstallData == InstallDefaultDataOption.ExceptValues &&

@idseefeld idseefeld marked this pull request as ready for review February 4, 2026 19:40
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