some stuff

This commit is contained in:
john 2025-05-18 17:18:34 +02:00
parent a98a61e040
commit d061cac489
3 changed files with 78 additions and 17 deletions

View file

@ -14,7 +14,10 @@
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="Migrations\" />
<None Update="Migrations\**\*.sql">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>

View file

@ -6,7 +6,7 @@ using Npgsql;
var nameArg = new Argument<string>("name", "the name of the migration");
var migrationsDirectoryOption = new Option<string>(
["--migrations-directory"],
() => "./Migrations",
() => System.Environment.GetEnvironmentVariable("MigrationsDirectory") ?? "./Migrations",
"the directory where the migrations are stored"
);
@ -15,6 +15,7 @@ var newCommand = new Command("new", "creates a new migrations")
nameArg,
migrationsDirectoryOption,
};
newCommand.SetHandler(MakeNewMigration, nameArg, migrationsDirectoryOption);
var connectionStringArg = new Argument<string>(
@ -22,30 +23,53 @@ var connectionStringArg = new Argument<string>(
"the connection string to the database"
);
var connectionStringOption = new Option<string?>(
["-c", "--connection-string"],
() => System.Environment.GetEnvironmentVariable("ConnectionStrings__Database") ?? null,
"the connection string to the database"
);
var upCommand = new Command("up", "update the database to the most current migration")
{
migrationsDirectoryOption,
connectionStringArg,
};
upCommand.SetHandler(MigrateUp, migrationsDirectoryOption, connectionStringArg);
upCommand.SetHandler(
async (directory, connectionString) =>
{
if (connectionString is null)
{
throw new ArgumentException("Connection string is required.");
}
await MigrateUp(directory, connectionString);
},
migrationsDirectoryOption,
connectionStringOption
);
var seedCommand = new Command("seed", "seed the database with test data") { connectionStringArg };
seedCommand.SetHandler(Seed, connectionStringArg);
// Add these near the top with other command definitions
var yesOption = new Option<bool>(
["-y", "--yes"],
"Skip confirmation prompt"
);
var yesOption = new Option<bool>(["-y", "--yes"], "Skip confirmation prompt");
var resetCommand = new Command("reset", "drops the existing database, runs migrations, and seeds test data")
var resetCommand = new Command(
"reset",
"drops the existing database, runs migrations, and seeds test data"
)
{
connectionStringArg,
migrationsDirectoryOption,
yesOption
yesOption,
};
resetCommand.SetHandler(CreateTestDatabase, connectionStringArg, migrationsDirectoryOption, yesOption);
resetCommand.SetHandler(
CreateTestDatabase,
connectionStringArg,
migrationsDirectoryOption,
yesOption
);
var rootCommand = new RootCommand("migrator") { newCommand, upCommand, seedCommand, resetCommand };
@ -85,19 +109,23 @@ static async Task Seed(string connectionString)
await TestDataSeeder.Seed(dataSource);
}
static async Task CreateTestDatabase(string connectionString, string migrationsDirectory, bool skipConfirmation)
static async Task CreateTestDatabase(
string connectionString,
string migrationsDirectory,
bool skipConfirmation
)
{
var builder = new NpgsqlConnectionStringBuilder(connectionString);
if (!skipConfirmation)
{
builder.Database = "postgres";
try
{
await using var dataSource = NpgsqlDataSource.Create(builder.ConnectionString);
await using var conn = await dataSource.OpenConnectionAsync();
await using var cmd = conn.CreateCommand();
cmd.CommandText = $"SELECT 1 FROM pg_database WHERE datname = '{builder.Database}'";
var exists = await cmd.ExecuteScalarAsync();
if (exists is true)
@ -128,7 +156,7 @@ static async Task CreateTestDatabase(string connectionString, string migrationsD
await using var dataSource = NpgsqlDataSource.Create(builder.ConnectionString);
await using var conn = await dataSource.OpenConnectionAsync();
await using var cmd = conn.CreateCommand();
// Drop database if it exists
cmd.CommandText = $"DROP DATABASE IF EXISTS {databaseName} WITH (FORCE)";
await cmd.ExecuteNonQueryAsync();