Skip to content

Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays #2429

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 9, 2025

Conversation

aprilmintacpineda
Copy link
Contributor

@aprilmintacpineda aprilmintacpineda commented Jun 3, 2024

Summary of changes

  • Removed type restriction on non-empty arrays on mysqlEnum
  • Removed type restriction on non-empty arrays on pgEnum
  • Added check to ensure that values parameter for pgEnum is not an empty array, if it is, throw an error.

Related discussion: #1914

Problem:

mysqlEnum and pgEnum both expect the 2nd argument to be of type [string, ...string[]] which means it ensures that we are not passing an empty array to it. However, this would only work smoothly for arrays but not for enums. To get around the limitation, we always have to use the as keyword like so:

mysqlEnum(
  'type',
  Object.values(eCategoryType) as [
    eCategoryType,
    ...eCategoryType[],
  ],
).notNull()

Reason being is because Object.values(eCategoryType) becomes a type of eCategoryType[] which doesn't fit what we are explicitly checking for [eCategoryType, ...eCategoryType[]].

I understand that this restriction was put in place to ensure that no one passes an empty array in there. In this case, I think we can add that kind of restriction in-code rather than in-type -- which looking at the mysqlEnum code, we are already doing by checking values.length === 0.

Note

I saw on some other files that enums are generally expected as [string, ...string[]], these other places are not covered in this PR, this only aims to fix the issue on mysqlEnum and pgEnum.

What will work

enums will work -- now without needing to use as keyword.

image

Regular arrays will still work

image

Empty arrays will now work -- previously there will be a TS error. However, in-code validation will prevent this from happening and throw an error.

image

Patch Package

You can use patch-package to apply the fix right now.

Step 1

yarn add -D patch-package

Step 2

2.1. cd /to/root/of/your/project
2.2. touch patches/drizzle-orm+0.31.0.patch

Step 3

Copy the contents below to the file you just created

patches/drizzle-orm+0.31.0.patch
diff --git a/node_modules/drizzle-orm/mysql-core/columns/enum.d.ts b/node_modules/drizzle-orm/mysql-core/columns/enum.d.ts
index 274c06a..9a6d67c 100644
--- a/node_modules/drizzle-orm/mysql-core/columns/enum.d.ts
+++ b/node_modules/drizzle-orm/mysql-core/columns/enum.d.ts
@@ -3,7 +3,7 @@ import type { ColumnBaseConfig } from "../../column.js";
 import { entityKind } from "../../entity.js";
 import type { Writable } from "../../utils.js";
 import { MySqlColumn, MySqlColumnBuilder } from "./common.js";
