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
22.28k stars 517 forks source link

[BUG]: MySQL introspect generation/non idempotent generation #1306

Open DeForce opened 10 months ago

DeForce commented 10 months ago

What version of drizzle-orm are you using?

0.28.6

What version of drizzle-kit are you using?

0.19.13

Describe the Bug

Running drizzle-kit introspect:mysql gives scheme output that is not valid, and forces manual edits in the Typescript schema:

  1. Incorrectly generated index names (none, instead of real names)
  2. Incorrect value on non-null values
  3. If you run the drizzle-kit generate:mysql again - you'll get a migration

sample table creation:

create table test_table
(
    id         int auto_increment
        primary key,
    some_key   int                                   not null,
    some_value int                                   null,
    timestamp  timestamp default current_timestamp() not null
);

create index test_table_some_key_index
    on test_table (some_key);

create index test_table_some_value_index
    on test_table (some_value);

running drizzle-kit introspect:mysql

export const testTable = mysqlTable("test_table", {
    id: int("id").autoincrement().notNull(),
    someKey: int("some_key").notNull(),
    someValue: int("some_value").default('NULL'),
    timestamp: timestamp("timestamp", { mode: 'string' }).default('current_timestamp()').notNull(),
},
(table) => {
    return {
        someKeyIdx: index().on(table.someKey),
        someValueIdx: index().on(table.someValue),
    }
});

Issue with .default('NULL'):

Argument of type 'string' is not assignable to parameter of type 'number | SQL '

Issue with someKeyIdx: index().on(table.someKey),

Expected 1 arguments, but got 0.

Running drizzle-kit generate:mysql will create a migration, when the values already correct:

ALTER TABLE `test_table` MODIFY COLUMN `id` int AUTO_INCREMENT NOT NULL;--> statement-breakpoint
ALTER TABLE `test_table` MODIFY COLUMN `some_key` int NOT NULL;--> statement-breakpoint
ALTER TABLE `test_table` MODIFY COLUMN `some_value` int;

Expected behavior

  1. Correct typescript definition of generated table:

    export const testTable = mysqlTable("test_table", {
    id: int("id").autoincrement().notNull(),
    someKey: int("some_key").notNull(),
    someValue: int("some_value"),
    timestamp: timestamp("timestamp", { mode: 'string' }).default('current_timestamp()').notNull(),
    },
    (table) => {
    return {
        someKeyIdx: index("test_table_some_key_index").on(table.someKey),
        someValueIdx: index("test_table_some_value_index").on(table.someValue),
    }
    });
  2. Idempotent migration. It shouldn't create a new migration on a newly introspected table

Environment & setup

Mariadb: 10.10.2-MariaDB-1:10.10.2+maria~ubu2204

npm: "mysql2": "^3.6.1" "drizzle-orm": "^0.28.6" "drizzle-kit": "^0.19.13"

node: v16.20.2

estubmo commented 10 months ago

I'm having the exact same issue.

alan345 commented 1 month ago

Same issue