-
-
Notifications
You must be signed in to change notification settings - Fork 643
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG]: Drizzle ORM with Expo SQLite driver crashes app when more than one migration is present #2384
Comments
Okay, it seems like the SQL file was in fact the problem. Specifically, it seems like if there are any multi-line comments in the SQL file, the migration process will crash (and somehow not be catched by the try-catch block and therefore the The fact that an empty migration file crashed as well for me seems to have been a cache issue. I don't know if this is intended behavior, but if it is, it's nowhere documented. |
I may have experienced the same problem just now. I made some changes to the schema for the app I'm developing and ran the migration command. When I tried to start the app again, Expo Go entirely crashed and returned to the Expo dashboard. Tried a few things, clearing cache, etc. no luck in either Android or iOS simulators. Fortunately I found your issue when searching for possible causes; sure enough the new migration SQL file contained a multiline comment regarding a I've really gotten to like Drizzle and would be happy to hear if this bug could be prevented so that other devs won't run into the same problem.
|
I have the same experience too. Adding
In order to alter the column with
"react-native": "0.74.3",
"expo": "~51.0.18",
"expo-sqlite": "~14.0.4",
"drizzle-orm": "^0.31.4",
"drizzle-kit": "^0.22.8", The app will longer crash in my case. Hope this help someone! |
I believe I had the same issue and thank you all for saving my behind. This issue is reproducible on Android & expo-sql with latest versions package.json -- original failure, also reproduced on new version "drizzle-orm": "^0.32.2",
"drizzle-kit": "^0.22.7",
"expo-sqlite": "^14.0.4", package.json -- reproduced after bumping versions, drizzle-kit up & drizzle-kit migrate. "drizzle-orm": "^0.33.0",
"expo-sqlite": "^14.0.4",
"drizzle-kit": "^0.24.1", 🚩 Some where down the line it started crashing again. This was after it briefly started working when I removed the multi-line comments. I think bumping versions to reproduce created more issues.. I have now resorted to nuking the drizzle/ folder and re-created migrations. This needed a further step in the app to allow users to remove the Multi-line comment was generated because - can't generate foreign key & can't add primary column You're trying to add PRIMARY KEY(cluster_id,email_id) to 'emails_to_clusters' table
SQLite does not support adding primary key to an already created table
You can do it in 3 steps with drizzle orm:
- create new mirror table with needed pk, rename current table to old_table, generate SQL
- migrate old data from one table to another
- delete old_table in schema, generate sql
...
SQLite does not support "Creating foreign key on existing column" out of the box,
.... Ignore the below, just adding some specific tags to help people find this issue
|
regenerating the last migration and removing the comments solved the issue |
I think the issue is with the Expo SQLite library, but it can be solved from the Drizzle side. The app crashes if you attempt to run a SQL statement that is just a comment. The migration code splits the SQL files based on this string: Because Stripping the comments from the file before running the SQL fixes the problem. const result = query
// strip comments
.replace(/(\/\*[^*]*\*\/)|(\/\/[^*]*)|(--[^.].*)/gm, '')
.split("--> statement-breakpoint").map((it) => {
return it;
})
.filter((it) => it.trim().length > 0);
if(result.length === 0) {
continue;
} This will make the migrations run correctly now. There might be other ways to solve this:
If you manually edit the migration files, completely remove any build folders (iOS/Android) locally to ensure your build gets fresh migrations. |
Can you link to your migrations so I can see them? The way it looks like drizzle handles the migrations is to only look at the timestamp. It might be ok. I opened a PR to fix the migration table primary key FYI. |
@mphill Yeah I also just realised that Here's the migrations and journal files: |
#2970 should fix the migration tables using serial Also, see this:
Your migrations file looks good to me. If you are using the Dev Expo client, make sure to remove the iOS or Android folder and try again with a fresh install. |
I can confirm migrations work after patching diff --git a/sqlite-core/dialect.js b/sqlite-core/dialect.js
index f249890cb4f00498cec0ab394221a1dc0e781304..f05cb57a847b7c02ea317d5fe9ba4faa5dee61f9 100644
--- a/sqlite-core/dialect.js
+++ b/sqlite-core/dialect.js
@@ -531,9 +531,9 @@ class SQLiteSyncDialect extends SQLiteDialect {
const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
- id SERIAL PRIMARY KEY,
- hash text NOT NULL,
- created_at numeric
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+ hash TEXT NOT NULL,
+ created_at INTEGER
)
`;
session.run(migrationTableCreate);
@@ -566,9 +566,9 @@ class SQLiteAsyncDialect extends SQLiteDialect {
const migrationsTable = config === void 0 ? "__drizzle_migrations" : typeof config === "string" ? "__drizzle_migrations" : config.migrationsTable ?? "__drizzle_migrations";
const migrationTableCreate = sql`
CREATE TABLE IF NOT EXISTS ${sql.identifier(migrationsTable)} (
- id SERIAL PRIMARY KEY,
- hash text NOT NULL,
- created_at numeric
+ id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
+ hash TEXT NOT NULL,
+ created_at INTEGER
)
`;
await session.run(migrationTableCreate); |
I found this as I also had RN crashing on running migrations, although I have a slightly different issue, hopefully it's sufficiently related to warrant crashing the thread... I generated my initial schema this generates and runs just fine. Then I wanted to seed some initial data, so to which I added some insert statements, eg
this runs ONLY THE FIRST LINE
Compared to the initial schema
When I compared my seeding sql to the generated initial schema sql, I noted the
however, putting the breakpoint comments at the end of each insert statement causes the app to crash on startup... which led me here. I can't find any documentation about whether the breakpoints are needed, or how better to data seed, https://orm.drizzle.team/docs/drizzle-kit-generate#custom-migrations would seem to imply my first pass attempt should have worked...? Main question: anyone know how to successfully run data seeding?!? Secondary question: anyone know how to easily refresh whatever cache is being created (where??)? UPDATE: for anyone finding the same issue later, I worked around it by running one statement per data type/migration file using the INSERT INTO ... UNION ALL sqlite syntax which was feasible in my situation as I only had a few data types to insert. |
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, creating a database schema, the initial migration using
drizzle-kit
and the provideduseMigrations()
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:drizzle-orm/drizzle-orm/src/sqlite-core/dialect.ts
Line 758 in a78eefe
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):
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
The text was updated successfully, but these errors were encountered: