MCPcopy Create free account
hub / github.com/mikro-orm/mikro-orm / createIndex

Method createIndex

packages/mssql/src/MsSqlSchemaHelper.ts:1018–1045  ·  view source on GitHub ↗
(index: IndexDef, table: DatabaseTable, createPrimary = false)

Source from the content-addressed store, hash-verified

1016 }
1017
1018 override createIndex(index: IndexDef, table: DatabaseTable, createPrimary = false): string {
1019 if (index.primary) {
1020 return '';
1021 }
1022
1023 if (index.expression) {
1024 return index.expression;
1025 }
1026
1027 const needsAutoNotNull = index.unique && index.columnNames.some(column => table.getColumn(column)?.nullable);
1028
1029 if (!needsAutoNotNull) {
1030 return this.getCreateIndexSQL(table.getShortestName(), index);
1031 }
1032
1033 // Strip `index.where` from the base SQL so we can combine it with the auto NOT-NULL guard
1034 // ourselves, wrapping the user predicate in parens to defuse operator precedence
1035 // (a bare `a = 1 or b = 2 and [col] is not null` would bind as `a = 1 or (b = 2 and …)`).
1036 let sql = this.getCreateIndexSQL(table.getShortestName(), { ...index, where: undefined, disabled: false });
1037 const autoNotNull = index.columnNames.map(c => `${this.quote(c)} is not null`).join(' and ');
1038 sql += index.where ? ` where (${index.where}) and ${autoNotNull}` : ` where ${autoNotNull}`;
1039
1040 if (index.disabled) {
1041 sql += `;\nalter index ${this.quote(index.keyName)} on ${table.getQuotedName()} disable`;
1042 }
1043
1044 return sql;
1045 }
1046
1047 override dropForeignKey(tableName: string, constraintName: string): string {
1048 return `alter table ${this.quote(tableName)} drop constraint ${this.quote(constraintName)}`;

Callers 6

createIndexesMethod · 0.45
createTableMethod · 0.45
alterTableMethod · 0.45
alterTableFunction · 0.45
createTableFunction · 0.45

Calls 7

getCreateIndexSQLMethod · 0.95
getColumnMethod · 0.80
getShortestNameMethod · 0.80
getQuotedNameMethod · 0.80
joinMethod · 0.65
mapMethod · 0.45
quoteMethod · 0.45

Tested by

no test coverage detected