Synnotech-AG/Synnotech.Migrations

Latest MigrationInfo is incorrect if all previous migrations have the same AppliedAt time stamp

andreas-koenig opened this issue · 1 comments

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:

  1. There are more than Take migrations (defaults to 100)
  2. 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
  3. During the next run of the MigrationEngine it is up to the database engine to determine the order as the AppliedAt values of all MigrationInfos are equal. In the case of MSSQL Server the first Take 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 from Take + 1.

Possible Workarounds

  1. Use different timestamps for every pending migration. This does not solve the problem for existing databases, though.
  2. Order by the AppliedAt AND the Id 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.