VersionLoader doesn't recreate the version table after it is dropped while the loader is alive

Open
#2,437 6 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

@seragAhmed is already working on this.

Since Nov 13, 2026.

Assessment

Difficulty
4/5
Estimated time
1-2 days
Newbie friendliness
54/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
csharp, sqlite
Domain
databases

Research direction

Start with src/FluentMigrator.Runner.Core/VersionLoader.cs, especially LoadVersionInfo() and RemoveVersionTable(), and trace how the creation flags interact with preview mode and HasCreatedVersionTable. Reproduce with SQLite using the same runner scope: migrate up, roll back to version 0, then migrate up again. Done means the version table is recreated safely after removal, including when another process dropped it, while preview behavior and creation reporting remain correct.

Written by the indexing model from the issue text.

Description

area:VersionInfo bug

Summary

VersionLoader remembers that it has created the version table, and never checks that again. If the version table is later dropped while that VersionLoader instance is still alive, the next MigrateUp() through the same runner skips recreating the table. It then fails when it records the applied migration:

SQLite Error 1: 'no such table: VersionInfo'.

Any of these can drop the table:

  • RollbackToVersion(0)
  • Rollback(n) that reverts every applied migration
  • another process doing either of those

This has been the behaviour since 2012. It does not depend on the application locking added in #2432, but that review is where it came up (see below).

How to reproduce

The version table must not exist when the runner's VersionLoader is created, so the loader creates it. Then, in the same dependency-injection scope:

// Empty database, so no VersionInfo table yet
using var scope = serviceProvider.CreateScope();
var runner = scope.ServiceProvider.GetRequiredService<IMigrationRunner>();

runner.MigrateUp();          // VersionLoader creates VersionInfo, then migration 1 is applied
runner.RollbackToVersion(0); // migration 1 reverted, VersionInfo dropped
runner.MigrateUp();          // throws: no such table: VersionInfo

It also fails across two processes. Instance A runs MigrateUp() against an empty database and keeps its scope open. Instance B runs RollbackToVersion(0). A's next MigrateUp() fails the same way.

It works when the version table already existed before the runner was resolved, because then the loader never set its "created" flag. Run MigrateUp() once in a separate scope first, and the sequence above succeeds.

Reproduced on main at cd7ae9e3, with SQLite. Nothing in the code is provider-specific.

Why it happens

VersionLoader (src/FluentMigrator.Runner.Core/VersionLoader.cs) has four flags, one per version-table step:

Flag Step it guards
_versionSchemaMigrationAlreadyRun create the schema
_versionMigrationAlreadyRun create the table
_versionUniqueMigrationAlreadyRun add the AppliedOn column and unique index
_versionDescriptionMigrationAlreadyRun add the Description column

LoadVersionInfo() runs a step only when the object is missing in the database and the flag is not set:

if (!AlreadyCreatedVersionTable && !_versionMigrationAlreadyRun)
{
    Runner.Up(VersionMigration);
    _versionMigrationAlreadyRun = true;
}

RemoveVersionTable() drops the table, and the schema when OwnsSchema is true, but never clears the flags. So once a loader has created the table, it will not create it again, even after the table is gone. LoadVersionInfo() then sees no table, loads an empty version list, and the runner applies migrations. UpdateVersionInfo() then inserts into a table that does not exist.

The runner's VersionLoader lives for the whole scope: it is created lazily and cached by MigrationRunner. Long-lived scopes therefore hit this, for example a hosted service or a test fixture that reuses one runner.

Why the flags can't simply be deleted

The flags do two jobs that a fix must keep:

  1. Preview mode. With --preview / AsGlobalPreview(), the processor logs the CREATE TABLE instead of running it, so the table never exists. Without a flag, every call to LoadVersionInfo() would emit the version-table DDL again.
  2. HasCreatedVersionTable and HasCreatedVersionSchema (added for #2170 in #2405) report whether this loader created those objects. MigrationRunner.LoadVersionInfoIfRequired() uses that to tell the caller a table was created. The existence check alone can't answer that, because the table already exists once the constructor returns.

Possible fixes

  • Reset the flags in RemoveVersionTable(). This is the smallest change and fixes the single-process case. It does not fix the two-process case, because instance B's RemoveVersionTable() cannot reset instance A's flags.
  • Trust the database when not in preview. Use the flags only to stop repeated DDL in preview mode, and otherwise act on AlreadyCreated* alone. Keep "this loader created it" as a separate value for HasCreated*. This fixes both cases. The extra existence checks are already run today on every LoadVersionInfo(), so there is no new cost.

Either way, the check and the create should stay under the application lock from #2432 (IApplicationLocker). The two-process case is a race unless they do.

Is it a blocker?

No. It needs the version table to be dropped while a runner that created it is still in use, which is rare outside tests and teardown scripts. Most applications run MigrateUp() once per scope and exit. Suggested milestone: 10.0.0, alongside the async runner work, which reshapes VersionLoader anyway. It could also go into a 9.x patch release if the reset-in-RemoveVersionTable() fix is enough.

Background

GitHub Copilot raised this while reviewing #2432 (comment). It was deferred there because #2432 adds an opt-in application lock (#1949), and this bug exists with or without that lock. #2432 did fix a related problem: Rollback, RollbackToVersion and MigrateDown now reload the version list before choosing what to revert. That PR did not change how VersionLoader decides whether to recreate the table.

🤖 Generated with Claude Code

Dominant language
C#
Stars
3.5k
Forks
702
Avg merge
2d 15h
Merged PRs (30d)
19

Getting set up

Open in Codespaces

Starts the project's dev container in your browser, under your own GitHub account.

  • No Dockerfile or Docker Compose file
  • No pull request template
  • No contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from fluentmigrator/fluentmigrator

All issues in fluentmigrator/fluentmigrator

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.