Latest MigrationInfo is incorrect if all previous migrations have the same AppliedAt time stamp
andreas-koenig opened this issue · 1 comments
andreas-koenig commented
Description
PR #8 solved #7 by correctly querying the most recently applied MigrationInfo in descending order:
var migrationInfos = await DataConnection.GetTable<TMigrationInfo>()
.OrderByDescending(migrationInfo => migrationInfo.AppliedAt)
.Take(Take)
.ToListAsync(cancellationToken);
return migrationInfos.GetLatestMigrationInfo();
Unfortunately an incorrect migration is still returned in the following scenario:
- There are more than
Take
migrations (defaults to 100) - All migrations are applied onto an empty database in a single run of
MigrationEnginge.MigrateAsync()
. All of the corresponding MigrationInfos are assigned the same time stamp - During the next run of the MigrationEngine it is up to the database engine to determine the order as the
AppliedAt
values of allMigrationInfos
are equal. In the case of MSSQL Server the firstTake
migrations are returned, not the last. Consequently, migration #Take
is incorrectly identified as the last one and the engine tries to reapply the remaining migrations starting fromTake + 1
.
Possible Workarounds
- Use different timestamps for every pending migration. This does not solve the problem for existing databases, though.
- Order by the
AppliedAt
AND theId
column. This workaround depends on the ID generation for the MigrationInfos table.
feO2x commented
Yep, we should order by AppliedAt
and Id
- this is the best solution.