Hi guys, quite a problem I try to tackle.
Background:
I am trying to create an Electron/React/Drizzle/better-sqlite3 app. Database should be hosted locally on users computer, in AppData folder.
I have created migration files and added them to "asar" folder when application is built.
It works great on dev (I should create some kind of storage for information if migration should run, but it is fine for now if it runs all the time), but when I build and install app, sometimes I get error referring that database is not created.
The biggest problem is that if I check created database with DBeaver, it has no tables so migration didn't run and yes I get errors in my app that table doesn't exist.
This is my first try to create an Electron app. Can you maybe help me get this right? What am I doing wrong?
app
.whenReady()
.then(() => {
runMigrations()
})
.then(() => {
electronApp.setAppUserModelId('com.electron')
app.on('browser-window-created', (_, window) => {
optimizer.watchWindowShortcuts(window)
})
createWindow()
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) createWindow()
})
})
const migrateDb = (database: typeof db) => {
const migrationsPath =
process.env.NODE_ENV === 'development'
? 'public/drizzle' // Local development path
: join(app.getAppPath(), 'resources', 'app.asar.unpacked', 'public', 'drizzle')
migrate(database, { migrationsFolder: migrationsPath })
}
const main = () => {
try {
migrateDb(db)
console.log('Migration successful')
} catch (e) {
console.log(e)
}
}
export default main
const dbPath =
process.env.NODE_ENV === 'development'
? './animal_care.db'
: join(app.getPath('appData'), 'animal-care', 'animal_care.db')
const sqlite = new Database(dbPath)
sqlite.pragma('journal_mode = WAL')
const db = drizzle(sqlite, { schema })
export default db
Thank you in advance! 🙂