Skip to content

Commit

Permalink
Update entity relationships and migration to make all DB providers ha…
Browse files Browse the repository at this point in the history
…ppy.
  • Loading branch information
bitbound committed Aug 1, 2023
1 parent b2e2430 commit 0b8fcc5
Show file tree
Hide file tree
Showing 14 changed files with 2,435 additions and 3,118 deletions.
104 changes: 66 additions & 38 deletions Server/Data/AppDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,30 @@ protected override void OnModelCreating(ModelBuilder builder)
.WithOne(x => x.Organization);
builder.Entity<Organization>()
.HasMany(x => x.DeviceGroups)
.WithOne(x => x.Organization);
.WithOne(x => x.Organization)
.OnDelete(DeleteBehavior.ClientCascade);
builder.Entity<Organization>()
.HasMany(x => x.InviteLinks)
.WithOne(x => x.Organization);
.HasMany(x => x.InviteLinks)
.WithOne(x => x.Organization);
builder.Entity<Organization>()
.HasMany(x => x.SharedFiles)
.WithOne(x => x.Organization)
.IsRequired(false);
.HasMany(x => x.SharedFiles)
.WithOne(x => x.Organization)
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);
builder.Entity<Organization>()
.HasMany(x => x.ApiTokens)
.WithOne(x => x.Organization);
.HasMany(x => x.ApiTokens)
.WithOne(x => x.Organization);
builder.Entity<Organization>()
.HasMany(x => x.Alerts)
.WithOne(x => x.Organization);
builder.Entity<Organization>()
.HasMany(x => x.ScriptRuns)
.WithOne(x => x.Organization);
.WithOne(x => x.Organization)
.OnDelete(DeleteBehavior.ClientCascade);
builder.Entity<Organization>()
.HasMany(x => x.ScriptSchedules)
.WithOne(x => x.Organization);
.WithOne(x => x.Organization)
.OnDelete(DeleteBehavior.ClientCascade);
builder.Entity<Organization>()
.HasMany(x => x.ScriptResults)
.WithOne(x => x.Organization);
Expand All @@ -103,29 +107,37 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<Organization>()
.HasOne(x => x.BrandingInfo)
.WithOne(x => x.Organization)
.HasForeignKey<BrandingInfo>(x => x.OrganizationId);
.HasForeignKey<BrandingInfo>(x => x.OrganizationId)
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);

builder.Entity<RemotelyUser>()
.HasOne(x => x.Organization)
.WithMany(x => x.RemotelyUsers);

builder.Entity<RemotelyUser>()
.HasMany(x => x.DeviceGroups)
.WithMany(x => x.Users);

builder.Entity<RemotelyUser>()
.HasMany(x => x.Alerts)
.WithOne(x => x.User);
.WithOne(x => x.User)
.OnDelete(DeleteBehavior.ClientCascade);

builder.Entity<RemotelyUser>()
.Property(x => x.UserOptions)
.HasConversion(
x => JsonSerializer.Serialize(x, jsonOptions),
x => JsonSerializer.Deserialize<RemotelyUserOptions>(x, jsonOptions));

builder.Entity<RemotelyUser>()
.HasMany(x => x.SavedScripts)
.WithOne(x => x.Creator);
.WithOne(x => x.Creator)
.HasForeignKey(x => x.CreatorId)
.OnDelete(DeleteBehavior.ClientCascade);

builder.Entity<RemotelyUser>()
.HasMany(x => x.ScriptSchedules)
.WithOne(x => x.Creator);
.WithOne(x => x.Creator)
.HasForeignKey(x => x.CreatorId)
.OnDelete(DeleteBehavior.ClientCascade);

builder.Entity<RemotelyUser>()
.HasIndex(x => x.UserName);
Expand All @@ -135,24 +147,32 @@ protected override void OnModelCreating(ModelBuilder builder)
.HasConversion(
x => JsonSerializer.Serialize(x, jsonOptions),
x => TryDeserializeProperty<List<Drive>>(x, jsonOptions));

builder.Entity<Device>()
.Property(x => x.Drives)
.Metadata.SetValueComparer(new ValueComparer<List<Drive>>(true));

builder.Entity<Device>()
.HasIndex(x => x.DeviceName);

builder.Entity<Device>()
.HasMany(x => x.Alerts)
.WithOne(x => x.Device);
.WithOne(x => x.Device)
.OnDelete(DeleteBehavior.ClientCascade);

builder.Entity<Device>()
.HasMany(x => x.ScriptRuns)
.WithMany(x => x.Devices);

builder.Entity<Device>()
.HasMany(x => x.ScriptSchedules)
.WithMany(x => x.Devices);

builder.Entity<Device>()
.HasOne(x => x.DeviceGroup)
.WithMany(x => x.Devices)
.IsRequired(false);
.HasMany(x => x.ScriptResults)
.WithOne(x => x.Device)
.OnDelete(DeleteBehavior.ClientCascade);

builder.Entity<Device>()
.Property(x => x.MacAddresses)
.HasConversion(
Expand All @@ -163,22 +183,23 @@ protected override void OnModelCreating(ModelBuilder builder)
builder.Entity<DeviceGroup>()
.HasMany(x => x.Devices)
.WithOne(x => x.DeviceGroup)
.IsRequired(false);
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);

builder.Entity<DeviceGroup>()
.HasMany(x => x.ScriptSchedules)
.WithMany(x => x.DeviceGroups);

builder.Entity<ScriptRun>()
.HasMany(x => x.Devices)
.WithMany(x => x.ScriptRuns);

builder.Entity<ScriptRun>()
.HasMany(x => x.Results)
.WithOne(x => x.ScriptRun)
.IsRequired(false);
builder.Entity<ScriptRun>()
.HasOne(x => x.SavedScript)
.WithMany(x => x.ScriptRuns)
.IsRequired(false);
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);


builder.Entity<ScriptResult>()
.Property(x => x.ErrorOutput)
Expand All @@ -195,18 +216,25 @@ protected override void OnModelCreating(ModelBuilder builder)
x => DeserializeStringArray(x, jsonOptions))
.Metadata
.SetValueComparer(_stringArrayComparer);
builder.Entity<ScriptResult>()
.HasOne(x => x.ScriptRun)
.WithMany(x => x.Results)
.IsRequired(false);
builder.Entity<ScriptResult>()
.HasOne(x => x.SavedScript)
.WithMany(x => x.ScriptResults)
.IsRequired(false);

builder.Entity<Alert>()
.HasOne(x => x.User)
.WithMany(x => x.Alerts);
builder.Entity<SavedScript>()
.HasMany(x => x.ScriptRuns)
.WithOne(x => x.SavedScript)
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);

builder.Entity<SavedScript>()
.HasMany(x => x.ScriptResults)
.WithOne(x => x.SavedScript)
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);

builder.Entity<ScriptSchedule>()
.HasMany(x => x.ScriptRuns)
.WithOne(x => x.Schedule)
.IsRequired(false)
.OnDelete(DeleteBehavior.ClientSetNull);


var isSqlite = Database.IsSqlite();
var isPostgres = Database.IsNpgsql();
Expand Down
Loading

0 comments on commit 0b8fcc5

Please sign in to comment.