Added IDatabase interface and refactored to use it to handle database type - updated Installer to dynamically add databases to selector
This commit is contained in:
16
Oqtane.Server/Databases/LocalDbDatabase.cs
Normal file
16
Oqtane.Server/Databases/LocalDbDatabase.cs
Normal file
@ -0,0 +1,16 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Interfaces;
|
||||
|
||||
namespace Oqtane.Repository.Databases
|
||||
{
|
||||
public class LocalDbDatabase : IDatabase
|
||||
{
|
||||
public string FriendlyName => "Local Database";
|
||||
public string Name => "LocalDB";
|
||||
|
||||
public DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseSqlServer(connectionString);
|
||||
}
|
||||
}
|
||||
}
|
17
Oqtane.Server/Databases/SqlServerDatabase.cs
Normal file
17
Oqtane.Server/Databases/SqlServerDatabase.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Interfaces;
|
||||
|
||||
namespace Oqtane.Repository.Databases
|
||||
{
|
||||
public class SqlServerDatabase : IDatabase
|
||||
{
|
||||
public string FriendlyName => "SQL Server";
|
||||
|
||||
public string Name => "SqlServer";
|
||||
|
||||
public DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseSqlServer(connectionString);
|
||||
}
|
||||
}
|
||||
}
|
17
Oqtane.Server/Databases/SqliteDatabase.cs
Normal file
17
Oqtane.Server/Databases/SqliteDatabase.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Oqtane.Interfaces;
|
||||
|
||||
namespace Oqtane.Repository.Databases
|
||||
{
|
||||
public class SqliteDatabase : IDatabase
|
||||
{
|
||||
public string FriendlyName => Name;
|
||||
|
||||
public string Name => "Sqlite";
|
||||
|
||||
public DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseSqlite(connectionString);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,5 +1,10 @@
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
using System.Linq;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Oqtane.Interfaces;
|
||||
// ReSharper disable ConvertToUsingDeclaration
|
||||
|
||||
namespace Oqtane.Extensions
|
||||
{
|
||||
@ -7,16 +12,10 @@ namespace Oqtane.Extensions
|
||||
{
|
||||
public static DbContextOptionsBuilder UseOqtaneDatabase([NotNull] this DbContextOptionsBuilder optionsBuilder, string databaseType, string connectionString)
|
||||
{
|
||||
switch (databaseType)
|
||||
{
|
||||
case "SqlServer":
|
||||
optionsBuilder.UseSqlServer(connectionString);
|
||||
var type = Type.GetType(databaseType);
|
||||
var database = Activator.CreateInstance(type) as IDatabase;
|
||||
|
||||
break;
|
||||
case "Sqlite":
|
||||
optionsBuilder.UseSqlite(connectionString);
|
||||
break;
|
||||
}
|
||||
database.UseDatabase(optionsBuilder, connectionString);
|
||||
|
||||
return optionsBuilder;
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Oqtane.Infrastructure;
|
||||
using Oqtane.Interfaces;
|
||||
using Oqtane.Modules;
|
||||
using Oqtane.Services;
|
||||
using Oqtane.Shared;
|
||||
@ -46,6 +47,17 @@ namespace Microsoft.Extensions.DependencyInjection
|
||||
}
|
||||
}
|
||||
|
||||
// dynamically register database providers
|
||||
var databaseTypes = assembly.GetInterfaces<IDatabase>();
|
||||
foreach (var databaseType in databaseTypes)
|
||||
{
|
||||
if (databaseType.AssemblyQualifiedName != null)
|
||||
{
|
||||
var serviceType = Type.GetType("Oqtane.Interfaces.IDatabase, Oqtane.Shared");
|
||||
services.AddScoped(serviceType ?? databaseType, databaseType);
|
||||
}
|
||||
}
|
||||
|
||||
// dynamically register hosted services
|
||||
var serviceTypes = assembly.GetTypes(hostedServiceType);
|
||||
foreach (var serviceType in serviceTypes)
|
||||
|
@ -234,18 +234,18 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
if (!string.IsNullOrEmpty(install.TenantName) && !string.IsNullOrEmpty(install.Aliases))
|
||||
{
|
||||
using (var db = new InstallationContext(install.DatabaseType, NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey))))
|
||||
using (var db = GetInstallationContext())
|
||||
{
|
||||
Tenant tenant;
|
||||
if (install.IsNewTenant)
|
||||
{
|
||||
tenant = new Tenant { Name = install.TenantName,
|
||||
DBConnectionString = DenormalizeConnectionString(install.ConnectionString),
|
||||
DBType = install.DatabaseType,
|
||||
CreatedBy = "",
|
||||
CreatedOn = DateTime.UtcNow,
|
||||
ModifiedBy = "",
|
||||
ModifiedOn = DateTime.UtcNow };
|
||||
DBConnectionString = DenormalizeConnectionString(install.ConnectionString),
|
||||
DBType = install.DatabaseType,
|
||||
CreatedBy = "",
|
||||
CreatedOn = DateTime.UtcNow,
|
||||
ModifiedBy = "",
|
||||
ModifiedOn = DateTime.UtcNow };
|
||||
db.Tenant.Add(tenant);
|
||||
db.SaveChanges();
|
||||
_cache.Remove("tenants");
|
||||
@ -274,6 +274,14 @@ namespace Oqtane.Infrastructure
|
||||
return result;
|
||||
}
|
||||
|
||||
private InstallationContext GetInstallationContext()
|
||||
{
|
||||
var databaseType = _config.GetSection(SettingKeys.DatabaseSection)[SettingKeys.DatabaseTypeKey];
|
||||
var connectionString = NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey));
|
||||
|
||||
return new InstallationContext(databaseType, connectionString);
|
||||
}
|
||||
|
||||
private Installation MigrateTenants(InstallConfig install)
|
||||
{
|
||||
var result = new Installation { Success = false, Message = string.Empty };
|
||||
@ -284,9 +292,7 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
var upgrades = scope.ServiceProvider.GetRequiredService<IUpgradeManager>();
|
||||
|
||||
var databaseType = _config.GetSection(SettingKeys.DatabaseSection)[SettingKeys.DatabaseTypeKey];
|
||||
var connectionString = NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey));
|
||||
using (var db = new InstallationContext(databaseType, connectionString))
|
||||
using (var db = GetInstallationContext())
|
||||
{
|
||||
foreach (var tenant in db.Tenant.ToList())
|
||||
{
|
||||
@ -347,9 +353,8 @@ namespace Oqtane.Infrastructure
|
||||
if (moduleType != null)
|
||||
{
|
||||
var versions = moduleDefinition.ReleaseVersions.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
|
||||
var databaseType = _config.GetSection(SettingKeys.DatabaseSection)[SettingKeys.DatabaseTypeKey];
|
||||
var connectionString = NormalizeConnectionString(_config.GetConnectionString(SettingKeys.ConnectionStringKey));
|
||||
using (var db = new InstallationContext(databaseType, connectionString)) {
|
||||
using (var db = GetInstallationContext())
|
||||
{
|
||||
foreach (var tenant in db.Tenant.ToList())
|
||||
{
|
||||
var index = Array.FindIndex(versions, item => item == moduleDefinition.Version);
|
||||
|
@ -19,7 +19,7 @@ namespace Oqtane.Migrations
|
||||
migrationBuilder.Sql(
|
||||
@"
|
||||
UPDATE Tenant
|
||||
SET DBType = 'SqlServer'
|
||||
SET DBType = 'Oqtane.Repository.Databases.SqlServerDatabase, Oqtane.Server'
|
||||
");
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,9 @@ namespace Oqtane.Migrations.Extensions
|
||||
public static OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(this ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false)
|
||||
.Annotation("SqlServer:Identity", "1, 1")
|
||||
.Annotation("Sqlite:Autoincrement", true);
|
||||
.Annotation("SqlServer:Identity", "1, 1")
|
||||
.Annotation("Sqlite:Autoincrement", true)
|
||||
.Annotation("MySql:ValueGeneratedOnAdd", true);
|
||||
}
|
||||
|
||||
public static OperationBuilder<AddColumnOperation> AddBooleanColumn(this ColumnsBuilder table, string name, bool nullable = false)
|
||||
|
@ -1,12 +1,10 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Oqtane.Extensions;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Shared;
|
||||
|
||||
// ReSharper disable BuiltInTypeReferenceStyleForMemberAccess
|
||||
|
Reference in New Issue
Block a user