drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

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

Open aokigit opened this issue 2 years ago • 32 comments

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

aokigit avatar Nov 02 '24 13:11 aokigit

Same Error for me, using supabase

williamneves avatar Nov 02 '24 14:11 williamneves

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

97albertus avatar Nov 02 '24 18:11 97albertus

exactly the same thing for me too

girumgizachew1 avatar Nov 02 '24 20:11 girumgizachew1

the same thing for me too

TapokSapok avatar Nov 02 '24 22:11 TapokSapok

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

enkyuan avatar Nov 03 '24 01:11 enkyuan

I'm having the same error

olhamide avatar Nov 03 '24 01:11 olhamide

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.

enkyuan avatar Nov 03 '24 02:11 enkyuan

same error for me with postgres

samihanine avatar Nov 03 '24 03:11 samihanine

same

eduardomoroni avatar Nov 03 '24 06:11 eduardomoroni

  • same package drizzle & drizzle-kit versions
  • getting same error when running drizzle-kit generate
  • postgres

guidefari avatar Nov 03 '24 09:11 guidefari

I have the same, for postgres generate.

firtoz avatar Nov 03 '24 09:11 firtoz

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 avatar Nov 03 '24 09:11 firtoz

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

firtoz avatar Nov 03 '24 11:11 firtoz

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

guidefari avatar Nov 03 '24 15:11 guidefari

same

farhan-helmy avatar Nov 04 '24 08:11 farhan-helmy

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.

BryanAbate avatar Nov 04 '24 12:11 BryanAbate

pushing a fix in 30 mins

AndriiSherman avatar Nov 05 '24 07:11 AndriiSherman

sorry for a long fix for this blocker

AndriiSherman avatar Nov 05 '24 07:11 AndriiSherman

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

AndriiSherman avatar Nov 05 '24 07:11 AndriiSherman

tysm!

enkyuan avatar Nov 05 '24 18:11 enkyuan