drizzle-team / drizzle-team/drizzle-orm
[BUG]: Drizzle ORM with Expo SQLite driver crashes app when more than one migration is present
- Dominant language
- TypeScript
- Stars
- 35.8k
- Forks
- 1.6k
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 4
Description
### What version of `drizzle-orm` are you using?
0.30.10
### What version of `drizzle-kit` are you using?
0.21.4
### Describe the Bug
When installing Drizzle into a blank Expo SDK 51 project as per [the documentation](https://orm.drizzle.team/docs/get-started-sqlite#expo-sqlite), creating a database schema, the initial migration using `drizzle-kit` and the provided `useMigrations()` hook provided by Drizzle, everything works as expected. The migration is being applied and the database file created.
If you now generate another migration (it can even be an empty migration file generated through `drizzle-kit generate --custom`, or a migration file generated through actual schema changes) and re-open the app, the app immediately crashes. No error logs are printed.
The Xcode console also does not show any relevant log output.
The culprit seems to be somewhere in this function (which is performing the actual migration). Commenting it out leads to the crash not occurring and the `__drizzle_migrations` table being populated accordingly: https://github.com/drizzle-team/drizzle-orm/blob/a78eefe08e127922565486143e0150a718b27e8a/drizzle-orm/src/sqlite-core/dialect.ts#L758
Interestingly, when replacing lines 755-766 (the for loop) in the above file with the following code snippet, which ignores all the migration files other than the first one and instead performs some generic SQL query, the crash does **not** occur (but obviously the migrations aren't applied, this is simply for diagnostic purposes):
```js
for (const index in migrations) {
const migration = migrations[index];
if (!lastDbMigration || Number(lastDbMigration[2]) < migration.folderMillis) {
for (const stmt of migration.sql) {
if (index == 0) session.run(sql.raw(stmt));
else {
session.run(sql.raw('SELECT 1'));
console.log("Skipping migration: ", stmt)
}
}
session.run(
sql`INSERT INTO ${sql.identifier(migrationsTable)} ("hash", "created_at") VALUES(${migration.hash}, ${migration.folderMillis})`
);
}
}
```
I've logged the contents of `stmt` and that looks fine. Again, this also happens with a completely empty migration file, so the SQL query itself shouldn't be the problem.
### Expected behavior
All migrations should be applied accordingly.
### Environment & setup
- iOS 17
- Expo SDK 51.0.8
- expo-sqlite 14.0.3
Contributor guide
Assessment
This issue has not been assessed yet.