From e17889748a57987d052693a2246ccd3e3dd8fc16 Mon Sep 17 00:00:00 2001 From: smoh Date: Thu, 11 Dec 2025 14:34:54 +0900 Subject: [PATCH] =?UTF-8?q?=EC=B4=88=EA=B8=B0=ED=99=94=20=EB=B0=A9?= =?UTF-8?q?=EC=8B=9D=20=EB=B3=80=EA=B2=BD,=20JiraIssue=EA=B4=80=EB=A0=A8?= =?UTF-8?q?=20=EB=85=BC=EB=A6=AC=20=EC=B6=94=EA=B0=80.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Services/AppDbService.cs | 109 +++++++++++++++++++++++++++++++++++---- 1 file changed, 100 insertions(+), 9 deletions(-) diff --git a/Services/AppDbService.cs b/Services/AppDbService.cs index b6c30c4..1f71396 100644 --- a/Services/AppDbService.cs +++ b/Services/AppDbService.cs @@ -31,15 +31,6 @@ public class AppDbService { _log.Information("Database already exists"); } - - // Migration - var pendingMigrations = await _db.Database.GetPendingMigrationsAsync(); - if (pendingMigrations.Any()) - { - _log.Information("Applying pending migrations..."); - await _db.Database.MigrateAsync(); - _log.Information("Migrations applied successfully"); - } } catch (Exception ex) { @@ -48,6 +39,7 @@ public class AppDbService } } + #region Table: Configs public async Task> LoadConfigAsync() { try @@ -100,6 +92,105 @@ public class AppDbService throw; } } + #endregion Table: Configs + public async Task UpsertJiraIssueAsync(JiraIssue issue, CancellationToken ct = default) + { + try + { + var existing = await _db.JiraIssues.FindAsync([issue.Key], ct); + + if (existing == null) + { + _db.JiraIssues.Add(issue); + _log.Debug("Inserting new Jira issue: {key}", issue.Key); + } + else + { + if (issue.Updated > existing.Updated) + { + existing.Summary = issue.Summary; + existing.Parent = issue.Parent; + existing.Type = issue.Type; + existing.Status = issue.Status; + existing.Assignee = issue.Assignee; + existing.Manager = issue.Manager; + existing.Due = issue.Due; + existing.Updated = issue.Updated; + existing.ObjectId = issue.ObjectId; + existing.NeedNotify = issue.NeedNotify; + + _db.JiraIssues.Update(existing); + _log.Debug("Updating existing Jira issue: {key}", issue.Key); + } + else + { + _log.Debug("Skipping Jira issue (not newer): {key}", issue.Key); + } + } + + await _db.SaveChangesAsync(ct); + } + catch (Exception ex) + { + _log.Error(ex, "Failed to upsert Jira issue: {key}", issue.Key); + throw; + } + } + + public async Task UpsertJiraIssuesBatchAsync(IEnumerable issues, CancellationToken ct = default) + { + try + { + var issueList = issues.ToList(); + if (!issueList.Any()) + return; + + var keys = issueList.Select(i => i.Key).ToList(); + var existingIssues = await _db.JiraIssues + .Where(ji => keys.Contains(ji.Key)) + .ToDictionaryAsync(ji => ji.Key, ct); + + int insertCount = 0, updateCount = 0, skipCount = 0; + + foreach (var issue in issueList) + { + if (!existingIssues.TryGetValue(issue.Key, out var existing)) + { + _db.JiraIssues.Add(issue); + insertCount++; + } + else if (issue.Updated > existing.Updated) + { + existing.Summary = issue.Summary; + existing.Parent = issue.Parent; + existing.Type = issue.Type; + existing.Status = issue.Status; + existing.Assignee = issue.Assignee; + existing.Manager = issue.Manager; + existing.Due = issue.Due; + existing.Updated = issue.Updated; + existing.ObjectId = issue.ObjectId; + existing.NeedNotify = issue.NeedNotify; + + _db.JiraIssues.Update(existing); + updateCount++; + } + else + { + skipCount++; + } + } + + await _db.SaveChangesAsync(ct); + _log.Information("Batch completed: {insert} inserted, {update} updated, {skip} skipped out of {total} issues", + insertCount, updateCount, skipCount, issueList.Count); + } + catch (Exception ex) + { + _log.Error(ex, "Failed to batch upsert Jira issues"); + throw; + } + } }