Fix sync server migrations (#6346)

* Fix sync-server migrations to use ESM loader

* Add release notes

* Apply CodeRabbit suggestions

* [autofix.ci] apply automated fixes

* Add file extension filter to sync-server migrations import

* [autofix.ci] apply automated fixes

* Ensure migrations occur synchronously

* [autofix.ci] apply automated fixes

* Minor cleanup

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
Co-authored-by: Matiss Janis Aboltins <matiss@mja.lv>
Co-authored-by: Matt Fiddaman <github@m.fiddaman.uk>
This commit is contained in:
Joshua Granick
2026-02-04 12:00:42 -08:00
committed by GitHub
parent 6da6f505e6
commit 7732fac8b6
3 changed files with 64 additions and 39 deletions

View File

@@ -1,39 +0,0 @@
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import migrate from 'migrate';
import { config } from './load-config';
export function run(direction = 'up') {
console.log(
`Checking if there are any migrations to run for direction "${direction}"...`,
);
const __dirname = dirname(fileURLToPath(import.meta.url)); // this directory
return new Promise(resolve =>
migrate.load(
{
stateStore: `${path.join(config.get('dataDir'), '.migrate')}${
config.get('mode') === 'test' ? '-test' : ''
}`,
migrationsDirectory: path.join(__dirname, '../migrations'),
},
(err, set) => {
if (err) {
throw err;
}
set[direction](err => {
if (err) {
throw err;
}
console.log('Migrations: DONE');
resolve();
});
},
),
);
}

View File

@@ -0,0 +1,58 @@
import { readdir } from 'node:fs/promises';
import path, { dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { load } from 'migrate';
import { config } from './load-config';
type MigrationCallback = (err?: Error) => void;
export async function run(direction: 'up' | 'down' = 'up'): Promise<void> {
console.log(
`Checking if there are any migrations to run for direction "${direction}"...`,
);
const __dirname = dirname(fileURLToPath(import.meta.url)); // this directory
const migrationsDir = path.join(__dirname, '../migrations');
try {
// Load all script files in the migrations directory
const files = await readdir(migrationsDir);
const migrationsModules: Record<
string,
{
up: (next?: MigrationCallback) => void;
down: (next?: MigrationCallback) => void;
}
> = {};
for (const f of files
.filter(f => f.endsWith('.js') || f.endsWith('.ts'))
.sort()) {
migrationsModules[f] = await import(path.join(migrationsDir, f));
}
return new Promise<void>((resolve, reject) => {
load(
{
stateStore: `${path.join(config.get('dataDir'), '.migrate')}${config.get('mode') === 'test' ? '-test' : ''}`,
migrations: migrationsModules,
},
(err, set) => {
if (err) return reject(err);
set[direction](err => {
if (err) return reject(err);
console.log('Migrations: DONE');
resolve();
});
},
);
});
} catch (err) {
console.error('Error during migration process:', err);
throw err;
}
}

View File

@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [jgranick]
---
Fix sync-server migrations to use ESM loader