Skip to content

Commit 31c0dce

Browse files
committed
Merge branch 'issues' of https://github.com/drizzle-team/drizzle-orm into issues
2 parents f61ee9b + 3496eb9 commit 31c0dce

Some content is hidden

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

59 files changed

+4806
-414
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- MySQL use/force/ingore index now accepts `unique constraints`

drizzle-arktype/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "drizzle-arktype",
3-
"version": "0.1.3",
3+
"version": "1.0.0-beta.2",
44
"description": "Generate arktype schemas from Drizzle ORM schemas",
55
"type": "module",
66
"scripts": {
@@ -59,7 +59,7 @@
5959
"license": "Apache-2.0",
6060
"peerDependencies": {
6161
"arktype": ">=2.0.0",
62-
"drizzle-orm": ">=1.0.0-beta.1"
62+
"drizzle-orm": ">=1.0.0-beta.2"
6363
},
6464
"devDependencies": {
6565
"@ark/attest": "^0.45.8",

drizzle-kit/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ Drizzle Kit is a CLI migrator tool for Drizzle ORM. It is probably the one and o
77

88
Check the full documentation on [the website](https://orm.drizzle.team/kit-docs/overview).
99

10+
1011
### How it works
1112

1213
Drizzle Kit traverses a schema module and generates a snapshot to compare with the previous version, if there is one.

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

drizzle-kit/src/cli/commands/check.ts

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ export const checkHandler = async (out: string, dialect: Dialect) => {
77
const { snapshots } = prepareOutFolder(out);
88
const validator = validatorForDialect(dialect);
99

10-
const snapshotsData: any[] = [];
10+
// const snapshotsData: PostgresSnapshot[] = [];
1111

1212
for (const snapshot of snapshots) {
1313
const raw = JSON.parse(readFileSync(`./${snapshot}`).toString());
1414

15-
snapshotsData.push(raw);
15+
// snapshotsData.push(raw);
1616

1717
const res = validator(raw);
1818
if (res.status === 'unsupported') {
@@ -35,24 +35,18 @@ export const checkHandler = async (out: string, dialect: Dialect) => {
3535
}
3636
}
3737

38-
// Non-commutative detection for branching
3938
// try {
40-
// const nc = await detectNonCommutative(snapshotsData, dialect);
41-
// if (nc.conflicts.length > 0) {
39+
// const response = await detectNonCommutative(snapshots, dialect);
40+
// if (response!.conflicts.length > 0) {
4241
// console.log('\nNon-commutative migration branches detected:');
43-
// for (const c of nc.conflicts) {
42+
// for (const c of response!.conflicts) {
4443
// console.log(`- Parent ${c.parentId}${c.parentPath ? ` (${c.parentPath})` : ''}`);
4544
// console.log(` A: ${c.branchA.headId} (${c.branchA.path})`);
4645
// console.log(` B: ${c.branchB.headId} (${c.branchB.path})`);
4746
// // for (const r of c.reasons) console.log(` • ${r}`);
4847
// }
4948
// }
5049
// } catch (e) {
51-
// }
52-
53-
// const abort = report.malformed.length!! || collisionEntries.length > 0;
54-
55-
// if (abort) {
56-
// process.exit(1);
50+
// console.error(e);
5751
// }
5852
};
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
import { existsSync, readFileSync, rmSync, unlinkSync, writeFileSync } from 'fs';
2+
import { join } from 'path';
3+
import type { Column, MySqlSchemaV4, MySqlSchemaV5, Table } from '../../legacy/mysql-v5/mysqlSchema';
4+
import type { Journal } from '../../utils';
5+
6+
export const upMysqlHandler = (out: string) => {
7+
// if there is meta folder - and there is a journal - it's version <8
8+
const metaPath = join(out, 'meta');
9+
const journalPath = join(metaPath, '_journal.json');
10+
if (existsSync(metaPath) && existsSync(journalPath)) {
11+
const journal: Journal = JSON.parse(readFileSync(journalPath).toString());
12+
if (Number(journal.version) < 8) {
13+
for (const entry of journal.entries) {
14+
const snapshotPrefix = entry.tag.split('_')[0];
15+
const oldSnapshot = readFileSync(join(metaPath, `${snapshotPrefix}_snapshot.json`));
16+
const oldSql = readFileSync(join(out, `${entry.tag}.sql`));
17+
18+
writeFileSync(join(out, `${entry.tag}/snapshot.json`), oldSnapshot);
19+
writeFileSync(join(out, `${entry.tag}/migration.sql`), oldSql);
20+
21+
unlinkSync(join(out, `${entry.tag}.sql`));
22+
}
23+
24+
rmSync(metaPath);
25+
}
26+
}
27+
};
28+
29+
export const upMySqlHandlerV4toV5 = (obj: MySqlSchemaV4): MySqlSchemaV5 => {
30+
const mappedTables: Record<string, Table> = {};
31+
32+
for (const [key, table] of Object.entries(obj.tables)) {
33+
const mappedColumns: Record<string, Column> = {};
34+
for (const [ckey, column] of Object.entries(table.columns)) {
35+
let newDefault: any = column.default;
36+
let newType: string = column.type;
37+
let newAutoIncrement: boolean | undefined = column.autoincrement;
38+
39+
if (column.type.toLowerCase().startsWith('datetime')) {
40+
if (typeof column.default !== 'undefined') {
41+
if (column.default.startsWith("'") && column.default.endsWith("'")) {
42+
newDefault = `'${
43+
column.default
44+
.substring(1, column.default.length - 1)
45+
.replace('T', ' ')
46+
.slice(0, 23)
47+
}'`;
48+
} else {
49+
newDefault = column.default.replace('T', ' ').slice(0, 23);
50+
}
51+
}
52+
53+
newType = column.type.toLowerCase().replace('datetime (', 'datetime(');
54+
} else if (column.type.toLowerCase() === 'date') {
55+
if (typeof column.default !== 'undefined') {
56+
if (column.default.startsWith("'") && column.default.endsWith("'")) {
57+
newDefault = `'${
58+
column.default
59+
.substring(1, column.default.length - 1)
60+
.split('T')[0]
61+
}'`;
62+
} else {
63+
newDefault = column.default.split('T')[0];
64+
}
65+
}
66+
newType = column.type.toLowerCase().replace('date (', 'date(');
67+
} else if (column.type.toLowerCase().startsWith('timestamp')) {
68+
if (typeof column.default !== 'undefined') {
69+
if (column.default.startsWith("'") && column.default.endsWith("'")) {
70+
newDefault = `'${
71+
column.default
72+
.substring(1, column.default.length - 1)
73+
.replace('T', ' ')
74+
.slice(0, 23)
75+
}'`;
76+
} else {
77+
newDefault = column.default.replace('T', ' ').slice(0, 23);
78+
}
79+
}
80+
newType = column.type
81+
.toLowerCase()
82+
.replace('timestamp (', 'timestamp(');
83+
} else if (column.type.toLowerCase().startsWith('time')) {
84+
newType = column.type.toLowerCase().replace('time (', 'time(');
85+
} else if (column.type.toLowerCase().startsWith('decimal')) {
86+
newType = column.type.toLowerCase().replace(', ', ',');
87+
} else if (column.type.toLowerCase().startsWith('enum')) {
88+
newType = column.type.toLowerCase();
89+
} else if (column.type.toLowerCase().startsWith('serial')) {
90+
newAutoIncrement = true;
91+
}
92+
mappedColumns[ckey] = {
93+
...column,
94+
default: newDefault,
95+
type: newType,
96+
autoincrement: newAutoIncrement,
97+
};
98+
}
99+
100+
mappedTables[key] = {
101+
...table,
102+
columns: mappedColumns,
103+
compositePrimaryKeys: {},
104+
uniqueConstraints: {},
105+
checkConstraint: {},
106+
};
107+
}
108+
109+
return {
110+
version: '5',
111+
dialect: obj.dialect,
112+
id: obj.id,
113+
prevId: obj.prevId,
114+
tables: mappedTables,
115+
schemas: obj.schemas,
116+
_meta: {
117+
schemas: {} as Record<string, string>,
118+
tables: {} as Record<string, string>,
119+
columns: {} as Record<string, string>,
120+
},
121+
};
122+
};

drizzle-kit/src/cli/commands/pull-cockroach.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import type {
1919
Sequence,
2020
View,
2121
} from '../../dialects/cockroach/ddl';
22-
import { createDDL, interimToDDL } from '../../dialects/cockroach/ddl';
22+
import { cockroachToRelationsPull, createDDL, interimToDDL } from '../../dialects/cockroach/ddl';
2323
import { ddlDiff } from '../../dialects/cockroach/diff';
2424
import { fromDatabaseForDrizzle } from '../../dialects/cockroach/introspect';
2525
import { ddlToTypeScript as cockroachSequenceSchemaToTypeScript } from '../../dialects/cockroach/typescript';
@@ -66,7 +66,7 @@ export const handle = async (
6666
}
6767

6868
const ts = cockroachSequenceSchemaToTypeScript(ddl2, res.viewColumns, casing);
69-
const relationsTs = relationsToTypeScript(ddl2.fks.list(), casing);
69+
const relationsTs = relationsToTypeScript(cockroachToRelationsPull(ddl2), casing);
7070

7171
const schemaFile = join(out, 'schema.ts');
7272
writeFileSync(schemaFile, ts.file);

0 commit comments

Comments
 (0)