Skip to content

Commit

Permalink
Add migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
andchiind committed Jul 27, 2023
1 parent a91e614 commit 00118e1
Show file tree
Hide file tree
Showing 10 changed files with 3,006 additions and 95 deletions.
2 changes: 1 addition & 1 deletion backend/api/Controllers/AreaController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ public async Task<ActionResult<MapMetadata>> GetMapMetadata([FromRoute] string i
};
try
{
mapMetadata = await _mapService.ChooseMapFromPositions(positions, area.Deck.Plant.Installation.InstallationCode);
mapMetadata = await _mapService.ChooseMapFromPositions(positions, area.Installation.InstallationCode);
}
catch (RequestFailedException e)
{
Expand Down
31 changes: 22 additions & 9 deletions backend/api/Database/Context/FlotillaDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
);
}
);
//missionRunEntity.HasOne(missionRun => missionRun.MissionDefinition);
}
);

modelBuilder.Entity<MissionDefinition>().HasOne(m => m.Source);
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.MapMetadata).OwnsOne(t => t.TransformationMatrices);
modelBuilder.Entity<MissionRun>().OwnsOne(m => m.MapMetadata).OwnsOne(b => b.Boundary);
//modelBuilder.Entity<MissionDefinition>().HasOne(m => m.LastRun).WithOne(m => m.MissionDefinition).HasForeignKey<MissionRun>(m => m.Id);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Orientation);
modelBuilder.Entity<Robot>().OwnsOne(r => r.Pose).OwnsOne(p => p.Position);
modelBuilder.Entity<Robot>().OwnsMany(r => r.VideoStreams);
Expand All @@ -73,12 +70,12 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
poseBuilder.OwnsOne(pose => pose.Position);
poseBuilder.OwnsOne(pose => pose.Orientation);
});
modelBuilder.Entity<Area>().HasOne(a => a.Deck);
modelBuilder.Entity<Area>().HasOne(a => a.Installation);
modelBuilder.Entity<Area>().HasOne(a => a.Plant);
modelBuilder.Entity<Deck>().HasOne(d => d.Plant);
modelBuilder.Entity<Deck>().HasOne(d => d.Installation);
modelBuilder.Entity<Plant>().HasOne(a => a.Installation);
modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany();
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany();
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany();
modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany();
modelBuilder.Entity<Plant>().HasOne(a => a.Installation).WithMany();

modelBuilder.Entity<SafePosition>().OwnsOne(s => s.Pose, poseBuilder =>
{
Expand All @@ -92,6 +89,22 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
// There can only be one unique installation and plant shortname
modelBuilder.Entity<Installation>().HasIndex(a => new { a.InstallationCode }).IsUnique();
modelBuilder.Entity<Plant>().HasIndex(a => new { a.PlantCode }).IsUnique();

modelBuilder.Entity<Area>().HasOne(a => a.Deck).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Area>().HasOne(a => a.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Plant).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Deck>().HasOne(d => d.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);
modelBuilder.Entity<Plant>().HasOne(p => p.Installation).WithMany().OnDelete(DeleteBehavior.Restrict);

foreach (var entityType in modelBuilder.Model.GetEntityTypes())
{
entityType.SetTableName(entityType.DisplayName());
entityType.GetForeignKeys()
.Where(fk => !fk.IsOwnership && fk.DeleteBehavior == DeleteBehavior.Cascade)
.ToList()
.ForEach(fk => fk.DeleteBehavior = DeleteBehavior.Restrict);
}
}

// SQLite does not have proper support for DateTimeOffset via Entity Framework Core, see the limitations
Expand Down
12 changes: 6 additions & 6 deletions backend/api/Database/Context/InitDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ private static List<MissionDefinition> GetMissionDefinitions()
{
Id = Guid.NewGuid().ToString(),
Name = "Placeholder Mission 1",
InstallationCode = areas[0].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[0].Installation.InstallationCode,
Area = areas[0],
Source = sources[0],
Comment = "Interesting comment",
Expand All @@ -195,7 +195,7 @@ private static List<MissionDefinition> GetMissionDefinitions()
{
Id = Guid.NewGuid().ToString(),
Name = "Placeholder Mission 2",
InstallationCode = areas[1].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Source = sources[1],
InspectionFrequency = new DateTime().AddDays(7) - new DateTime(),
Expand All @@ -206,7 +206,7 @@ private static List<MissionDefinition> GetMissionDefinitions()
{
Id = Guid.NewGuid().ToString(),
Name = "Placeholder Mission 3",
InstallationCode = areas[1].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
Source = sources[1],
LastRun = null
Expand All @@ -221,7 +221,7 @@ private static List<MissionRun> GetMissionRuns()
{
Name = "Placeholder Mission 1",
Robot = robots[0],
InstallationCode = areas[0].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[0].Installation.InstallationCode,
Area = areas[0],
MissionId = missionDefinitions[0].Id,
Status = MissionStatus.Successful,
Expand All @@ -234,7 +234,7 @@ private static List<MissionRun> GetMissionRuns()
{
Name = "Placeholder Mission 2",
Robot = robots[1],
InstallationCode = areas[1].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
MissionId = missionDefinitions[0].Id,
Status = MissionStatus.Successful,
Expand All @@ -248,7 +248,7 @@ private static List<MissionRun> GetMissionRuns()
{
Name = "Placeholder Mission 3",
Robot = robots[2],
InstallationCode = areas[1].Deck.Plant.Installation.InstallationCode,
InstallationCode = areas[1].Installation.InstallationCode,
Area = areas[1],
MissionId = missionDefinitions[1].Id,
Status = MissionStatus.Successful,
Expand Down
6 changes: 2 additions & 4 deletions backend/api/Database/Models/Deck.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ public class Deck
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }

[Required]
public virtual Plant Plant { get; set; }
public virtual Plant? Plant { get; set; }

[Required]
public virtual Installation Installation { get; set; }
public virtual Installation? Installation { get; set; }

[Required]
[MaxLength(200)]
Expand Down
3 changes: 1 addition & 2 deletions backend/api/Database/Models/Plant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ public class Plant
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public string Id { get; set; }

[Required]
public virtual Installation Installation { get; set; }
public virtual Installation? Installation { get; set; }

[Required]
[MaxLength(10)]
Expand Down
Loading

0 comments on commit 00118e1

Please sign in to comment.