Skip to content

Fix "table already exists" crash on startup when deploying consolidated baseline migration#5

Merged
softcraftsman merged 3 commits into
mainfrom
copilot/fix-database-access-error
Mar 13, 2026
Merged

Fix "table already exists" crash on startup when deploying consolidated baseline migration#5
softcraftsman merged 3 commits into
mainfrom
copilot/fix-database-access-error

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

When migrations were consolidated into a single 20260313031641_NewDatabase baseline, already-deployed databases have all the tables but no record of that migration ID in __EFMigrationsHistory. Migrate() then tries to CREATE TABLE AuditLogs and crashes.

Changes

Program.cs — adds MarkBaselineMigrationIfTablesExist(db) called before Migrate():

  • Checks INFORMATION_SCHEMA.TABLES for ≥ 2 of AuditLogs, Institutions, DataProducts to confirm an existing schema (multi-table guard avoids false-positives)
  • Uses IHistoryRepository.GetCreateIfNotExistsScript() to create __EFMigrationsHistory if absent (covers databases bootstrapped via EnsureCreated())
  • Inserts the baseline migration record via IHistoryRepository.GetInsertScript() so Migrate() skips it
  • Reads the EF Core product version dynamically from the assembly rather than hardcoding
  • Returns early on a fresh database, leaving normal Migrate() behaviour intact
// Before Migrate(), mark the consolidated baseline as already applied
// if the schema pre-dates migration consolidation.
MarkBaselineMigrationIfTablesExist(db);
db.Database.Migrate();

Copilot AI and others added 3 commits March 13, 2026 10:10
Co-authored-by: softcraftsman <3756829+softcraftsman@users.noreply.github.com>
…se migration

Co-authored-by: softcraftsman <3756829+softcraftsman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants