Merge branch '3-set-up-the-database' into 'main'

Resolve "Set up the database"

Closes #3

See merge request open-source/dotnet/i18n-commander!3
This commit is contained in:
Thorsten 2022-06-12 15:16:36 +00:00
commit c93c903c26
14 changed files with 355 additions and 1 deletions

View File

@ -0,0 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="6.0.5" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="6.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="6.0.5" />
<PackageReference Include="Microsoft.Extensions.Hosting" Version="6.0.1" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,28 @@
using Microsoft.EntityFrameworkCore;
namespace DataModel.Database.Common;
public sealed class DataContext : DbContext
{
public DbSet<Setting>? Settings { get; set; }
public DataContext(DbContextOptions<DataContext> contextOptions) : base(contextOptions)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
#region Ideas
modelBuilder.Entity<Setting>().HasIndex(n => n.Id);
modelBuilder.Entity<Setting>().HasIndex(n => n.Name).IsUnique();
modelBuilder.Entity<Setting>().HasIndex(n => n.BoolValue);
modelBuilder.Entity<Setting>().HasIndex(n => n.GuidValue);
modelBuilder.Entity<Setting>().HasIndex(n => n.IntegerValue);
modelBuilder.Entity<Setting>().HasIndex(n => n.TextValue);
#endregion
}
}

View File

@ -0,0 +1,19 @@
using System.ComponentModel.DataAnnotations;
namespace DataModel.Database;
public class Setting
{
[Key]
public int Id { get; set; }
public string Name { get; set; } = string.Empty;
public string TextValue { get; set; } = string.Empty;
public bool BoolValue { get; set; }
public int IntegerValue { get; set; }
public Guid GuidValue { get; set; }
}

View File

@ -0,0 +1,65 @@
// <auto-generated />
using System;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DataModel.Migrations
{
[DbContext(typeof(DataContext))]
[Migration("20220612151133_202206AddSettings")]
partial class _202206AddSettings
{
protected override void BuildTargetModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.5");
modelBuilder.Entity("DataModel.Database.Setting", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("BoolValue")
.HasColumnType("INTEGER");
b.Property<Guid>("GuidValue")
.HasColumnType("TEXT");
b.Property<int>("IntegerValue")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("TextValue")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("BoolValue");
b.HasIndex("GuidValue");
b.HasIndex("Id");
b.HasIndex("IntegerValue");
b.HasIndex("Name")
.IsUnique();
b.HasIndex("TextValue");
b.ToTable("Settings");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,67 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace DataModel.Migrations
{
public partial class _202206AddSettings : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Settings",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
TextValue = table.Column<string>(type: "TEXT", nullable: false),
BoolValue = table.Column<bool>(type: "INTEGER", nullable: false),
IntegerValue = table.Column<int>(type: "INTEGER", nullable: false),
GuidValue = table.Column<Guid>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Settings", x => x.Id);
});
migrationBuilder.CreateIndex(
name: "IX_Settings_BoolValue",
table: "Settings",
column: "BoolValue");
migrationBuilder.CreateIndex(
name: "IX_Settings_GuidValue",
table: "Settings",
column: "GuidValue");
migrationBuilder.CreateIndex(
name: "IX_Settings_Id",
table: "Settings",
column: "Id");
migrationBuilder.CreateIndex(
name: "IX_Settings_IntegerValue",
table: "Settings",
column: "IntegerValue");
migrationBuilder.CreateIndex(
name: "IX_Settings_Name",
table: "Settings",
column: "Name",
unique: true);
migrationBuilder.CreateIndex(
name: "IX_Settings_TextValue",
table: "Settings",
column: "TextValue");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Settings");
}
}
}

View File

@ -0,0 +1,63 @@
// <auto-generated />
using System;
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace DataModel.Migrations
{
[DbContext(typeof(DataContext))]
partial class DataContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "6.0.5");
modelBuilder.Entity("DataModel.Database.Setting", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");
b.Property<bool>("BoolValue")
.HasColumnType("INTEGER");
b.Property<Guid>("GuidValue")
.HasColumnType("TEXT");
b.Property<int>("IntegerValue")
.HasColumnType("INTEGER");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");
b.Property<string>("TextValue")
.IsRequired()
.HasColumnType("TEXT");
b.HasKey("Id");
b.HasIndex("BoolValue");
b.HasIndex("GuidValue");
b.HasIndex("Id");
b.HasIndex("IntegerValue");
b.HasIndex("Name")
.IsUnique();
b.HasIndex("TextValue");
b.ToTable("Settings");
});
#pragma warning restore 612, 618
}
}
}

