@mikro-orm/knex 6.0.0-rc.0 → 6.0.0-rc.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -22,7 +22,7 @@ class SchemaComparator {
22
22
  * operations to change the schema stored in fromSchema to the schema that is
23
23
  * stored in toSchema.
24
24
  */
25
- compare(fromSchema, toSchema) {
25
+ compare(fromSchema, toSchema, inverseDiff) {
26
26
  const diff = { newTables: {}, removedTables: {}, changedTables: {}, orphanedForeignKeys: [], newNamespaces: new Set(), removedNamespaces: new Set(), fromSchema };
27
27
  const foreignKeysToTable = {};
28
28
  for (const namespace of toSchema.getNamespaces()) {
@@ -43,7 +43,7 @@ class SchemaComparator {
43
43
  diff.newTables[tableName] = toSchema.getTable(tableName);
44
44
  }
45
45
  else {
46
- const tableDifferences = this.diffTable(fromSchema.getTable(tableName), toSchema.getTable(tableName));
46
+ const tableDifferences = this.diffTable(fromSchema.getTable(tableName), toSchema.getTable(tableName), inverseDiff?.changedTables[tableName]);
47
47
  if (tableDifferences !== false) {
48
48
  diff.changedTables[tableName] = tableDifferences;
49
49
  }
@@ -91,7 +91,7 @@ class SchemaComparator {
91
91
  * Returns the difference between the tables fromTable and toTable.
92
92
  * If there are no differences this method returns the boolean false.
93
93
  */
94
- diffTable(fromTable, toTable) {
94
+ diffTable(fromTable, toTable, inverseTableDiff) {
95
95
  let changes = 0;
96
96
  const tableDifferences = {
97
97
  name: fromTable.getShortestName(),
@@ -157,7 +157,7 @@ class SchemaComparator {
157
157
  this.log(`column ${tableDifferences.name}.${column.name} changed`, { changedProperties });
158
158
  changes++;
159
159
  }
160
- this.detectColumnRenamings(tableDifferences);
160
+ this.detectColumnRenamings(tableDifferences, inverseTableDiff);
161
161
  const fromTableIndexes = fromTable.getIndexes();
162
162
  const toTableIndexes = toTable.getIndexes();
163
163
  // See if all the indexes in "from" table exist in "to" table
@@ -249,7 +249,7 @@ class SchemaComparator {
249
249
  * Try to find columns that only changed their name, rename operations maybe cheaper than add/drop
250
250
  * however ambiguities between different possibilities should not lead to renaming at all.
251
251
  */
252
- detectColumnRenamings(tableDifferences) {
252
+ detectColumnRenamings(tableDifferences, inverseTableDiff) {
253
253
  const renameCandidates = {};
254
254
  for (const addedColumn of Object.values(tableDifferences.addedColumns)) {
255
255
  for (const removedColumn of Object.values(tableDifferences.removedColumns)) {
@@ -257,6 +257,10 @@ class SchemaComparator {
257
257
  if (diff.size !== 0) {
258
258
  continue;
259
259
  }
260
+ const renamedColumn = inverseTableDiff?.renamedColumns[addedColumn.name];
261
+ if (renamedColumn && renamedColumn?.name !== removedColumn.name) {
262
+ continue;
263
+ }
260
264
  renameCandidates[addedColumn.name] = renameCandidates[addedColumn.name] ?? [];
261
265
  renameCandidates[addedColumn.name].push([removedColumn, addedColumn]);
262
266
  }
@@ -455,8 +459,8 @@ class SchemaComparator {
455
459
  return defaultValueFrom === defaultValueTo;
456
460
  }
457
461
  if (to.mappedType instanceof core_1.JsonType) {
458
- const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^'(.*)'$/, '$1'));
459
- const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^'(.*)'$/, '$1'));
462
+ const defaultValueFrom = (0, core_1.parseJsonSafe)(from.default.replace(/^(_\w+\\)?'(.*?)\\?'$/, '$2'));
463
+ const defaultValueTo = (0, core_1.parseJsonSafe)(to.default?.replace(/^\(?'(.*?)'\)?$/, '$1'));
460
464
  return core_1.Utils.equals(defaultValueFrom, defaultValueTo);
461
465
  }
462
466
  if (to.mappedType instanceof core_1.DateTimeType && from.default && to.default) {
@@ -23,7 +23,7 @@ export declare abstract class SchemaHelper {
23
23
  loadInformationSchema(schema: DatabaseSchema, connection: AbstractSqlConnection, tables: Table[], schemas?: string[]): Promise<void>;
24
24
  getListTablesSQL(schemaName?: string): string;
25
25
  getRenameColumnSQL(tableName: string, oldColumnName: string, to: Column, schemaName?: string): string;
26
- getCreateIndexSQL(tableName: string, index: IndexDef): string;
26
+ getCreateIndexSQL(tableName: string, index: IndexDef, partialExpression?: boolean): string;
27
27
  getDropIndexSQL(tableName: string, index: IndexDef): string;
28
28
  getRenameIndexSQL(tableName: string, index: IndexDef, oldIndexName: string): string;
29
29
  hasNonDefaultPrimaryKeyName(table: DatabaseTable): boolean;
@@ -77,14 +77,17 @@ class SchemaHelper {
77
77
  const tableReference = schemaReference + tableName;
78
78
  return `alter table ${tableReference} rename column ${oldColumnName} to ${columnName}`;
79
79
  }
80
- getCreateIndexSQL(tableName, index) {
81
- /* istanbul ignore if */
82
- if (index.expression) {
80
+ getCreateIndexSQL(tableName, index, partialExpression = false) {
81
+ if (index.expression && !partialExpression) {
83
82
  return index.expression;
84
83
  }
85
84
  tableName = this.platform.quoteIdentifier(tableName);
86
85
  const keyName = this.platform.quoteIdentifier(index.keyName);
87
- return `create index ${keyName} on ${tableName} (${index.columnNames.map(c => this.platform.quoteIdentifier(c)).join(', ')})`;
86
+ const sql = `create ${index.unique ? 'unique ' : ''}index ${keyName} on ${tableName} `;
87
+ if (index.expression && partialExpression) {
88
+ return `${sql}(${index.expression})`;
89
+ }
90
+ return `${sql}(${index.columnNames.map(c => this.platform.quoteIdentifier(c)).join(', ')})`;
88
91
  }
89
92
  getDropIndexSQL(tableName, index) {
90
93
  return `drop index ${this.platform.quoteIdentifier(index.keyName)}`;
@@ -143,7 +143,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
143
143
  const { fromSchema, toSchema } = await this.prepareSchemaForComparison(options);
144
144
  const comparator = new SchemaComparator_1.SchemaComparator(this.platform);
145
145
  const diffUp = comparator.compare(fromSchema, toSchema);
146
- const diffDown = comparator.compare(toSchema, fromSchema);
146
+ const diffDown = comparator.compare(toSchema, fromSchema, diffUp);
147
147
  return {
148
148
  up: await this.diffToSQL(diffUp, options),
149
149
  down: this.platform.supportsDownMigrations() ? await this.diffToSQL(diffDown, options) : '',
@@ -215,7 +215,7 @@ class SqlSchemaGenerator extends core_1.AbstractSchemaGenerator {
215
215
  if (schema && schemaName === '*') {
216
216
  return `${schema}.${referencedTableName.replace(/^\*\./, '')}`;
217
217
  }
218
- if (schemaName === this.platform.getDefaultSchemaName()) {
218
+ if (!schemaName || schemaName === this.platform.getDefaultSchemaName()) {
219
219
  return tableName;
220
220
  }
221
221
  return `${schemaName}.${tableName}`;
package/typings.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Knex } from 'knex';
2
- import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type } from '@mikro-orm/core';
2
+ import type { CheckCallback, Dictionary, EntityProperty, GroupOperator, RawQueryFragment, QBFilterQuery, QueryOrderMap, Type, QueryFlag } from '@mikro-orm/core';
3
3
  import type { JoinType, QueryType } from './query/enums';
4
4
  import type { DatabaseSchema, DatabaseTable } from './schema';
5
5
  export interface Table {
@@ -126,6 +126,7 @@ export interface IQueryBuilder<T> {
126
126
  truncate(): this;
127
127
  count(field?: string | string[], distinct?: boolean): this;
128
128
  join(field: string, alias: string, cond?: QBFilterQuery, type?: JoinType, path?: string): this;
129
+ innerJoin(field: string, alias: string, cond?: QBFilterQuery): this;
129
130
  leftJoin(field: string, alias: string, cond?: QBFilterQuery): this;
130
131
  joinAndSelect(field: string, alias: string, cond?: QBFilterQuery): this;
131
132
  leftJoinAndSelect(field: string, alias: string, cond?: QBFilterQuery, fields?: string[]): this;
@@ -140,8 +141,19 @@ export interface IQueryBuilder<T> {
140
141
  orderBy(orderBy: QueryOrderMap<T>): this;
141
142
  groupBy(fields: (string | keyof T) | (string | keyof T)[]): this;
142
143
  having(cond?: QBFilterQuery | string, params?: any[]): this;
143
- getAliasForJoinPath(path: string): string | undefined;
144
+ getAliasForJoinPath(path: string, options?: ICriteriaNodeProcessOptions): string | undefined;
145
+ getJoinForPath(path?: string, options?: ICriteriaNodeProcessOptions): JoinOptions | undefined;
144
146
  getNextAlias(entityName?: string): string;
147
+ clone(reset?: boolean): IQueryBuilder<T>;
148
+ setFlag(flag: QueryFlag): this;
149
+ unsetFlag(flag: QueryFlag): this;
150
+ hasFlag(flag: QueryFlag): boolean;
151
+ }
152
+ export interface ICriteriaNodeProcessOptions {
153
+ alias?: string;
154
+ matchPopulateJoins?: boolean;
155
+ ignoreBranching?: boolean;
156
+ preferNoBranch?: boolean;
145
157
  }
146
158
  export interface ICriteriaNode<T extends object> {
147
159
  readonly entityName: string;
@@ -150,7 +162,7 @@ export interface ICriteriaNode<T extends object> {
150
162
  payload: any;
151
163
  prop?: EntityProperty;
152
164
  index?: number;
153
- process(qb: IQueryBuilder<T>, alias?: string): any;
165
+ process(qb: IQueryBuilder<T>, options?: ICriteriaNodeProcessOptions): any;
154
166
  shouldInline(payload: any): boolean;
155
167
  willAutoJoin(qb: IQueryBuilder<T>, alias?: string): boolean;
156
168
  shouldRename(payload: any): boolean;