drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
23.53k stars 577 forks source link

[BUG]: drizzle-kit introspect fails on required param that is defined #2719

Closed AndreasRoither closed 1 month ago

AndreasRoither commented 2 months ago

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.1

Describe the Bug

drizzle-kit introspect stops introspection for required param, even though it is defined.

Affected version 0.23.1 Working local version: 0.22.8 (did not test other versions)

introspect output

$ npx drizzle-kit introspect
No config path provided, using default 'drizzle.config.ts'
Reading config file '/home/test/project/test/drizzle.config.ts'
 Error  Please provide required params:
    [✓] dialect: 'postgresql'

config;

$ bat drizzle.config.ts

       │ File: /home/test/project/test/drizzle.config.ts
───────┼─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ import 'dotenv/config';
   2   │ import { defineConfig } from 'drizzle-kit';
   3   │
   4   │ export default defineConfig({
   5   │     dialect: 'postgresql',
   6   │     out: './src/drizzle',
   7   │     schema: './src/drizzle/schema.ts',
   8   │     dbCredentials: {
   9   │         host: process.env.DB_HOST ?? '',
  10   │         port: Number(process.env.DB_PORT ?? 0),
  11   │         user: process.env.DB_USERNAME ?? '',
  12   │         password: process.env.DB_PASSWORD ?? '',
  13   │         database: process.env.DB_NAME ?? '',
  14   │         ssl: false,
  15   │     },
  17   │     verbose: true,
  19   │     strict: true,
  20   │ });

env:

  $ bat .env
  12   │ # DB
  13   │ DB_HOST=localhost
  14   │ DB_PORT=5432
  15   │ DB_USERNAME=test
  16   │ DB_PASSWORD=test
  17   │ DB_NAME=test

env is correct and tested, config works with 0.22.8

Expected behavior

Introspect running normally on version 0.22.8:

$ npx drizzle-kit introspect
drizzle-kit: v0.22.8
drizzle-orm: v0.32.1

No config path provided, using default path
Reading config file '/home/test/project/test/drizzle.config.ts
Pulling from ['public'] list of schemas

Using 'pg' driver for database querying
[✓] 8 tables fetched
[✓] 23 columns fetched
[✓] 0 enums fetched
[✓] 0 indexes fetched
[✓] 7 foreign keys fetched

[i] No SQL generated, you already have migrations in project
[✓] You schema file is ready ➜ src/drizzle/schema.ts 🚀

Environment & setup

OS: Ubuntu 22.04.4 LTS (Jammy Jellyfish) PostgreSQL: 16.3 on x86_64-pc-linux-gnu

TheOrdinaryWow commented 2 months ago

Same here just occurred few minutes ago with mysql.

× bunx drizzle-kit introspect                                                                                                                                                                                                              
No config path provided, using default 'drizzle.config.ts'
Reading config file '.../packages/database/drizzle.config.ts'
 Error  Please provide required params:
    [✓] dialect: 'mysql'

Just solved by downgrading to v0.22.8, as instructed. But indeed on v0.23.1 we got this error.

for reference, related files are followed.

drizzle.config.ts

> cat drizzle.config.ts
import { defineConfig } from "drizzle-kit";
import { connCredentials } from "./src/client";

export default defineConfig({
  dialect: "mysql",
  schema: "drizzle/schema/*",
  out: "drizzle/migration",
  dbCredentials: {
    host: connCredentials.host,
    port: connCredentials.port,
    user: connCredentials.user,
    password: connCredentials.password,
    database: connCredentials.database,
  },
  strict: true,
});

client.ts

if (
  !process.env.MYSQL_HOST ||
  !process.env.MYSQL_USER ||
  !process.env.MYSQL_PASSWORD ||
  !process.env.MYSQL_DATABASE
) {
  throw new Error(
    "MYSQL_HOST or MYSQL_USER or MYSQL_PASSWORD or MYSQL_DATABASE is missing.",
  );
}

export const connCredentials = {
  host: process.env.MYSQL_HOST,
  port: Number(process.env.MYSQL_PORT),
  user: process.env.MYSQL_USER,
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_DATABASE,
};

// ...

.env

MYSQL_HOST="localhost"
MYSQL_PORT="3306"
MYSQL_USER="exampleUser"
MYSQL_PASSWORD="example"
MYSQL_DATABASE="exampleDb"

Environment & setup

OS: macOS Sequoia 15.0 arm64 MySQL: mariadb 15.1 Distrib 10.11.7-MariaDB, for debian-linux-gnu (x86_64) using EditLine wrapper

barrynorman commented 2 months ago

Same here. Just tried to pull the db schema but not working. This is critical. I need the introspect ^^

With

"drizzle-kit": "^0.22.0",

its working again.

Problems start with the versions after this one

claytonkruse commented 1 month ago

I am having the same problem on EndeavourOS.

ahmed-sudani commented 1 month ago

same error.

cshif commented 1 month ago

