-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#102) Database: use EFCore to initialize a database
- Loading branch information
Showing
12 changed files
with
228 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"version": 1, | ||
"isRoot": true, | ||
"tools": { | ||
"dotnet-ef": { | ||
"version": "5.0.10", | ||
"commands": [ | ||
"dotnet-ef" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net5.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Compile Include="Models.fs" /> | ||
<Compile Include="EmulsionDbContext.fs" /> | ||
<Compile Include="Migrations\*.fs" /> | ||
<Compile Include="Initializer.fs" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="EntityFrameworkCore.FSharp" Version="5.0.3-beta006" /> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.10"> | ||
<PrivateAssets>all</PrivateAssets> | ||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
</PackageReference> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="5.0.10" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
namespace Emulsion.Database | ||
|
||
open Emulsion.Database.Models | ||
open Microsoft.EntityFrameworkCore | ||
open Microsoft.EntityFrameworkCore.Design | ||
|
||
type EmulsionDbContext(dataSource: string) = | ||
inherit DbContext() | ||
|
||
[<DefaultValue>] val mutable telegramContents: DbSet<TelegramContent> | ||
member this.TelegramContents with get() = this.telegramContents and set v = this.telegramContents <- v | ||
|
||
override _.OnConfiguring options = | ||
options.UseSqlite($"Data Source={dataSource};") |> ignore | ||
|
||
/// This type is used by the EFCore infrastructure when creating a new migration. | ||
type EmulsionDbContextDesignFactory() = | ||
interface IDesignTimeDbContextFactory<EmulsionDbContext> with | ||
member this.CreateDbContext _ = | ||
new EmulsionDbContext(":memory:") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Emulsion.Database.Initializer | ||
|
||
open Microsoft.EntityFrameworkCore | ||
|
||
let initializeDatabase(context: EmulsionDbContext): Async<unit> = async { | ||
do! Async.AwaitTask(context.Database.MigrateAsync()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// <auto-generated /> | ||
namespace Emulsion.Database.Migrations | ||
|
||
open System | ||
open Emulsion.Database | ||
open Microsoft.EntityFrameworkCore | ||
open Microsoft.EntityFrameworkCore.Infrastructure | ||
open Microsoft.EntityFrameworkCore.Metadata | ||
open Microsoft.EntityFrameworkCore.Migrations | ||
open Microsoft.EntityFrameworkCore.Storage.ValueConversion | ||
|
||
[<DbContext(typeof<EmulsionDbContext>)>] | ||
[<Migration("20210926114410_Initialize")>] | ||
type Initialize() = | ||
inherit Migration() | ||
|
||
override this.Up(migrationBuilder:MigrationBuilder) = | ||
migrationBuilder.CreateTable( | ||
name = "TelegramContents" | ||
,columns = (fun table -> | ||
{| | ||
Id = | ||
table.Column<Guid>( | ||
nullable = false | ||
,``type`` = "TEXT" | ||
) | ||
|}) | ||
,constraints = | ||
(fun table -> | ||
table.PrimaryKey("PK_TelegramContents", (fun x -> (x.Id) :> obj)) |> ignore | ||
) | ||
) |> ignore | ||
|
||
|
||
override this.Down(migrationBuilder:MigrationBuilder) = | ||
migrationBuilder.DropTable( | ||
name = "TelegramContents" | ||
) |> ignore | ||
|
||
|
||
override this.BuildTargetModel(modelBuilder: ModelBuilder) = | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "5.0.10") | ||
|> ignore | ||
|
||
modelBuilder.Entity("Emulsion.Database.Models.TelegramContent", (fun b -> | ||
|
||
b.Property<Guid>("Id") | ||
.IsRequired(true) | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("TEXT") |> ignore | ||
|
||
b.HasKey("Id") |> ignore | ||
|
||
b.ToTable("TelegramContents") |> ignore | ||
|
||
)) |> ignore | ||
|
33 changes: 33 additions & 0 deletions
33
Emulsion.Database/Migrations/EmulsionDbContextModelSnapshot.fs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// <auto-generated /> | ||
namespace Emulsion.Database.Migrations | ||
|
||
open System | ||
open Emulsion.Database | ||
open Microsoft.EntityFrameworkCore | ||
open Microsoft.EntityFrameworkCore.Infrastructure | ||
open Microsoft.EntityFrameworkCore.Metadata | ||
open Microsoft.EntityFrameworkCore.Migrations | ||
open Microsoft.EntityFrameworkCore.Storage.ValueConversion | ||
|
||
[<DbContext(typeof<EmulsionDbContext>)>] | ||
type EmulsionDbContextModelSnapshot() = | ||
inherit ModelSnapshot() | ||
|
||
override this.BuildModel(modelBuilder: ModelBuilder) = | ||
modelBuilder | ||
.HasAnnotation("ProductVersion", "5.0.10") | ||
|> ignore | ||
|
||
modelBuilder.Entity("Emulsion.Database.Models.TelegramContent", (fun b -> | ||
|
||
b.Property<Guid>("Id") | ||
.IsRequired(true) | ||
.ValueGeneratedOnAdd() | ||
.HasColumnType("TEXT") |> ignore | ||
|
||
b.HasKey("Id") |> ignore | ||
|
||
b.ToTable("TelegramContents") |> ignore | ||
|
||
)) |> ignore | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
namespace Emulsion.Database.Models | ||
|
||
open System | ||
open System.ComponentModel.DataAnnotations | ||
|
||
[<CLIMutable>] | ||
type TelegramContent = { | ||
[<Key>] Id: Guid | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
module Emulsion.Tests.Database.InitializerTests | ||
|
||
open System.IO | ||
open Emulsion.Database | ||
open Xunit | ||
|
||
[<Fact>] | ||
let ``Database initialization``(): unit = | ||
async { | ||
let databasePath = Path.Combine(Path.GetTempPath(), "emulsion-test.db") | ||
use context = new EmulsionDbContext(databasePath) | ||
let! _ = Async.AwaitTask(context.Database.EnsureDeletedAsync()) | ||
do! Initializer.initializeDatabase context | ||
} |> Async.RunSynchronously |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
How to Create a Database Migration | ||
================================== | ||
|
||
This article explains how to create a database migration using [EFCore.FSharp][efcore.fsharp]. | ||
|
||
1. Change the entity type (see `Emulsion.Database/Models.fs`), update the `EmulsionDbContext` if required. | ||
2. Run the following shell commands: | ||
|
||
```console | ||
$ dotnet tool restore | ||
$ cd Emulsion.Database | ||
$ dotnet ef migrations add <migration-name> | ||
``` | ||
|
||
[efcore.fsharp]: https://github.com/efcore/EFCore.FSharp |