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
24.26k stars 623 forks source link

[BUG]: Undefined properties when using drizzle-kit push #3391

Open aokigit opened 3 days ago

aokigit commented 3 days ago

What version of drizzle-orm are you using?

0.36.0

What version of drizzle-kit are you using?

0.27.1

Describe the Bug

When using drizzle-kit push I'm receiving the following error: TypeError: Cannot read properties of undefined (reading 'isRLSEnabled')

My client setup:

import { drizzle } from "drizzle-orm/neon-http";
import * as schema from "@/lib/database/schema";

export const db = drizzle(process.env.DATABASE_URL!, { schema: schema });

Drizzle config:

import dotenv from "dotenv";
import { defineConfig } from "drizzle-kit";

dotenv.config({ path: ".env.local" });

export default defineConfig({
  dialect: "postgresql",
  schema: "./lib/database/schema.ts",
  out: "./lib/database/migrations",
  dbCredentials: {
    url: process.env.DATABASE_URL!,
  },
  strict: true,
});

Expected behavior

No response

Environment & setup

Development

williamneves commented 3 days ago

Same Error for me, using supabase

97albertus commented 3 days ago

exact same thing here, seems like some null checks are missing in applyPgSnapshotsDiff

girumgizachew1 commented 3 days ago

exactly the same thing for me too

TapokSapok commented 3 days ago

the same thing for me too

enkyuan commented 3 days ago

same problem occurring for me as well. not sure what's up with this; a recent migration worked out alright--i'm using xata postgres

Danieljs-codes commented 3 days ago

I'm having the same error

enkyuan commented 3 days ago

i did something really weird...so i have multiple schemas defined in my schema.ts and i removed all of them except one. did npx drizzle-kit generate and then npx drizzle-kit migrate. repeated the process by re-adding my schemas and somehow everything worked...not sure what's up with that though.

samihanine commented 3 days ago

same error for me with postgres

eduardomoroni commented 3 days ago

same

guidefari commented 2 days ago
firtoz commented 2 days ago

I have the same, for postgres generate.

firtoz commented 2 days ago

Update: Tried with 0.26.2, tells me to upgrade...

This version of drizzle-kit is outdated Please update drizzle-kit package to the latest version 👍

firtoz commented 2 days ago

I tried the "delete schema, generate, revert partially, generate again, then use latest schema, and generate one more time" and it worked somehow

guidefari commented 2 days ago

a folder with 'bad' migrations was my issue. deleted that and all's well now🙌🏽

farhan-helmy commented 2 days ago

same

BryanAbate commented 1 day ago

It seems there is a missing nullcheck and adding an optional chaining operator in drizzle-kit code fixes the issue. I cannot assure it will works if you avec RLS defined, but if you don't you should be able to generate migrations correctly.

  1. Go to node_modules/drizzle-kit/bin.cjs
  2. Edit line 29886. Replace
    if (table4.isRLSEnabled !== tableInPreviousState.isRLSEnabled) {

    With

    if (table4.isRLSEnabled !== tableInPreviousState?.isRLSEnabled) {

About the issue: tableInPreviousState is known to be possibly undefined as line 29878 checks for it:

const policiesInPreviousState = tableInPreviousState ? Object.keys(tableInPreviousState.policies) : [];

There must be a type issue or misconfiguration somewhere that Typescript did not catch it at compile time.

AndriiSherman commented 1 day ago

pushing a fix in 30 mins

AndriiSherman commented 1 day ago

sorry for a long fix for this blocker

AndriiSherman commented 1 day ago

Just tested with drizzle-kit@rls-fix Should be fixed there. Including in the latest now

enkyuan commented 14 hours ago

tysm!