@mikro-orm/mssql 7.3.0-dev.10 → 7.3.0-dev.12

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.
@@ -29,13 +29,16 @@ export class MsSqlSchemaGenerator extends SchemaGenerator {
29
29
  }
30
30
  // https://stackoverflow.com/questions/253849/cannot-truncate-table-because-it-is-being-referenced-by-a-foreign-key-constraint
31
31
  for (const meta of this.getOrderedMetadataForClear(options?.schema).reverse()) {
32
- const res = await this.driver.nativeDelete(meta.class, {}, options);
33
- // a TPT child table has no identity of its own, its PK references the parent
34
- if (!meta.tptParent && meta.getPrimaryProps().some(pk => pk.autoincrement)) {
32
+ await this.driver.nativeDelete(meta.class, {}, options);
33
+ // a table has at most one identity column: a TPT child's inherited PK references the parent instead, and a non-PK one is only reseeded when it declares `sequence`
34
+ const props = meta.tptParent ? (meta.ownProps ?? []) : meta.props;
35
+ const identity = props.find(prop => prop.autoincrement && (prop.primary || !!prop.sequence));
36
+ if (identity) {
35
37
  const tableName = this.driver.getTableName(meta, { schema: options?.schema }, false);
36
- await this.execute(`dbcc checkident ('${tableName}', reseed, ${res.affectedRows > 0 ? 0 : 1})`, {
37
- ctx: this.em?.getTransactionContext(),
38
- });
38
+ const { incrementBy = 1, startWith = incrementBy < 0 ? -1 : 1 } = identity.sequence ?? {};
39
+ // once the identity was used (`last_value` is set), the next value is the reseed value plus the table's actual increment
40
+ const lastValue = `(select last_value from sys.identity_columns where object_id = object_id('${tableName}'))`;
41
+ await this.execute(`declare @reseed bigint = case when ${lastValue} is null then ${startWith} else ${startWith} - ident_incr('${tableName}') end; dbcc checkident ('${tableName}', reseed, @reseed)`, { ctx: this.em?.getTransactionContext() });
39
42
  }
40
43
  }
41
44
  this.clearIdentityMap();
@@ -115,9 +115,12 @@ export class MsSqlSchemaHelper extends SchemaHelper {
115
115
  datetime_precision as datetime_precision,
116
116
  character_maximum_length as character_maximum_length,
117
117
  columnproperty(sc.object_id, column_name, 'IsIdentity') is_identity,
118
- nullif(ic.collation_name, convert(nvarchar(128), databasepropertyex(db_name(), 'Collation'))) as collation_name
118
+ nullif(ic.collation_name, convert(nvarchar(128), databasepropertyex(db_name(), 'Collation'))) as collation_name,
119
+ cast(idc.seed_value as bigint) as sequence_start,
120
+ cast(idc.increment_value as bigint) as sequence_increment
119
121
  from information_schema.columns ic
120
122
  inner join sys.columns sc on sc.name = ic.column_name and sc.object_id = object_id(ic.table_schema + '.' + ic.table_name)
123
+ left join sys.identity_columns idc on idc.object_id = sc.object_id and idc.column_id = sc.column_id
121
124
  left join sys.computed_columns cmp on cmp.name = ic.column_name and cmp.object_id = object_id(ic.table_schema + '.' + ic.table_name)
122
125
  left join sys.extended_properties t4 on t4.major_id = object_id(ic.table_schema + '.' + ic.table_name) and t4.name = 'MS_Description' and t4.minor_id = sc.column_id
123
126
  left join sys.default_constraints t5 on sc.default_object_id = t5.object_id
@@ -164,6 +167,7 @@ export class MsSqlSchemaHelper extends SchemaHelper {
164
167
  defaultConstraint: col.column_default_name,
165
168
  nullable: col.is_nullable === 'YES',
166
169
  autoincrement: increments,
170
+ sequence: increments ? this.getIntrospectedSequence(col.sequence_start, col.sequence_increment) : undefined,
167
171
  precision: col.numeric_precision,
168
172
  scale: col.numeric_scale,
169
173
  comment: col.column_comment,
@@ -660,9 +664,11 @@ export class MsSqlSchemaHelper extends SchemaHelper {
660
664
  col.push(columnType);
661
665
  }
662
666
  Utils.runIfNotEmpty(() => col.push(this.getCollateSQL(column.collation)), column.collation);
667
+ // a descending identity starts at -1 by default, same as on the other drivers
668
+ const { incrementBy = 1, startWith = incrementBy < 0 ? -1 : 1 } = column.sequence ?? {};
663
669
  // `IDENTITY(1,1)` is rejected inside `ALTER COLUMN`, so it must only be emitted when the
664
670
  // change actually involves the identity attribute or is a fresh column (no `changedProperties`).
665
- Utils.runIfNotEmpty(() => col.push('identity(1,1)'), column.autoincrement &&
671
+ Utils.runIfNotEmpty(() => col.push(`identity(${startWith},${incrementBy})`), column.autoincrement &&
666
672
  (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type')));
667
673
  Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
668
674
  Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mikro-orm/mssql",
3
- "version": "7.3.0-dev.10",
3
+ "version": "7.3.0-dev.12",
4
4
  "description": "TypeScript ORM for Node.js based on Data Mapper, Unit of Work and Identity Map patterns. Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript.",
5
5
  "keywords": [
6
6
  "data-mapper",
@@ -47,17 +47,17 @@
47
47
  "copy": "node ../../scripts/copy.mjs"
48
48
  },
49
49
  "dependencies": {
50
- "@mikro-orm/sql": "7.3.0-dev.10",
50
+ "@mikro-orm/sql": "7.3.0-dev.12",
51
51
  "kysely": "0.29.6",
52
52
  "tarn": "3.1.2",
53
53
  "tedious": "20.0.0",
54
54
  "tsqlstring": "1.0.1"
55
55
  },
56
56
  "devDependencies": {
57
- "@mikro-orm/core": "^7.2.2"
57
+ "@mikro-orm/core": "^7.2.3"
58
58
  },
59
59
  "peerDependencies": {
60
- "@mikro-orm/core": "7.3.0-dev.10"
60
+ "@mikro-orm/core": "7.3.0-dev.12"
61
61
  },
62
62
  "engines": {
63
63
  "node": ">= 22.17.0"