Skip to content

Commit df6d5f7

Browse files
AndriiShermanZamiellayrtonaprilmintacpinedaMatthewAry
authored
0.41 (#4416)
* fix: incorrect types for inArray (#1774) Co-authored-by: Andrii Sherman <[email protected]> * 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 <[email protected]> * 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 <[email protected]> * Export mapColumnToSchema function (#2495) Co-authored-by: Andrii Sherman <[email protected]> * [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 <[email protected]> * 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 <[email protected]> * Drizzle-kit: fix recreate enums + altering data type to enums, from enums in pg (#4330) Co-authored-by: AndriiSherman <[email protected]> * Skip test and try latest gel * Add release notes --------- Co-authored-by: James <[email protected]> Co-authored-by: Ayrton <[email protected]> Co-authored-by: April Mintac Pineda <[email protected]> Co-authored-by: Matthew Ary <[email protected]> Co-authored-by: Kratious <[email protected]> Co-authored-by: Toti Muñoz <[email protected]> Co-authored-by: Dan Kochetov <[email protected]> Co-authored-by: Paul Marsicovetere <[email protected]> Co-authored-by: Aleksandr Sherman <[email protected]>
1 parent 6ab1bbe commit df6d5f7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

86 files changed

+9029
-8902
lines changed

Diff for: changelogs/drizzle-kit/0.31.0.md

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
## Features and improvements
2+
3+
### Enum DDL improvements
4+
5+
For situations where you drop an `enum` value or reorder values in an `enum`, there is no native way to do this in PostgreSQL. To handle these cases, `drizzle-kit` used to:
6+
7+
- Change the column data types from the enum to text
8+
- Drop the old enum
9+
- Add the new enum
10+
- Change the column data types back to the new enum
11+
12+
However, there were a few scenarios that weren’t covered: `PostgreSQL` wasn’t updating default expressions for columns when their data types changed
13+
14+
Therefore, for cases where you either change a column’s data type from an `enum` to some other type, drop an `enum` value, or reorder `enum` values, we now do the following:
15+
16+
- Change the column data types from the enum to text
17+
- Set the default using the ::text expression
18+
- Drop the old enum
19+
- Add the new enum
20+
- Change the column data types back to the new enum
21+
- Set the default using the ::<new_enum> expression
22+
23+
### `esbuild` version upgrade
24+
25+
For `drizzle-kit` we upgraded the version to latest (`0.25.2`), thanks @paulmarsicloud
26+
27+
## Bug fixes
28+
29+
- [[BUG]: Error on Malformed Array Literal](https://github.com/drizzle-team/drizzle-orm/issues/2715) - thanks @Kratious
30+
- [[BUG]: Postgres drizzle-kit: Error while pulling indexes from a table with json/jsonb deep field index](https://github.com/drizzle-team/drizzle-orm/issues/2744) - thanks @Kratious
31+
- [goog-vulnz flags CVE-2024-24790 in esbuild 0.19.7](https://github.com/drizzle-team/drizzle-orm/issues/4045)

Diff for: changelogs/drizzle-orm/0.42.0.md

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
## Features
2+
3+
### Duplicate imports removal
4+
5+
When importing from `drizzle-orm` using custom loaders, you may encounter issues such as: `SyntaxError: The requested module 'drizzle-orm' does not provide an export named 'eq'`
6+
7+
This issue arose because there were duplicated exports in `drizzle-orm`. To address this, we added a set of tests that checks every file in `drizzle-orm` to ensure all exports are valid. These tests will fail if any new duplicated exports appear.
8+
9+
In this release, we’ve removed all duplicated exports, so you should no longer encounter this issue.
10+
11+
### `pgEnum` and `mysqlEnum` now can accept both strings and TS enums
12+
13+
If you provide a TypeScript enum, all your types will be inferred as that enum - so you can insert and retrieve enum values directly. If you provide a string union, it will work as before.
14+
15+
```ts
16+
enum Test {
17+
a = 'a',
18+
b = 'b',
19+
c = 'c',
20+
}
21+
22+
const tableWithTsEnums = mysqlTable('enums_test_case', {
23+
id: serial().primaryKey(),
24+
enum1: mysqlEnum(Test).notNull(),
25+
enum2: mysqlEnum(Test).default(Test.a),
26+
});
27+
28+
await db.insert(tableWithTsEnums).values([
29+
{ id: 1, enum1: Test.a, enum2: Test.b, enum3: Test.c },
30+
{ id: 2, enum1: Test.a, enum3: Test.c },
31+
{ id: 3, enum1: Test.a },
32+
]);
33+
34+
const res = await db.select().from(tableWithTsEnums);
35+
36+
expect(res).toEqual([
37+
{ id: 1, enum1: 'a', enum2: 'b', enum3: 'c' },
38+
{ id: 2, enum1: 'a', enum2: 'a', enum3: 'c' },
39+
{ id: 3, enum1: 'a', enum2: 'a', enum3: 'b' },
40+
]);
41+
```
42+
43+
## Improvements
44+
- Make `inArray` accept `ReadonlyArray` as a value - thanks @Zamiell
45+
- Pass row type parameter to `@planetscale/database`'s execute - thanks @ayrton
46+
- New `InferEnum` type - thanks @totigm
47+
48+
## Issues closed
49+
50+
- [Add first-class support for TS native enums](https://github.com/drizzle-team/drizzle-orm/issues/332)
51+
- [[FEATURE]: support const enums](https://github.com/drizzle-team/drizzle-orm/issues/2798)
52+
- [[BUG]: SyntaxError: The requested module 'drizzle-orm' does not provide an export named 'lte'](https://github.com/drizzle-team/drizzle-orm/issues/4079)

Diff for: changelogs/drizzle-typebox/0.3.2.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Functions `getColumns`, `handleColumns` and `handleEnum` were exported from `drizzle-typebox`

Diff for: drizzle-kit/package.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-kit",
3-
"version": "0.30.6",
3+
"version": "0.31.0",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",
@@ -45,9 +45,8 @@
4545
"dependencies": {
4646
"@drizzle-team/brocli": "^0.10.2",
4747
"@esbuild-kit/esm-loader": "^2.5.5",
48-
"esbuild": "^0.19.7",
49-
"esbuild-register": "^3.5.0",
50-
"gel": "^2.0.0"
48+
"esbuild": "^0.25.2",
49+
"esbuild-register": "^3.5.0"
5150
},
5251
"devDependencies": {
5352
"@arethetypeswrong/cli": "^0.15.3",
@@ -60,7 +59,7 @@
6059
"@neondatabase/serverless": "^0.9.1",
6160
"@originjs/vite-plugin-commonjs": "^1.0.3",
6261
"@planetscale/database": "^1.16.0",
63-
"@types/better-sqlite3": "^7.6.4",
62+
"@types/better-sqlite3": "^7.6.13",
6463
"@types/dockerode": "^3.3.28",
6564
"@types/glob": "^8.1.0",
6665
"@types/json-diff": "^1.0.3",
@@ -76,7 +75,7 @@
7675
"@typescript-eslint/parser": "^7.2.0",
7776
"@vercel/postgres": "^0.8.0",
7877
"ava": "^5.1.0",
79-
"better-sqlite3": "^9.4.3",
78+
"better-sqlite3": "^11.9.1",
8079
"bun-types": "^0.6.6",
8180
"camelcase": "^7.0.1",
8281
"chalk": "^5.2.0",
@@ -90,6 +89,7 @@
9089
"eslint": "^8.57.0",
9190
"eslint-config-prettier": "^9.1.0",
9291
"eslint-plugin-prettier": "^5.1.3",
92+
"gel": "^2.0.0",
9393
"get-port": "^6.1.2",
9494
"glob": "^8.1.0",
9595
"hanji": "^0.0.5",

Diff for: drizzle-kit/src/jsonStatements.ts

+55-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
Index,
88
MatViewWithOption,
99
PgSchema,
10+
PgSchemaSquashed,
1011
PgSquasher,
1112
Policy,
1213
Role,
@@ -168,10 +169,10 @@ export interface JsonAlterRoleStatement {
168169
export interface JsonDropValueFromEnumStatement {
169170
type: 'alter_type_drop_value';
170171
name: string;
171-
schema: string;
172+
enumSchema: string;
172173
deletedValues: string[];
173174
newValues: string[];
174-
columnsWithEnum: { schema: string; table: string; column: string }[];
175+
columnsWithEnum: { tableSchema: string; table: string; column: string; default?: string; columnType: string }[];
175176
}
176177

177178
export interface JsonCreateSequenceStatement {
@@ -472,6 +473,22 @@ export interface JsonAlterColumnTypeStatement {
472473
columnGenerated?: { as: string; type: 'stored' | 'virtual' };
473474
}
474475

476+
export interface JsonAlterColumnPgTypeStatement {
477+
type: 'pg_alter_table_alter_column_set_type';
478+
tableName: string;
479+
columnName: string;
480+
typeSchema: string | undefined;
481+
newDataType: { name: string; isEnum: boolean };
482+
oldDataType: { name: string; isEnum: boolean };
483+
schema: string;
484+
columnDefault: string;
485+
columnOnUpdate: boolean;
486+
columnNotNull: boolean;
487+
columnAutoIncrement: boolean;
488+
columnPk: boolean;
489+
columnGenerated?: { as: string; type: 'stored' | 'virtual' };
490+
}
491+
475492
export interface JsonAlterColumnSetPrimaryKeyStatement {
476493
type: 'alter_table_alter_column_set_pk';
477494
tableName: string;
@@ -784,6 +801,7 @@ export type JsonAlterViewStatement =
784801
export type JsonAlterColumnStatement =
785802
| JsonRenameColumnStatement
786803
| JsonAlterColumnTypeStatement
804+
| JsonAlterColumnPgTypeStatement
787805
| JsonAlterColumnSetDefaultStatement
788806
| JsonAlterColumnDropDefaultStatement
789807
| JsonAlterColumnSetNotNullStatement
@@ -1044,22 +1062,32 @@ export const prepareDropEnumValues = (
10441062
): JsonDropValueFromEnumStatement[] => {
10451063
if (!removedValues.length) return [];
10461064

1047-
const affectedColumns: { schema: string; table: string; column: string }[] = [];
1065+
const affectedColumns: JsonDropValueFromEnumStatement['columnsWithEnum'] = [];
10481066

10491067
for (const tableKey in json2.tables) {
10501068
const table = json2.tables[tableKey];
10511069
for (const columnKey in table.columns) {
10521070
const column = table.columns[columnKey];
1053-
if (column.type === name && column.typeSchema === schema) {
1054-
affectedColumns.push({ schema: table.schema || 'public', table: table.name, column: column.name });
1071+
1072+
const arrayDefinitionRegex = /\[\d*(?:\[\d*\])*\]/g;
1073+
const parsedColumnType = column.type.replace(arrayDefinitionRegex, '');
1074+
1075+
if (parsedColumnType === name && column.typeSchema === schema) {
1076+
affectedColumns.push({
1077+
tableSchema: table.schema,
1078+
table: table.name,
1079+
column: column.name,
1080+
columnType: column.type,
1081+
default: column.default,
1082+
});
10551083
}
10561084
}
10571085
}
10581086

10591087
return [{
10601088
type: 'alter_type_drop_value',
10611089
name: name,
1062-
schema: schema,
1090+
enumSchema: schema,
10631091
deletedValues: removedValues,
10641092
newValues: json2.enums[`${schema}.${name}`].values,
10651093
columnsWithEnum: affectedColumns,
@@ -2048,7 +2076,8 @@ export const preparePgAlterColumns = (
20482076
schema: string,
20492077
columns: AlteredColumn[],
20502078
// TODO: remove?
2051-
json2: CommonSquashedSchema,
2079+
json2: PgSchemaSquashed,
2080+
json1: PgSchemaSquashed,
20522081
action?: 'push' | undefined,
20532082
): JsonAlterColumnStatement[] => {
20542083
const tableKey = `${schema || 'public'}.${_tableName}`;
@@ -2074,6 +2103,8 @@ export const preparePgAlterColumns = (
20742103
).autoincrement;
20752104
const columnPk = (json2.tables[tableKey].columns[columnName] as any)
20762105
.primaryKey;
2106+
const typeSchema = json2.tables[tableKey].columns[columnName].typeSchema;
2107+
const json1ColumnTypeSchema = json1.tables[tableKey].columns[columnName].typeSchema;
20772108

20782109
const compositePk = json2.tables[tableKey].compositePrimaryKeys[`${tableName}_${columnName}`];
20792110

@@ -2088,12 +2119,26 @@ export const preparePgAlterColumns = (
20882119
}
20892120

20902121
if (column.type?.type === 'changed') {
2122+
const arrayDefinitionRegex = /\[\d*(?:\[\d*\])*\]/g;
2123+
const parsedNewColumnType = column.type.new.replace(arrayDefinitionRegex, '');
2124+
const parsedOldColumnType = column.type.old.replace(arrayDefinitionRegex, '');
2125+
2126+
const isNewTypeIsEnum = json2.enums[`${typeSchema}.${parsedNewColumnType}`];
2127+
const isOldTypeIsEnum = json1.enums[`${json1ColumnTypeSchema}.${parsedOldColumnType}`];
2128+
20912129
statements.push({
2092-
type: 'alter_table_alter_column_set_type',
2130+
type: 'pg_alter_table_alter_column_set_type',
20932131
tableName,
20942132
columnName,
2095-
newDataType: column.type.new,
2096-
oldDataType: column.type.old,
2133+
typeSchema: typeSchema,
2134+
newDataType: {
2135+
name: column.type.new,
2136+
isEnum: isNewTypeIsEnum ? true : false,
2137+
},
2138+
oldDataType: {
2139+
name: column.type.old,
2140+
isEnum: isOldTypeIsEnum ? true : false,
2141+
},
20972142
schema,
20982143
columnDefault,
20992144
columnOnUpdate,

Diff for: drizzle-kit/src/serializer/pgSerializer.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
getTableConfig,
77
getViewConfig,
88
IndexedColumn,
9+
PgArray,
910
PgColumn,
1011
PgDialect,
1112
PgEnum,
@@ -158,7 +159,14 @@ export const generatePgSnapshot = (
158159
const primaryKey: boolean = column.primary;
159160
const sqlTypeLowered = column.getSQLType().toLowerCase();
160161

161-
const typeSchema = is(column, PgEnumColumn) ? column.enum.schema || 'public' : undefined;
162+
const getEnumSchema = (column: PgColumn) => {
163+
while (is(column, PgArray)) {
164+
column = column.baseColumn;
165+
}
166+
return is(column, PgEnumColumn) ? column.enum.schema || 'public' : undefined;
167+
};
168+
const typeSchema: string | undefined = getEnumSchema(column);
169+
162170
const generated = column.generated;
163171
const identity = column.generatedIdentity;
164172

@@ -1536,14 +1544,7 @@ WHERE
15361544
i.indisunique as is_unique,
15371545
am.amname as method,
15381546
ic.reloptions as with,
1539-
coalesce(a.attname,
1540-
(('{' || pg_get_expr(
1541-
i.indexprs,
1542-
i.indrelid
1543-
)
1544-
|| '}')::text[]
1545-
)[k.i]
1546-
) AS column_name,
1547+
coalesce(a.attname, pg_get_indexdef(i.indexrelid, k.i, false)) AS column_name,
15471548
CASE
15481549
WHEN pg_get_expr(i.indexprs, i.indrelid) IS NOT NULL THEN 1
15491550
ELSE 0

0 commit comments

Comments
 (0)