Matt Wise
10/21/2022, 2:37 PMMatt Wise
10/21/2022, 2:37 PMMatt Wise
10/21/2022, 2:37 PMKeyNotFoundException: The given key 'System.Collections.Generic.List1[Umbraco.Community.CSPManager.Models.CspDefinitionSource]' was not present in the dictionary.
Matt Wise
10/21/2022, 2:38 PMpublic sealed class InitialCspManagerMigration : MigrationBase
{
public const string MigrationKey = "csp-manager-init";
public InitialCspManagerMigration(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
if (!TableExists(nameof(CspDefinitionSource)))
{
Create.Table<CspDefinitionSource>().Do();
}
}
}
public sealed class PopulateDefaultBackOfficeCspMigration : MigrationBase
{
public const string MigrationKey = "csp-manager-default-backoffice";
public PopulateDefaultBackOfficeCspMigration(IMigrationContext context) : base(context)
{
}
protected override void Migrate()
{
if (!TableExists(nameof(CspDefinition)))
{
Create.Table<CspDefinition>().Do();
// Insert.IntoTable(nameof(CspDefinition)).Row(new CspDefinition
// {
// IsBackOffice = true, Enabled = false, Sources = CspConstants.DefaultBackOfficeCsp
// }).Do();
}
}
}
Matt Wise
10/21/2022, 2:38 PMDavid Brendel
10/21/2022, 2:43 PMMatt Wise
10/21/2022, 2:44 PMMatt Wise
10/21/2022, 2:44 PM[TableName((nameof(CspDefinition)))]
[PrimaryKey(nameof(Id), AutoIncrement = false)]
public class CspDefinition
{
public Guid Id { get; set; }
public bool Enabled { get; set; }
public bool ReportOnly { get; set; }
public bool IsBackOffice { get; set; }
[References(typeof(CspDefinitionSource))]
public List<CspDefinitionSource> Sources { get; set; } = new();
}
[TableName((nameof(CspDefinitionSource)))]
[PrimaryKey(new []{ nameof(DefinitionId), nameof(Source)})]
public class CspDefinitionSource
{
[References(typeof(CspDefinition))]
public Guid DefinitionId { get; set; }
public string Source { get; init; }
[SerializedColumn]
[SpecialDbType(SpecialDbTypes.NVARCHARMAX)]
public List<string> Directives { get; set; } = new();
}
David Brendel
10/21/2022, 2:47 PMDavid Brendel
10/21/2022, 2:50 PMMatt Wise
10/21/2022, 2:52 PMmigrationPlan.From(string.Empty)
.To<InitialCspManagerMigration>(InitialCspManagerMigration.MigrationKey)
.To<PopulateDefaultBackOfficeCspMigration>(PopulateDefaultBackOfficeCspMigration.MigrationKey);
Matt Wise
10/21/2022, 2:53 PMDavid Brendel
10/21/2022, 2:56 PMMatt Wise
10/21/2022, 2:56 PMpublic sealed class RunCspManagerMigration : INotificationHandler<UmbracoApplicationStartingNotification>
{
private readonly IMigrationPlanExecutor _migrationPlanExecutor;
private readonly ICoreScopeProvider _coreScopeProvider;
private readonly IKeyValueService _keyValueService;
private readonly IRuntimeState _runtimeState;
public RunCspManagerMigration(
ICoreScopeProvider coreScopeProvider,
IMigrationPlanExecutor migrationPlanExecutor,
IKeyValueService keyValueService,
IRuntimeState runtimeState)
{
_migrationPlanExecutor = migrationPlanExecutor;
_coreScopeProvider = coreScopeProvider;
_keyValueService = keyValueService;
_runtimeState = runtimeState;
}
public void Handle(UmbracoApplicationStartingNotification notification)
{
if (_runtimeState.Level < RuntimeLevel.Run)
{
return;
}
var migrationPlan = new MigrationPlan("CspManagerMigration");
// This is the steps we need to take
// Each step in the migration adds a unique value
migrationPlan.From(string.Empty)
.To<InitialCspManagerMigration>(InitialCspManagerMigration.MigrationKey)
.To<PopulateDefaultBackOfficeCspMigration>(PopulateDefaultBackOfficeCspMigration.MigrationKey);
// Go and upgrade our site (Will check if it needs to do the work or not)
// Based on the current/latest step
var upgrader = new Upgrader(migrationPlan);
upgrader.Execute(
_migrationPlanExecutor,
_coreScopeProvider,
_keyValueService);
}
}
Matt Wise
10/21/2022, 2:56 PMMatt Wise
10/21/2022, 2:58 PMDavid Brendel
10/21/2022, 3:01 PMMatt Wise
10/21/2022, 3:05 PM