Skip to content

Commit 1d0fc06

Browse files
AleksandrShermanOleksiiKH0240AlexBlokhAndriiSherman
authored
Issues (#5168)
* updated tests * + * updated tests * [drizzle-kit] updated tests * [drizzle-kit] added tests * [drizzle-kit] added tests * [drizzle-kit] updated test * added tests * updated pull tests * + * [integration-tests] updated tests * corrected issue links * [drizzle kit] added test * [drizzle-kit] added test * [drizzle-kit] added tests * [feat-orm]: index logic Added ability to pass table column to index, uniqueIndex, using in outer const. Without using 3rd param in table * added tests * updated tests * [drizzle-kit] added tests * [fix+update]: mysql use/force/ignore index update + pg asc/nullsFirst index update Mysql: - Added ability to handle unique constraints in use/force/ignore indexes in select queries - Added tests Pg: - in drizzle-kit/drizzle.ts file updated logic regarding nullsFirst and asc/desc creation pg docs: https://www.postgresql.org/docs/current/sql-createindex.html * [update]: changelog * [drizzle-kit] added tests * [psql]: fixed broken tests in kit * [mysql]: fixed some an issue in kit * [sqlite]: isssue + fk introspect bugs fixed * [mssql]: index bug fix in kit * [cockroach]: fixed tests in kit * [sqlite]: fk introspect fix * [psql]: kit pk tests * [update]: changelog for drizzle-kit * [mssql]: kit fix * [drizzle-kit] added tests * [mssql-kit]: up to v2 * [kit]: psql + cockroach enum tests * [drizzle-kit] added tests * updated comments in tests * [update]: kit tests update * [update]: ignored ts error for deploy * [update-casing]: removed casing from Name class * [update]: isNull wrapped in () * [updates]: tests + bug fixes - Fixed "buildIndex" method in mysql for `use/force/ignore` indexes - Updated some integration tests * [update]: fixed int:sqlite tests * [update]: singlestore int test * [update]: singlestore test * [update]: changelog for kit + up of package.json versions of orm and kit * [update]: changelog + test * [drizzle-kit] added tests * [drizzle-kit] added test * updated test * [update-kit]: psql test * [update-kit]: fixed tests Added: - handling on column alters to serial in pg - suggestions for diff in mysql * [update-kit]: deprecated 'strict' flag * [update-kit]: test * [update-kit]: test * [update-kit]: mssql test * [update-kit]: fix * [update-kit]: added explain to every dialect * added tests * fixed prompt for column conflicts; added tests * [update-kit]: sqlite up fixes - Added tests on up (like pg and mysql had) - fixed some minor fixes * [fix-kit]: errors in mocks * [fix-kit]: mssql fixes * [fix-kit]: tests fix to deploy * [fix-kit] * [fix-kit] * [fix-kit] * [update]: versions + changelog for sqlite * [update]: comment * [update-kit]: fixed mssql + new tests * [fix-kit]: --explain handling * Update release notes --------- Co-authored-by: OleksiiKH0240 <homenko0240@gmail.com> Co-authored-by: Alex Blokh <aleksandrblokh@gmail.com> Co-authored-by: AndriiSherman <andreysherman11@gmail.com>
1 parent 4605abe commit 1d0fc06

90 files changed

Lines changed: 11866 additions & 682 deletions

File tree

Some content is hidden

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

changelogs/drizzle-kit/1.0.0-beta.4.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ Regression issues fixed(from `-beta.x` releases)
55

66
Issues fixed(from latest tag)
77

8-
- [[BUG]: drizzle-kit check wrongly tries to use AWS Data API](https://github.com/drizzle-team/drizzle-orm/issues/4775)
8+
- [[BUG]: drizzle-kit check wrongly tries to use AWS Data API](https://github.com/drizzle-team/drizzle-orm/issues/4775)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Bug fixes
2+
3+
- [[BUG]: error: type "serial" does not exist](https://github.com/drizzle-team/drizzle-orm/issues/2183)
4+
- [[BUG]: jsonb default with boolean literals gets generated truen instead of true](https://github.com/drizzle-team/drizzle-orm/issues/5149)
5+
- [[BUG]: MSSQL view incorrect syntax](https://github.com/drizzle-team/drizzle-orm/issues/5113)
6+
7+
## Changes to SQLite `drizzle-kit up` command
8+
9+
> Important!
10+
>
11+
> If you were already using SQLite in any `beta.x` version and have used the `drizzle-kit up` command, you will not receive the latest `up` changes from this release. If you are unable to reset migrations and start from scratch, you will need to contact us for support with upgrading
12+
13+
### What was changed?
14+
15+
#### Handling of UNIQUE constraints in SQLite
16+
17+
In the new version `drizzle-kit` handles `UNIQUE` constraints. This decision was made because when a unique constraint is created it cannot be removed, whereas an index can be dropped
18+
19+
Previous version of Drizzle-Kit always created `.unique()` as a `uniqueIndex` and stored it in the snapshot that way. Because of this during an up we lack of sufficient information and if a user used `.unique()` an upped will generate a diff on generate and push
20+
21+
**Solution:**
22+
23+
We are replacing all unique constraints with `uniqueIndex`
24+
25+
`uniqueIndex` requires a name and the name must follow this format:
26+
27+
`<table>_<column1>*_*<column2>_..._unique`
28+
29+
#### Foreign key name handling in the old(pre 1.0) drizzle-kit
30+
31+
The old `drizzle-kit` did not handle foreign key names when generated sql migrations. A foreign key name could be defined in the ts schema, but it was not passed through when generating sql
32+
33+
```tsx
34+
export const table = sqliteTable("table", {
35+
column1: integer(),
36+
column2: integer()},
37+
(t) => [
38+
foreignKey({
39+
name: "name",
40+
columns: [t.column1],
41+
foreignColumns: [t.column2],
42+
}),
43+
]
44+
);
45+
46+
// no name provided
47+
FOREIGN KEY (`timest`) REFERENCES `b`(`timest1`) ON UPDATE no action ON DELETE no action
48+
```
49+
50+
On `introspect` new drizzle-kit parses ddl to find constraint name, if no name found - use default name
51+
52+
After running `drizzle-kit up` the first `push` command will result in a diff that recreates the table (no name from db, but there is name in ts schema). To avoid this foreign key names should be removed - in that case no diff will be generated.
53+
54+
`drizzle-kit generate` command will behave as expected, no changes needed.
55+
56+
#### Bug in the old drizzle-kit related to foreign keys
57+
58+
If you add a column to an existing table that has a foreign key and specify `onDelete` or `onUpdate`, column will be added with the foreign key, but without those parameters
59+
60+
```tsx
61+
export const table = sqliteTable("table", {
62+
column1: integer()
63+
});
64+
65+
export const table = sqliteTable("table", {
66+
column1: integer(),
67+
column2: integer().references((): AnySQLiteColumn => table.column1,
68+
{
69+
onDelete: "set null",
70+
onUpdate: "set default"
71+
})
72+
});
73+
74+
ALTER TABLE `table` ADD `column2` integer REFERENCES table(column1);
75+
```
76+
77+
There is no way to fix this in the old snapshot. It leads to persistent diffs during `push` with old Drizzle-Kit.
78+
79+
New `drizzle-kit` will recreate table after push command with the correct SQL. When using `generate` command, no diffs will appear, but the actual database state may differ
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
## Bug fixes
2+
3+
- [[BUG]: error: type "serial" does not exist](https://github.com/drizzle-team/drizzle-orm/issues/2183)
4+
- [[BUG]: jsonb default with boolean literals gets generated truen instead of true](https://github.com/drizzle-team/drizzle-orm/issues/5149)
5+
- [[BUG]: MSSQL view incorrect syntax](https://github.com/drizzle-team/drizzle-orm/issues/5113)
6+
7+
## Changes to SQLite `drizzle-kit up` command
8+
9+
> Important!
10+
>
11+
> If you were already using SQLite in any `beta.x` version and have used the `drizzle-kit up` command, you will not receive the latest `up` changes from this release. If you are unable to reset migrations and start from scratch, you will need to contact us for support with upgrading
12+
13+
### What was changed?
14+
15+
#### Handling of UNIQUE constraints in SQLite
16+
17+
In the new version `drizzle-kit` handles `UNIQUE` constraints. This decision was made because when a unique constraint is created it cannot be removed, whereas an index can be dropped
18+
19+
Previous version of Drizzle-Kit always created `.unique()` as a `uniqueIndex` and stored it in the snapshot that way. Because of this during an up we lack of sufficient information and if a user used `.unique()` an upped will generate a diff on generate and push
20+
21+
**Solution:**
22+
23+
We are replacing all unique constraints with `uniqueIndex`
24+
25+
`uniqueIndex` requires a name and the name must follow this format:
26+
27+
`<table>_<column1>*_*<column2>_..._unique`
28+
29+
#### Foreign key name handling in the old(pre 1.0) drizzle-kit
30+
31+
The old `drizzle-kit` did not handle foreign key names when generated sql migrations. A foreign key name could be defined in the ts schema, but it was not passed through when generating sql
32+
33+
```tsx
34+
export const table = sqliteTable("table", {
35+
column1: integer(),
36+
column2: integer()},
37+
(t) => [
38+
foreignKey({
39+
name: "name",
40+
columns: [t.column1],
41+
foreignColumns: [t.column2],
42+
}),
43+
]
44+
);
45+
46+
// no name provided
47+
FOREIGN KEY (`timest`) REFERENCES `b`(`timest1`) ON UPDATE no action ON DELETE no action
48+
```
49+
50+
On `introspect` new drizzle-kit parses ddl to find constraint name, if no name found - use default name
51+
52+
After running `drizzle-kit up` the first `push` command will result in a diff that recreates the table (no name from db, but there is name in ts schema). To avoid this foreign key names should be removed - in that case no diff will be generated.
53+
54+
`drizzle-kit generate` command will behave as expected, no changes needed.
55+
56+
#### Bug in the old drizzle-kit related to foreign keys
57+
58+
If you add a column to an existing table that has a foreign key and specify `onDelete` or `onUpdate`, column will be added with the foreign key, but without those parameters
59+
60+
```tsx
61+
export const table = sqliteTable("table", {
62+
column1: integer()
63+
});
64+
65+
export const table = sqliteTable("table", {
66+
column1: integer(),
67+
column2: integer().references((): AnySQLiteColumn => table.column1,
68+
{
69+
onDelete: "set null",
70+
onUpdate: "set default"
71+
})
72+
});
73+
74+
ALTER TABLE `table` ADD `column2` integer REFERENCES table(column1);
75+
```
76+
77+
There is no way to fix this in the old snapshot. It leads to persistent diffs during `push` with old Drizzle-Kit.
78+
79+
New `drizzle-kit` will recreate table after push command with the correct SQL. When using `generate` command, no diffs will appear, but the actual database state may differ

drizzle-arktype/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-arktype",
3-
"version": "1.0.0-beta.4",
3+
"version": "1.0.0-beta.5",
44
"description": "Generate arktype schemas from Drizzle ORM schemas",
55
"type": "module",
66
"scripts": {

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": "1.0.0-beta.4",
3+
"version": "1.0.0-beta.5",
44
"homepage": "https://orm.drizzle.team",
55
"keywords": [
66
"drizzle",

drizzle-kit/src/cli/commands/generate-mysql.ts

Lines changed: 86 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,91 @@
11
import { fromDrizzleSchema, prepareFromSchemaFiles } from 'src/dialects/mysql/drizzle';
22
import { prepareSnapshot } from 'src/dialects/mysql/serializer';
3+
import type { JsonStatement } from 'src/dialects/mysql/statements';
34
import { prepareFilenames, prepareOutFolder } from 'src/utils/utils-node';
4-
import { type Column, createDDL, interimToDDL, type Table, type View } from '../../dialects/mysql/ddl';
5+
import { type Column, createDDL, interimToDDL, type MysqlDDL, type Table, type View } from '../../dialects/mysql/ddl';
56
import { ddlDiff, ddlDiffDry } from '../../dialects/mysql/diff';
67
import { resolver } from '../prompts';
8+
import { withStyle } from '../validations/outputs';
79
import { explain } from '../views';
810
import { writeResult } from './generate-common';
911
import type { ExportConfig, GenerateConfig } from './utils';
1012

13+
export const suggestions = (jsonStatements: JsonStatement[], ddl2: MysqlDDL) => {
14+
const grouped: { hints: string[]; errors: string[] } = { errors: [], hints: [] };
15+
16+
for (const statement of jsonStatements) {
17+
if (statement.type === 'create_fk' && statement.cause !== 'alter_pk') {
18+
const { columnsTo, table, tableTo, columns } = statement.fk;
19+
20+
const indexes = ddl2.indexes.list({ isUnique: true, table: tableTo });
21+
const pk = ddl2.pks.one({ table: tableTo });
22+
23+
const columnsToSet = new Set(columnsTo);
24+
25+
const isUniqueFound = indexes.some((index) => {
26+
if (index.columns.length !== columnsToSet.size) {
27+
return false;
28+
}
29+
30+
return index.columns.every((col) => columnsToSet.has(col.value));
31+
});
32+
33+
const isPkFound = pk && pk.columns.length === columnsToSet.size
34+
&& pk.columns.every((col) => columnsToSet.has(col));
35+
36+
if (isPkFound || isUniqueFound) continue;
37+
38+
let composite = columnsTo.length > 1 ? 'composite ' : '';
39+
grouped.errors.push(
40+
`· You are trying to add reference from "${table}" ("${columns.join('", ')}") to "${tableTo}" ("${
41+
columnsTo.join(
42+
'", ',
43+
)
44+
}"). The referenced columns are not guaranteed to be unique together. A foreign key must point to a PRIMARY KEY or a set of columns with a UNIQUE constraint. You should add a ${composite}unique constraint to the referenced columns`,
45+
);
46+
47+
continue;
48+
}
49+
50+
if (statement.type === 'drop_pk') {
51+
const { table, columns } = statement.pk;
52+
53+
const fks = ddl2.fks.list({ tableTo: table });
54+
const indexes = ddl2.indexes.list({ table: table });
55+
56+
const fkFound = fks.filter((fk) => {
57+
if (fk.columnsTo.length !== columns.length) return false;
58+
59+
return fk.columnsTo.every((fkCol) => columns.includes(fkCol));
60+
});
61+
62+
if (fkFound.length === 0) continue;
63+
64+
const indexesFound = indexes.some((index) => {
65+
if (index.columns.length !== columns.length) {
66+
return false;
67+
}
68+
69+
return index.columns.every((col) => columns.includes(col.value));
70+
});
71+
72+
if (indexesFound) continue;
73+
74+
grouped.errors.push(
75+
`· You are trying to drop primary key from "${table}" ("${
76+
columns.join('", ')
77+
}"), but there is an existing reference on this column. You must either add a UNIQUE constraint to ("${
78+
columns.join('", ')
79+
}") or drop the foreign key constraint that references this column.`,
80+
);
81+
82+
continue;
83+
}
84+
}
85+
86+
return grouped;
87+
};
88+
1189
export const handle = async (config: GenerateConfig) => {
1290
const outFolder = config.out;
1391
const schemaPath = config.schema;
@@ -31,7 +109,7 @@ export const handle = async (config: GenerateConfig) => {
31109
return;
32110
}
33111

34-
const { sqlStatements, renames, groupedStatements } = await ddlDiff(
112+
const { sqlStatements, renames, groupedStatements, statements } = await ddlDiff(
35113
ddlPrev,
36114
ddlCur,
37115
resolver<Table>('table'),
@@ -40,6 +118,12 @@ export const handle = async (config: GenerateConfig) => {
40118
'default',
41119
);
42120

121+
const { errors } = suggestions(statements, ddlCur);
122+
if (errors.length) {
123+
console.log(errors.map((err) => withStyle.errorWarning(err)).join('\n\n'));
124+
process.exit(1);
125+
}
126+
43127
const explainMessage = explain('mysql', groupedStatements, false, []);
44128
if (explainMessage) console.log(explainMessage);
45129

0 commit comments

Comments
 (0)