mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
[PM-21075] Initial database seeder (#5703)
Adds a database seeder which can be used standalone using a CLI for seeding your local development environment, or used in unit tests to seed complex scenarios. --------- Co-authored-by: Robert Y <rkac@bitwarden.com>
This commit is contained in:
39
util/DbSeederUtility/Program.cs
Normal file
39
util/DbSeederUtility/Program.cs
Normal file
@ -0,0 +1,39 @@
|
||||
using Bit.Infrastructure.EntityFramework.Repositories;
|
||||
using Bit.Seeder.Recipes;
|
||||
using CommandDotNet;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.DbSeederUtility;
|
||||
|
||||
public class Program
|
||||
{
|
||||
private static int Main(string[] args)
|
||||
{
|
||||
return new AppRunner<Program>()
|
||||
.Run(args);
|
||||
}
|
||||
|
||||
[Command("organization", Description = "Seed an organization and organization users")]
|
||||
public void Organization(
|
||||
[Option('n', "Name", Description = "Name of organization")]
|
||||
string name,
|
||||
[Option('u', "users", Description = "Number of users to generate")]
|
||||
int users,
|
||||
[Option('d', "domain", Description = "Email domain for users")]
|
||||
string domain
|
||||
)
|
||||
{
|
||||
// Create service provider with necessary services
|
||||
var services = new ServiceCollection();
|
||||
ServiceCollectionExtension.ConfigureServices(services);
|
||||
var serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Get a scoped DB context
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var scopedServices = scope.ServiceProvider;
|
||||
var db = scopedServices.GetRequiredService<DatabaseContext>();
|
||||
|
||||
var recipe = new OrganizationWithUsersRecipe(db);
|
||||
recipe.Seed(name, users, domain);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user