diff --git a/src/Infrastructure.EntityFramework/Models/SeededData.cs b/src/Infrastructure.EntityFramework/Models/SeededData.cs new file mode 100644 index 0000000000..acd1662cda --- /dev/null +++ b/src/Infrastructure.EntityFramework/Models/SeededData.cs @@ -0,0 +1,9 @@ +namespace Bit.Infrastructure.EntityFramework.Models; + +public class SeededData +{ + public Guid Id { get; set; } + public required string RecipeName { get; set; } + public required string Data { get; set; } // JSON blob with entity tracking info + public DateTime CreationDate { get; set; } +} diff --git a/src/Infrastructure.EntityFramework/Repositories/DatabaseContext.cs b/src/Infrastructure.EntityFramework/Repositories/DatabaseContext.cs index 7446abdd97..ef238b5d2c 100644 --- a/src/Infrastructure.EntityFramework/Repositories/DatabaseContext.cs +++ b/src/Infrastructure.EntityFramework/Repositories/DatabaseContext.cs @@ -87,6 +87,7 @@ public class DatabaseContext : DbContext public DbSet OrganizationInstallations { get; set; } public DbSet OrganizationReports { get; set; } public DbSet OrganizationApplications { get; set; } + public DbSet SeededData { get; set; } protected override void OnModelCreating(ModelBuilder builder) { diff --git a/src/Sql/dbo/Tables/SeededData.sql b/src/Sql/dbo/Tables/SeededData.sql new file mode 100644 index 0000000000..b1e1d65821 --- /dev/null +++ b/src/Sql/dbo/Tables/SeededData.sql @@ -0,0 +1,6 @@ +CREATE TABLE [dbo].[SeededData] ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [RecipeName] NVARCHAR (MAX) NOT NULL, + [Data] NVARCHAR (MAX) NULL, + [CreationDate] DATETIME2 (7) NOT NULL, +); diff --git a/util/Migrator/DbScripts/2025-10-07_00_SeededData.sql b/util/Migrator/DbScripts/2025-10-07_00_SeededData.sql new file mode 100644 index 0000000000..c70091240e --- /dev/null +++ b/util/Migrator/DbScripts/2025-10-07_00_SeededData.sql @@ -0,0 +1,10 @@ +IF OBJECT_ID('dbo.SeededData') IS NULL +BEGIN + CREATE TABLE [dbo].[SeededData] ( + [Id] UNIQUEIDENTIFIER NOT NULL, + [RecipeName] NVARCHAR (MAX) NOT NULL, + [Data] NVARCHAR (MAX) NULL, + [CreationDate] DATETIME2 (7) NOT NULL, + ); +END +GO diff --git a/util/Seeder/RecipeResult.cs b/util/Seeder/RecipeResult.cs new file mode 100644 index 0000000000..b7b42b72ff --- /dev/null +++ b/util/Seeder/RecipeResult.cs @@ -0,0 +1,7 @@ +namespace Bit.Seeder; + +public class RecipeResult +{ + public required object Result { get; init; } + public Dictionary> TrackedEntities { get; init; } = new(); +} \ No newline at end of file diff --git a/util/Seeder/Recipes/OrganizationWithUsersRecipe.cs b/util/Seeder/Recipes/OrganizationWithUsersRecipe.cs index 5443e3d70e..723e95a0ef 100644 --- a/util/Seeder/Recipes/OrganizationWithUsersRecipe.cs +++ b/util/Seeder/Recipes/OrganizationWithUsersRecipe.cs @@ -1,15 +1,13 @@ -using Bit.Infrastructure.EntityFramework.AdminConsole.Models; -using Bit.Infrastructure.EntityFramework.Models; +using Bit.Infrastructure.EntityFramework.Models; using Bit.Infrastructure.EntityFramework.Repositories; using Bit.Seeder.Factories; using LinqToDB.EntityFrameworkCore; -using Microsoft.EntityFrameworkCore; namespace Bit.Seeder.Recipes; public class OrganizationWithUsersRecipe(DatabaseContext db) { - public Guid Seed(string name, int users, string domain) + public RecipeResult Seed(string name, int users, string domain) { var organization = OrganizationSeeder.CreateEnterprise(name, domain, users); var user = UserSeeder.CreateUser($"admin@{domain}"); @@ -34,20 +32,14 @@ public class OrganizationWithUsersRecipe(DatabaseContext db) db.BulkCopy(additionalUsers); db.BulkCopy(additionalOrgUsers); - return organization.Id; - } - - public void Destroy(Guid organizationId) - { - var organization = db.Organizations.Include(o => o.OrganizationUsers) - .ThenInclude(ou => ou.User).FirstOrDefault(p => p.Id == organizationId); - if (organization == null) + return new RecipeResult { - throw new Exception($"Organization with ID {organizationId} not found."); - } - var users = organization.OrganizationUsers.Select(u => u.User); - - db.RemoveRange(users); - db.Remove(organization); + Result = organization.Id, + TrackedEntities = new Dictionary> + { + ["Organization"] = [organization.Id], + ["User"] = [user.Id, .. additionalUsers.Select(u => u.Id)] + } + }; } } diff --git a/util/SeederApi/Controllers/SeedController.cs b/util/SeederApi/Controllers/SeedController.cs index 17a91f50cd..b547fac999 100644 --- a/util/SeederApi/Controllers/SeedController.cs +++ b/util/SeederApi/Controllers/SeedController.cs @@ -31,13 +31,14 @@ public class SeedController : Controller try { - var result = _recipeService.ExecuteRecipe(request.Template, request.Arguments); + var (result, seedId) = _recipeService.ExecuteRecipe(request.Template, request.Arguments); return Ok(new { Message = "Seed completed successfully", request.Template, - Result = result + Result = result, + SeedId = seedId }); } catch (RecipeNotFoundException ex) @@ -64,29 +65,24 @@ public class SeedController : Controller } } - [HttpDelete("/delete")] - public IActionResult Delete([FromBody] SeedRequestModel request) + [HttpDelete("/seed/{seedId}")] + public IActionResult Delete([FromRoute] Guid seedId) { - _logger.LogInformation("Deleting with template: {Template}", request.Template); + _logger.LogInformation("Deleting seeded data with ID: {SeedId}", seedId); try { - var result = _recipeService.DestroyRecipe(request.Template, request.Arguments); + var result = _recipeService.DestroyRecipe(seedId); return Ok(new { Message = "Delete completed successfully", - request.Template, Result = result }); } - catch (RecipeNotFoundException ex) - { - return NotFound(new { Error = ex.Message }); - } catch (RecipeExecutionException ex) { - _logger.LogError(ex, "Error executing recipe delete: {Template}", request.Template); + _logger.LogError(ex, "Error deleting seeded data: {SeedId}", seedId); return BadRequest(new { Error = ex.Message, @@ -95,7 +91,7 @@ public class SeedController : Controller } catch (Exception ex) { - _logger.LogError(ex, "Unexpected error deleting with template: {Template}", request.Template); + _logger.LogError(ex, "Unexpected error deleting seeded data: {SeedId}", seedId); return StatusCode(500, new { Error = "An unexpected error occurred while deleting", diff --git a/util/SeederApi/Services/IRecipeService.cs b/util/SeederApi/Services/IRecipeService.cs index b935eca82c..1347cf9b72 100644 --- a/util/SeederApi/Services/IRecipeService.cs +++ b/util/SeederApi/Services/IRecipeService.cs @@ -9,18 +9,16 @@ public interface IRecipeService /// /// The name of the recipe template (e.g., "OrganizationWithUsersRecipe") /// Optional JSON arguments to pass to the recipe's Seed method - /// The result returned by the recipe's Seed method + /// A tuple containing the result and optional seed ID for tracked entities /// Thrown when the recipe template is not found /// Thrown when there's an error executing the recipe - object? ExecuteRecipe(string templateName, JsonElement? arguments); + (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments); /// - /// Destroys data created by a recipe with the given template name and arguments. + /// Destroys data created by a recipe using the seeded data ID. /// - /// The name of the recipe template (e.g., "OrganizationWithUsersRecipe") - /// Optional JSON arguments to pass to the recipe's Destroy method - /// The result returned by the recipe's Destroy method - /// Thrown when the recipe template is not found - /// Thrown when there's an error executing the recipe - object? DestroyRecipe(string templateName, JsonElement? arguments); + /// The ID of the seeded data to destroy + /// The result of the destroy operation + /// Thrown when there's an error destroying the seeded data + object? DestroyRecipe(Guid seedId); } diff --git a/util/SeederApi/Services/RecipeService.cs b/util/SeederApi/Services/RecipeService.cs index 77ab196639..587ebb8356 100644 --- a/util/SeederApi/Services/RecipeService.cs +++ b/util/SeederApi/Services/RecipeService.cs @@ -1,4 +1,6 @@ +using Bit.Infrastructure.EntityFramework.Models; using Bit.Infrastructure.EntityFramework.Repositories; +using Bit.Seeder; using System.Reflection; using System.Text.Json; @@ -15,14 +17,71 @@ public class RecipeService : IRecipeService _logger = logger; } - public object? ExecuteRecipe(string templateName, JsonElement? arguments) + public (object? Result, Guid? SeedId) ExecuteRecipe(string templateName, JsonElement? arguments) { - return ExecuteRecipeMethod(templateName, arguments, "Seed"); + var result = ExecuteRecipeMethod(templateName, arguments, "Seed"); + + if (result is not RecipeResult recipeResult) + { + return (Result: result, SeedId: null); + } + + if (recipeResult.TrackedEntities.Count == 0) + { + return (Result: recipeResult.Result, SeedId: null); + } + + var seededData = new SeededData + { + Id = Guid.NewGuid(), + RecipeName = templateName, + Data = JsonSerializer.Serialize(recipeResult.TrackedEntities), + CreationDate = DateTime.UtcNow + }; + + _databaseContext.Add(seededData); + _databaseContext.SaveChanges(); + + _logger.LogInformation("Saved seeded data with ID {SeedId} for recipe {RecipeName}", + seededData.Id, templateName); + + return (Result: recipeResult.Result, SeedId: seededData.Id); } - public object? DestroyRecipe(string templateName, JsonElement? arguments) + public object? DestroyRecipe(Guid seedId) { - return ExecuteRecipeMethod(templateName, arguments, "Destroy"); + var seededData = _databaseContext.SeededData.FirstOrDefault(s => s.Id == seedId); + if (seededData == null) + { + throw new RecipeExecutionException($"Seeded data with ID {seedId} not found"); + } + + var trackedEntities = JsonSerializer.Deserialize>>(seededData.Data); + if (trackedEntities == null) + { + throw new RecipeExecutionException($"Failed to deserialize tracked entities for seed ID {seedId}"); + } + + // Delete in reverse order to respect foreign key constraints + if (trackedEntities.TryGetValue("User", out var userIds)) + { + var users = _databaseContext.Users.Where(u => userIds.Contains(u.Id)); + _databaseContext.RemoveRange(users); + } + + if (trackedEntities.TryGetValue("Organization", out var orgIds)) + { + var organizations = _databaseContext.Organizations.Where(o => orgIds.Contains(o.Id)); + _databaseContext.RemoveRange(organizations); + } + + _databaseContext.Remove(seededData); + _databaseContext.SaveChanges(); + + _logger.LogInformation("Successfully destroyed seeded data with ID {SeedId} for recipe {RecipeName}", + seedId, seededData.RecipeName); + + return new { SeedId = seedId, RecipeName = seededData.RecipeName }; } private object? ExecuteRecipeMethod(string templateName, JsonElement? arguments, string methodName)