|
| 1 | +# FluentMigrator with SharpCoreDB — Embedded Mode Guide (v1.6.0) |
| 2 | + |
| 3 | +**Scope:** `SharpCoreDB.Extensions` FluentMigrator integration in **embedded/local** execution mode |
| 4 | +**Version label:** **v1.6.0 (V 1.60)** |
| 5 | + |
| 6 | +--- |
| 7 | + |
| 8 | +## 1. What Embedded Mode Means |
| 9 | + |
| 10 | +Embedded mode runs migrations directly against a local SharpCoreDB engine instance in the same process. |
| 11 | + |
| 12 | +Execution path: |
| 13 | + |
| 14 | +1. `IMigrationRunner` executes migration expressions. |
| 15 | +2. `SharpCoreDbProcessor` translates/handles migration operations. |
| 16 | +3. `SharpCoreDbMigrationExecutor` resolves execution target. |
| 17 | +4. Embedded path uses `IDatabase` first (or `DbConnection` fallback). |
| 18 | +5. SQL executes locally without network transport. |
| 19 | + |
| 20 | +This mode is the default path when you call: |
| 21 | + |
| 22 | +- `AddSharpCoreDBFluentMigrator(...)` |
| 23 | + |
| 24 | +--- |
| 25 | + |
| 26 | +## 2. DI Registration (Embedded) |
| 27 | + |
| 28 | +Use `AddSharpCoreDBFluentMigrator(...)` and scan migrations: |
| 29 | + |
| 30 | +```csharp |
| 31 | +using FluentMigrator.Runner; |
| 32 | +using SharpCoreDB.Extensions.Extensions; |
| 33 | + |
| 34 | +builder.Services.AddSharpCoreDBFluentMigrator(runner => |
| 35 | +{ |
| 36 | + runner.ScanIn(typeof(Program).Assembly).For.Migrations(); |
| 37 | +}); |
| 38 | +``` |
| 39 | + |
| 40 | +### Recommended startup execution |
| 41 | + |
| 42 | +```csharp |
| 43 | +using FluentMigrator.Runner; |
| 44 | + |
| 45 | +using var scope = app.Services.CreateScope(); |
| 46 | +var migrationRunner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); |
| 47 | +migrationRunner.MigrateUp(); |
| 48 | +``` |
| 49 | + |
| 50 | +### Automatic startup execution (`IHostedService`) |
| 51 | + |
| 52 | +```csharp |
| 53 | +public sealed class SharpCoreDbMigrationHostedService(IServiceProvider serviceProvider) : IHostedService |
| 54 | +{ |
| 55 | + private readonly IServiceProvider _serviceProvider = serviceProvider; |
| 56 | + |
| 57 | + public Task StartAsync(CancellationToken cancellationToken) |
| 58 | + { |
| 59 | + using var scope = _serviceProvider.CreateScope(); |
| 60 | + var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>(); |
| 61 | + runner.MigrateUp(); |
| 62 | + return Task.CompletedTask; |
| 63 | + } |
| 64 | + |
| 65 | + public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask; |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | +--- |
| 70 | + |
| 71 | +## 3. Migration State and Version Table |
| 72 | + |
| 73 | +SharpCoreDB FluentMigrator integration stores version metadata in: |
| 74 | + |
| 75 | +- `__SharpMigrations` |
| 76 | + |
| 77 | +Columns: |
| 78 | + |
| 79 | +- `Version` (primary key) |
| 80 | +- `AppliedOn` |
| 81 | +- `Description` |
| 82 | + |
| 83 | +The table is created automatically by `SharpCoreDbMigrationExecutor.EnsureVersionTable(...)` when processor creation occurs. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 4. Supported Workflow in Embedded Mode |
| 88 | + |
| 89 | +Typical commands: |
| 90 | + |
| 91 | +- `runner.MigrateUp()` |
| 92 | +- `runner.MigrateUp(targetVersion)` |
| 93 | +- `runner.Rollback(steps)` |
| 94 | + |
| 95 | +Typical migration pattern: |
| 96 | + |
| 97 | +```csharp |
| 98 | +[Migration(2026032301, "Create users table")] |
| 99 | +public sealed class CreateUsersTableMigration : Migration |
| 100 | +{ |
| 101 | + public override void Up() |
| 102 | + { |
| 103 | + Create.Table("users") |
| 104 | + .WithColumn("id").AsInt64().PrimaryKey().Identity() |
| 105 | + .WithColumn("name").AsString(200).NotNullable() |
| 106 | + .WithColumn("created_utc").AsDateTime().NotNullable(); |
| 107 | + } |
| 108 | + |
| 109 | + public override void Down() |
| 110 | + { |
| 111 | + Delete.Table("users"); |
| 112 | + } |
| 113 | +} |
| 114 | +``` |
| 115 | + |
| 116 | +--- |
| 117 | + |
| 118 | +## 5. Embedded Mode Behavior Details |
| 119 | + |
| 120 | +### Resolution order in `SharpCoreDbMigrationExecutor` |
| 121 | + |
| 122 | +1. `ISharpCoreDbMigrationSqlExecutor` (custom override, if registered) |
| 123 | +2. `IDatabase` (embedded engine path) |
| 124 | +3. `DbConnection` fallback |
| 125 | + |
| 126 | +If you only use `AddSharpCoreDBFluentMigrator(...)` and embedded database DI, path (2) is used. |
| 127 | + |
| 128 | +### Transaction model |
| 129 | + |
| 130 | +FluentMigrator transaction lifecycle methods are present on the processor. |
| 131 | +Behavior depends on SharpCoreDB SQL capabilities and executed statements. |
| 132 | + |
| 133 | +--- |
| 134 | + |
| 135 | +## 6. Performance and Reliability Recommendations |
| 136 | + |
| 137 | +1. Run migrations once at startup before serving traffic. |
| 138 | +2. Keep migrations idempotent where practical. |
| 139 | +3. Prefer additive schema changes for online upgrades. |
| 140 | +4. Use explicit rollback logic in `Down()` for controlled reversions. |
| 141 | +5. Keep migration scripts deterministic (no environment-dependent random behavior). |
| 142 | + |
| 143 | +--- |
| 144 | + |
| 145 | +## 7. Troubleshooting (Embedded) |
| 146 | + |
| 147 | +### A) "No SharpCoreDB execution source found" |
| 148 | + |
| 149 | +Cause: |
| 150 | +- `IDatabase` and `DbConnection` not registered, and no custom executor registered. |
| 151 | + |
| 152 | +Fix: |
| 153 | +- Register SharpCoreDB database services before `AddSharpCoreDBFluentMigrator(...)`. |
| 154 | + |
| 155 | +### B) Migration expression not supported |
| 156 | + |
| 157 | +Cause: |
| 158 | +- Expression translates to SQL not supported by the current engine behavior. |
| 159 | + |
| 160 | +Fix: |
| 161 | +- Rewrite migration to equivalent supported SQL operations. |
| 162 | +- For advanced custom behavior, use explicit SQL in migration steps. |
| 163 | + |
| 164 | +### C) Version table missing |
| 165 | + |
| 166 | +Cause: |
| 167 | +- Processor not initialized in the migration pipeline. |
| 168 | + |
| 169 | +Fix: |
| 170 | +- Ensure runner is created from DI and migrations are actually invoked. |
| 171 | + |
| 172 | +--- |
| 173 | + |
| 174 | +## 8. Production Checklist (Embedded) |
| 175 | + |
| 176 | +- [ ] `AddSharpCoreDBFluentMigrator(...)` is registered. |
| 177 | +- [ ] Migrations are scanned from correct assembly. |
| 178 | +- [ ] Startup path executes `MigrateUp()` exactly once. |
| 179 | +- [ ] Backup/restore strategy exists before schema upgrades. |
| 180 | +- [ ] Rollback policy is documented for failed deployments. |
| 181 | +- [ ] `__SharpMigrations` is monitored in diagnostics. |
| 182 | + |
| 183 | +--- |
| 184 | + |
| 185 | +## 9. Related Docs |
| 186 | + |
| 187 | +- `docs/migration/FLUENTMIGRATOR_SERVER_MODE_v1.6.0.md` |
| 188 | +- `docs/migration/MIGRATION_GUIDE.md` |
| 189 | +- `src/SharpCoreDB.Extensions/README.md` |
0 commit comments