feat(server): add database context and migrations

This commit is contained in:
Stevan Freeborn
2026-01-04 21:10:44 -06:00
parent 18b8dec374
commit 7ba17ea0e3
12 changed files with 383 additions and 0 deletions
@@ -0,0 +1,32 @@
using GroundsForSupport.Server.Payments;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Options;
namespace GroundsForSupport.Server.Data;
internal sealed class Context(
IOptions<ContextOptions> ctxOptions,
DbContextOptions<Context> options
) : DbContext(options)
{
private const string DataSourceKey = "Data Source=";
private readonly ContextOptions _ctxOptions = ctxOptions.Value;
public DbSet<Payment> Payments => Set<Payment>();
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dbPath = _ctxOptions.GetFullyQualifiedDatabasePath();
var dbDirectory = Path.GetDirectoryName(dbPath) ?? throw new InvalidOperationException("Database directory path could not be determined.");
if (Directory.Exists(dbDirectory) is false)
{
Directory.CreateDirectory(dbDirectory);
}
var connectionString = $"{DataSourceKey}{dbPath}";
optionsBuilder.UseSqlite(connectionString);
}
}
@@ -0,0 +1,28 @@
using Microsoft.Extensions.Options;
namespace GroundsForSupport.Server.Data;
internal sealed record ContextOptions
{
public string DatabaseFilePath { get; init; } = string.Empty;
public string GetFullyQualifiedDatabasePath()
{
return Path.GetFullPath(DatabaseFilePath, AppContext.BaseDirectory);
}
}
internal sealed record ContextOptionsSetup : IConfigureOptions<ContextOptions>
{
private const string SectionName = nameof(ContextOptions);
private readonly IConfiguration _configuration;
public ContextOptionsSetup(IConfiguration configuration)
{
_configuration = configuration;
}
public void Configure(ContextOptions options)
{
_configuration.GetSection(SectionName).Bind(options);
}
}
@@ -0,0 +1,27 @@
using Microsoft.EntityFrameworkCore;
namespace GroundsForSupport.Server.Data;
internal sealed class MigrationService(
IServiceProvider serviceProvider,
ILogger<MigrationService> logger
) : IHostedService
{
private readonly IServiceProvider _serviceProvider = serviceProvider;
public async Task StartAsync(CancellationToken cancellationToken)
{
logger.LogInformation("Applying database migrations...");
using var scope = _serviceProvider.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<Context>();
await context.Database.MigrateAsync(cancellationToken);
logger.LogInformation("Database migrations applied.");
}
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
@@ -8,6 +8,11 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" /> <PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="10.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="10.0.1" />
<PackageReference Include="Stripe.net" Version="50.1.0" /> <PackageReference Include="Stripe.net" Version="50.1.0" />
</ItemGroup> </ItemGroup>
@@ -0,0 +1,34 @@
// <auto-generated />
using GroundsForSupport.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
[DbContext(typeof(Context))]
[Migration("20251230125831_AddPaymentsModel")]
partial class AddPaymentsModel
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("GroundsForSupport.Server.Payments.Payment", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Payments");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,32 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
/// <inheritdoc />
public partial class AddPaymentsModel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Payments",
columns: table => new
{
Id = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Payments", x => x.Id);
});
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Payments");
}
}
}
@@ -0,0 +1,37 @@
// <auto-generated />
using GroundsForSupport.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
[DbContext(typeof(Context))]
[Migration("20251231131814_AddAmountToPaymentModel")]
partial class AddAmountToPaymentModel
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("GroundsForSupport.Server.Payments.Payment", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<long>("Amount")
.HasColumnType("INTEGER");
b.HasKey("Id");
b.ToTable("Payments");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
/// <inheritdoc />
public partial class AddAmountToPaymentModel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<long>(
name: "Amount",
table: "Payments",
type: "INTEGER",
nullable: false,
defaultValue: 0L);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Amount",
table: "Payments");
}
}
}
@@ -0,0 +1,44 @@
// <auto-generated />
using GroundsForSupport.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
[DbContext(typeof(Context))]
[Migration("20260102131600_AddNameMessagePropertiesToPaymentModel")]
partial class AddNameMessagePropertiesToPaymentModel
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("GroundsForSupport.Server.Payments.Payment", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<long>("Amount")
.HasColumnType("INTEGER");
b.Property<string>("Message")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Payments");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,39 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
/// <inheritdoc />
public partial class AddNameMessagePropertiesToPaymentModel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
name: "Message",
table: "Payments",
type: "TEXT",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "Name",
table: "Payments",
type: "TEXT",
nullable: false,
defaultValue: "");
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "Message",
table: "Payments");
migrationBuilder.DropColumn(
name: "Name",
table: "Payments");
}
}
}
@@ -0,0 +1,47 @@
// <auto-generated />
using GroundsForSupport.Server.Data;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
[DbContext(typeof(Context))]
[Migration("20260102140326_AddCreatedAtTimeStampToPaymentsModel")]
partial class AddCreatedAtTimeStampToPaymentsModel
{
/// <inheritdoc />
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "10.0.1");
modelBuilder.Entity("GroundsForSupport.Server.Payments.Payment", b =>
{
b.Property<string>("Id")
.HasColumnType("TEXT");
b.Property<long>("Amount")
.HasColumnType("INTEGER");
b.Property<long>("CreatedAtUnix")
.HasColumnType("INTEGER");
b.Property<string>("Message")
.HasColumnType("TEXT");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.ToTable("Payments");
});
#pragma warning restore 612, 618
}
}
}
@@ -0,0 +1,29 @@
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace GroundsForSupport.Server.Migrations
{
/// <inheritdoc />
public partial class AddCreatedAtTimeStampToPaymentsModel : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<long>(
name: "CreatedAtUnix",
table: "Payments",
type: "INTEGER",
nullable: false,
defaultValue: 0L);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CreatedAtUnix",
table: "Payments");
}
}
}