-export type MySqlEnumColumnBuilderInitial<TName extends string, TEnum extends [string, ...string[]]> = MySqlEnumColumnBuilder<{
+export type MySqlEnumColumnBuilderInitial<TName extends string, TEnum extends string[]> = MySqlEnumColumnBuilder<{
     name: TName;
     dataType: 'string';
     columnType: 'MySqlEnumColumn';
@@ -24,4 +24,4 @@ export declare class MySqlEnumColumn<T extends ColumnBaseConfig<'string', 'MySql
     readonly enumValues: T["enumValues"];
     getSQLType(): string;
 }
-export declare function mysqlEnum<TName extends string, U extends string, T extends Readonly<[U, ...U[]]>>(name: TName, values: T | Writable<T>): MySqlEnumColumnBuilderInitial<TName, Writable<T>>;
+export declare function mysqlEnum<TName extends string, U extends string, T extends ReadonlyArray<U>>(name: TName, values: T | Writable<T>): MySqlEnumColumnBuilderInitial<TName, Writable<T>>;
diff --git a/node_modules/drizzle-orm/pg-core/columns/enum.d.ts b/node_modules/drizzle-orm/pg-core/columns/enum.d.ts
index 357f708..c240362 100644
--- a/node_modules/drizzle-orm/pg-core/columns/enum.d.ts
+++ b/node_modules/drizzle-orm/pg-core/columns/enum.d.ts
@@ -1,10 +1,10 @@
 import type { ColumnBuilderBaseConfig } from "../../column-builder.js";
 import type { ColumnBaseConfig } from "../../column.js";
 import { entityKind } from "../../entity.js";
-import type { AnyPgTable } from "../table.js";
 import type { Writable } from "../../utils.js";
+import type { AnyPgTable } from "../table.js";
 import { PgColumn, PgColumnBuilder } from "./common.js";
-export type PgEnumColumnBuilderInitial<TName extends string, TValues extends [string, ...string[]]> = PgEnumColumnBuilder<{
+export type PgEnumColumnBuilderInitial<TName extends string, TValues extends string[]> = PgEnumColumnBuilder<{
     name: TName;
     dataType: 'string';
     columnType: 'PgEnumColumn';
@@ -12,15 +12,15 @@ export type PgEnumColumnBuilderInitial<TName extends string, TValues extends [st
     enumValues: TValues;
     driverParam: string;
 }>;
-export interface PgEnum<TValues extends [string, ...string[]]> {
+export interface PgEnum<TValues extends string[]> {
     <TName extends string>(name: TName): PgEnumColumnBuilderInitial<TName, TValues>;
     readonly enumName: string;
     readonly enumValues: TValues;
     readonly schema: string | undefined;
 }
-export declare function isPgEnum(obj: unknown): obj is PgEnum<[string, ...string[]]>;
+export declare function isPgEnum(obj: unknown): obj is PgEnum<string[]>;
 export declare class PgEnumColumnBuilder<T extends ColumnBuilderBaseConfig<'string', 'PgEnumColumn'> & {
-    enumValues: [string, ...string[]];
+    enumValues: string[];
 }> extends PgColumnBuilder<T, {
     enum: PgEnum<T['enumValues']>;
 }> {
@@ -28,7 +28,7 @@ export declare class PgEnumColumnBuilder<T extends ColumnBuilderBaseConfig<'stri
     constructor(name: string, enumInstance: PgEnum<T['enumValues']>);
 }
 export declare class PgEnumColumn<T extends ColumnBaseConfig<'string', 'PgEnumColumn'> & {
-    enumValues: [string, ...string[]];
+    enumValues: string[];
 }> extends PgColumn<T, {
     enum: PgEnum<T['enumValues']>;
 }> {
@@ -40,4 +40,4 @@ export declare class PgEnumColumn<T extends ColumnBaseConfig<'string', 'PgEnumCo
     }>, config: PgEnumColumnBuilder<T>['config']);
     getSQLType(): string;
 }
-export declare function pgEnum<U extends string, T extends Readonly<[U, ...U[]]>>(enumName: string, values: T | Writable<T>): PgEnum<Writable<T>>;
+export declare function pgEnum<U extends string, T extends ReadonlyArray<U>>(enumName: string, values: T | Writable<T>): PgEnum<Writable<T>>;

Step 4

yarn patch-package

Step 5

Update your package.json file to the following:

"scripts": {
  // ... your other scripts
  "postinstall": "yarn patch-package"
},

@aprilmintacpineda aprilmintacpineda changed the title Don't enforce restrictions on enums being non-empty arrays? Don't enforce type restrictions on enums to be non-empty arrays Jun 3, 2024
@aprilmintacpineda aprilmintacpineda changed the title Don't enforce type restrictions on enums to be non-empty arrays Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays Jun 6, 2024
@AndriiSherman AndriiSherman changed the base branch from main to 0.41 April 9, 2025 13:16
@AndriiSherman AndriiSherman merged commit 4676e1d into drizzle-team:0.41 Apr 9, 2025
@AndriiSherman
Copy link
Member

#1914 (comment)

AndriiSherman added a commit that referenced this pull request Apr 15, 2025
* fix: incorrect types for inArray (#1774)

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* Pass row type parameter to @planetscale/database's execute  (#1852)

* Update session.ts

No need to cast, you can just pass a type parameter

* Update package.json

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays (#2429)

* Removed type restriction on non-empty arrays for mysqEnum

* Removed type restriction on non-empty arrays for pgEnum

* check values argument is not an empty array for pgEnum

* fix: typings

* Add type tests

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Export mapColumnToSchema function (#2495)


Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* [Pg-kit] Fix malformed array literal error on indexes (#2884)

* Fix malformed array literal error on indexes

The main issue is the expression text to array conversion happening in the edited line.
Commas in an expression become delimiters and split the expression up prematurely.
Some special characters like double quotes can cause the malformed array literal errors.

The postgres function pg_get_indexdef does what the snippet above is trying to do, but safely.

* Add index introspect test

* Update pg.test.ts

Remove .only in basic index test

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* add infer enum type (#2552)

* Update how enums work in pg and mysql

* Remove duplicated exports, add related test (#4413)

* Remove duplicated exports, add related test

Fixes #4079

* Fix test

* chore: updating esbuild version in drizzle-kit (#4046)

* chore: updating esbuild version in drizzle-kit

* Fix build errors

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Drizzle-kit: fix recreate enums + altering data type to enums, from enums in pg (#4330)

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Skip test and try latest gel

* Add release notes

---------

Co-authored-by: James <5511220+Zamiell@users.noreply.github.com>
Co-authored-by: Ayrton <git@ayrton.be>
Co-authored-by: April Mintac Pineda <21032419+aprilmintacpineda@users.noreply.github.com>
Co-authored-by: Matthew Ary <157217+MatthewAry@users.noreply.github.com>
Co-authored-by: Kratious <Kratious@users.noreply.github.com>
Co-authored-by: Toti Muñoz <64804554+totigm@users.noreply.github.com>
Co-authored-by: Dan Kochetov <danil.kochetov@gmail.com>
Co-authored-by: Paul Marsicovetere <71470776+paulmarsicloud@users.noreply.github.com>
Co-authored-by: Aleksandr Sherman <102579553+AleksandrSherman@users.noreply.github.com>
AndriiSherman added a commit that referenced this pull request Apr 23, 2025
* DPRINT!!!

* Updates to neon-http for `@neondatabase/serverless@1.0.0`, when released (#4237)

* Updates for @neondatabase/serverless@1.0.0 compatibility

* Clearer comments

* Linting

* Add release notes

* Shard integration tests, parallelise attw

* Thank you pnpm 10

* Split int tests by provider

* Update Neon config, fix bash

* Restore webSocketConstructor

* Use Docker for Neon

* Use docker-compose for Neon

* Fix env var

* Run Neon HTTP tests on real DB

* Downgrade attw

* Use Bun for attw

* Split unit tests

* Fix command

* Update latest pipeline

* Fix test

* Remove await

* Split relational tests

* Disable singlestore-relational

* Remove gel-relational

* Various fixes, features bundled for v0.41.0 (#4293)

* Various fixes, features bundled for v0.41.0

* Fixed broken test case

* D1 Buffer mapping fix, tests fix

* Disabled type parsers for `neon-http` driver

* [drizzle-kit] push to d1-http failed (#4268)

* drizzle-kit and push to d1-http failed
There are two reasons:
- d1-http don't support transactions with db.run("begin/commit/rollback")
- introspections run against some CF-D1 internal tables and fails later.
  that could mitigated with a right tablesFilter: ['!_cf_KV'].
  There was a incomplete mitigation in place which now includes _cf_KV
  tables.

* chore: there are more _cf_ prefixed tables

* fix: Add escaping to sqlite pull queries

* should we pass-with-no-tests ??

* D P R I N T

* Add ci vitest config

* Bump kit, added release notes

---------

Co-authored-by: Roman <nabukhotnyiroman@gmail.com>
Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Add Arktype validation (via `drizzle-arktype` package) (#4314)

* Export (almost) everything from validator packages

* Support infinitely recursive types in JSON columns

* Fix import

* Format

* Init drizzle-arktype

* Finish drizzle-arktype

* Sync fixes from other branch

* Update READMEs

* Update README

* Type optimizations and better debugging for tests

* Add CI/CD updates for arktype package

* Bump arktype

* bump to 0.1.2

* Update turbo config

* Bump all validator packages versions

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>
Co-authored-by: David Blass <david@arktype.io>

* feat: add lua scripts for get tag and onMutate

* Added hexp + peerDeps

* fix: use hexpire option in hexpire

* fix: add hset back

* dprint

* Update pnpm lock

* fix: getByTag script

* fix: exit getByTag script if compositeTable doesn't exist

* Add all test cases for pg

* Add mysql cache functions

* 0.41 (#4416)

* fix: incorrect types for inArray (#1774)

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* Pass row type parameter to @planetscale/database's execute  (#1852)

* Update session.ts

No need to cast, you can just pass a type parameter

* Update package.json

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Don't enforce type restrictions on mysqlEnum and pgEnum to be non-empty arrays (#2429)

* Removed type restriction on non-empty arrays for mysqEnum

* Removed type restriction on non-empty arrays for pgEnum

* check values argument is not an empty array for pgEnum

* fix: typings

* Add type tests

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Export mapColumnToSchema function (#2495)


Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* [Pg-kit] Fix malformed array literal error on indexes (#2884)

* Fix malformed array literal error on indexes

The main issue is the expression text to array conversion happening in the edited line.
Commas in an expression become delimiters and split the expression up prematurely.
Some special characters like double quotes can cause the malformed array literal errors.

The postgres function pg_get_indexdef does what the snippet above is trying to do, but safely.

* Add index introspect test

* Update pg.test.ts

Remove .only in basic index test

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>

* add infer enum type (#2552)

* Update how enums work in pg and mysql

* Remove duplicated exports, add related test (#4413)

* Remove duplicated exports, add related test

Fixes #4079

* Fix test

* chore: updating esbuild version in drizzle-kit (#4046)

* chore: updating esbuild version in drizzle-kit

* Fix build errors

---------

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Drizzle-kit: fix recreate enums + altering data type to enums, from enums in pg (#4330)

Co-authored-by: AndriiSherman <andreysherman11@gmail.com>

* Skip test and try latest gel

* Add release notes

---------

Co-authored-by: James <5511220+Zamiell@users.noreply.github.com>
Co-authored-by: Ayrton <git@ayrton.be>
Co-authored-by: April Mintac Pineda <21032419+aprilmintacpineda@users.noreply.github.com>
Co-authored-by: Matthew Ary <157217+MatthewAry@users.noreply.github.com>
Co-authored-by: Kratious <Kratious@users.noreply.github.com>
Co-authored-by: Toti Muñoz <64804554+totigm@users.noreply.github.com>
Co-authored-by: Dan Kochetov <danil.kochetov@gmail.com>
Co-authored-by: Paul Marsicovetere <71470776+paulmarsicloud@users.noreply.github.com>
Co-authored-by: Aleksandr Sherman <102579553+AleksandrSherman@users.noreply.github.com>

* Update CI/CD to 22.04

* add planetscale cache tests

* Add sqlite cache

* Add singlestore db

* Add gel cache

* Fix build errors

* Fix imports

* fix pg default schema

---------

Co-authored-by: Andrii Sherman <andreysherman11@gmail.com>
Co-authored-by: George MacKerron <george@mackerron.co.uk>
Co-authored-by: Dan Kochetov <danil.kochetov@gmail.com>
Co-authored-by: Sergey Reka <71607800+Sukairo-02@users.noreply.github.com>
Co-authored-by: Meno Abels <meno.abels@adviser.com>
Co-authored-by: Roman <nabukhotnyiroman@gmail.com>
Co-authored-by: L-Mario564 <ka.mario564@gmail.com>
Co-authored-by: David Blass <david@arktype.io>
Co-authored-by: James <5511220+Zamiell@users.noreply.github.com>
Co-authored-by: Ayrton <git@ayrton.be>
Co-authored-by: April Mintac Pineda <21032419+aprilmintacpineda@users.noreply.github.com>
Co-authored-by: Matthew Ary <157217+MatthewAry@users.noreply.github.com>
Co-authored-by: Kratious <Kratious@users.noreply.github.com>
Co-authored-by: Toti Muñoz <64804554+totigm@users.noreply.github.com>
Co-authored-by: Paul Marsicovetere <71470776+paulmarsicloud@users.noreply.github.com>
Co-authored-by: Aleksandr Sherman <102579553+AleksandrSherman@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants