Skip to content

Commit 5d53945

Browse files
Merge branch 'beta' of github.com:drizzle-team/drizzle-orm into feature/mysql-force-index
2 parents 575d231 + 29eb63c commit 5d53945

File tree

5 files changed

+40
-73
lines changed

5 files changed

+40
-73
lines changed

drizzle-kit/src/introspect-mysql.ts

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,6 @@ import {
1616
import { indexName } from './serializer/mysqlSerializer';
1717
import { unescapeSingleQuotes } from './utils';
1818

19-
// time precision to fsp
20-
// {mode: "string"} for timestamp by default
21-
2219
const mysqlImportsList = new Set([
2320
'mysqlTable',
2421
'mysqlEnum',
@@ -263,8 +260,7 @@ export const schemaToTypeScript = (
263260
|| Object.keys(table.checkConstraint).length > 0
264261
) {
265262
statement += ',\n';
266-
statement += '(table) => {\n';
267-
statement += '\treturn {\n';
263+
statement += '(table) => [';
268264
statement += createTableIndexes(
269265
table.name,
270266
Object.values(table.indexes),
@@ -283,8 +279,7 @@ export const schemaToTypeScript = (
283279
Object.values(table.checkConstraint),
284280
withCasing,
285281
);
286-
statement += '\t}\n';
287-
statement += '}';
282+
statement += '\n]';
288283
}
289284

290285
statement += ');';
@@ -932,15 +927,14 @@ const createTableIndexes = (
932927
const indexGeneratedName = indexName(tableName, it.columns);
933928
const escapedIndexName = indexGeneratedName === it.name ? '' : `"${it.name}"`;
934929

935-
statement += `\t\t${idxKey}: `;
930+
statement += `\n\t`;
936931
statement += it.isUnique ? 'uniqueIndex(' : 'index(';
937932
statement += `${escapedIndexName})`;
938933
statement += `.on(${
939934
it.columns
940935
.map((it) => `table.${casing(it)}`)
941936
.join(', ')
942937
}),`;
943-
statement += `\n`;
944938
});
945939

946940
return statement;
@@ -955,15 +949,14 @@ const createTableUniques = (
955949
unqs.forEach((it) => {
956950
const idxKey = casing(it.name);
957951

958-
statement += `\t\t${idxKey}: `;
952+
statement += `\n\t`;
959953
statement += 'unique(';
960954
statement += `"${it.name}")`;
961955
statement += `.on(${
962956
it.columns
963957
.map((it) => `table.${casing(it)}`)
964958
.join(', ')
965959
}),`;
966-
statement += `\n`;
967960
});
968961

969962
return statement;
@@ -976,13 +969,11 @@ const createTableChecks = (
976969
let statement = '';
977970

978971
checks.forEach((it) => {
979-
const checkKey = casing(it.name);
980-
981-
statement += `\t\t${checkKey}: `;
972+
statement += `\n\t`;
982973
statement += 'check(';
983974
statement += `"${it.name}", `;
984975
statement += `sql\`${it.value.replace(/`/g, '\\`')}\`)`;
985-
statement += `,\n`;
976+
statement += `,`;
986977
});
987978

988979
return statement;
@@ -997,7 +988,7 @@ const createTablePKs = (
997988
pks.forEach((it) => {
998989
let idxKey = casing(it.name);
999990

1000-
statement += `\t\t${idxKey}: `;
991+
statement += `\n\t`;
1001992
statement += 'primaryKey({ columns: [';
1002993
statement += `${
1003994
it.columns
@@ -1007,7 +998,6 @@ const createTablePKs = (
1007998
.join(', ')
1008999
}]${it.name ? `, name: "${it.name}"` : ''}}`;
10091000
statement += '),';
1010-
statement += `\n`;
10111001
});
10121002

10131003
return statement;
@@ -1022,7 +1012,8 @@ const createTableFKs = (
10221012
fks.forEach((it) => {
10231013
const isSelf = it.tableTo === it.tableFrom;
10241014
const tableTo = isSelf ? 'table' : `${casing(it.tableTo)}`;
1025-
statement += `\t\t${casing(it.name)}: foreignKey({\n`;
1015+
statement += `\n\t`;
1016+
statement += `foreignKey({\n`;
10261017
statement += `\t\t\tcolumns: [${
10271018
it.columnsFrom
10281019
.map((i) => `table.${casing(i)}`)
@@ -1044,7 +1035,7 @@ const createTableFKs = (
10441035
? `.onDelete("${it.onDelete}")`
10451036
: '';
10461037

1047-
statement += `,\n`;
1038+
statement += `,`;
10481039
});
10491040

10501041
return statement;

drizzle-kit/src/introspect-pg.ts

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
537537
|| Object.keys(table.checkConstraints).length > 0
538538
) {
539539
statement += ', ';
540-
statement += '(table) => {\n';
541-
statement += '\treturn {\n';
540+
statement += '(table) => [';
542541
statement += createTableIndexes(table.name, Object.values(table.indexes), casing);
543542
statement += createTableFKs(Object.values(table.foreignKeys), schemas, casing);
544543
statement += createTablePKs(
@@ -558,8 +557,7 @@ export const schemaToTypeScript = (schema: PgSchemaInternal, casing: Casing) =>
558557
Object.values(table.checkConstraints),
559558
casing,
560559
);
561-
statement += '\t}\n';
562-
statement += '}';
560+
statement += '\n]';
563561
}
564562

565563
statement += ');';
@@ -1216,7 +1214,7 @@ const createTableIndexes = (tableName: string, idxs: Index[], casing: Casing): s
12161214
);
12171215
const escapedIndexName = indexGeneratedName === it.name ? '' : `"${it.name}"`;
12181216

1219-
statement += `\t\t${idxKey}: `;
1217+
statement += `\n\t`;
12201218
statement += it.isUnique ? 'uniqueIndex(' : 'index(';
12211219
statement += `${escapedIndexName})`;
12221220
statement += `${it.concurrently ? `.concurrently()` : ''}`;
@@ -1252,7 +1250,7 @@ const createTableIndexes = (tableName: string, idxs: Index[], casing: Casing): s
12521250
}
12531251

12541252
statement += it.with && Object.keys(it.with).length > 0 ? `.with(${reverseLogic(it.with)})` : '';
1255-
statement += `,\n`;
1253+
statement += `,`;
12561254
});
12571255

12581256
return statement;
@@ -1262,9 +1260,7 @@ const createTablePKs = (pks: PrimaryKey[], casing: Casing): string => {
12621260
let statement = '';
12631261

12641262
pks.forEach((it) => {
1265-
let idxKey = withCasing(it.name, casing);
1266-
1267-
statement += `\t\t${idxKey}: `;
1263+
statement += `\n\t`;
12681264
statement += 'primaryKey({ columns: [';
12691265
statement += `${
12701266
it.columns
@@ -1274,7 +1270,7 @@ const createTablePKs = (pks: PrimaryKey[], casing: Casing): string => {
12741270
.join(', ')
12751271
}]${it.name ? `, name: "${it.name}"` : ''}}`;
12761272
statement += ')';
1277-
statement += `,\n`;
1273+
statement += `,`;
12781274
});
12791275

12801276
return statement;
@@ -1297,13 +1293,13 @@ const createTablePolicies = (
12971293
return rolesNameToTsKey[v] ? withCasing(rolesNameToTsKey[v], casing) : `"${v}"`;
12981294
});
12991295

1300-
statement += `\t\t${idxKey}: `;
1296+
statement += `\n\t`;
13011297
statement += 'pgPolicy(';
13021298
statement += `"${it.name}", { `;
13031299
statement += `as: "${it.as?.toLowerCase()}", for: "${it.for?.toLowerCase()}", to: [${mappedItTo?.join(', ')}]${
13041300
it.using ? `, using: sql\`${it.using}\`` : ''
13051301
}${it.withCheck ? `, withCheck: sql\`${it.withCheck}\` ` : ''}`;
1306-
statement += ` }),\n`;
1302+
statement += ` }),`;
13071303
});
13081304

13091305
return statement;
@@ -1316,14 +1312,12 @@ const createTableUniques = (
13161312
let statement = '';
13171313

13181314
unqs.forEach((it) => {
1319-
const idxKey = withCasing(it.name, casing);
1320-
1321-
statement += `\t\t${idxKey}: `;
1315+
statement += `\n\t`;
13221316
statement += 'unique(';
13231317
statement += `"${it.name}")`;
13241318
statement += `.on(${it.columns.map((it) => `table.${withCasing(it, casing)}`).join(', ')})`;
13251319
statement += it.nullsNotDistinct ? `.nullsNotDistinct()` : '';
1326-
statement += `,\n`;
1320+
statement += `,`;
13271321
});
13281322

13291323
return statement;
@@ -1336,12 +1330,11 @@ const createTableChecks = (
13361330
let statement = '';
13371331

13381332
checkConstraints.forEach((it) => {
1339-
const checkKey = withCasing(it.name, casing);
1340-
statement += `\t\t${checkKey}: `;
1333+
statement += `\n\t`;
13411334
statement += 'check(';
13421335
statement += `"${it.name}", `;
13431336
statement += `sql\`${it.value}\`)`;
1344-
statement += `,\n`;
1337+
statement += `,`;
13451338
});
13461339