same problem on macOS 15 and drizzle-kit v0.23.1 solved with setting options via CLI but another error message shown:

TypeError: movedTables is not iterable
    at applyPgSnapshotsDiff (/Users/cshif/.yarn/berry/cache/drizzle-kit-npm-0.23.1-c4c034d75c-10c0.zip/node_modules/drizzle-kit/bin.cjs:26289:22)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async introspectPostgres (/Users/cshif/.yarn/berry/cache/drizzle-kit-npm-0.23.1-c4c034d75c-10c0.zip/node_modules/drizzle-kit/bin.cjs:122649:42)
    at async Object.handler (/Users/cshif/.yarn/berry/cache/drizzle-kit-npm-0.23.1-c4c034d75c-10c0.zip/node_modules/drizzle-kit/bin.cjs:129293:9)
    at async rawCli (/Users/cshif/.yarn/berry/cache/drizzle-kit-npm-0.23.1-c4c034d75c-10c0.zip/node_modules/drizzle-kit/bin.cjs:127543:5)
    at async run (/Users/cshif/.yarn/berry/cache/drizzle-kit-npm-0.23.1-c4c034d75c-10c0.zip/node_modules/drizzle-kit/bin.cjs:127551:5)

consequently, it doesn't generate any .sql in /drizzle

siddiquipro commented 1 month ago

same error

ybaamel commented 1 month ago

Any updates on that one ? I'm having the same using both CLI & Config file image

shipotech commented 1 month ago

Up

rajanbalana commented 1 month ago

Same here. Just tried to pull the db schema but not working. This is critical. I need the introspect ^^

With

"drizzle-kit": "^0.22.0",

its working again.

Problems start with the versions after this one

This worked for me! Thanks!

itsmaty commented 1 month ago

I also have the same problem image

Downgrading to DrizzleKit Version 0.22.0 works

th1nkful commented 1 month ago

Got this issue today with ^0.24.0 so still an issue with the latest release. Like others, downgrading to ^0.22.0 did work.

Debugging the issue, it is caused by the validation of database from the pullParams Zod schema here: Its expecting an object with prefix in it.

preparePullConfig calls flattenPull before validating with the schema. flattenPull is putting dbCredentials properties at the root level, and database at the root is expecting a very different value to database inside dbCredentials.

As a secondary workaround to use the latest version, you can create a MySQL connection string and use that in dbCredentials for URL to avoid the validation issue:

  dbCredentials: {
    url:  `mysql://${dbCredentials.user}:${dbCredentials.password}@${dbCredentials.host}:${dbCredentials.port}/${dbCredentials.database}`,
  },
justyn-clark commented 1 month ago

Same for me. "drizzle-kit": "^0.24.0" won't let me introspect :(

AndriiSherman commented 1 month ago

releasing fix in a few hours, sorry for late response!

AndriiSherman commented 1 month ago

This should be fixed in drizzle-kit@0.24.1 If you still encounter this issue, please reopen the ticket

evanlong0803 commented 1 month ago

I use sqlite and it still has problems.

import { defineConfig } from 'drizzle-kit'
export default defineConfig({
  schema: './database/schema.ts',
  out: './database/migrations',
  dialect: 'sqlite',
  driver: 'turso',
  dbCredentials: {
    url: process.env.TURSO_DATABASE_URL!,
    authToken: process.env.TURSO_AUTH_TOKEN
  },
  verbose: true,
  strict: true
})

No config path provided, using default 'drizzle.config.ts'

bug-report.zip

evanlong0803 commented 1 month ago

When I run drizzle-kit push I get the same error as reported above.

madanbazgai commented 6 days ago

Seems like still the issue in "drizzle-kit": "^0.24.2", I am currently using it with bun. When i downgraded to "drizzle-kit": "^0.22.0", it started working correctly. Please review on it.

zhanglun commented 2 days ago

I encountered the same issue when using Drizzle with PgLite.

drizzle.config.ts

import "dotenv/config";
import type { Config } from "drizzle-kit";

export default {
  schema: "./src/db/schema.ts",
  out: "./src/db/drizzle",
  dialect: "postgresql",
  driver: "pglite",
  dbCredentials: {
    host: "localhost",
    port: "5432",
    password: "123",
    database: "bookwise",
    user: "bookwise",
    ssl: "allow",
  },
  verbose: true,
} satisfies Config;

package.json

{
"scripts": {
    "pglite:migration:generate": "drizzle-kit generate --config drizzle.config.ts",
    "pglite:migration:apply": "drizzle-kit migrate --config drizzle.config.ts",
    "pglite:migration:push": "drizzle-kit push --config drizzle.config.ts"
  }
}

The pglite:migration:generate command works fine, but errors occur when attempting to pglite:migration:apply and pglite:migration:push.

image

Environment & setup

"@electric-sql/pglite": "^0.2.7",
"drizzle-orm": "^0.33.0",
"drizzle-kit": "^0.24.2",