Migration Error after building with Electron

Hi everyone, thanks for your time and support in advance. I've come across some topics about migrations with Electron, but I'm still not sure how to proceed. I'm sorry if this is a trivial question, but how can I make it work? I'm using Electron-vite for scaffolding the project, and in development, it works, but throws an
Error: Can't find meta/_journal.json file at readMigrationFiles
Error: Can't find meta/_journal.json file at readMigrationFiles
error after the build. drizzle.config.ts:
import type { Config } from 'drizzle-kit'

export default {
schema: './src/database/schemas/!(exports).ts',
out: './src/database/migrations',
driver: 'better-sqlite'
} satisfies Config
import type { Config } from 'drizzle-kit'

export default {
schema: './src/database/schemas/!(exports).ts',
out: './src/database/migrations',
driver: 'better-sqlite'
} satisfies Config
src/database/index.ts:
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import Database from 'better-sqlite3'
import { dbPath } from './db-path'

import * as productSchema from './schemas/product'

const betterSqlite = new Database(dbPath)
const db = drizzle(betterSqlite, { schema: { ...productSchema } })

migrate(db, { migrationsFolder: 'src/database/migrations' })

export { db }
import { drizzle } from 'drizzle-orm/better-sqlite3'
import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import Database from 'better-sqlite3'
import { dbPath } from './db-path'

import * as productSchema from './schemas/product'

const betterSqlite = new Database(dbPath)
const db = drizzle(betterSqlite, { schema: { ...productSchema } })

migrate(db, { migrationsFolder: 'src/database/migrations' })

export { db }
error: A JavaScript error occurred in the main process Uncaught Exception: Error: Can't find meta/_journal.json file at readMigrationFiles (C:\Users\vitor\AppData\Local\Temp\2c5Ed9Qx4OlrkQZpC5qycYknJCU\resources\app.a..11) at Object.migrate (C:\Users\vitor\AppData\Local\Temp\2c5Ed9Qx4OIrkQZpC5qycYknJCU\resources\app.a...61) at Object. < anonymous> C\Users\vitor\AppData\Local\Temp\2c5Ed9Qx4OlrkQZpC5qycYknJCU\resources\app.a...10) at Module._compile (node:internal/modules/cjs/loader:1271:14) at Module. _extensions.js (node:internal/modules/cjs/loader:1326:10) at Module.load (node:internal/modules/cjs/loader:1126:32) at Module.load (node:internal/modules/cjs/loader:967:12) at c. load (node:electron/js2c/node_init:2:13672) at node:electron/js2c/browser_init:2:119675 at node:electron/js2c/bropvser_init:2;119884
No description
8 Replies
Lucas
Lucas11mo ago
Hey! Most likely you need to copy this folder manually into the output bundle. Since these assets are not actually copied automatically
Kacper Hemperek
Kacper Hemperek11mo ago
Hey, I encountered exactly same problem, I'm building my main process with webpack and added this this webpack config that builds for prod
const webpackConfig = {
// ...rest of webpack config
plugins: [
// ... other plugins
new CopyWebpackPlugin({
patterns: [
// other patherns if needed, in my case i also copy some config files
{
// this is a path to my migrations folder
from: path.join(webpackPaths.srcMainPath, 'db/drizzle'),
to: path.join(webpackPaths.distMainPath, 'db/drizzle'),
},
],
}),
]
}
const webpackConfig = {
// ...rest of webpack config
plugins: [
// ... other plugins
new CopyWebpackPlugin({
patterns: [
// other patherns if needed, in my case i also copy some config files
{
// this is a path to my migrations folder
from: path.join(webpackPaths.srcMainPath, 'db/drizzle'),
to: path.join(webpackPaths.distMainPath, 'db/drizzle'),
},
],
}),
]
}
even when I did that drizzle still can't find migration folder and I think thats because file path that I setup in drizzle.confg.ts is not coresponding with right folder after I build the app any idea what could i change?
VitorAndrey
VitorAndreyOP11mo ago
Hey man! I was actually trying to solve this right now then I saw your message lol. I've been fighting against this for a while now, and I remembered this morning that I used userData path to create the database file in in the same place it will be after build. Then I'm trying to do the same with migrations folder.
const userDataPath = app.getPath('userData')
const appDataPath = join(userDataPath, 'Database')

if (!fs.existsSync(appDataPath)) {
fs.mkdirSync(appDataPath)
}

const dbPath = join(appDataPath, 'sqlite.db')

export const betterSqlite = new Database(dbPath)
const db = drizzle(betterSqlite, { schema: { ...productSchema } })

migrate(db, { migrationsFolder: 'src/database/migrations' })

export { db }
const userDataPath = app.getPath('userData')
const appDataPath = join(userDataPath, 'Database')

if (!fs.existsSync(appDataPath)) {
fs.mkdirSync(appDataPath)
}

const dbPath = join(appDataPath, 'sqlite.db')

export const betterSqlite = new Database(dbPath)
const db = drizzle(betterSqlite, { schema: { ...productSchema } })

migrate(db, { migrationsFolder: 'src/database/migrations' })

export { db }
Notice that I used this dynamic dbPath variable which uses userDataPath to create the database file in an absolute location that works fine after build as well. I was looking for do the same thing with migrations folder something with same idea as your approach.
Kacper Hemperek
Kacper Hemperek11mo ago
Ok so here is what I did 1. Changed place where migrations are stored from earlier
{
from: path.join(webpackPaths.srcMainPath, 'db/drizzle'),
// changed db/drizzle to drizzle to keep the same relative
// path after app is built to only main.js file
to: path.join(webpackPaths.distMainPath, 'drizzle'),
},
{
from: path.join(webpackPaths.srcMainPath, 'db/drizzle'),
// changed db/drizzle to drizzle to keep the same relative
// path after app is built to only main.js file
to: path.join(webpackPaths.distMainPath, 'drizzle'),
},
Now my dist folder looks like this
dist
config (my project specific config files)
drizzle (my folder with migrations)
main.js
preload.js
dist
config (my project specific config files)
drizzle (my folder with migrations)
main.js
preload.js
2. I changed my drizzle.config.ts to be relative to path depending if im in production environment (not sure if that does anything tbh but did it just in case)
const migrationFolder =
process.env.NODE_ENV === 'development'
? 'src/main/db/drizzle'
: 'release/app/dist/main/db/drizzle';

// eslint-disable-next-line import/no-default-export
export default {
schema: './src/main/db/schema.ts',
out: migrationFolder,
} satisfies Config;
const migrationFolder =
process.env.NODE_ENV === 'development'
? 'src/main/db/drizzle'
: 'release/app/dist/main/db/drizzle';

// eslint-disable-next-line import/no-default-export
export default {
schema: './src/main/db/schema.ts',
out: migrationFolder,
} satisfies Config;
3. I modified my database setup function to get path to migration directory using __dirname like this
function getMigrationPath() {
return path.join(__dirname, 'drizzle');
}

export function setupDatabaseDrizzle() {
const migrationsFolder = getMigrationPath();
// this just returns path to db file
const dbPath = getDatabasePath();
// this DatabaseConnection is alias for Database from better-sqlite
const sqlite = DatabaseConnection(dbPath);
const db = drizzle(sqlite);
migrate(db, { migrationsFolder });
return db;
}
function getMigrationPath() {
return path.join(__dirname, 'drizzle');
}

export function setupDatabaseDrizzle() {
const migrationsFolder = getMigrationPath();
// this just returns path to db file
const dbPath = getDatabasePath();
// this DatabaseConnection is alias for Database from better-sqlite
const sqlite = DatabaseConnection(dbPath);
const db = drizzle(sqlite);
migrate(db, { migrationsFolder });
return db;
}
After i created path to migrations folder with __dirname it started working just fine Hope this helps, if you have any other questions let me know
VitorAndrey
VitorAndreyOP11mo ago
I see 👏 ,very clever! now I've to get it done with this electron-vite template I'm testing https://electron-vite.org/guide/. I'm not sure if they use webpack, I will check in docs but you helped a lot, now I lest I know what I need to learn to get it done. 😁 Thnx!
Getting Started | electron-vite
Next generation Electron build tooling based on Vite.
Kacper Hemperek
Kacper Hemperek11mo ago
They use vite instead of webpack not sure how to accomplish the same thing there but I bet you should look if you even have migration folder inside dist file and if not adjust vite config to clone. When you have that working my sollution should work just fine
Kacper Hemperek
Kacper Hemperek11mo ago
You should edit main part of that most likely https://electron-vite.org/guide/#configuring-electron-vite
Getting Started | electron-vite
Next generation Electron build tooling based on Vite.
VitorAndrey
VitorAndreyOP11mo ago
Ok thank you so much man!
Want results from more Discord servers?
Add your server