Skip to content

Commit

Permalink
setup sqlite database with migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
a.civier committed May 17, 2024
1 parent b105b74 commit a9261bc
Show file tree
Hide file tree
Showing 8 changed files with 157 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
*.userosscache
*.sln.docstates

# SQl Lite Database
app.db*

# Vite files
.vite

Expand Down
2 changes: 2 additions & 0 deletions Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<Company>Don't Nod</Company>
<!-- Uncomment the following line to use InMemoryDatabase -->
<DefineConstants>$(DefineConstants);USE_SQL_LITE</DefineConstants>
<Product />
</PropertyGroup>
</Project>
3 changes: 3 additions & 0 deletions src/Api/appsettings.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"ConnectionStrings": {
"DefaultConnection": "DataSource=app.db;Cache=Shared"
},
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "dont-nod.com",
Expand Down
3 changes: 1 addition & 2 deletions src/Infrastructure/Data/ApplicationDbContextInitialiser.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace WebAppStarter.Infrastructure.Data;

using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
using WebAppStarter.Domain.Entities;

Expand All @@ -8,7 +9,6 @@ public class ApplicationDbContextInitialiser(ILogger<ApplicationDbContextInitial
#if USE_SQL_LITE
public async Task InitialiseAsync()
{

try
{
await context.Database.MigrateAsync();
Expand All @@ -18,7 +18,6 @@ public async Task InitialiseAsync()
logger.LogError(ex, "An error occurred while initialising the database.");
throw;
}

}
#endif

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

40 changes: 40 additions & 0 deletions src/Infrastructure/Data/Migrations/20240517132224_InitialCreate.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#nullable disable

namespace WebAppStarter.Infrastructure.Data.Migrations
{
using System;
using Microsoft.EntityFrameworkCore.Migrations;

/// <inheritdoc />
public partial class InitialCreate : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "TodoItems",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Owner = table.Column<Guid>(type: "TEXT", nullable: false),
Description = table.Column<string>(type: "TEXT", nullable: false),
Created = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
CreatedBy = table.Column<Guid>(type: "TEXT", nullable: true),
LastModified = table.Column<DateTimeOffset>(type: "TEXT", nullable: false),
LastModifiedBy = table.Column<Guid>(type: "TEXT", nullable: true),
},
constraints: table =>
{
table.PrimaryKey("PK_TodoItems", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "TodoItems");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// <auto-generated />
using System;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using WebAppStarter.Infrastructure.Data;

#nullable disable

namespace WebAppStarter.Infrastructure.Data.Migrations
{
[DbContext(typeof(ApplicationDbContext))]
partial class ApplicationDbContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.5");

modelBuilder.Entity("WebAppStarter.Domain.Entities.TodoItem", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<DateTimeOffset>("Created")
.HasColumnType("TEXT");

b.Property<Guid?>("CreatedBy")
.HasColumnType("TEXT");

b.Property<string>("Description")
.IsRequired()
.HasColumnType("TEXT");

b.Property<DateTimeOffset>("LastModified")
.HasColumnType("TEXT");

b.Property<Guid?>("LastModifiedBy")
.HasColumnType("TEXT");

b.Property<Guid>("Owner")
.HasColumnType("TEXT");

b.HasKey("Id");

b.ToTable("TodoItems");
});
#pragma warning restore 612, 618
}
}
}
3 changes: 1 addition & 2 deletions tests/Application.FunctionalTests/TestDatabaseFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ public static class TestDatabaseFactory
public static async Task<ITestDatabase> CreateAsync()
{
#if USE_SQL_LITE
var database = new SqliteTestDatabase();
var database = new SqliteTestDatabase();
#else

var database = new InMemoryTestDatabase();

#endif

await database.InitialiseAsync();
Expand Down

0 comments on commit a9261bc

Please sign in to comment.