diff options
author | crupest <crupest@outlook.com> | 2022-12-25 14:52:46 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-12-25 14:53:58 +0800 |
commit | c8e6e2081b6d1a1b1f4b7ddd8923e2af70f82e29 (patch) | |
tree | 0004822a2ea7a987176e28396c7cc74cb61ef692 /docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs | |
parent | 71bda510363822defa74760d93947ff33a8775f0 (diff) | |
download | crupest-c8e6e2081b6d1a1b1f4b7ddd8923e2af70f82e29.tar.gz crupest-c8e6e2081b6d1a1b1f4b7ddd8923e2af70f82e29.tar.bz2 crupest-c8e6e2081b6d1a1b1f4b7ddd8923e2af70f82e29.zip |
Add migration.
Diffstat (limited to 'docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs')
-rw-r--r-- | docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs | 30 |
1 files changed, 18 insertions, 12 deletions
diff --git a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs index 9a0ec95..b8a1bbe 100644 --- a/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs +++ b/docker/crupest-api/CrupestApi/CrupestApi.Commons/Secrets/SecretService.cs @@ -1,26 +1,32 @@ using System.Data; using CrupestApi.Commons.Crud; +using CrupestApi.Commons.Crud.Migrations; namespace CrupestApi.Commons.Secrets; public class SecretService : CrudService<SecretInfo>, ISecretService { - public SecretService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, ILoggerFactory loggerFactory) - : base(tableInfoFactory, dbConnectionFactory, loggerFactory) - { + private readonly ILogger<SecretService> _logger; + public SecretService(ITableInfoFactory tableInfoFactory, IDbConnectionFactory dbConnectionFactory, IDatabaseMigrator migrator, ILoggerFactory loggerFactory) + : base(tableInfoFactory, dbConnectionFactory, migrator, loggerFactory) + { + _logger = loggerFactory.CreateLogger<SecretService>(); } - protected override void DoInitializeDatabase(IDbConnection connection) + protected override void AfterMigrate(IDbConnection connection, TableInfo table, ILoggerFactory loggerFactory) { - base.DoInitializeDatabase(connection); - using var transaction = connection.BeginTransaction(); - var insertClause = InsertClause.Create() - .Add(nameof(SecretInfo.Key), SecretsConstants.SecretManagementKey) - .Add(nameof(SecretInfo.Secret), "crupest") - .Add(nameof(SecretInfo.Description), "This is the init key. Please revoke it immediately after creating a new one."); - _table.Insert(connection, insertClause, out var _); - transaction.Commit(); + if (table.SelectCount(connection) == 0) + { + loggerFactory.CreateLogger<SecretService>().LogInformation("No secrets found, insert default secrets."); + using var transaction = connection.BeginTransaction(); + var insertClause = InsertClause.Create() + .Add(nameof(SecretInfo.Key), SecretsConstants.SecretManagementKey) + .Add(nameof(SecretInfo.Secret), "crupest") + .Add(nameof(SecretInfo.Description), "This is the init key. Please revoke it immediately after creating a new one."); + _table.Insert(connection, insertClause, out var _); + transaction.Commit(); + } } public void CreateTestSecret(string key, string secret) |