Performing Drizzle ORM migration in vitest results in permission errors

In a test i'm using Drizzle ORM to do migrations in vitest beforeAll, but it doesn't work like when I run a script from

    "db:migrate": "tsx db/migrate.ts", //runs with no issues

which then calls this

import { migrate } from 'drizzle-orm/better-sqlite3/migrator'
import { db } from './db'

import { fileURLToPath } from 'url'
import { dirname, join } from 'path'

export const migrateWrapper = async () => {
    await migrate(db, { migrationsFolder: migrationsPath }) // does the actual migration
}

but when running in a test file I get root cause error:

// console.log('yearly-recurring-events.test.ts')
beforeAll(async () => {
  await resetDatabase() //includes the migration call
  await seedRecurringEvents()
})


Caused by: SqliteError: disk I/O error
 ❯ PreparedQuery.run node_modules/.pnpm/drizzle-orm@0.29.2_@types+react@18.2.45_better-sqlite3@9.2.2_expo-sqlite@13.2.0_react@18.2.0/node_modules/src/better-sqlite3/session.ts:103:20
 ❯ BetterSQLiteSession.run node_modules/.pnpm/drizzle-orm@0.29.2_@types+react@18.2.45_better-sqlite3@9.2.2_expo-sqlite@13.2.0_react@18.2.0/node_modules/src/sqlite-core/session.ts:140:67
 ❯ SQLiteSyncDialect.migrate node_modules/.pnpm/drizzle-orm@0.29.2_@types+react@18.2.45_better-sqlite3@9.2.2_expo-sqlite@13.2.0_react@18.2.0/node_modules/src/sqlite-core/dialect.ts:721:11
 .
.


and then results in

Migrating the database...

stderr | Module.migrateWrapper (/Users/nikos/WebstormProjects/drizzle-sqlite-recurring-events/db/migrateUtil.ts:19:13)
Failed to migrate the database: DrizzleError: Failed to run the query '
                        CREATE TABLE IF NOT EXISTS "__drizzle_migrations" (
                                id SERIAL PRIMARY KEY,
                                hash text NOT NULL,
                                created_at numeric
                        )
                '
Was this page helpful?