Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

After loading SiteSettings in migration, exception is being raised #17529

Closed
C-Coretex opened this issue Feb 25, 2025 · 2 comments
Closed

After loading SiteSettings in migration, exception is being raised #17529

C-Coretex opened this issue Feb 25, 2025 · 2 comments
Labels

Comments

@C-Coretex
Copy link

C-Coretex commented Feb 25, 2025

Describe the bug

I need to update SiteSettings from migration. First problem I have encountered, await siteService.LoadSiteSettingsAsync() throws exception Microsoft.Data.Sqlite.SqliteException: 'SQLite Error 1: 'no such table: ContentItemIndex'.'
As I understood, migrations are being run before some of the tables are created. I decided to write this piece of code:

OrchardCore.Settings.ISite siteSettings;
try
{
    siteSettings = await siteService.LoadSiteSettingsAsync();
}
catch
{
    //if it's first migration, it's possible that SiteSettings are not yet initialized.
    //Then, we are creating the settings by ourselves.
    siteSettings = new SiteSettings();
}

siteSettings.Alter<LocalizationSettings>("LocalizationSettings", localizationSettings =>
{
    localizationSettings.DefaultCulture = "en";
    localizationSettings.SupportedCultures = ["ru", "en", "lv"];
});

but if siteService.LoadSiteSettingsAsync is being used, I'm getting such exception (when the migration is already finished)

fail: OrchardCore.Setup.Services.SetupService[0]
      Unable to import a recipe during setup.
      OrchardCore.Recipes.Models.RecipeExecutionException: The document with Id '3' and type 'OrchardCore.ContentManagement.Metadata.Records.ContentDefinitionRecord, OrchardCore.ContentManagement.Abstractions' could not be updated as it has been changed by another process.
       ---> YesSql.ConcurrencyException: The document with Id '3' and type 'OrchardCore.ContentManagement.Metadata.Records.ContentDefinitionRecord, OrchardCore.ContentManagement.Abstractions' could not be updated as it has been changed by another process.
         at YesSql.Commands.UpdateDocumentCommand.ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger)
         at YesSql.Session.FlushInternalAsync(Boolean saving)
         at YesSql.Session.FlushInternalAsync(Boolean saving)
         at YesSql.Services.DefaultQuery.Query`1.FirstOrDefaultImpl()
         at OrchardCore.Data.Documents.DocumentStore.GetOrCreateImmutableAsync[T](Func`1 factoryAsync)
         at OrchardCore.Documents.DocumentManager`1.GetOrCreateImmutableAsync(Func`1 factoryAsync)
         at OrchardCore.Settings.Services.SiteService.GetSiteSettingsAsync()
         at OrchardCore.Settings.SiteServiceExtensions.GetSettingsAsync[T](ISiteService siteService)
         at OrchardCore.Media.Processing.MediaTokenSettingsUpdater.ActivatedAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.<>c.<<ActivateShellInternalAsync>b__43_0>d.MoveNext()
      --- End of stack trace from previous location ---
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.ActivateShellInternalAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.ActivateShellInternalAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Recipes.Services.RecipeExecutor.ExecuteStepAsync(RecipeExecutionContext recipeStep)
         at OrchardCore.Recipes.Services.RecipeExecutor.ExecuteAsync(String executionId, RecipeDescriptor recipeDescriptor, IDictionary`2 environment, CancellationToken cancellationToken)
         --- End of inner exception stack trace ---
         at OrchardCore.Recipes.Services.RecipeExecutor.ExecuteAsync(String executionId, RecipeDescriptor recipeDescriptor, IDictionary`2 environment, CancellationToken cancellationToken)
         at OrchardCore.Recipes.Services.RecipeExecutor.ExecuteAsync(String executionId, RecipeDescriptor recipeDescriptor, IDictionary`2 environment, CancellationToken cancellationToken)
         at OrchardCore.Recipes.Services.RecipeExecutor.ExecuteAsync(String executionId, RecipeDescriptor recipeDescriptor, IDictionary`2 environment, CancellationToken cancellationToken)
         at OrchardCore.Setup.Services.SetupService.SetupInternalAsync(SetupContext context)

and

