BETH stack tutorial non-blocking: `pushToTenantDb` returns exit code of 1

Per the title this bit of code is causing a small problem:
onExit(subprocess, exitCode, signalCode, error) {
console.log({ exitCode, error }, "hi from tenant index");
unlinkSync(tempConfigPath);
if (exitCode === 0) {
resolve(void 0);
} else {
console.error("Error pushing to tenant db");
reject(error);
}
},
onExit(subprocess, exitCode, signalCode, error) {
console.log({ exitCode, error }, "hi from tenant index");
unlinkSync(tempConfigPath);
if (exitCode === 0) {
resolve(void 0);
} else {
console.error("Error pushing to tenant db");
reject(error);
}
},
The exitCode evaluates to 1 and the error evaluates to undefined. The tenant db is successfully created in Turso and the unlinkSync I think is working as well, so I'm not sure what's causing the exit code. Here's the error:
/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48441
[0] return new LibsqlError(e.message, code, e);
[0] ^
[0]
[0] LibsqlError: HRANA_WEBSOCKET_ERROR: Unexpected server response: 502
[0] at mapHranaError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48441:12)
[0] at #openStream (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48575:17)
[0] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[0] at async HranaClient.execute (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48474:29)
[0] at async TursoSqlite.query (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:42938:21)
[0] at async fromDatabase3 (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:12938:23)
[0] at async sqlitePushIntrospect (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:51949:19)
[0] at async Command.<anonymous> (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:53436:31) {
[0] code: 'HRANA_WEBSOCKET_ERROR',
[0] [cause]: WebSocketError: Unexpected server response: 502
[0] at #onSocketError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48099:25)
[0] at _WebSocket.<anonymous> (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48022:78)
[0] at callListener (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:45693:18)
[0] at _WebSocket.onError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:45647:13)
[0] at _WebSocket.emit (node:events:513:28)
[0] at emitErrorAndClose (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:46607:17)
[0] at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
[0] }
[0]
[0] Node.js v18.13.0
[0] {
[0] exitCode: 1,
[0] error: undefined
[0] } hi from tenant index
[0] 2023-10-08T21:46:22.499ZUTC | POST /api/organization - 500 (4891 ms)
[0] Error pushing to tenant db
[0] 109 |
[0] 110 | let er0 = handleErrors[0](
[0] 111 | Object.assign(c, {
[0] 112 | error: error,
[0] 113 |
[0] 114 | code: error.code ?? error[ERROR_CODE] ?? "UNKNOWN"
[0] ^
[0] TypeError: undefined is not an object (evaluating 'error.code')
[0] at processTicksAndRejections (:55:76)
[0] POST - http://localhost:3000/api/organization failed
[1] unlink src/db/tenant/drizzle.config.ts
/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48441
[0] return new LibsqlError(e.message, code, e);
[0] ^
[0]
[0] LibsqlError: HRANA_WEBSOCKET_ERROR: Unexpected server response: 502
[0] at mapHranaError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48441:12)
[0] at #openStream (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48575:17)
[0] at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
[0] at async HranaClient.execute (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48474:29)
[0] at async TursoSqlite.query (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:42938:21)
[0] at async fromDatabase3 (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:12938:23)
[0] at async sqlitePushIntrospect (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:51949:19)
[0] at async Command.<anonymous> (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:53436:31) {
[0] code: 'HRANA_WEBSOCKET_ERROR',
[0] [cause]: WebSocketError: Unexpected server response: 502
[0] at #onSocketError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48099:25)
[0] at _WebSocket.<anonymous> (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:48022:78)
[0] at callListener (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:45693:18)
[0] at _WebSocket.onError (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:45647:13)
[0] at _WebSocket.emit (node:events:513:28)
[0] at emitErrorAndClose (/Users/trevorfehrman/Documents/Code/Personal/beth-app/node_modules/drizzle-kit/index.cjs:46607:17)
[0] at process.processTicksAndRejections (node:internal/process/task_queues:82:21)
[0] }
[0]
[0] Node.js v18.13.0
[0] {
[0] exitCode: 1,
[0] error: undefined
[0] } hi from tenant index
[0] 2023-10-08T21:46:22.499ZUTC | POST /api/organization - 500 (4891 ms)
[0] Error pushing to tenant db
[0] 109 |
[0] 110 | let er0 = handleErrors[0](
[0] 111 | Object.assign(c, {
[0] 112 | error: error,
[0] 113 |
[0] 114 | code: error.code ?? error[ERROR_CODE] ?? "UNKNOWN"
[0] ^
[0] TypeError: undefined is not an object (evaluating 'error.code')
[0] at processTicksAndRejections (:55:76)
[0] POST - http://localhost:3000/api/organization failed
[1] unlink src/db/tenant/drizzle.config.ts
Noticing that that tenant db is indeed successfully getting pushed to Turso I wrapped the call to this function in the organization controller with a try catch and just logged out the error.
try {
await pushToTenantDb({
dbName: Name,
authToken: jwt,
});
} catch (e) {
console.log(e);
}
try {
await pushToTenantDb({
dbName: Name,
authToken: jwt,
});
} catch (e) {
console.log(e);
}
I still log the errors, but this pushes the tenant db to Turso and adds the record to the organization table. It also updates the user table as it should.
16 Replies
Trevor
TrevorOP14mo ago
Probably related and actually blocking: I just realized I'm not able to connect to the newly created db's.
❯ turso db shell org-u7susxv
Error: failed to connect to database. err: failed to execute SQL: SELECT 1;
unexpected end of JSON input
❯ turso db shell org-u7susxv
Error: failed to connect to database. err: failed to execute SQL: SELECT 1;
unexpected end of JSON input
It seems like somehow the schema isn't getting applied from this command maybe?
["bunx", "drizzle-kit", "push:sqlite", `--config=${tempConfigPath}`],
["bunx", "drizzle-kit", "push:sqlite", `--config=${tempConfigPath}`],
whatplan
whatplan14mo ago
Will take a look when I get home feel free to ping me if I forget
Trevor
TrevorOP14mo ago
coolcoolcool no rush, i really think i've narrowed it down to the pushToTenantDb function as everything else is getting added to the other tables as expected. it's as if the tenant dbs are getting created but they're not accepting the schema for some reason here's a link to the code as is in case that helps: https://github.com/trevorfehrman/beth-stack
whatplan
whatplan14mo ago
hm can you try running with the input arg to true to see the drizzlekit stdout
Trevor
TrevorOP14mo ago
sure lemme try that hrm i don't see anything new getting logged out what's weird is that it successfully creates a turso db, which means the command we're passing to the spawned process is executing, at no point does it say it can't find the config file or something, but the db that gets created is in a corrupted state or something somehow
Trevor
TrevorOP14mo ago
No description
Trevor
TrevorOP14mo ago
No description
Trevor
TrevorOP14mo ago
No description
whatplan
whatplan14mo ago
uh remove the line that unlinks the file and see if it gets written / what it contains
Trevor
TrevorOP14mo ago
oh that's a good idea
export default {
schema: "./src/db/tenant/schema/index.ts",
driver: "turso",
dbCredentials: {
url: "libsql://org-tnkco0n-trevorfehrman.turso.io",
authToken: "[[my auth token]]",
},
tablesFilter: ["!libsql_wasm_func_table"]
}
export default {
schema: "./src/db/tenant/schema/index.ts",
driver: "turso",
dbCredentials: {
url: "libsql://org-tnkco0n-trevorfehrman.turso.io",
authToken: "[[my auth token]]",
},
tablesFilter: ["!libsql_wasm_func_table"]
}
whatplan
whatplan14mo ago
can you try running the command just manually then like the
["bunx", "drizzle-kit", "push:sqlite", --config=${tempConfigPath}],
Trevor
TrevorOP14mo ago
yep hm it's hanging
❯ bunx drizzle-kit push:sqlite --config=./src/db/tenant/drizzle.config.ts
Custom config path was provided, using './src/db/tenant/drizzle.config.ts'
Reading config file '/Users/trevorfehrman/Documents/Code/Personal/beth-app/src/db/tenant/drizzle.config.ts'
drizzle-kit: v0.19.13
drizzle-orm: v0.28.6
❯ bunx drizzle-kit push:sqlite --config=./src/db/tenant/drizzle.config.ts
Custom config path was provided, using './src/db/tenant/drizzle.config.ts'
Reading config file '/Users/trevorfehrman/Documents/Code/Personal/beth-app/src/db/tenant/drizzle.config.ts'
drizzle-kit: v0.19.13
drizzle-orm: v0.28.6
maybe i just need to wait for the drizzle-kit CLI to get updated to take args, i think you mentioned in your video they were working on it 🤔
whatplan
whatplan14mo ago
does the primary dirzzle kit work?
Trevor
TrevorOP14mo ago
Yes
whatplan
whatplan14mo ago
uh im a bit stumped then
Trevor
TrevorOP14mo ago
yeah weird. If I had to guess it's a Bun issue, bun installs the wrong version of a node module based on my architecture Thanks for trying though : D One last comment on this: For reasons unrelated to the problems detailed in this thread I recently reformatted my laptop. This resolved the earlier weirdness I was experiencing with Bun installing the wrong version of a node module for my CPU arch but when it comes to posting tenant dbs I'm still getting the exact same issue as described. It successfully posts a new tenant db to Turso but there aren't any tables in it and I get the same errors described above. I think this is a great tutorial and you can totally use it as a guide to make a full stack app with just one db, which is what most apps are going to want to do anyway, I just wanted to leave this here in case anybody runs into the same issue in the future.
Want results from more Discord servers?
Add your server