Skip to content

Commit

Permalink
Add design time contexts. Add migrations.
Browse files Browse the repository at this point in the history
  • Loading branch information
bitbound committed Feb 21, 2024
1 parent 8afdd97 commit 05a4348
Show file tree
Hide file tree
Showing 19 changed files with 4,253 additions and 416 deletions.
27 changes: 24 additions & 3 deletions Server/Data/AppDbFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,31 @@ public interface IAppDbFactory
AppDb GetContext();
}

public class AppDbFactory(IServiceProvider _services) : IAppDbFactory

public class AppDbFactory : IAppDbFactory
{
private readonly IConfiguration _configuration;
private readonly IWebHostEnvironment _hostEnv;

public AppDbFactory(
IConfiguration configuration,
IWebHostEnvironment hostEnv)
{
_configuration = configuration;
_hostEnv = hostEnv;
}

public AppDb GetContext()
{
return _services.GetRequiredService<AppDb>();
var dbProvider = _configuration["ApplicationOptions:DbProvider"]?.ToLower();

return dbProvider switch
{
"sqlite" => new SqliteDbContext(_configuration, _hostEnv),
"sqlserver" => new SqlServerDbContext(_configuration, _hostEnv),
"postgresql" => new PostgreSqlDbContext(_configuration, _hostEnv),
"inmemory" => new TestingDbContext(_hostEnv),
_ => throw new ArgumentException("Unknown DB provider."),
};
}
}
}
70 changes: 70 additions & 0 deletions Server/Data/DesignTimeContexts.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.FileProviders;

namespace Remotely.Server.Data;

public class SqliteDbContextDesignTime : IDesignTimeDbContextFactory<SqliteDbContext>
{
public SqliteDbContext CreateDbContext(string[] args)
{
var appSettings = new Dictionary<string, string?>
{
["ConnectionStrings:SQLite"] = "Data Source=remotely.db",
["ApplicationOptions:DbProvider"] = "sqlite"
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(appSettings)
.Build();

return new SqliteDbContext(configuration, new DesignTimeWebHostEnvironment());
}

}
public class SqlServerDbContextDesignTime : IDesignTimeDbContextFactory<SqlServerDbContext>
{
public SqlServerDbContext CreateDbContext(string[] args)
{
var appSettings = new Dictionary<string, string?>
{
["ConnectionStrings:SqlServer"] = "Server=(localdb)\\mssqllocaldb;Database=Remotely-Server-53bc9b9d-9d6a-45d4-8429-2a2761773502;Trusted_Connection=True;MultipleActiveResultSets=true",
["ApplicationOptions:DbProvider"] = "SqlServer"
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(appSettings)
.Build();

return new SqlServerDbContext(configuration, new DesignTimeWebHostEnvironment());
}
}

public class PostgreSqlDbContextDesignTime : IDesignTimeDbContextFactory<PostgreSqlDbContext>
{
public PostgreSqlDbContext CreateDbContext(string[] args)
{
var appSettings = new Dictionary<string, string?>
{
["ConnectionStrings:PostgreSql"] = "Host=localhost;Database=Remotely;Username=postgres;",
["ApplicationOptions:DbProvider"] = "PostgreSql"
};

var configuration = new ConfigurationBuilder()
.AddInMemoryCollection(appSettings)
.Build();

return new PostgreSqlDbContext(configuration, new DesignTimeWebHostEnvironment());
}
}


public class DesignTimeWebHostEnvironment : IWebHostEnvironment
{
public string WebRootPath { get; set; } = string.Empty;
public IFileProvider WebRootFileProvider { get; set; } = default!;
public string ApplicationName { get; set; } = string.Empty;
public IFileProvider ContentRootFileProvider { get; set; } = default!;
public string ContentRootPath { get; set; } = string.Empty;
public string EnvironmentName { get; set; } = "Development";
}
12 changes: 2 additions & 10 deletions Server/Data/SqliteDbContext.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;

namespace Remotely.Server.Data;

Expand All @@ -25,4 +17,4 @@ protected override void OnConfiguring(DbContextOptionsBuilder options)
options.UseSqlite(_configuration.GetConnectionString("SQLite"));
base.OnConfiguring(options);
}
}
}
215 changes: 107 additions & 108 deletions Server/Migrations/PostgreSql/20230821190450_Remove_TitleBranding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,116 +2,115 @@

#nullable disable

namespace Remotely.Server.Migrations.PostgreSql
namespace Remotely.Server.Migrations.PostgreSql;

/// <inheritdoc />
public partial class Remove_TitleBranding : Migration
{
/// <inheritdoc />
public partial class Remove_TitleBranding : Migration
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ButtonForegroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "ButtonForegroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "ButtonForegroundRed",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundRed",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundRed",
table: "BrandingInfos");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "ButtonForegroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "ButtonForegroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "ButtonForegroundRed",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleBackgroundRed",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundBlue",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundGreen",
table: "BrandingInfos");

migrationBuilder.DropColumn(
name: "TitleForegroundRed",
table: "BrandingInfos");
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);
}
migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "ButtonForegroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleBackgroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundBlue",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundGreen",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);

migrationBuilder.AddColumn<byte>(
name: "TitleForegroundRed",
table: "BrandingInfos",
type: "smallint",
nullable: false,
defaultValue: (byte)0);
}
}
Loading

0 comments on commit 05a4348

Please sign in to comment.