View File

@ -0,0 +1,13 @@
using Microsoft.Extensions.Hosting;
namespace DataModel;
public static class Program
{
public static void Main(string[] args)
{
Console.WriteLine("This app is intended for the EF tooling. You cannot start this data project, though.");
Environment.Exit(0);
}
public static IHostBuilder CreateHostBuilder(string[] args) => Setup.Setup4EFTooling(args);
}

View File

@ -0,0 +1,56 @@
using DataModel.Database.Common;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace DataModel;
public static class Setup
{
private const string ENV_EF_TOOLING_DATABASE = "ENV_EF_TOOLING_DATABASE";
private const string DB_READ_WRITE_MODE = "ReadWrite";
private const string DB_READ_WRITE_CREATE_MODE = "ReadWriteCreate";
/// <summary>
/// Tries to migrate the data file.
/// </summary>
public static async Task PerformDataMigration(DataContext dbContext)
{
var pendingMigrations = (await dbContext.Database.GetPendingMigrationsAsync()).ToList();
foreach (var pendingMigration in pendingMigrations)
{
Console.WriteLine($"The migration '{pendingMigration}' is pending.");
}
await dbContext.Database.MigrateAsync();
}
/// <summary>
/// Add the database to the DI system
/// </summary>
public static void AddDatabase(this IServiceCollection serviceCollection, string path2DataFile, bool createWhenNecessary = true)
{
serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={path2DataFile};Mode={(createWhenNecessary ? DB_READ_WRITE_CREATE_MODE : DB_READ_WRITE_MODE)}"));
}
/// <summary>
/// Sets up the DI & db context ready for the EF tooling.
/// </summary>
public static IHostBuilder Setup4EFTooling(string[] args)
{
var dataFile = Environment.GetEnvironmentVariable(ENV_EF_TOOLING_DATABASE);
if (string.IsNullOrWhiteSpace(dataFile))
{
Console.WriteLine("In order to use EF tooling, point the environment variable ENV_EF_TOOLING_DATABASE to the data file, which should be used for the EF tooling.");
Environment.Exit(100);
}
var builder = Host.CreateDefaultBuilder(args);
builder.ConfigureServices((hostContext, serviceCollection) =>
{
serviceCollection.AddDbContext<DataContext>(options => options.UseSqlite($"Filename={dataFile};Mode=ReadWriteCreate"));
});
return builder;
}
}

View File

@ -0,0 +1,4 @@
@echo off
set /p migrationName="Please enter the new migration's name: "
dotnet tool update --global dotnet-ef
dotnet ef migrations add %migrationName%

View File

@ -0,0 +1,4 @@
@echo off
set /p migrationName="Please enter the migration's name which should be the **ACTIVE** one: "
dotnet tool update --global dotnet-ef
dotnet ef database update %migrationName%

View File

@ -5,7 +5,9 @@ VisualStudioVersion = 17.3.32519.111
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Processor", "Processor\Processor.csproj", "{E24B7026-05BE-434D-9481-7CA5785BC7A8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UI WinForms", "UI WinForms\UI WinForms.csproj", "{5AE84E7C-3141-46CA-B390-4E42878B6195}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UI WinForms", "UI WinForms\UI WinForms.csproj", "{5AE84E7C-3141-46CA-B390-4E42878B6195}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DataModel", "DataModel\DataModel.csproj", "{D18DD193-3F93-4D21-92DC-BA0E26B0342A}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@ -21,6 +23,10 @@ Global
{5AE84E7C-3141-46CA-B390-4E42878B6195}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5AE84E7C-3141-46CA-B390-4E42878B6195}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5AE84E7C-3141-46CA-B390-4E42878B6195}.Release|Any CPU.Build.0 = Release|Any CPU
{D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D18DD193-3F93-4D21-92DC-BA0E26B0342A}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View File

@ -7,4 +7,8 @@
<LangVersion>default</LangVersion>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DataModel\DataModel.csproj" />
</ItemGroup>
</Project>

View File

@ -11,6 +11,7 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\DataModel\DataModel.csproj" />
<ProjectReference Include="..\Processor\Processor.csproj" />
</ItemGroup>

View File