From c844131bbe1cc9716473a2e2d449d430e4c5416b Mon Sep 17 00:00:00 2001 From: Olga Nemt Date: Fri, 24 Jan 2025 17:31:58 +0500 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B5=D0=B8=D0=BC=D0=B5?= =?UTF-8?q?=D0=BD=D0=BE=D0=B2=D0=B0=D0=BD=D0=B8=D0=B5=20=D0=B1=D0=B0=D0=B7?= =?UTF-8?q?=D1=8B=20=D0=B4=D0=B0=D0=BD=D0=BD=D1=8B=D1=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- DD.Persistence.API/DependencyInjection.cs | 11 ++-- DD.Persistence.App/appsettings.json | 3 +- .../Services/ArchiveService.cs | 56 +++++++++++++++++++ .../Services/Interfaces/IArchiveService.cs | 18 +++--- 4 files changed, 71 insertions(+), 17 deletions(-) create mode 100644 DD.Persistence.Database.Postgres/Services/ArchiveService.cs diff --git a/DD.Persistence.API/DependencyInjection.cs b/DD.Persistence.API/DependencyInjection.cs index b543841..214fb61 100644 --- a/DD.Persistence.API/DependencyInjection.cs +++ b/DD.Persistence.API/DependencyInjection.cs @@ -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(); services.AddTransient(); + services.AddTransient(); } #region Authentication diff --git a/DD.Persistence.App/appsettings.json b/DD.Persistence.App/appsettings.json index 7ad8c67..0d69803 100644 --- a/DD.Persistence.App/appsettings.json +++ b/DD.Persistence.App/appsettings.json @@ -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, diff --git a/DD.Persistence.Database.Postgres/Services/ArchiveService.cs b/DD.Persistence.Database.Postgres/Services/ArchiveService.cs new file mode 100644 index 0000000..248102d --- /dev/null +++ b/DD.Persistence.Database.Postgres/Services/ArchiveService.cs @@ -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; + +/// +/// Сервис по работе с источником данных - базой postgres +/// +public class PostgresArchiveService : IArchiveService +{ + private string connectionString; + private ILogger logger; + + /// + /// Сервис по работе с источником данных - базой postgres + /// + /// + public PostgresArchiveService(IConfiguration configuration, ILogger 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; + } + + } +} diff --git a/DD.Persistence/Services/Interfaces/IArchiveService.cs b/DD.Persistence/Services/Interfaces/IArchiveService.cs index 9829e21..1081417 100644 --- a/DD.Persistence/Services/Interfaces/IArchiveService.cs +++ b/DD.Persistence/Services/Interfaces/IArchiveService.cs @@ -1,25 +1,23 @@ namespace DD.Persistence.Services.Interfaces; /// -/// Сервис по работе с БД +/// Сервис по работе источниками данных /// -internal interface IArchiveService +public interface IArchiveService { /// - /// Переименование БД + /// Переименование источника данных /// - /// - /// + /// наименование источника данных /// /// - Task RenameDatabase(string connectionString, string databaseName, CancellationToken token); + Task Rename(string newName, CancellationToken token); /// - /// Создание БД + /// Создание источника данных /// - /// - /// + /// /// /// - Task CreateDatabase(string connectionString, string databaseName, CancellationToken token); + Task Create(string newName, CancellationToken token); }