mirror of
https://github.com/actualbudget/actual.git
synced 2026-03-11 17:47:26 -05:00
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:
@@ -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();
|
||||
});
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
58
packages/sync-server/src/migrations.ts
Normal file
58
packages/sync-server/src/migrations.ts
Normal 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;
|
||||
}
|
||||
}
|
||||
6
upcoming-release-notes/6346.md
Normal file
6
upcoming-release-notes/6346.md
Normal file
@@ -0,0 +1,6 @@
|
||||
---
|
||||
category: Bugfix
|
||||
authors: [jgranick]
|
||||
---
|
||||
|
||||
Fix sync-server migrations to use ESM loader
|
||||
Reference in New Issue
Block a user