migrate database providers to core framework
This commit is contained in:
49
Oqtane.Server/Databases/PostgreSQL/HistoryRepository.cs
Normal file
49
Oqtane.Server/Databases/PostgreSQL/HistoryRepository.cs
Normal file
@ -0,0 +1,49 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Metadata.Builders;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Migrations.Internal;
|
||||
using Oqtane.Migrations.Framework;
|
||||
using Oqtane.Models;
|
||||
|
||||
// ReSharper disable ClassNeverInstantiated.Global
|
||||
|
||||
namespace Oqtane.Database.PostgreSQL
|
||||
{
|
||||
public class HistoryRepository : NpgsqlHistoryRepository
|
||||
{
|
||||
private string _appliedDateColumnName = "applied_date";
|
||||
private string _appliedVersionColumnName = "applied_version";
|
||||
private MigrationHistoryTable _migrationHistoryTable;
|
||||
|
||||
public HistoryRepository(HistoryRepositoryDependencies dependencies) : base(dependencies)
|
||||
{
|
||||
_migrationHistoryTable = new MigrationHistoryTable
|
||||
{
|
||||
TableName = TableName,
|
||||
TableSchema = TableSchema,
|
||||
MigrationIdColumnName = MigrationIdColumnName,
|
||||
ProductVersionColumnName = ProductVersionColumnName,
|
||||
AppliedVersionColumnName = _appliedVersionColumnName,
|
||||
AppliedDateColumnName = _appliedDateColumnName
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
protected override void ConfigureTable(EntityTypeBuilder<HistoryRow> history)
|
||||
{
|
||||
base.ConfigureTable(history);
|
||||
history.Property<string>(_appliedVersionColumnName).HasMaxLength(10);
|
||||
history.Property<DateTime>(_appliedDateColumnName);
|
||||
}
|
||||
|
||||
public override string GetInsertScript(HistoryRow row)
|
||||
{
|
||||
if (row == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(row));
|
||||
}
|
||||
|
||||
return MigrationUtils.BuildInsertScript(row, Dependencies, _migrationHistoryTable);
|
||||
}
|
||||
}
|
||||
}
|
165
Oqtane.Server/Databases/PostgreSQL/PostgreSQLDatabase.cs
Normal file
165
Oqtane.Server/Databases/PostgreSQL/PostgreSQLDatabase.cs
Normal file
@ -0,0 +1,165 @@
|
||||
using System;
|
||||
using System.Data;
|
||||
using System.Globalization;
|
||||
using EFCore.NamingConventions.Internal;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations.Operations.Builders;
|
||||
using Npgsql;
|
||||
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
|
||||
using Oqtane.Databases;
|
||||
|
||||
namespace Oqtane.Database.PostgreSQL
|
||||
{
|
||||
public class PostgreSQLDatabase : DatabaseBase
|
||||
{
|
||||
private static string _friendlyName => "PostgreSQL";
|
||||
|
||||
private static string _name => "PostgreSQL";
|
||||
|
||||
private readonly INameRewriter _rewriter;
|
||||
|
||||
static PostgreSQLDatabase()
|
||||
{
|
||||
Initialize(typeof(PostgreSQLDatabase));
|
||||
}
|
||||
|
||||
public PostgreSQLDatabase() : base(_name, _friendlyName)
|
||||
{
|
||||
_rewriter = new SnakeCaseNameRewriter(CultureInfo.InvariantCulture);
|
||||
}
|
||||
|
||||
public override string Provider => "Npgsql.EntityFrameworkCore.PostgreSQL";
|
||||
|
||||
public override OperationBuilder<AddColumnOperation> AddAutoIncrementColumn(ColumnsBuilder table, string name)
|
||||
{
|
||||
return table.Column<int>(name: name, nullable: false).Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityAlwaysColumn);
|
||||
}
|
||||
|
||||
public override string ConcatenateSql(params string[] values)
|
||||
{
|
||||
var returnValue = String.Empty;
|
||||
for (var i = 0; i < values.Length; i++)
|
||||
{
|
||||
if (i > 0)
|
||||
{
|
||||
returnValue += " || ";
|
||||
}
|
||||
returnValue += values[i];
|
||||
}
|
||||
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
public override int ExecuteNonQuery(string connectionString, string query)
|
||||
{
|
||||
var conn = new NpgsqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
using (conn)
|
||||
{
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var val = -1;
|
||||
try
|
||||
{
|
||||
val = cmd.ExecuteNonQuery();
|
||||
}
|
||||
catch
|
||||
{
|
||||
// an error occurred executing the query
|
||||
}
|
||||
return val;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public override IDataReader ExecuteReader(string connectionString, string query)
|
||||
{
|
||||
var conn = new NpgsqlConnection(connectionString);
|
||||
var cmd = conn.CreateCommand();
|
||||
PrepareCommand(conn, cmd, query);
|
||||
var dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
|
||||
return dr;
|
||||
}
|
||||
|
||||
public override string RewriteName(string name)
|
||||
{
|
||||
return _rewriter.RewriteName(name);
|
||||
}
|
||||
|
||||
public override string DelimitName(string name)
|
||||
{
|
||||
return $"\"{name}\"";
|
||||
}
|
||||
|
||||
public override string RewriteValue(object value)
|
||||
{
|
||||
var type = value.GetType().Name;
|
||||
if (type == "DateTime")
|
||||
{
|
||||
return $"'{value}'";
|
||||
}
|
||||
if (type == "Boolean")
|
||||
{
|
||||
return (bool)value ? "true" : "false"; // PostgreSQL uses true/false for boolean values
|
||||
}
|
||||
return value.ToString();
|
||||
}
|
||||
|
||||
public override void UpdateIdentityStoreTableNames(ModelBuilder builder)
|
||||
{
|
||||
foreach(var entity in builder.Model.GetEntityTypes())
|
||||
{
|
||||
var tableName = entity.GetTableName();
|
||||
if (tableName.StartsWith("AspNetUser"))
|
||||
{
|
||||
// replace table name
|
||||
entity.SetTableName(RewriteName(entity.GetTableName()));
|
||||
|
||||
// replace column names
|
||||
foreach(var property in entity.GetProperties())
|
||||
{
|
||||
property.SetColumnName(RewriteName(property.Name));
|
||||
}
|
||||
|
||||
// replace key names
|
||||
foreach(var key in entity.GetKeys())
|
||||
{
|
||||
key.SetName(RewriteName(key.GetName()));
|
||||
}
|
||||
|
||||
// replace foreign key names
|
||||
foreach (var key in entity.GetForeignKeys())
|
||||
{
|
||||
key.PrincipalKey.SetName(RewriteName(key.PrincipalKey.GetName()));
|
||||
}
|
||||
|
||||
// replace index names
|
||||
foreach (var index in entity.GetIndexes())
|
||||
{
|
||||
index.SetDatabaseName(RewriteName(index.GetDatabaseName()));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override DbContextOptionsBuilder UseDatabase(DbContextOptionsBuilder optionsBuilder, string connectionString)
|
||||
{
|
||||
return optionsBuilder.UseNpgsql(connectionString)
|
||||
.UseSnakeCaseNamingConvention()
|
||||
.ReplaceService<IHistoryRepository, HistoryRepository>();
|
||||
}
|
||||
|
||||
private void PrepareCommand(NpgsqlConnection conn, NpgsqlCommand cmd, string query)
|
||||
{
|
||||
if (conn.State != ConnectionState.Open)
|
||||
{
|
||||
conn.Open();
|
||||
}
|
||||
|
||||
cmd.Connection = conn;
|
||||
cmd.CommandText = query;
|
||||
cmd.CommandType = CommandType.Text;
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user