umbraco / Umbraco-CMS

Umbraco is a free and open source .NET content management system helping you deliver delightful digital experiences.
https://umbraco.com
MIT License
4.42k stars 2.67k forks source link

The last database migration always fires at the system startup #17109

Open piotrbach opened 5 days ago

piotrbach commented 5 days ago

Which Umbraco version are you using? (Please write the exact version, example: 10.1.0)

13.5.0

Bug summary

After updating the system from 11.5.0 to 13.5.0, the last migration consistently starts when Umbraco is launched.

This behavior could have serious consequences.

We did not observe this issue in version 11.5.0, where migrations were created.

The problem is evident in the system logs of Umbraco 13.5.0:

[10:08:04 INF] Acquiring MainDom. [10:08:04 INF] Acquired MainDom. [10:08:05 INF] Starting 'DefaultDatabaseMigrationPlan'... [10:08:05 INF] At AddContentTypeIdColumnUmbracoSynchronizedMfCsEntityTableMigration-state [10:08:05 INF] Done [10:08:05 INF] The NuCache IgnoreLocalDb setting is set to 'False' by appsettings.json.

cc: @wtct

Specifics

The migration runs even though the migration was completed more than a year ago, as can be seen in the screenshot from dbo.umbracoKeyValue table:

db-migrations-umbraco

Below is the code that initializes the migration:

public class DatabaseUpgradeComponent: IComponent
{
    private readonly ICoreScopeProvider _scopeProvider;
    private readonly IMigrationPlanExecutor _migrationPlanExecutor;
    private readonly IKeyValueService _keyValueService;
    private readonly IRuntimeState _runtimeState;

    const string MigrationPlanName = "DefaultDatabaseMigrationPlan";

    public DatabaseUpgradeComponent(
        ICoreScopeProvider scopeProvider, 
        IMigrationPlanExecutor migrationPlanExecutor, 
        IKeyValueService keyValueService,
        IRuntimeState runtimeState
        )
    {
        _scopeProvider = scopeProvider;
        _migrationPlanExecutor = migrationPlanExecutor;
        _keyValueService = keyValueService;
        _runtimeState = runtimeState;
    }

    public void Initialize()
    {
        if (_runtimeState.Level < RuntimeLevel.Run)
            return;

        var migrationPlan = new MigrationPlan(MigrationPlanName);

        migrationPlan.From(string.Empty)
            .To<CreateUmbracoSynchronizedCadEntityTableMigration>(CreateUmbracoSynchronizedCadEntityTableMigration.MigrationTargetState)
            .To<CreateUmbracoSynchronizedMfCsEntityTableMigration>(CreateUmbracoSynchronizedMfCsEntityTableMigration.MigrationTargetState)
            .To<AddDeleteColumnsUmbracoSynchronizedMfCsEntityTableMigration>(AddDeleteColumnsUmbracoSynchronizedMfCsEntityTableMigration.MigrationTargetState)
            .To<AddContentTypeIdColumnUmbracoSynchronizedMfCsEntityTableMigration>(AddContentTypeIdColumnUmbracoSynchronizedMfCsEntityTableMigration.MigrationTargetState);

        var upgrader = new Upgrader(migrationPlan);

        upgrader.Execute(_migrationPlanExecutor, _scopeProvider, _keyValueService);
    }

    public void Terminate() { }
}

Steps to reproduce

  1. At least two migrations should be created and should be completed.
  2. Umbraco should be restarted and the startup logs should be reviewed
  3. The last migration is performed every time the system is started

Expected result / actual result

Migrations should be executed only once.

github-actions[bot] commented 5 days ago

Hi there @piotrbach!

Firstly, a big thank you for raising this issue. Every piece of feedback we receive helps us to make Umbraco better.

We really appreciate your patience while we wait for our team to have a look at this but we wanted to let you know that we see this and share with you the plan for what comes next.

We wish we could work with everyone directly and assess your issue immediately but we're in the fortunate position of having lots of contributions to work with and only a few humans who are able to do it. We are making progress though and in the meantime, we will keep you in the loop and let you know when we have any questions.

Thanks, from your friendly Umbraco GitHub bot :robot: :slightly_smiling_face:

wtct commented 5 days ago

It's worth to mention that we haven't noticed such an issue in the v13.4.1, so this is probably a fresh bug.