Skip to content

Commit 6adbd78

Browse files
Merge pull request drizzle-team#2760 from drizzle-team/beta
Beta
2 parents 7d2ae84 + f2a2b5f commit 6adbd78

File tree

20 files changed

+890
-35
lines changed

20 files changed

+890
-35
lines changed

changelogs/drizzle-kit/0.24.0.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
## Breaking changes (for SQLite users)
2+
3+
#### Fixed [Composite primary key order is not consistent](https://github.com/drizzle-team/drizzle-kit-mirror/issues/342) by removing `sort` in SQLite and to be consistant with the same logic in PostgreSQL and MySQL
4+
5+
The issue that may arise for SQLite users with any driver using composite primary keys is that the order in the database may differ from the Drizzle schema.
6+
7+
- If you are using `push`, you **MAY** be prompted to update your table with a new order of columns in the composite primary key. You will need to either change it manually in the database or push the changes, but this may lead to data loss, etc.
8+
9+
- If you are using `generate`, you **MAY** also be prompted to update your table with a new order of columns in the composite primary key. You can either keep that migration or skip it by emptying the SQL migration file.
10+
11+
If nothing works for you and you are blocked, please reach out to me @AndriiSherman. I will try to help you!
12+
13+
14+
## Bug fixes
15+
16+
- [[BUG] When using double type columns, import is not inserted](https://github.com/drizzle-team/drizzle-kit-mirror/issues/403) - thanks @Karibash
17+
- [[BUG] A number value is specified as the default for a column of type char](https://github.com/drizzle-team/drizzle-kit-mirror/issues/404) - thanks @Karibash
18+
- [[BUG]: Array default in migrations are wrong](https://github.com/drizzle-team/drizzle-orm/issues/2621) - thanks @L-Mario564
19+
- [[FEATURE]: Simpler default array fields](https://github.com/drizzle-team/drizzle-orm/issues/2709) - thanks @L-Mario564
20+
- [[BUG]: drizzle-kit generate succeeds but generates invalid SQL for default([]) - Postgres](https://github.com/drizzle-team/drizzle-orm/issues/2432) - thanks @L-Mario564
21+
- [[BUG]: Incorrect type for array column default value](https://github.com/drizzle-team/drizzle-orm/issues/2334) - thanks @L-Mario564
22+
- [[BUG]: error: column is of type integer[] but default expression is of type integer](https://github.com/drizzle-team/drizzle-orm/issues/2224) - thanks @L-Mario564
23+
- [[BUG]: Default value in array generating wrong migration file](https://github.com/drizzle-team/drizzle-orm/issues/1003) - thanks @L-Mario564
24+
- [[BUG]: enum as array, not possible?](https://github.com/drizzle-team/drizzle-orm/issues/1564) - thanks @L-Mario564

changelogs/drizzle-orm/0.33.0.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Breaking changes (for some of postgres.js users)
2+
3+
#### Bugs fixed for this breaking change
4+
5+
- [Open
6+
[BUG]: jsonb always inserted as a json string when using postgres-js](https://github.com/drizzle-team/drizzle-orm/issues/724)
7+
- [[BUG]: jsonb type on postgres implement incorrectly](https://github.com/drizzle-team/drizzle-orm/issues/1511)
8+
9+
> As we are doing with other drivers, we've changed the behavior of PostgreSQL-JS to pass raw JSON values, the same as you see them in the database. So if you are using the PostgreSQL-JS driver and passing data to Drizzle elsewhere, please check the new behavior of the client after it is passed to Drizzle.
10+
11+
> We will update it to ensure it does not override driver behaviors, but this will be done as a complex task for everything in Drizzle in other releases
12+
13+
If you were using `postgres-js` with `jsonb` fields, you might have seen stringified objects in your database, while drizzle insert and select operations were working as expected.
14+
15+
You need to convert those fields from strings to actual JSON objects. To do this, you can use the following query to update your database:
16+
17+
**if you are using jsonb:**
18+
```sql
19+
update table_name
20+
set jsonb_column = (jsonb_column #>> '{}')::jsonb;
21+
```
22+
23+
**if you are using json:**
24+
```sql
25+
update table_name
26+
set json_column = (json_column #>> '{}')::json;
27+
```
28+
29+
We've tested it in several cases, and it worked well, but only if all stringified objects are arrays or objects. If you have primitives like strings, numbers, booleans, etc., you can use this query to update all the fields
30+
31+
**if you are using jsonb:**
32+
```sql
33+
UPDATE table_name
34+
SET jsonb_column = CASE
35+
-- Convert to JSONB if it is a valid JSON object or array
36+
WHEN jsonb_column #>> '{}' LIKE '{%' OR jsonb_column #>> '{}' LIKE '[%' THEN
37+
(jsonb_column #>> '{}')::jsonb
38+
ELSE
39+
jsonb_column
40+
END
41+
WHERE
42+
jsonb_column IS NOT NULL;
43+
```
44+
45+
**if you are using json:**
46+
```sql
47+
UPDATE table_name
48+
SET json_column = CASE
49+
-- Convert to JSON if it is a valid JSON object or array
50+
WHEN json_column #>> '{}' LIKE '{%' OR json_column #>> '{}' LIKE '[%' THEN
51+
(json_column #>> '{}')::json
52+
ELSE
53+
json_column
54+
END
55+
WHERE json_column IS NOT NULL;
56+
```
57+
58+
If nothing works for you and you are blocked, please reach out to me @AndriiSherman. I will try to help you!
59+
60+
## Bug Fixes
61+
62+
- [[BUG]: boolean mode not working with prepared statements (bettersqlite)](https://github.com/drizzle-team/drizzle-orm/issues/2568) - thanks @veloii
63+
- [[BUG]: isTable helper function is not working](https://github.com/drizzle-team/drizzle-orm/issues/2672) - thanks @hajek-raven
64+
- [[BUG]: Documentation is outdated on inArray and notInArray Methods](https://github.com/drizzle-team/drizzle-orm/issues/2690) - thanks @RemiPeruto

drizzle-kit/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-kit",
3-
"version": "0.23.2",
3+
"version": "0.24.0",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",

drizzle-kit/src/api.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
} from './cli/commands/migrate';
1313
import { pgPushIntrospect } from './cli/commands/pgIntrospect';
1414
import { pgSuggestions } from './cli/commands/pgPushUtils';
15-
import { updateUpToV6 as upPgV6 } from './cli/commands/pgUp';
15+
import { updateUpToV6 as upPgV6, updateUpToV7 as upPgV7 } from './cli/commands/pgUp';
1616
import { sqlitePushIntrospect } from './cli/commands/sqliteIntrospect';
1717
import { logSuggestionsAndReturn } from './cli/commands/sqlitePushUtils';
1818
import { originUUID } from './global';
@@ -341,5 +341,11 @@ export const pushMySQLSchema = async (
341341
};
342342

343343
export const upPgSnapshot = (snapshot: Record<string, unknown>) => {
344-
return upPgV6(snapshot);
344+
if (snapshot.version === '5') {
345+
return upPgV7(upPgV6(snapshot));
346+
}
347+
if (snapshot.version === '6') {
348+
return upPgV7(snapshot);
349+
}
350+
return snapshot;
345351
};

drizzle-kit/src/introspect-mysql.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,7 @@ export const schemaToTypeScript = (
153153
patched = patched.startsWith('datetime(') ? 'datetime' : patched;
154154
patched = patched.startsWith('varbinary(') ? 'varbinary' : patched;
155155
patched = patched.startsWith('int(') ? 'int' : patched;
156+
patched = patched.startsWith('double(') ? 'double' : patched;
156157
return patched;
157158
})
158159
.filter((type) => {

drizzle-kit/src/serializer/mysqlSerializer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,7 @@ export const fromDatabase = async (
481481
default: columnDefault === null
482482
? undefined
483483
: /^-?[\d.]+(?:e-?\d+)?$/.test(columnDefault)
484-
&& !columnType.startsWith('decimal')
484+
&& !['decimal', 'char', 'varchar'].some((type) => columnType.startsWith(type))
485485
? Number(columnDefault)
486486
: isDefaultAnExpression
487487
? clearDefaults(columnDefault, collation)

drizzle-kit/src/serializer/pgSerializer.ts

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import type {
3030
Table,
3131
UniqueConstraint,
3232
} from '../serializer/pgSchema';
33-
import type { DB } from '../utils';
33+
import { type DB, isPgArrayType } from '../utils';
3434
import { sqlToStr } from '.';
3535

3636
const dialect = new PgDialect();
@@ -75,6 +75,43 @@ function stringFromDatabaseIdentityProperty(field: any): string | undefined {
7575
: String(field);
7676
}
7777

78+
function buildArrayString(array: any[], sqlType: string): string {
79+
sqlType = sqlType.split('[')[0];
80+
const values = array
81+
.map((value) => {
82+
if (typeof value === 'number' || typeof value === 'bigint') {
83+
return value.toString();
84+
} else if (typeof value === 'boolean') {
85+
return value ? 'true' : 'false';
86+
} else if (Array.isArray(value)) {
87+
return buildArrayString(value, sqlType);
88+
} else if (value instanceof Date) {
89+
if (sqlType === 'date') {
90+
return `"${value.toISOString().split('T')[0]}"`;
91+
} else if (sqlType === 'timestamp') {
92+
return `"${
93+
value.toISOString()
94+
.replace('T', ' ')
95+
.slice(0, 23)
96+
}"`;
97+
} else {
98+
return `"${value.toISOString()}"`;
99+
}
100+
} else if (typeof value === 'object') {
101+
return `"${
102+
JSON
103+
.stringify(value)
104+
.replaceAll('"', '\\"')
105+
}"`;
106+
}
107+
108+
return `"${value}"`;
109+
})
110+
.join(',');
111+
112+
return `{${values}}`;
113+
}
114+
78115
export const generatePgSnapshot = (
79116
tables: AnyPgTable[],
80117
enums: PgEnum<any>[],
@@ -226,6 +263,13 @@ export const generatePgSnapshot = (
226263
} else {
227264
columnToSet.default = `'${column.default.toISOString()}'`;
228265
}
266+
} else if (isPgArrayType(sqlTypeLowered) && Array.isArray(column.default)) {
267+
columnToSet.default = `'${
268+
buildArrayString(
269+
column.default,
270+
sqlTypeLowered,
271+
)
272+
}'::${sqlTypeLowered}`;
229273
} else {
230274
// Should do for all types
231275
// columnToSet.default = `'${column.default}'::${sqlTypeLowered}`;

drizzle-kit/src/serializer/sqliteSerializer.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ export const generateSqliteSnapshot = (
237237
primaryKeys.forEach((it) => {
238238
if (it.columns.length > 1) {
239239
primaryKeysObject[it.getName()] = {
240-
columns: it.columns.map((it) => it.name).sort(),
240+
columns: it.columns.map((it) => it.name),
241241
name: it.getName(),
242242
};
243243
} else {
@@ -509,7 +509,6 @@ export const fromDatabase = async (
509509

510510
for (const [key, value] of Object.entries(tableToPk)) {
511511
if (value.length > 1) {
512-
value.sort();
513512
result[key].compositePrimaryKeys = {
514513
[`${key}_${value.join('_')}_pk`]: {
515514
columns: value,

drizzle-kit/src/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,3 +327,7 @@ export const normaliseSQLiteUrl = (
327327

328328
assertUnreachable(type);
329329
};
330+
331+
export function isPgArrayType(sqlType: string) {
332+
return sqlType.match(/.*\[\d*\].*|.*\[\].*/g) !== null;
333+
}

drizzle-kit/tests/introspect/mysql.test.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import Docker from 'dockerode';
22
import { SQL, sql } from 'drizzle-orm';
3-
import { int, mysqlTable, text } from 'drizzle-orm/mysql-core';
3+
import { char, int, mysqlTable, text, varchar } from 'drizzle-orm/mysql-core';
44
import * as fs from 'fs';
55
import getPort from 'get-port';
66
import { Connection, createConnection } from 'mysql2/promise';
@@ -123,3 +123,45 @@ test('generated always column virtual: link to another column', async () => {
123123

124124
await client.query(`drop table users;`);
125125
});
126+
127+
test('Default value of character type column: char', async () => {
128+
const schema = {
129+
users: mysqlTable('users', {
130+
id: int('id'),
131+
sortKey: char('sortKey', { length: 255 }).default('0'),
132+
}),
133+
};
134+
135+
const { statements, sqlStatements } = await introspectMySQLToFile(
136+
client,
137+
schema,
138+
'default-value-char-column',
139+
'drizzle',
140+
);
141+
142+
expect(statements.length).toBe(0);
143+
expect(sqlStatements.length).toBe(0);
144+
145+
await client.query(`drop table users;`);
146+
});
147+
148+
test('Default value of character type column: varchar', async () => {
149+
const schema = {
150+
users: mysqlTable('users', {
151+
id: int('id'),
152+
sortKey: varchar('sortKey', { length: 255 }).default('0'),
153+
}),
154+
};
155+
156+
const { statements, sqlStatements } = await introspectMySQLToFile(
157+
client,
158+
schema,
159+
'default-value-varchar-column',
160+
'drizzle',
161+
);
162+
163+
expect(statements.length).toBe(0);
164+
expect(sqlStatements.length).toBe(0);
165+
166+
await client.query(`drop table users;`);
167+
});

0 commit comments

Comments
 (0)