Skip to content

Commit

Permalink
Added runtime migration for ef core (#58)
Browse files Browse the repository at this point in the history
* Added runtime migration for ef core

* mostly copied from warfabrik clone, but also with some fixed and new features, so that the default ids for migrations work and the database context can be configured better
* added name of configurators so that one can have seperated logic for the different configurators
* made it possible to load multiple configurators, so that one could have multiple databases for different use cases in one program
  • Loading branch information
susch19 authored Jul 2, 2023
1 parent bf354e3 commit 9908ba4
Show file tree
Hide file tree
Showing 26 changed files with 799 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

using NonSucking.Framework.Extension.EntityFrameworkCore;
using NonSucking.Framework.Extension.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore;

namespace NonSucking.Framework.Extension.Database.InMemory;
public class InMemoryConfigurator : IDatabaseConfigurator
{
public string Name => "InMemory";
public class MigrationContext : MigrationDatabaseContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseInMemoryDatabase("Empty");
base.OnConfiguring(optionsBuilder);
}
}

public IAutoMigrationContextBuilder GetEmptyForMigration()
{
return new MigrationContext();
}

public DbContextOptionsBuilder OnConfiguring(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
return optionsBuilder.UseInMemoryDatabase(connectionString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="7.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NonSucking.Framework.Extension.EntityFrameworkCore\NonSucking.Framework.Extension.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<Version>${VERSION_FULL}</Version>
<AssemblyVersion>${VERSION_LONG}</AssemblyVersion>
<Authors>${AUTHORS}</Authors>
<Copyright>${COPYRIGHT}</Copyright>
<Company>${COMPANY}</Company>
<Title>${TITLE_INMEMORY}</Title>
<Description>${DESCRIPTION_INMEMORY}</Description>
<PackageReleaseNotes>
This is a first preview version and not intended for productive use. Not everything has been tested or commented yet.
</PackageReleaseNotes>
<PackageTags>
${TAGS_INMEMORY}
</PackageTags>
<!-- <PackageIconUrl>${ICON_URL}</PackageIconUrl> -->
<RepositoryUrl>${REPO_URL}</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

using NonSucking.Framework.Extension.EntityFrameworkCore;
using NonSucking.Framework.Extension.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore;

namespace NonSucking.Framework.Extension.Database.MSSQL;
public class MSSQLConfigurator : IDatabaseConfigurator
{
public string Name => "MSSQL";
public class MigrationContext : MigrationDatabaseContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseSqlServer();
base.OnConfiguring(optionsBuilder);
}

}

public IAutoMigrationContextBuilder GetEmptyForMigration()
{
return new MigrationContext();
}

public DbContextOptionsBuilder OnConfiguring(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
return optionsBuilder.UseSqlServer(connectionString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NonSucking.Framework.Extension.EntityFrameworkCore\NonSucking.Framework.Extension.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<Version>${VERSION_FULL}</Version>
<AssemblyVersion>${VERSION_LONG}</AssemblyVersion>
<Authors>${AUTHORS}</Authors>
<Copyright>${COPYRIGHT}</Copyright>
<Company>${COMPANY}</Company>
<Title>${TITLE_MSSQL}</Title>
<Description>${DESCRIPTION_MSSQL}</Description>
<PackageReleaseNotes>
This is a first preview version and not intended for productive use. Not everything has been tested or commented yet.
</PackageReleaseNotes>
<PackageTags>
${TAGS_MSSQL}
</PackageTags>
<!-- <PackageIconUrl>${ICON_URL}</PackageIconUrl> -->
<RepositoryUrl>${REPO_URL}</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

using NonSucking.Framework.Extension.EntityFrameworkCore;
using NonSucking.Framework.Extension.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore;

namespace NonSucking.Framework.Extension.Database.MySql;
public class MySQLConfigurator : IDatabaseConfigurator
{
public string Name => "MySQL";
public class MigrationContext : MigrationDatabaseContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseMySql("server=none;userid=none;password=none;database=none", ServerVersion.Create(10, 9, 3, Pomelo.EntityFrameworkCore.MySql.Infrastructure.ServerType.MariaDb));
base.OnConfiguring(optionsBuilder);
}

}

public IAutoMigrationContextBuilder GetEmptyForMigration()
{
return new MigrationContext();
}

public DbContextOptionsBuilder OnConfiguring(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
return optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="7.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NonSucking.Framework.Extension.EntityFrameworkCore\NonSucking.Framework.Extension.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<Version>${VERSION_FULL}</Version>
<AssemblyVersion>${VERSION_LONG}</AssemblyVersion>
<Authors>${AUTHORS}</Authors>
<Copyright>${COPYRIGHT}</Copyright>
<Company>${COMPANY}</Company>
<Title>${TITLE_MYSQL}</Title>
<Description>${DESCRIPTION_MYSQL}</Description>
<PackageReleaseNotes>
This is a first preview version and not intended for productive use. Not everything has been tested or commented yet.
</PackageReleaseNotes>
<PackageTags>
${TAGS_MYSQL}
</PackageTags>
<!-- <PackageIconUrl>${ICON_URL}</PackageIconUrl> -->
<RepositoryUrl>${REPO_URL}</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<Version>${VERSION_FULL}</Version>
<AssemblyVersion>${VERSION_LONG}</AssemblyVersion>
<Authors>${AUTHORS}</Authors>
<Copyright>${COPYRIGHT}</Copyright>
<Company>${COMPANY}</Company>
<Title>${TITLE_POSTGRESQL}</Title>
<Description>${DESCRIPTION_POSTGRESQL}</Description>
<PackageReleaseNotes>
This is a first preview version and not intended for productive use. Not everything has been tested or commented yet.
</PackageReleaseNotes>
<PackageTags>
${TAGS_POSTGRESQL}
</PackageTags>
<!-- <PackageIconUrl>${ICON_URL}</PackageIconUrl> -->
<RepositoryUrl>${REPO_URL}</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="7.0.4" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\NonSucking.Framework.Extension.EntityFrameworkCore\NonSucking.Framework.Extension.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@

using NonSucking.Framework.Extension.EntityFrameworkCore;
using NonSucking.Framework.Extension.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore;

namespace NonSucking.Framework.Extension.Database.PostrgeSQL;
public class PostgresConfigurator : IDatabaseConfigurator
{
public string Name => "PostrgeSQL";
public class MigrationContext : MigrationDatabaseContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseNpgsql();
base.OnConfiguring(optionsBuilder);
}

}

public IAutoMigrationContextBuilder GetEmptyForMigration()
{
return new MigrationContext();
}
public DbContextOptionsBuilder OnConfiguring(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
return optionsBuilder.UseNpgsql(connectionString);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="7.0.8" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\NonSucking.Framework.Extension.EntityFrameworkCore\NonSucking.Framework.Extension.EntityFrameworkCore.csproj" />
</ItemGroup>

<ItemGroup>
<None Include="..\..\LICENSE">
<Pack>True</Pack>
<PackagePath></PackagePath>
</None>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<Project>
<PropertyGroup>
<Version>${VERSION_FULL}</Version>
<AssemblyVersion>${VERSION_LONG}</AssemblyVersion>
<Authors>${AUTHORS}</Authors>
<Copyright>${COPYRIGHT}</Copyright>
<Company>${COMPANY}</Company>
<Title>${TITLE_SQLITE}</Title>
<Description>${DESCRIPTION_SQLITE}</Description>
<PackageReleaseNotes>
This is a first preview version and not intended for productive use. Not everything has been tested or commented yet.
</PackageReleaseNotes>
<PackageTags>
${TAGS_SQLITE}
</PackageTags>
<!-- <PackageIconUrl>${ICON_URL}</PackageIconUrl> -->
<RepositoryUrl>${REPO_URL}</RepositoryUrl>
<PackageProjectUrl>$(RepositoryUrl)</PackageProjectUrl>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<NeutralLanguage>en-US</NeutralLanguage>
</PropertyGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

using Microsoft.EntityFrameworkCore;

using NonSucking.Framework.Extension.EntityFrameworkCore;
using NonSucking.Framework.Extension.EntityFrameworkCore.Migrations;

namespace NonSucking.Framework.Extension.Database.Sqlite;
public class SqLiteConfigurator : IDatabaseConfigurator
{
public string Name => "SQLite";

public class MigrationContext : MigrationDatabaseContext
{
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
_ = optionsBuilder.UseSqlite();
base.OnConfiguring(optionsBuilder);
}

}

public IAutoMigrationContextBuilder GetEmptyForMigration()
{
return new MigrationContext();
}
public DbContextOptionsBuilder OnConfiguring(DbContextOptionsBuilder optionsBuilder, string connectionString)
{
return optionsBuilder.UseSqlite(connectionString);
}
}
Loading

0 comments on commit 9908ba4

Please sign in to comment.