13471340
return statement;
@@ -1356,7 +1349,8 @@ const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casi
13561349

13571350
const isSelf = it.tableTo === it.tableFrom;
13581351
const tableTo = isSelf ? 'table' : `${withCasing(paramName, casing)}`;
1359-
statement += `\t\t${withCasing(it.name, casing)}: foreignKey({\n`;
1352+
statement += `\n\t`;
1353+
statement += `foreignKey({\n`;
13601354
statement += `\t\t\tcolumns: [${it.columnsFrom.map((i) => `table.${withCasing(i, casing)}`).join(', ')}],\n`;
13611355
statement += `\t\t\tforeignColumns: [${
13621356
it.columnsTo.map((i) => `${tableTo}.${withCasing(i, casing)}`).join(', ')
@@ -1368,7 +1362,7 @@ const createTableFKs = (fks: ForeignKey[], schemas: Record<string, string>, casi
13681362

13691363
statement += it.onDelete && it.onDelete !== 'no action' ? `.onDelete("${it.onDelete}")` : '';
13701364

1371-
statement += `,\n`;
1365+
statement += `,`;
13721366
});
13731367

13741368
return statement;

drizzle-kit/src/introspect-singlestore.ts

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,7 @@ export const schemaToTypeScript = (
249249
|| Object.keys(table.uniqueConstraints).length > 0
250250
) {
251251
statement += ',\n';
252-
statement += '(table) => {\n';
253-
statement += '\treturn {\n';
252+
statement += '(table) => [';
254253
statement += createTableIndexes(
255254
table.name,
256255
Object.values(table.indexes),
@@ -264,8 +263,7 @@ export const schemaToTypeScript = (
264263
Object.values(table.uniqueConstraints),
265264
withCasing,
266265
);
267-
statement += '\t}\n';
268-
statement += '}';
266+
statement += '\n]';
269267
}
270268

271269
statement += ');';
@@ -855,15 +853,14 @@ const createTableIndexes = (
855853
const indexGeneratedName = indexName(tableName, it.columns);
856854
const escapedIndexName = indexGeneratedName === it.name ? '' : `"${it.name}"`;
857855

858-
statement += `\t\t${idxKey}: `;
856+
statement += `\n\t`;
859857
statement += it.isUnique ? 'uniqueIndex(' : 'index(';
860858
statement += `${escapedIndexName})`;
861859
statement += `.on(${
862860
it.columns
863861
.map((it) => `table.${casing(it)}`)
864862
.join(', ')
865863
}),`;
866-
statement += `\n`;
867864
});
868865

869866
return statement;
@@ -876,17 +873,14 @@ const createTableUniques = (
876873
let statement = '';
877874

878875
unqs.forEach((it) => {
879-
const idxKey = casing(it.name);
880-
881-
statement += `\t\t${idxKey}: `;
876+
statement += `\n\t`;
882877
statement += 'unique(';
883878
statement += `"${it.name}")`;
884879
statement += `.on(${
885880
it.columns
886881
.map((it) => `table.${casing(it)}`)
887882
.join(', ')
888883
}),`;
889-
statement += `\n`;
890884
});
891885

892886
return statement;
@@ -901,7 +895,7 @@ const createTablePKs = (
901895
pks.forEach((it) => {
902896
let idxKey = casing(it.name);
903897

904-
statement += `\t\t${idxKey}: `;
898+
statement += `\n\t`;
905899
statement += 'primaryKey({ columns: [';
906900
statement += `${
907901
it.columns
@@ -911,7 +905,6 @@ const createTablePKs = (
911905
.join(', ')
912906
}]${it.name ? `, name: "${it.name}"` : ''}}`;
913907
statement += '),';
914-
statement += `\n`;
915908
});
916909

917910
return statement;

0 commit comments

Comments
 (0)