Skip to content

Commit eb279ca

Browse files
author
aligneddev
committed
Sqllite migration workaround
1 parent ef63e22 commit eb279ca

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

src/BikeTracking.Api/Program.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,37 @@
107107
await using (var scope = app.Services.CreateAsyncScope())
108108
{
109109
var dbContext = scope.ServiceProvider.GetRequiredService<BikeTrackingDbContext>();
110+
111+
// SQLite cannot execute DropCheckConstraint migrations. Apply the
112+
// supported migrations, mark those legacy migrations as applied,
113+
// then continue migration so endpoint queries run on migrated schema.
114+
var sqliteUnsupportedConstraintMigrations = new[]
115+
{
116+
"20260327165005_AddRideMilesUpperBound",
117+
"20260327171355_FixRideMilesUpperBoundNumericComparison",
118+
};
119+
120+
var provider = dbContext.Database.ProviderName;
121+
if (provider == "Microsoft.EntityFrameworkCore.Sqlite")
122+
{
123+
await dbContext.Database.MigrateAsync("20260327000000_AddRideVersion");
124+
125+
var applied = (await dbContext.Database.GetAppliedMigrationsAsync()).ToHashSet();
126+
foreach (var migration in sqliteUnsupportedConstraintMigrations)
127+
{
128+
if (applied.Contains(migration))
129+
{
130+
continue;
131+
}
132+
133+
await dbContext.Database.ExecuteSqlRawAsync(
134+
"INSERT INTO \"__EFMigrationsHistory\" (\"MigrationId\", \"ProductVersion\") VALUES ({0}, {1})",
135+
migration,
136+
"10.0.5"
137+
);
138+
}
139+
}
140+
110141
await dbContext.Database.MigrateAsync();
111142
}
112143

0 commit comments

Comments
 (0)