Skip to content

Commit

Permalink
Don't crash on profile migration failures
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertBeekman committed Mar 13, 2024
1 parent da7a3e4 commit afc2256
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
4 changes: 2 additions & 2 deletions src/Artemis.Core/DryIoc/ContainerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public static class ContainerExtensions
/// <param name="container">The builder building the current container</param>
public static void RegisterCore(this IContainer container)
{
Assembly[] coreAssembly = {typeof(IArtemisService).Assembly};
Assembly[] storageAssembly = {typeof(IRepository).Assembly};
Assembly[] coreAssembly = [typeof(IArtemisService).Assembly];
Assembly[] storageAssembly = [typeof(IRepository).Assembly];

// Bind all services as singletons
container.RegisterMany(coreAssembly, type => type.IsAssignableTo<IArtemisService>(), Reuse.Singleton);
Expand Down
6 changes: 6 additions & 0 deletions src/Artemis.Storage/Repositories/ProfileCategoryRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public List<ProfileCategoryEntity> GetAll()

public ProfileCategoryEntity? Get(Guid id)
{
if (!_migratedProfiles)
{
profileRepository.MigrateProfiles();
_migratedProfiles = true;
}

using ArtemisDbContext dbContext = getContext();
return dbContext.ProfileCategories.Include(c => c.ProfileConfigurations).FirstOrDefault(c => c.Id == id);
}
Expand Down
46 changes: 28 additions & 18 deletions src/Artemis.Storage/Repositories/ProfileRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,22 +54,32 @@ public void MigrateProfiles()

foreach (RawProfileContainer rawProfileContainer in containers)
{
JsonObject? profileConfiguration = JsonNode.Parse(rawProfileContainer.ProfileConfiguration)?.AsObject();
JsonObject? profile = JsonNode.Parse(rawProfileContainer.Profile)?.AsObject();

if (profileConfiguration == null || profile == null)
throw new ArtemisStorageException("Failed to parse profile or profile configuration");

MigrateProfile(profileConfiguration, profile);
rawProfileContainer.Profile = profile.ToString();
rawProfileContainer.ProfileConfiguration = profileConfiguration.ToString();

// Write the updated containers back to the database
dbContext.Database.ExecuteSqlRaw(
"UPDATE ProfileContainers SET Profile = {0}, ProfileConfiguration = {1} WHERE Id = {2}",
rawProfileContainer.Profile,
rawProfileContainer.ProfileConfiguration,
rawProfileContainer.Id);
try
{
JsonObject? profileConfiguration = JsonNode.Parse(rawProfileContainer.ProfileConfiguration)?.AsObject();
JsonObject? profile = JsonNode.Parse(rawProfileContainer.Profile)?.AsObject();

if (profileConfiguration == null || profile == null)
{
logger.Error("Failed to parse profile or profile configuration of profile container {Id}", rawProfileContainer.Id);
continue;
}

MigrateProfile(profileConfiguration, profile);
rawProfileContainer.Profile = profile.ToString();
rawProfileContainer.ProfileConfiguration = profileConfiguration.ToString();

// Write the updated containers back to the database
dbContext.Database.ExecuteSqlRaw(
"UPDATE ProfileContainers SET Profile = {0}, ProfileConfiguration = {1} WHERE Id = {2}",
rawProfileContainer.Profile,
rawProfileContainer.ProfileConfiguration,
rawProfileContainer.Id);
}
catch (Exception e)
{
logger.Error(e, "Failed to migrate profile container {Id}", rawProfileContainer.Id);
}
}
}

Expand All @@ -84,8 +94,8 @@ public void MigrateProfile(JsonObject? configurationJson, JsonObject? profileJso
{
if (profileMigrator.Version <= configurationJson["Version"]!.GetValue<int>())
continue;
logger.Information("Migrating profile from version {OldVersion} to {NewVersion}", configurationJson["Version"], profileMigrator.Version);

logger.Information("Migrating profile '{Name}' from version {OldVersion} to {NewVersion}", configurationJson["Name"], configurationJson["Version"], profileMigrator.Version);

profileMigrator.Migrate(configurationJson, profileJson);
configurationJson["Version"] = profileMigrator.Version;
Expand Down
1 change: 1 addition & 0 deletions src/Artemis.Storage/StorageManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public static ArtemisDbContext CreateDbContext(string dataFolder)
return dbContext;

dbContext.Database.Migrate();
dbContext.Database.ExecuteSqlRaw("PRAGMA optimize");
_ranMigrations = true;

return dbContext;
Expand Down

0 comments on commit afc2256

Please sign in to comment.