Compare commits

...

1 Commits

Author SHA1 Message Date
c844131bbe Переименование базы данных
All checks were successful
Unit tests / test (push) Successful in 56s
2025-01-24 17:31:58 +05:00
4 changed files with 71 additions and 17 deletions

View File

@ -1,16 +1,14 @@
using Mapster;
using DD.Persistence.Database.Postgres.Services;
using DD.Persistence.Models.Configurations;
using DD.Persistence.Services;
using DD.Persistence.Services.Interfaces;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Any;
using Microsoft.OpenApi.Models;
using DD.Persistence.Models;
using DD.Persistence.Models.Configurations;
using DD.Persistence.Services;
using DD.Persistence.Services.Interfaces;
using Swashbuckle.AspNetCore.SwaggerGen;
using System.Reflection;
using System.Text.Json.Nodes;
using DD.Persistence.Database.Entity;
namespace DD.Persistence.API;
@ -54,6 +52,7 @@ public static class DependencyInjection
{
services.AddTransient<IWitsDataService, WitsDataService>();
services.AddTransient<ITimestampedValuesService, TimestampedValuesService>();
services.AddTransient<IArchiveService, PostgresArchiveService>();
}
#region Authentication

View File

@ -6,7 +6,8 @@
}
},
"ConnectionStrings": {
"DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=postgres;Persist Security Info=True"
"DefaultConnection": "Host=localhost;Database=persistence;Username=postgres;Password=q;Persist Security Info=True",
"SystemBaseConnection": "Host=localhost;Database=postgres;Username=postgres;Password=q;Persist Security Info=True"
},
"AllowedHosts": "*",
"NeedUseKeyCloak": false,

View File

@ -0,0 +1,56 @@
using DD.Persistence.Services.Interfaces;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Npgsql;
namespace DD.Persistence.Database.Postgres.Services;
/// <summary>
/// Сервис по работе с источником данных - базой postgres
/// </summary>
public class PostgresArchiveService : IArchiveService
{
private string connectionString;
private ILogger<PostgresArchiveService> logger;
/// <summary>
/// Сервис по работе с источником данных - базой postgres
/// </summary>
/// <param name="configuration"></param>
public PostgresArchiveService(IConfiguration configuration, ILogger<PostgresArchiveService> logger)
{
this.connectionString = configuration.GetConnectionString("SystemBaseConnection")!;
this.logger = logger;
}
public Task Create(string newName, CancellationToken token)
{
throw new NotImplementedException();
}
public async Task Rename(string newName, CancellationToken token)
{
await using var connection = new NpgsqlConnection(connectionString);
try
{
await connection.OpenAsync();
var sqlCloseConnect =
$"SELECT pg_terminate_backend(pg_stat_activity.pid) " +
$"FROM pg_stat_activity " +
$"WHERE pg_stat_activity.datname = 'persistence' AND pid <> pg_backend_pid();";
await using var command = new NpgsqlCommand(sqlCloseConnect, connection);
await command.ExecuteNonQueryAsync();
var rename = $"ALTER DATABASE persistence RENAME TO {newName};";
await using var sqlRenameDatabase = new NpgsqlCommand(rename, connection);
await sqlRenameDatabase.ExecuteNonQueryAsync();
}
catch (NpgsqlException exception)
{
this.logger.LogError(exception.Message);
throw;
}
}
}

View File

@ -1,25 +1,23 @@
namespace DD.Persistence.Services.Interfaces;
/// <summary>
/// Сервис по работе с БД
/// Сервис по работе источниками данных
/// </summary>
internal interface IArchiveService
public interface IArchiveService
{
/// <summary>
/// Переименование БД
/// Переименование источника данных
/// </summary>
/// <param name="connectionString"></param>
/// <param name="databaseName"></param>
/// <param name="newName">наименование источника данных</param>
/// <param name="token"></param>
/// <returns></returns>
Task RenameDatabase(string connectionString, string databaseName, CancellationToken token);
Task Rename(string newName, CancellationToken token);
/// <summary>
/// Создание БД
/// Создание источника данных
/// </summary>
/// <param name="connectionString"></param>
/// <param name="databaseName"></param>
/// <param name="newName"></param>
/// <param name="token"></param>
/// <returns></returns>
Task CreateDatabase(string connectionString, string databaseName, CancellationToken token);
Task Create(string newName, CancellationToken token);
}