t3-oss / create-t3-app

The best way to start a full-stack, typesafe Next.js app
https://create.t3.gg
MIT License
25.19k stars 1.15k forks source link

bug: wrong drizzle connection #1587

Closed statusunknown418 closed 11 months ago

statusunknown418 commented 1 year ago

Provide environment information

System: OS: macOS 14.0 CPU: (8) arm64 Apple M1 Pro Memory: 241.38 MB / 16.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.17.1 - ~/.nvm/versions/node/v18.17.1/bin/node Yarn: 1.22.19 - ~/Library/pnpm/yarn npm: 9.6.7 - ~/.nvm/versions/node/v18.17.1/bin/npm pnpm: 8.6.6 - /opt/homebrew/bin/pnpm Watchman: 2023.09.25.00 - /opt/homebrew/bin/watchman

Describe the bug

basically the way of connecting safely to planetscale with drizzle should be by using PORT, USERNAME and PASSWORD instead of a databaseUrl in the db/index.ts and the latter should only be used for the drizzle.config.ts

import { env } from "~/env.mjs"; import * as schema from "./schema";

export const db = drizzle( new Client({ url: env.DATABASE_URL, }).connection(), { schema } );


*Doesn't work, throws a `Fetch failed` error coming from either drizzle or @planetscale/database* 

```log
{
  e: TypeError: fetch failed
      at Object.fetch (node:internal/deps/undici/undici:11576:11)
      at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
      at async postJSON (file:///Users/a3tech/Developer/personal_projects/remarkable/node_modules/.pnpm/@planetscale+database@1.11.0/node_modules/@planetscale/database/dist/index.js:128:22)
      at async Connection.execute (file:///Users/a3tech/Developer/personal_projects/remarkable/node_modules/.pnpm/@planetscale+database@1.11.0/node_modules/@planetscale/database/dist/index.js:86:23)
      at async PlanetScalePreparedQuery.execute (file:///Users/a3tech/Developer/personal_projects/remarkable/node_modules/.pnpm/drizzle-orm@0.28.6_@planetscale+database@1.11.0/node_modules/drizzle-orm/planetscale-serverless/index.mjs:31:26)
     file:///Users/a3tech/Developer/personal_projects/remarkable/node_modules/.pnpm/@trpc+server@10.38.5/node_modules/@trpc/server/dist/adapters/next.mjs:44:9 {
    cause: [Error: 0093F4DB01000000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:../deps/openssl/openssl/ssl/record/ssl3_record.c:355:
    ] {
      library: 'SSL routines',
      reason: 'wrong version number',
      code: 'ERR_SSL_WRONG_VERSION_NUMBER'
    }
  }

Note that .env is:

DATABASE_URL=mysql://username:password@host:3306/<db_name>?ssl={"rejectUnauthorized":true}

same error is thrown

import { connect } from "@planetscale/database"; import { drizzle } from "drizzle-orm/planetscale-serverless";

// create the connection const connection = connect({ host: env.DATABASE_HOST, username: env.DATABASE_USERNAME, password: env.DATABASE_PASSWORD, });

export const db = drizzle(connection, { schema });



### Reproduction repo

create-t3-app --drizzle

### To reproduce

basically run the app with generated credentials from planetscale

### Additional information

_No response_
juancamiloqhz commented 11 months ago

Is this solved yet? I'm having the same issue

statusunknown418 commented 11 months ago

I managed to solve this by using the regular connection details - USERNAME, PASSWORD, HOST, and only using the connection_url to do db:push @juancamiloqhz

juancamiloqhz commented 11 months ago

I managed to solve this by using the regular connection details - USERNAME, PASSWORD, HOST, and only using the connection_url to do db:push @juancamiloqhz

Thanks for your answer @statusunknown418. I solved this issue with a new database instance in planetscale. For some reason, my existing database was unable to connect with the new app I'm making.

statusunknown418 commented 11 months ago

great!, closing this then

jackblackCH commented 11 months ago

The docs are outdated. Whe following the steps on planetsclale, they don't offer a connection string anymore. The example connection code in T3 should be updated with the new one PlanetScale provides.

hteen commented 9 months ago

this works for me

import { drizzle } from "drizzle-orm/mysql2";
import mysql from "mysql2/promise";
import { env } from "~/env";
import * as schema from "./schema";

const connection = await mysql.createConnection({
  uri: env.DATABASE_URL
});

export const db = drizzle(connection, { schema, mode: "default" });