Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 86 additions & 30 deletions src/Caster.Api/Data/CasterContext.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,107 @@
// Copyright 2021 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Caster.Api.Domain.Models;
using Caster.Api.Infrastructure.Extensions;
using Microsoft.EntityFrameworkCore;

namespace Caster.Api.Data
namespace Caster.Api.Data;

public partial class CasterContext : DbContext
{
public partial class CasterContext : DbContext
public List<Entry> Entries { get; set; } = new List<Entry>();

private DbContextOptions<CasterContext> _options;

public CasterContext(DbContextOptions<CasterContext> options) : base(options)
{
_options = options;
}

public DbSet<Project> Projects { get; set; }
public DbSet<Directory> Directories { get; set; }
public DbSet<File> Files { get; set; }
public DbSet<FileVersion> FileVersions { get; set; }
public DbSet<Workspace> Workspaces { get; set; }
public DbSet<Run> Runs { get; set; }
public DbSet<Plan> Plans { get; set; }
public DbSet<Apply> Applies { get; set; }
public DbSet<RemovedResource> RemovedResources { get; set; }
public DbSet<Module> Modules { get; set; }
public DbSet<ModuleVersion> ModuleVersions { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserPermission> UserPermissions { get; set; }
public DbSet<Host> Hosts { get; set; }
public DbSet<HostMachine> HostMachines { get; set; }
public DbSet<Design> Designs { get; set; }
public DbSet<DesignModule> DesignModules { get; set; }
public DbSet<Variable> Variables { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
private DbContextOptions<CasterContext> _options;
modelBuilder.ApplyConfigurations();

public CasterContext(DbContextOptions<CasterContext> options) : base(options) {
_options = options;
// Apply PostgreSQL specific options
if (Database.IsNpgsql())
{
modelBuilder.AddPostgresUUIDGeneration();
modelBuilder.UsePostgresCasing();
}
}

public DbSet<Project> Projects { get; set; }
public DbSet<Directory> Directories { get; set; }
public DbSet<File> Files { get; set; }
public DbSet<FileVersion> FileVersions { get; set; }
public DbSet<Workspace> Workspaces { get; set; }
public DbSet<Run> Runs { get; set; }
public DbSet<Plan> Plans { get; set; }
public DbSet<Apply> Applies { get; set; }
public DbSet<RemovedResource> RemovedResources { get; set; }
public DbSet<Module> Modules { get; set; }
public DbSet<ModuleVersion> ModuleVersions { get; set; }
public DbSet<Permission> Permissions { get; set; }
public DbSet<User> Users { get; set; }
public DbSet<UserPermission> UserPermissions { get; set; }
public DbSet<Host> Hosts { get; set; }
public DbSet<HostMachine> HostMachines { get; set; }

protected override void OnModelCreating(ModelBuilder modelBuilder)
public override async Task<int> SaveChangesAsync(CancellationToken ct = default)
{
SaveEntries();
return await base.SaveChangesAsync(ct);
}

/// <summary>
/// keep track of changes across multiple savechanges in a transaction, without duplicates
/// </summary>
private void SaveEntries()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is very cool. I like it!

{
foreach (var entry in ChangeTracker.Entries())
{
modelBuilder.ApplyConfigurations();
// find value of id property
var id = entry.Properties
.FirstOrDefault(x =>
x.Metadata.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAdd)?.CurrentValue;

// Apply PostgreSQL specific options
if (Database.IsNpgsql())
// find matching existing entry, if any
var e = Entries.FirstOrDefault(x => x.Properties.FirstOrDefault(y =>
y.Metadata.ValueGenerated == Microsoft.EntityFrameworkCore.Metadata.ValueGenerated.OnAdd)?.CurrentValue == id);

if (e != null)
{
// if entry already exists, mark which properties were previously modified,
// remove old entry and add new one, to avoid duplicates
var modifiedProperties = e.Properties
.Where(x => x.IsModified)
.Select(x => x.Metadata.Name)
.ToArray();

var newEntry = new Entry(entry);

foreach (var property in newEntry.Properties)
{
if (modifiedProperties.Contains(property.Metadata.Name))
{
property.IsModified = true;
}
}

Entries.Remove(e);
Entries.Add(newEntry);
}
else
{
modelBuilder.AddPostgresUUIDGeneration();
modelBuilder.UsePostgresCasing();
Entries.Add(new Entry(entry));
}
}
}
}
}
23 changes: 23 additions & 0 deletions src/Caster.Api/Data/Entry.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2021 Carnegie Mellon University. All Rights Reserved.
// Released under a MIT (SEI)-style license. See LICENSE.md in the project root for license information.

using System;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;

namespace Caster.Api.Data;

public class Entry
{
public Entry(EntityEntry entry)
{
Entity = entry.Entity;
State = entry.State;
Properties = entry.Properties;
}

public object Entity { get; set; }
public EntityState State { get; set; }
public IEnumerable<PropertyEntry> Properties { get; set; }
}
Loading