Database migrations - typeORM sqlIte #1289
Replies: 1 comment
-
|
Got this figured out. Used a shotgun approach on the migration globs function and then later /**
* Return the absolute glob to your migrations folder without duplicating "db".
* Call this from a file that lives under src/main/db/** (compiled to dist/main/db/**).
*/
export function resolveMigrationGlobs(metaUrl: string) {
const here = dirnameOfThisFile(metaUrl); // directory of the caller
const ext = isPackaged() ? '*.js' : '*.ts';
// Start from the caller's dir and detect where "migrations" actually is.
// Try the most common layouts first:
const candidates = [
path.join(here, 'migrations'),
path.join(here, 'db', 'migrations'),
path.join(here, '..', 'db', 'migrations'),
];
if (!isPackaged()) {
candidates.unshift(path.join(here, '..', 'src', 'db', 'migrations'));
}
const migrationsDir =
candidates.find(d => fs.existsSync(d) && fs.statSync(d).isDirectory()) ??
path.join(here, 'migrations'); // last resort
logDiagnostic(`[migrations] base: ${here}`);
logDiagnostic(`[migrations] picked: ${migrationsDir}, ext: ${ext}`);
debugListMigrations(migrationsDir);
return [path.join(migrationsDir, ext)];
}and within the db init, export async function initDb() {
if (AppDataSource?.isInitialized) return AppDataSource;
await ensureElectronReady();
const dbPath = resolveDbPath();
const migrations = resolveMigrationGlobs(import.meta.url);
AppDataSource = new DataSource({
type: 'sqlite',
database: dbPath,
entities: [Run, Step, Log, Artifact, Setting],
migrations,
synchronize: false,
migrationsTableName: 'typeorm_migrations',
logging: false,
});
logDiagnostic(`[DB] Initializing database at ${dbPath}`);
await AppDataSource.initialize();
await applySQLitePragmas(AppDataSource);
await runPendingMigrations(AppDataSource);
return AppDataSource;
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I'm using sqlite with typeORM to manage state in my application. Getting the database migrations to compile has not been easy. The problem i still have is getting my migration runner to find the migration files.
i added a
tsconfig.migrations.jsonin packages/main. The build script is now,"build": "vite build && tsc -p tsconfig.migrations.json",. This gets the migrations compile and into the dist folder and eventually into the rootdist/node_modules/@app/main/dist/db/migrations.tsconfig.migrations.json{ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "dist/db/migrations", "declaration": false, "sourceMap": false, "module": "NodeNext", "moduleResolution": "NodeNext", "target": "ES2020", "emitDecoratorMetadata": true, "experimentalDecorators": true, "useDefineForClassFields": false }, "include": [ "src/db/migrations/**/*.ts" ] }Beta Was this translation helpful? Give feedback.
All reactions