Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#843 - Use JS sort method to sort migrations by name #844

Merged
2 changes: 1 addition & 1 deletion src/migration/migrator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -513,7 +513,7 @@ export class Migrator {
.orderBy(['timestamp', 'name'])
.execute()

return executedMigrations.map((it) => it.name)
return executedMigrations.map((it) => it.name).sort()
}

DavesBorges marked this conversation as resolved.
Show resolved Hide resolved
#ensureNoMissingMigrations(
Expand Down
42 changes: 42 additions & 0 deletions test/node/src/migration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,48 @@ for (const dialect of DIALECTS) {
'migration1',
])
})

describe('Migrate up should work when timestamps are equal', () => {
// The following lines of code simulate a situation where the migrations would have the
// timestamp

let originalToIsoString: typeof Date.prototype.toISOString

before(() => {
originalToIsoString = Date.prototype.toISOString
const defaultDateIsoString = new Date(2024, 0, 11).toISOString()
Date.prototype.toISOString = () => defaultDateIsoString
})

after(() => {
// Reset to originalToIsoString function so that upcoming tests are not affected
Date.prototype.toISOString = originalToIsoString
})
igalklebanov marked this conversation as resolved.
Show resolved Hide resolved

it('should use the same ordering strategy for migrations for both not executed migrations and executed migrations', async () => {
const [migrator1, executedUpMethods1] = createMigrations([
'2024-01-01-create-table',
'2024-01-01.2-update-table',
])

await migrator1.migrateToLatest()

const [migrator2, executedUpMethods2] = createMigrations([
'2024-01-01-create-table',
'2024-01-01.2-update-table',
])

const { results: results2, error } = await migrator2.migrateToLatest()
expect(error).to.be.undefined
expect(results2).to.eql([])

expect(executedUpMethods1).to.eql([
'2024-01-01-create-table',
'2024-01-01.2-update-table',
])
expect(executedUpMethods2).to.eql([])
})
})
})

if (dialect === 'postgres') {
Expand Down
Loading