fail: Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware[1]
      An unhandled exception has occurred while executing the request.
      YesSql.ConcurrencyException: The document with Id '9' and type 'OrchardCore.ContentManagement.Metadata.Records.ContentDefinitionRecord, OrchardCore.ContentManagement.Abstractions' could not be updated as it has been changed by another process.
         at YesSql.Commands.UpdateDocumentCommand.ExecuteAsync(DbConnection connection, DbTransaction transaction, ISqlDialect dialect, ILogger logger)
         at YesSql.Session.FlushInternalAsync(Boolean saving)
         at YesSql.Session.FlushInternalAsync(Boolean saving)
         at YesSql.Services.DefaultQuery.Query`1.FirstOrDefaultImpl()
         at OrchardCore.Data.Documents.DocumentStore.GetOrCreateImmutableAsync[T](Func`1 factoryAsync)
         at OrchardCore.Documents.DocumentManager`1.GetOrCreateImmutableAsync(Func`1 factoryAsync)
         at OrchardCore.Settings.Services.SiteService.GetSiteSettingsAsync()
         at OrchardCore.Settings.SiteServiceExtensions.GetSettingsAsync[T](ISiteService siteService)
         at OrchardCore.Media.Processing.MediaTokenSettingsUpdater.ActivatedAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.<>c.<<ActivateShellInternalAsync>b__43_0>d.MoveNext()
      --- End of stack trace from previous location ---
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.ActivateShellInternalAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.ActivateShellInternalAsync()
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Setup.Services.SetupService.SetupInternalAsync(SetupContext context)
         at OrchardCore.Setup.Services.SetupService.SetupAsync(SetupContext context)
         at OrchardCore.Setup.Services.SetupService.SetupAsync(SetupContext context)
         at OrchardCore.AutoSetup.AutoSetupMiddleware.SetupTenantAsync(ISetupService setupService, TenantSetupOptions setupOptions, ShellSettings shellSettings)
         at OrchardCore.AutoSetup.AutoSetupMiddleware.InvokeAsync(HttpContext httpContext)
         at OrchardCore.AutoSetup.AutoSetupMiddleware.InvokeAsync(HttpContext httpContext)
         at Microsoft.AspNetCore.Localization.RequestLocalizationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
         at OrchardCore.Modules.ModularTenantRouterMiddleware.Invoke(HttpContext httpContext)
         at OrchardCore.Modules.ModularTenantContainerMiddleware.<>c__DisplayClass4_0.<<Invoke>b__0>d.MoveNext()
      --- End of stack trace from previous location ---
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Environment.Shell.Scope.ShellScope.UsingAsync(Func`2 execute, Boolean activateShell)
         at OrchardCore.Modules.ModularTenantContainerMiddleware.Invoke(HttpContext httpContext)
         at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
         at Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddlewareImpl.Invoke(HttpContext context)

Program.cs:

builder.Services.AddOrchardCms()
    .AddSetupFeatures("OrchardCore.AutoSetup")
    .EnableFeature("OrchardCore.Contents")
    .EnableFeature("OrchardCore.ContentTypes")
    .EnableFeature("OrchardCore.Media")
    .EnableFeature("OrchardCore.Localization")
    .EnableFeature("OrchardCore.ContentLocalization")
    .EnableFeature("OrchardCore.Autoroute")
    .ConfigureServices(services =>
    {
        // Custom parts must be registered
        services.AddContentPart<IndexPagePart>();
        services.AddDataMigration<Migrations>();
    });

Orchard Core version

OrchardCore.Application.Cms.Core.Targets 2.1.6

To Reproduce

Create a migration with code I've provided and run it

Expected behavior

siteService.LoadSiteSettingsAsync should not have to break the initial setup

Logs and screenshots

Image

Image

Image

P.S.
Could be useful to have a possibility to run Migrations after all the orchard initialization is complete

Copy link
Contributor

Thank you for submitting your first issue, awesome! 🚀 We're thrilled to receive your input. If you haven't completed the template yet, please take a moment to do so. This ensures that we fully understand your feature request or bug report. On what happens next, see the docs.

If you like Orchard Core, please star our repo and join our community channels.

@C-Coretex C-Coretex changed the title When trying to update SiteSettings in migration, getting exception After loading SiteSettings in migration, exception is being raised Feb 25, 2025
@C-Coretex
Copy link
Author

Ok, siteSettings must be changed at the start of the migration. In my case I just moved the siteService.LoadSiteSettingsAsync to the top and it stopped throwing the exception

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant