| 1 | using System.Data; |
| 2 | using Microsoft.EntityFrameworkCore; |
| 3 | |
| 4 | namespace AgentForge.Data; |
| 5 | |
| 6 | internal static class ForgeSchemaProbe |
| 7 | { |
| 8 | internal static bool TableExists(ForgeDbContext db, string table) |
| 9 | { |
| 10 | var connection = db.Database.GetDbConnection(); |
| 11 | var openedHere = connection.State != ConnectionState.Open; |
| 12 | if (openedHere) |
| 13 | connection.Open(); |
| 14 | |
| 15 | try |
| 16 | { |
| 17 | using var command = connection.CreateCommand(); |
| 18 | command.CommandText = $"SELECT 1 FROM \"{table}\" LIMIT 1"; |
| 19 | _ = command.ExecuteScalar(); |
| 20 | return true; |
| 21 | } |
| 22 | catch |
| 23 | { |
| 24 | return false; |
| 25 | } |
| 26 | finally |
| 27 | { |
| 28 | if (openedHere) |
| 29 | connection.Close(); |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | internal static void TryAddColumn(ForgeDbContext db, string table, string column, string definition) |
| 34 | { |
| 35 | try |
| 36 | { |
| 37 | #pragma warning disable EF1002 |
| 38 | db.Database.ExecuteSqlRaw($"ALTER TABLE \"{table}\" ADD COLUMN \"{column}\" {definition}"); |
| 39 | #pragma warning restore EF1002 |
| 40 | } |
| 41 | catch |
| 42 | { |
| 43 | // Column already exists on partially migrated volumes. |
| 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
View only · write via MCP/CIDE