Skip to content

Commit c286807

Browse files
[mssql]: index bug fix in kit
1 parent fdad45b commit c286807

File tree

7 files changed

+57
-15
lines changed

7 files changed

+57
-15
lines changed

drizzle-kit/src/dialects/mssql/convertor.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,11 @@ const createIndex = convertor('create_index', (st) => {
218218
const { name, table, columns, isUnique, where, schema } = st.index;
219219
const indexPart = isUnique ? 'UNIQUE INDEX' : 'INDEX';
220220

221-
const uniqueString = `[${columns.join('],[')}]`;
221+
const uniqueString = `${
222+
columns.map((it) => {
223+
return it.isExpression ? it.value : `[${it.value}]`;
224+
})
225+
}`;
222226

223227
const whereClause = where ? ` WHERE ${where}` : '';
224228

drizzle-kit/src/dialects/mssql/ddl.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,13 @@ export const createDDL = () => {
3939
indexes: {
4040
schema: 'required',
4141
table: 'required',
42-
columns: 'string[]', // does not supported indexing expressions
42+
// TODO add asc/desc: asc and desc feature exists in mssql
43+
columns: [
44+
{
45+
value: 'string',
46+
isExpression: 'boolean',
47+
},
48+
],
4349
isUnique: 'boolean',
4450
where: 'string?',
4551
},

drizzle-kit/src/dialects/mssql/drizzle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,9 +272,9 @@ export const fromDrizzleSchema = (
272272
columns: columns.map((it) => {
273273
if (is(it, SQL)) {
274274
const sql = dialect.sqlToQuery(it, 'indexes').sql;
275-
return sql;
275+
return { value: sql, isExpression: true };
276276
} else {
277-
return getColumnCasing(it, casing);
277+
return { value: getColumnCasing(it, casing), isExpression: false };
278278
}
279279
}),
280280
isUnique: index.config.unique ?? false,

drizzle-kit/src/dialects/mssql/introspect.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,7 @@ WHERE obj.type in ('U', 'V')
535535
const column = columnsList.find((column) =>
536536
column.table_object_id === index.table_id && column.column_id === it
537537
)!;
538-
return column.name;
538+
return { value: column.name, isExpression: false };
539539
});
540540

541541
indexes.push({

drizzle-kit/src/dialects/mssql/typescript.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,11 @@ const createTableIndexes = (tableName: string, idxs: Index[], casing: Casing): s
442442
statement += `.on(${
443443
it.columns
444444
.map((it) => {
445-
return `table.${withCasing(it, casing)}`;
445+
if (it.isExpression) {
446+
return `sql\`${it.value}\``;
447+
} else {
448+
return `table.${withCasing(it.value, casing)}`;
449+
}
446450
})
447451
.join(', ')
448452
})`;

drizzle-kit/tests/mssql/indexes.test.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -116,16 +116,16 @@ test('indexes #2', async () => {
116116
const { sqlStatements: pst1 } = await push({ db, to: schema1 });
117117

118118
const expectedSt1 = [
119-
'CREATE TABLE `table1` (\n'
120-
+ '\t`col1` int,\n'
121-
+ '\t`col2` int,\n'
122-
+ '\tCONSTRAINT `index1` UNIQUE INDEX(`col1`),\n'
123-
+ '\tCONSTRAINT `index2` UNIQUE INDEX(`col1`,`col2`)\n'
119+
'CREATE TABLE [table1] (\n'
120+
+ '\t[col1] int,\n'
121+
+ '\t[col2] int\n'
124122
+ ');\n',
125-
'CREATE INDEX `index3` ON `table1` (`col1`);',
126-
'CREATE INDEX `index4` ON `table1` (`col1`,`col2`);',
127-
'CREATE INDEX `index5` ON `table1` (`col1` asc);',
128-
'CREATE INDEX `index6` ON `table1` (`col1` asc,`col2` desc);',
123+
`CREATE UNIQUE INDEX [index1] ON [table1] ([col1]);`,
124+
`CREATE UNIQUE INDEX [index2] ON [table1] ([col1],[col2]);`,
125+
'CREATE INDEX [index3] ON [table1] ([col1]);',
126+
'CREATE INDEX [index4] ON [table1] ([col1],[col2]);',
127+
'CREATE INDEX [index5] ON [table1] ([col1] asc);',
128+
'CREATE INDEX [index6] ON [table1] ([col1] asc,[col2] desc);',
129129
];
130130
expect(st1).toStrictEqual(expectedSt1);
131131
expect(pst1).toStrictEqual(expectedSt1);

drizzle-kit/tests/mssql/pull.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,3 +491,31 @@ test('introspect empty db', async () => {
491491

492492
expect(introspectDDL.entities.list().length).toBe(0);
493493
});
494+
495+
test('indexes #2', async () => {
496+
const table1 = mssqlTable('table1', {
497+
col1: int(),
498+
col2: int(),
499+
}, () => [
500+
index1,
501+
index2,
502+
index3,
503+
index4,
504+
index5,
505+
index6,
506+
]);
507+
508+
const index1 = uniqueIndex('index1').on(table1.col1);
509+
const index2 = uniqueIndex('index2').on(table1.col1, table1.col2);
510+
const index3 = index('index3').on(table1.col1);
511+
const index4 = index('index4').on(table1.col1, table1.col2);
512+
const index5 = index('index5').on(sql`${table1.col1} asc`);
513+
const index6 = index('index6').on(sql`${table1.col1} asc`, sql`${table1.col2} desc`);
514+
515+
const schema = { table1 };
516+
517+
const { statements, sqlStatements } = await diffIntrospect(db, schema, 'sql-in-index');
518+
519+
expect(statements).toStrictEqual([]);
520+
expect(sqlStatements).toStrictEqual([]);
521+
});

0 commit comments

Comments
 (0)