@mikro-orm/mssql 7.0.14 → 7.0.15-dev.0

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.
@@ -1,98 +1,96 @@
1
- import { EnumType, SchemaHelper, StringType, TextType, Utils } from '@mikro-orm/sql';
1
+ import { EnumType, SchemaHelper, StringType, TextType, Utils, } from '@mikro-orm/sql';
2
2
  import { UnicodeStringType } from './UnicodeStringType.js';
3
3
  /** Schema introspection helper for Microsoft SQL Server. */
4
4
  export class MsSqlSchemaHelper extends SchemaHelper {
5
- static DEFAULT_VALUES = {
6
- true: ['1'],
7
- false: ['0'],
8
- 'getdate()': ['current_timestamp'],
9
- };
10
- getManagementDbName() {
11
- return 'master';
12
- }
13
- getDropDatabaseSQL(name) {
14
- // `set offline` rejects all connections including the issuing session, so there is no
15
- // single-user race window where a torn-down pool connection can reconnect between the
16
- // mode switch and the drop (SQL Server error 3702 "currently in use"). Dropping an
17
- // offline database leaves the underlying `.mdf`/`.ldf` files behind though, which
18
- // makes a subsequent `create database` with the same name fail with error 5170
19
- // ("file already exists"). Capture the physical paths up front and call
20
- // `master.sys.xp_delete_files` after the drop to clean them up.
21
- const quoted = this.quote(name);
22
- const literal = this.platform.quoteValue(name);
23
- return (
24
- `if db_id(${literal}) is not null begin ` +
25
- `declare @drop_files table (path nvarchar(260)); ` +
26
- `insert into @drop_files (path) select physical_name from sys.master_files where database_id = db_id(${literal}); ` +
27
- `alter database ${quoted} set offline with rollback immediate; ` +
28
- `drop database ${quoted}; ` +
29
- `declare @drop_path nvarchar(260); ` +
30
- `declare drop_files_cursor cursor local fast_forward for select path from @drop_files; ` +
31
- `open drop_files_cursor; fetch next from drop_files_cursor into @drop_path; ` +
32
- `while @@fetch_status = 0 begin ` +
33
- `begin try exec master.sys.xp_delete_files @drop_path; end try begin catch end catch; ` +
34
- `fetch next from drop_files_cursor into @drop_path; ` +
35
- `end ` +
36
- `close drop_files_cursor; deallocate drop_files_cursor; ` +
37
- `end`
38
- );
39
- }
40
- disableForeignKeysSQL() {
41
- return `exec sp_MSforeachtable 'alter table ? nocheck constraint all';`;
42
- }
43
- enableForeignKeysSQL() {
44
- return `exec sp_MSforeachtable 'alter table ? check constraint all';`;
45
- }
46
- getDatabaseExistsSQL(name) {
47
- return `select 1 from sys.databases where name = N'${name}'`;
48
- }
49
- getListTablesSQL() {
50
- return `select t.name as table_name, schema_name(t2.schema_id) schema_name, ep.value as table_comment
5
+ static DEFAULT_VALUES = {
6
+ true: ['1'],
7
+ false: ['0'],
8
+ 'getdate()': ['current_timestamp'],
9
+ };
10
+ getManagementDbName() {
11
+ return 'master';
12
+ }
13
+ getDropDatabaseSQL(name) {
14
+ // `set offline` rejects all connections including the issuing session, so there is no
15
+ // single-user race window where a torn-down pool connection can reconnect between the
16
+ // mode switch and the drop (SQL Server error 3702 "currently in use"). Dropping an
17
+ // offline database leaves the underlying `.mdf`/`.ldf` files behind though, which
18
+ // makes a subsequent `create database` with the same name fail with error 5170
19
+ // ("file already exists"). Capture the physical paths up front and call
20
+ // `master.sys.xp_delete_files` after the drop to clean them up.
21
+ const quoted = this.quote(name);
22
+ const literal = this.platform.quoteValue(name);
23
+ return (`if db_id(${literal}) is not null begin ` +
24
+ `declare @drop_files table (path nvarchar(260)); ` +
25
+ `insert into @drop_files (path) select physical_name from sys.master_files where database_id = db_id(${literal}); ` +
26
+ `alter database ${quoted} set offline with rollback immediate; ` +
27
+ `drop database ${quoted}; ` +
28
+ `declare @drop_path nvarchar(260); ` +
29
+ `declare drop_files_cursor cursor local fast_forward for select path from @drop_files; ` +
30
+ `open drop_files_cursor; fetch next from drop_files_cursor into @drop_path; ` +
31
+ `while @@fetch_status = 0 begin ` +
32
+ `begin try exec master.sys.xp_delete_files @drop_path; end try begin catch end catch; ` +
33
+ `fetch next from drop_files_cursor into @drop_path; ` +
34
+ `end ` +
35
+ `close drop_files_cursor; deallocate drop_files_cursor; ` +
36
+ `end`);
37
+ }
38
+ disableForeignKeysSQL() {
39
+ return `exec sp_MSforeachtable 'alter table ? nocheck constraint all';`;
40
+ }
41
+ enableForeignKeysSQL() {
42
+ return `exec sp_MSforeachtable 'alter table ? check constraint all';`;
43
+ }
44
+ getDatabaseExistsSQL(name) {
45
+ return `select 1 from sys.databases where name = N'${name}'`;
46
+ }
47
+ getListTablesSQL() {
48
+ return `select t.name as table_name, schema_name(t2.schema_id) schema_name, ep.value as table_comment
51
49
  from sysobjects t
52
50
  inner join sys.tables t2 on t2.object_id = t.id
53
51
  left join sys.extended_properties ep on ep.major_id = t.id and ep.name = 'MS_Description' and ep.minor_id = 0
54
52
  order by schema_name(t2.schema_id), t.name`;
55
- }
56
- getListViewsSQL() {
57
- return `select v.name as view_name, schema_name(v.schema_id) as schema_name, m.definition as view_definition
53
+ }
54
+ getListViewsSQL() {
55
+ return `select v.name as view_name, schema_name(v.schema_id) as schema_name, m.definition as view_definition
58
56
  from sys.views v
59
57
  inner join sys.sql_modules m on v.object_id = m.object_id
60
58
  order by schema_name(v.schema_id), v.name`;
61
- }
62
- async loadViews(schema, connection, schemaName, ctx) {
63
- const views = await connection.execute(this.getListViewsSQL(), [], 'all', ctx);
64
- for (const view of views) {
65
- // Extract SELECT statement from CREATE VIEW ... AS SELECT ...
66
- const match = /\bAS\s+(.+)$/is.exec(view.view_definition);
67
- const definition = match?.[1]?.trim();
68
- if (definition) {
69
- const schemaName = view.schema_name === this.platform.getDefaultSchemaName() ? undefined : view.schema_name;
70
- schema.addView(view.view_name, schemaName, definition);
71
- }
72
- }
73
- }
74
- async getNamespaces(connection, ctx) {
75
- const sql = `select name as schema_name from sys.schemas order by name`;
76
- const res = await connection.execute(sql, [], 'all', ctx);
77
- return res.map(row => row.schema_name);
78
- }
79
- normalizeDefaultValue(defaultValue, length, defaultValues = {}, stripQuotes = false) {
80
- let match = /^\((.*)\)$/.exec(defaultValue);
81
- if (match) {
82
- defaultValue = match[1];
83
- }
84
- match = /^\((.*)\)$/.exec(defaultValue);
85
- if (match) {
86
- defaultValue = match[1];
87
- }
88
- match = /^'(.*)'$/.exec(defaultValue);
89
- if (stripQuotes && match) {
90
- defaultValue = match[1];
91
- }
92
- return super.normalizeDefaultValue(defaultValue, length, MsSqlSchemaHelper.DEFAULT_VALUES);
93
- }
94
- async getAllColumns(connection, tablesBySchemas, ctx) {
95
- const sql = `select table_name as table_name,
59
+ }
60
+ async loadViews(schema, connection, schemaName, ctx) {
61
+ const views = await connection.execute(this.getListViewsSQL(), [], 'all', ctx);
62
+ for (const view of views) {
63
+ // Extract SELECT statement from CREATE VIEW ... AS SELECT ...
64
+ const match = /\bAS\s+(.+)$/is.exec(view.view_definition);
65
+ const definition = match?.[1]?.trim();
66
+ if (definition) {
67
+ const schemaName = view.schema_name === this.platform.getDefaultSchemaName() ? undefined : view.schema_name;
68
+ schema.addView(view.view_name, schemaName, definition);
69
+ }
70
+ }
71
+ }
72
+ async getNamespaces(connection, ctx) {
73
+ const sql = `select name as schema_name from sys.schemas order by name`;
74
+ const res = await connection.execute(sql, [], 'all', ctx);
75
+ return res.map(row => row.schema_name);
76
+ }
77
+ normalizeDefaultValue(defaultValue, length, defaultValues = {}, stripQuotes = false) {
78
+ let match = /^\((.*)\)$/.exec(defaultValue);
79
+ if (match) {
80
+ defaultValue = match[1];
81
+ }
82
+ match = /^\((.*)\)$/.exec(defaultValue);
83
+ if (match) {
84
+ defaultValue = match[1];
85
+ }
86
+ match = /^'(.*)'$/.exec(defaultValue);
87
+ if (stripQuotes && match) {
88
+ defaultValue = match[1];
89
+ }
90
+ return super.normalizeDefaultValue(defaultValue, length, MsSqlSchemaHelper.DEFAULT_VALUES);
91
+ }
92
+ async getAllColumns(connection, tablesBySchemas, ctx) {
93
+ const sql = `select table_name as table_name,
96
94
  table_schema as schema_name,
97
95
  column_name as column_name,
98
96
  column_default as column_default,
@@ -114,57 +112,57 @@ export class MsSqlSchemaHelper extends SchemaHelper {
114
112
  left join sys.default_constraints t5 on sc.default_object_id = t5.object_id
115
113
  where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(ic.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and ic.table_schema = '${schema}')`).join(' or ')})
116
114
  order by ordinal_position`;
117
- const allColumns = await connection.execute(sql, [], 'all', ctx);
118
- const str = val => (val != null ? '' + val : val);
119
- const ret = {};
120
- for (const col of allColumns) {
121
- const mappedType = this.platform.getMappedType(col.data_type);
122
- const defaultValue = str(this.normalizeDefaultValue(col.column_default, col.length, {}, true));
123
- const increments = col.is_identity === 1 && connection.getPlatform().isNumericColumn(mappedType);
124
- const key = this.getTableKey(col);
125
- /* v8 ignore next */
126
- const generated = col.generation_expression
127
- ? `${col.generation_expression}${col.is_persisted ? ' persisted' : ''}`
128
- : undefined;
129
- let type = col.data_type;
130
- if (['varchar', 'nvarchar', 'char', 'nchar', 'varbinary'].includes(col.data_type)) {
131
- col.length = col.character_maximum_length;
132
- }
133
- if (['timestamp', 'datetime', 'datetime2', 'time', 'datetimeoffset'].includes(col.data_type)) {
134
- col.length = col.datetime_precision;
135
- }
136
- if (col.length != null && !type.endsWith(`(${col.length})`) && !['text', 'date'].includes(type)) {
137
- type += `(${col.length === -1 ? 'max' : col.length})`;
138
- }
139
- if (type === 'numeric' && col.numeric_precision != null && col.numeric_scale != null) {
140
- type += `(${col.numeric_precision},${col.numeric_scale})`;
141
- }
142
- if (type === 'float' && col.numeric_precision != null) {
143
- type += `(${col.numeric_precision})`;
144
- }
145
- ret[key] ??= [];
146
- ret[key].push({
147
- name: col.column_name,
148
- type: this.platform.isNumericColumn(mappedType)
149
- ? col.data_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '')
150
- : type,
151
- mappedType,
152
- unsigned: col.data_type.endsWith(' unsigned'),
153
- length: col.length,
154
- default: this.wrap(defaultValue, mappedType),
155
- defaultConstraint: col.column_default_name,
156
- nullable: col.is_nullable === 'YES',
157
- autoincrement: increments,
158
- precision: col.numeric_precision,
159
- scale: col.numeric_scale,
160
- comment: col.column_comment,
161
- generated,
162
- });
163
- }
164
- return ret;
165
- }
166
- async getAllIndexes(connection, tablesBySchemas, ctx) {
167
- const sql = `select t.name as table_name,
115
+ const allColumns = await connection.execute(sql, [], 'all', ctx);
116
+ const str = (val) => (val != null ? '' + val : val);
117
+ const ret = {};
118
+ for (const col of allColumns) {
119
+ const mappedType = this.platform.getMappedType(col.data_type);
120
+ const defaultValue = str(this.normalizeDefaultValue(col.column_default, col.length, {}, true));
121
+ const increments = col.is_identity === 1 && connection.getPlatform().isNumericColumn(mappedType);
122
+ const key = this.getTableKey(col);
123
+ /* v8 ignore next */
124
+ const generated = col.generation_expression
125
+ ? `${col.generation_expression}${col.is_persisted ? ' persisted' : ''}`
126
+ : undefined;
127
+ let type = col.data_type;
128
+ if (['varchar', 'nvarchar', 'char', 'nchar', 'varbinary'].includes(col.data_type)) {
129
+ col.length = col.character_maximum_length;
130
+ }
131
+ if (['timestamp', 'datetime', 'datetime2', 'time', 'datetimeoffset'].includes(col.data_type)) {
132
+ col.length = col.datetime_precision;
133
+ }
134
+ if (col.length != null && !type.endsWith(`(${col.length})`) && !['text', 'date'].includes(type)) {
135
+ type += `(${col.length === -1 ? 'max' : col.length})`;
136
+ }
137
+ if (type === 'numeric' && col.numeric_precision != null && col.numeric_scale != null) {
138
+ type += `(${col.numeric_precision},${col.numeric_scale})`;
139
+ }
140
+ if (type === 'float' && col.numeric_precision != null) {
141
+ type += `(${col.numeric_precision})`;
142
+ }
143
+ ret[key] ??= [];
144
+ ret[key].push({
145
+ name: col.column_name,
146
+ type: this.platform.isNumericColumn(mappedType)
147
+ ? col.data_type.replace(/ unsigned$/, '').replace(/\(\d+\)$/, '')
148
+ : type,
149
+ mappedType,
150
+ unsigned: col.data_type.endsWith(' unsigned'),
151
+ length: col.length,
152
+ default: this.wrap(defaultValue, mappedType),
153
+ defaultConstraint: col.column_default_name,
154
+ nullable: col.is_nullable === 'YES',
155
+ autoincrement: increments,
156
+ precision: col.numeric_precision,
157
+ scale: col.numeric_scale,
158
+ comment: col.column_comment,
159
+ generated,
160
+ });
161
+ }
162
+ return ret;
163
+ }
164
+ async getAllIndexes(connection, tablesBySchemas, ctx) {
165
+ const sql = `select t.name as table_name,
168
166
  ind.name as index_name,
169
167
  is_unique as is_unique,
170
168
  ind.is_primary_key as is_primary_key,
@@ -183,60 +181,60 @@ export class MsSqlSchemaHelper extends SchemaHelper {
183
181
  where
184
182
  (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(t.name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and schema_name(t.schema_id) = '${schema}')`).join(' OR ')})
185
183
  order by t.name, ind.name, ic.is_included_column, ic.key_ordinal`;
186
- const allIndexes = await connection.execute(sql, [], 'all', ctx);
187
- const ret = {};
188
- for (const index of allIndexes) {
189
- const key = this.getTableKey(index);
190
- const isIncluded = index.is_included_column;
191
- const indexDef = {
192
- columnNames: isIncluded ? [] : [index.column_name],
193
- keyName: index.index_name,
194
- unique: index.is_unique,
195
- primary: index.is_primary_key,
196
- constraint: index.is_unique,
197
- };
198
- // Capture INCLUDE columns
199
- if (isIncluded) {
200
- indexDef.include = [index.column_name];
201
- }
202
- // Capture sort order for key columns
203
- if (!isIncluded && index.is_descending_key) {
204
- indexDef.columns = [{ name: index.column_name, sort: 'DESC' }];
205
- }
206
- // Capture disabled flag
207
- if (index.is_disabled) {
208
- indexDef.disabled = true;
209
- }
210
- // Capture clustered flag (type 1 = clustered)
211
- if (index.index_type === 1 && !index.is_primary_key) {
212
- indexDef.clustered = true;
213
- }
214
- // Capture fill factor (0 means default, so only set if non-zero)
215
- if (index.fill_factor > 0) {
216
- indexDef.fillFactor = index.fill_factor;
217
- }
218
- if (index.column_name?.match(/[(): ,"'`]/) || index.expression?.match(/where /i)) {
219
- indexDef.expression = index.expression; // required for the `getCreateIndexSQL()` call
220
- indexDef.expression = this.getCreateIndexSQL(index.table_name, indexDef, !!index.expression);
221
- }
222
- ret[key] ??= [];
223
- ret[key].push(indexDef);
224
- }
225
- for (const key of Object.keys(ret)) {
226
- ret[key] = await this.mapIndexes(ret[key]);
227
- }
228
- return ret;
229
- }
230
- mapForeignKeys(fks, tableName, schemaName) {
231
- const ret = super.mapForeignKeys(fks, tableName, schemaName);
232
- for (const fk of Utils.values(ret)) {
233
- fk.columnNames = Utils.unique(fk.columnNames);
234
- fk.referencedColumnNames = Utils.unique(fk.referencedColumnNames);
235
- }
236
- return ret;
237
- }
238
- async getAllForeignKeys(connection, tablesBySchemas, ctx) {
239
- const sql = `select ccu.constraint_name, ccu.table_name, ccu.table_schema schema_name, ccu.column_name,
184
+ const allIndexes = await connection.execute(sql, [], 'all', ctx);
185
+ const ret = {};
186
+ for (const index of allIndexes) {
187
+ const key = this.getTableKey(index);
188
+ const isIncluded = index.is_included_column;
189
+ const indexDef = {
190
+ columnNames: isIncluded ? [] : [index.column_name],
191
+ keyName: index.index_name,
192
+ unique: index.is_unique,
193
+ primary: index.is_primary_key,
194
+ constraint: index.is_unique,
195
+ };
196
+ // Capture INCLUDE columns
197
+ if (isIncluded) {
198
+ indexDef.include = [index.column_name];
199
+ }
200
+ // Capture sort order for key columns
201
+ if (!isIncluded && index.is_descending_key) {
202
+ indexDef.columns = [{ name: index.column_name, sort: 'DESC' }];
203
+ }
204
+ // Capture disabled flag
205
+ if (index.is_disabled) {
206
+ indexDef.disabled = true;
207
+ }
208
+ // Capture clustered flag (type 1 = clustered)
209
+ if (index.index_type === 1 && !index.is_primary_key) {
210
+ indexDef.clustered = true;
211
+ }
212
+ // Capture fill factor (0 means default, so only set if non-zero)
213
+ if (index.fill_factor > 0) {
214
+ indexDef.fillFactor = index.fill_factor;
215
+ }
216
+ if (index.column_name?.match(/[(): ,"'`]/) || index.expression?.match(/where /i)) {
217
+ indexDef.expression = index.expression; // required for the `getCreateIndexSQL()` call
218
+ indexDef.expression = this.getCreateIndexSQL(index.table_name, indexDef, !!index.expression);
219
+ }
220
+ ret[key] ??= [];
221
+ ret[key].push(indexDef);
222
+ }
223
+ for (const key of Object.keys(ret)) {
224
+ ret[key] = await this.mapIndexes(ret[key]);
225
+ }
226
+ return ret;
227
+ }
228
+ mapForeignKeys(fks, tableName, schemaName) {
229
+ const ret = super.mapForeignKeys(fks, tableName, schemaName);
230
+ for (const fk of Utils.values(ret)) {
231
+ fk.columnNames = Utils.unique(fk.columnNames);
232
+ fk.referencedColumnNames = Utils.unique(fk.referencedColumnNames);
233
+ }
234
+ return ret;
235
+ }
236
+ async getAllForeignKeys(connection, tablesBySchemas, ctx) {
237
+ const sql = `select ccu.constraint_name, ccu.table_name, ccu.table_schema schema_name, ccu.column_name,
240
238
  kcu.constraint_schema referenced_schema_name,
241
239
  kcu.column_name referenced_column_name,
242
240
  kcu.table_name referenced_table_name,
@@ -247,40 +245,40 @@ export class MsSqlSchemaHelper extends SchemaHelper {
247
245
  inner join information_schema.key_column_usage kcu on kcu.constraint_name = rc.unique_constraint_name and rc.unique_constraint_schema = kcu.constraint_schema
248
246
  where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `(ccu.table_name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and ccu.table_schema = '${schema}')`).join(' or ')})
249
247
  order by kcu.table_schema, kcu.table_name, kcu.ordinal_position, kcu.constraint_name`;
250
- const allFks = await connection.execute(sql, [], 'all', ctx);
251
- const ret = {};
252
- for (const fk of allFks) {
253
- const key = this.getTableKey(fk);
254
- ret[key] ??= [];
255
- ret[key].push(fk);
256
- }
257
- Object.keys(ret).forEach(key => {
258
- const [schemaName, tableName] = key.split('.');
259
- ret[key] = this.mapForeignKeys(ret[key], tableName, schemaName);
260
- });
261
- return ret;
262
- }
263
- getEnumDefinitions(checks) {
264
- return checks.reduce((o, item, index) => {
265
- // check constraints are defined as
266
- // `([type]='owner' OR [type]='manager' OR [type]='employee')`
267
- const m1 = item.definition?.match(/^check \((.*)\)/);
268
- let items = m1?.[1].split(' OR ');
269
- /* v8 ignore next */
270
- const hasItems = (items?.length ?? 0) > 0;
271
- if (item.columnName && hasItems) {
272
- items = items
273
- .map(val => /^\(?'(.*)'/.exec(val.trim().replace(`[${item.columnName}]=`, ''))?.[1])
274
- .filter(Boolean);
275
- if (items.length > 0) {
276
- o[item.columnName] = items.reverse();
277
- }
278
- }
279
- return o;
280
- }, {});
281
- }
282
- getChecksSQL(tablesBySchemas) {
283
- return `select con.name as name,
248
+ const allFks = await connection.execute(sql, [], 'all', ctx);
249
+ const ret = {};
250
+ for (const fk of allFks) {
251
+ const key = this.getTableKey(fk);
252
+ ret[key] ??= [];
253
+ ret[key].push(fk);
254
+ }
255
+ Object.keys(ret).forEach(key => {
256
+ const [schemaName, tableName] = key.split('.');
257
+ ret[key] = this.mapForeignKeys(ret[key], tableName, schemaName);
258
+ });
259
+ return ret;
260
+ }
261
+ getEnumDefinitions(checks) {
262
+ return checks.reduce((o, item, index) => {
263
+ // check constraints are defined as
264
+ // `([type]='owner' OR [type]='manager' OR [type]='employee')`
265
+ const m1 = item.definition?.match(/^check \((.*)\)/);
266
+ let items = m1?.[1].split(' OR ');
267
+ /* v8 ignore next */
268
+ const hasItems = (items?.length ?? 0) > 0;
269
+ if (item.columnName && hasItems) {
270
+ items = items
271
+ .map(val => /^\(?'(.*)'/.exec(val.trim().replace(`[${item.columnName}]=`, ''))?.[1])
272
+ .filter(Boolean);
273
+ if (items.length > 0) {
274
+ o[item.columnName] = items.reverse();
275
+ }
276
+ }
277
+ return o;
278
+ }, {});
279
+ }
280
+ getChecksSQL(tablesBySchemas) {
281
+ return `select con.name as name,
284
282
  schema_name(t.schema_id) schema_name,
285
283
  t.name table_name,
286
284
  col.name column_name,
@@ -290,339 +288,324 @@ export class MsSqlSchemaHelper extends SchemaHelper {
290
288
  left outer join sys.all_columns col on con.parent_column_id = col.column_id and con.parent_object_id = col.object_id
291
289
  where (${[...tablesBySchemas.entries()].map(([schema, tables]) => `t.name in (${tables.map(t => this.platform.quoteValue(t.table_name)).join(',')}) and schema_name(t.schema_id) = '${schema}'`).join(' or ')})
292
290
  order by con.name`;
293
- }
294
- async getAllChecks(connection, tablesBySchemas, ctx) {
295
- const sql = this.getChecksSQL(tablesBySchemas);
296
- const allChecks = await connection.execute(sql, [], 'all', ctx);
297
- const ret = {};
298
- for (const check of allChecks) {
299
- const key = this.getTableKey(check);
300
- ret[key] ??= [];
301
- const expression = check.expression.replace(/^\((.*)\)$/, '$1');
302
- ret[key].push({
303
- name: check.name,
304
- columnName: check.column_name,
305
- definition: `check (${expression})`,
306
- expression,
307
- });
308
- }
309
- return ret;
310
- }
311
- async loadInformationSchema(schema, connection, tables, schemas, ctx) {
312
- if (tables.length === 0) {
313
- return;
314
- }
315
- const tablesBySchema = this.getTablesGroupedBySchemas(tables);
316
- const columns = await this.getAllColumns(connection, tablesBySchema, ctx);
317
- const indexes = await this.getAllIndexes(connection, tablesBySchema, ctx);
318
- const checks = await this.getAllChecks(connection, tablesBySchema, ctx);
319
- const fks = await this.getAllForeignKeys(connection, tablesBySchema, ctx);
320
- for (const t of tables) {
321
- const key = this.getTableKey(t);
322
- const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
323
- const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
324
- const enums = this.getEnumDefinitions(checks[key] ?? []);
325
- table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums);
326
- }
327
- }
328
- getPreAlterTable(tableDiff, safe) {
329
- const ret = [];
330
- const indexes = tableDiff.fromTable.getIndexes();
331
- const parts = tableDiff.name.split('.');
332
- const tableName = parts.pop();
333
- const schemaName = parts.pop();
334
- /* v8 ignore next */
335
- const name =
336
- (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName;
337
- const quotedName = this.quote(name);
338
- // indexes need to be first dropped to be able to change a column type
339
- const changedTypes = Object.values(tableDiff.changedColumns).filter(col => col.changedProperties.has('type'));
340
- for (const col of changedTypes) {
341
- for (const index of indexes) {
342
- if (index.columnNames.includes(col.column.name)) {
343
- ret.push(this.getDropIndexSQL(name, index));
344
- }
345
- }
346
- // convert to string first if it's not already a string or has a smaller length
347
- const type = this.platform.extractSimpleType(col.fromColumn.type);
348
- if (!['varchar', 'nvarchar', 'varbinary'].includes(type) || col.fromColumn.length < col.column.length) {
349
- ret.push(`alter table ${quotedName} alter column [${col.oldColumnName}] nvarchar(max)`);
350
- }
351
- }
352
- return ret;
353
- }
354
- getPostAlterTable(tableDiff, safe) {
355
- const ret = [];
356
- const indexes = tableDiff.fromTable.getIndexes();
357
- const parts = tableDiff.name.split('.');
358
- const tableName = parts.pop();
359
- const schemaName = parts.pop();
360
- /* v8 ignore next */
361
- const name =
362
- (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName;
363
- // indexes need to be first dropped to be able to change a column type
364
- const changedTypes = Object.values(tableDiff.changedColumns).filter(col => col.changedProperties.has('type'));
365
- for (const col of changedTypes) {
366
- for (const index of indexes) {
367
- if (index.columnNames.includes(col.column.name)) {
368
- this.append(ret, this.getCreateIndexSQL(name, index));
369
- }
370
- }
371
- }
372
- return ret;
373
- }
374
- getCreateNamespaceSQL(name) {
375
- return `if (schema_id(${this.platform.quoteValue(name)}) is null) begin exec ('create schema ${this.quote(name)} authorization [dbo]') end`;
376
- }
377
- getDropNamespaceSQL(name) {
378
- return `drop schema if exists ${this.quote(name)}`;
379
- }
380
- getDropIndexSQL(tableName, index) {
381
- return `drop index ${this.quote(index.keyName)} on ${this.quote(tableName)}`;
382
- }
383
- dropIndex(table, index, oldIndexName = index.keyName) {
384
- if (index.primary) {
385
- return `alter table ${this.quote(table)} drop constraint ${this.quote(oldIndexName)}`;
386
- }
387
- return `drop index ${this.quote(oldIndexName)} on ${this.quote(table)}`;
388
- }
389
- getDropColumnsSQL(tableName, columns, schemaName) {
390
- /* v8 ignore next */
391
- const tableNameRaw = this.quote(
392
- (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName,
393
- );
394
- const drops = [];
395
- const constraints = this.getDropDefaultsSQL(tableName, columns, schemaName);
396
- for (const column of columns) {
397
- drops.push(this.quote(column.name));
398
- }
399
- return `${constraints.join(';\n')};\nalter table ${tableNameRaw} drop column ${drops.join(', ')}`;
400
- }
401
- getDropDefaultsSQL(tableName, columns, schemaName) {
402
- /* v8 ignore next */
403
- const tableNameRaw = this.quote(
404
- (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName,
405
- );
406
- const constraints = [];
407
- schemaName ??= this.platform.getDefaultSchemaName();
408
- for (const column of columns) {
409
- if (column.defaultConstraint) {
410
- constraints.push(`alter table ${tableNameRaw} drop constraint ${this.quote(column.defaultConstraint)}`);
411
- continue;
412
- }
413
- const i = globalThis.idx;
414
- globalThis.idx++;
415
- constraints.push(
416
- `declare @constraint${i} varchar(100) = (select default_constraints.name from sys.all_columns` +
417
- ' join sys.tables on all_columns.object_id = tables.object_id' +
418
- ' join sys.schemas on tables.schema_id = schemas.schema_id' +
419
- ' join sys.default_constraints on all_columns.default_object_id = default_constraints.object_id' +
420
- ` where schemas.name = '${schemaName}' and tables.name = '${tableName}' and all_columns.name = '${column.name}')` +
421
- ` if @constraint${i} is not null exec('alter table ${tableNameRaw} drop constraint ' + @constraint${i})`,
422
- );
423
- }
424
- return constraints;
425
- }
426
- getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
427
- /* v8 ignore next */
428
- const oldName =
429
- (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') +
430
- tableName +
431
- '.' +
432
- oldColumnName;
433
- const columnName = this.platform.quoteValue(to.name);
434
- return `exec sp_rename ${this.platform.quoteValue(oldName)}, ${columnName}, 'COLUMN'`;
435
- }
436
- createTableColumn(column, table, changedProperties) {
437
- const compositePK = table.getPrimaryKey()?.composite;
438
- const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(table);
439
- const columnType = column.generated ? `as ${column.generated}` : column.type;
440
- const col = [this.quote(column.name)];
441
- if (
442
- column.autoincrement &&
443
- !column.generated &&
444
- !compositePK &&
445
- (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))
446
- ) {
447
- col.push(column.mappedType.getColumnType({ autoincrement: true }, this.platform));
448
- } else {
449
- col.push(columnType);
450
- }
451
- Utils.runIfNotEmpty(() => col.push('identity(1,1)'), column.autoincrement);
452
- Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
453
- Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
454
- if (
455
- column.autoincrement &&
456
- !column.generated &&
457
- !compositePK &&
458
- (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))
459
- ) {
460
- Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
461
- }
462
- const useDefault = changedProperties
463
- ? false
464
- : column.default != null && column.default !== 'null' && !column.autoincrement;
465
- const defaultName = this.platform.getConfig().getNamingStrategy().indexName(table.name, [column.name], 'default');
466
- Utils.runIfNotEmpty(() => col.push(`constraint ${this.quote(defaultName)} default ${column.default}`), useDefault);
467
- return col.join(' ');
468
- }
469
- alterTableColumn(column, table, changedProperties) {
470
- const parts = [];
471
- if (changedProperties.has('default')) {
472
- const [constraint] = this.getDropDefaultsSQL(table.name, [column], table.schema);
473
- parts.push(constraint);
474
- }
475
- if (changedProperties.has('type') || changedProperties.has('nullable')) {
476
- const col = this.createTableColumn(column, table, changedProperties);
477
- parts.push(`alter table ${table.getQuotedName()} alter column ${col}`);
478
- }
479
- if (changedProperties.has('default') && column.default != null) {
480
- const defaultName = this.platform.getConfig().getNamingStrategy().indexName(table.name, [column.name], 'default');
481
- parts.push(
482
- `alter table ${table.getQuotedName()} add constraint ${this.quote(defaultName)} default ${column.default} for ${this.quote(column.name)}`,
483
- );
484
- }
485
- return parts;
486
- }
487
- getCreateIndexSQL(tableName, index, partialExpression = false) {
488
- /* v8 ignore next */
489
- if (index.expression && !partialExpression) {
490
- return index.expression;
491
- }
492
- if (index.fillFactor != null && (index.fillFactor < 0 || index.fillFactor > 100)) {
493
- throw new Error(`fillFactor must be between 0 and 100, got ${index.fillFactor} for index '${index.keyName}'`);
494
- }
495
- const keyName = this.quote(index.keyName);
496
- // Only add clustered keyword when explicitly requested, otherwise omit (defaults to nonclustered)
497
- const clustered = index.clustered ? 'clustered ' : '';
498
- let sql = `create ${index.unique ? 'unique ' : ''}${clustered}index ${keyName} on ${this.quote(tableName)} `;
499
- if (index.expression && partialExpression) {
500
- return sql + `(${index.expression})` + this.getMsSqlIndexSuffix(index);
501
- }
502
- // Build column list with advanced options
503
- const columns = this.getIndexColumns(index);
504
- sql += `(${columns})`;
505
- // Add INCLUDE clause for covering indexes
506
- if (index.include?.length) {
507
- sql += ` include (${index.include.map(c => this.quote(c)).join(', ')})`;
508
- }
509
- sql += this.getMsSqlIndexSuffix(index);
510
- // Disabled indexes need to be created first, then disabled
511
- if (index.disabled) {
512
- sql += `;\nalter index ${keyName} on ${this.quote(tableName)} disable`;
513
- }
514
- return sql;
515
- }
516
- /**
517
- * Build the column list for a MSSQL index.
518
- */
519
- getIndexColumns(index) {
520
- if (index.columns?.length) {
521
- return index.columns
522
- .map(col => {
523
- let colDef = this.quote(col.name);
524
- // MSSQL supports sort order
525
- if (col.sort) {
526
- colDef += ` ${col.sort}`;
527
- }
528
- return colDef;
291
+ }
292
+ async getAllChecks(connection, tablesBySchemas, ctx) {
293
+ const sql = this.getChecksSQL(tablesBySchemas);
294
+ const allChecks = await connection.execute(sql, [], 'all', ctx);
295
+ const ret = {};
296
+ for (const check of allChecks) {
297
+ const key = this.getTableKey(check);
298
+ ret[key] ??= [];
299
+ const expression = check.expression.replace(/^\((.*)\)$/, '$1');
300
+ ret[key].push({
301
+ name: check.name,
302
+ columnName: check.column_name,
303
+ definition: `check (${expression})`,
304
+ expression,
305
+ });
306
+ }
307
+ return ret;
308
+ }
309
+ async loadInformationSchema(schema, connection, tables, schemas, ctx) {
310
+ if (tables.length === 0) {
311
+ return;
312
+ }
313
+ const tablesBySchema = this.getTablesGroupedBySchemas(tables);
314
+ const columns = await this.getAllColumns(connection, tablesBySchema, ctx);
315
+ const indexes = await this.getAllIndexes(connection, tablesBySchema, ctx);
316
+ const checks = await this.getAllChecks(connection, tablesBySchema, ctx);
317
+ const fks = await this.getAllForeignKeys(connection, tablesBySchema, ctx);
318
+ for (const t of tables) {
319
+ const key = this.getTableKey(t);
320
+ const table = schema.addTable(t.table_name, t.schema_name, t.table_comment);
321
+ const pks = await this.getPrimaryKeys(connection, indexes[key], table.name, table.schema);
322
+ const enums = this.getEnumDefinitions(checks[key] ?? []);
323
+ table.init(columns[key], indexes[key], checks[key], pks, fks[key], enums);
324
+ }
325
+ }
326
+ getPreAlterTable(tableDiff, safe) {
327
+ const ret = [];
328
+ const indexes = tableDiff.fromTable.getIndexes();
329
+ const parts = tableDiff.name.split('.');
330
+ const tableName = parts.pop();
331
+ const schemaName = parts.pop();
332
+ /* v8 ignore next */
333
+ const name = (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName;
334
+ const quotedName = this.quote(name);
335
+ // indexes need to be first dropped to be able to change a column type
336
+ const changedTypes = Object.values(tableDiff.changedColumns).filter(col => col.changedProperties.has('type'));
337
+ for (const col of changedTypes) {
338
+ for (const index of indexes) {
339
+ if (index.columnNames.includes(col.column.name)) {
340
+ ret.push(this.getDropIndexSQL(name, index));
341
+ }
342
+ }
343
+ // convert to string first if it's not already a string or has a smaller length
344
+ const type = this.platform.extractSimpleType(col.fromColumn.type);
345
+ if (!['varchar', 'nvarchar', 'varbinary'].includes(type) || col.fromColumn.length < col.column.length) {
346
+ ret.push(`alter table ${quotedName} alter column [${col.oldColumnName}] nvarchar(max)`);
347
+ }
348
+ }
349
+ return ret;
350
+ }
351
+ getPostAlterTable(tableDiff, safe) {
352
+ const ret = [];
353
+ const indexes = tableDiff.fromTable.getIndexes();
354
+ const parts = tableDiff.name.split('.');
355
+ const tableName = parts.pop();
356
+ const schemaName = parts.pop();
357
+ /* v8 ignore next */
358
+ const name = (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName;
359
+ // indexes need to be first dropped to be able to change a column type
360
+ const changedTypes = Object.values(tableDiff.changedColumns).filter(col => col.changedProperties.has('type'));
361
+ for (const col of changedTypes) {
362
+ for (const index of indexes) {
363
+ if (index.columnNames.includes(col.column.name)) {
364
+ this.append(ret, this.getCreateIndexSQL(name, index));
365
+ }
366
+ }
367
+ }
368
+ return ret;
369
+ }
370
+ getCreateNamespaceSQL(name) {
371
+ return `if (schema_id(${this.platform.quoteValue(name)}) is null) begin exec ('create schema ${this.quote(name)} authorization [dbo]') end`;
372
+ }
373
+ getDropNamespaceSQL(name) {
374
+ return `drop schema if exists ${this.quote(name)}`;
375
+ }
376
+ getDropIndexSQL(tableName, index) {
377
+ return `drop index ${this.quote(index.keyName)} on ${this.quote(tableName)}`;
378
+ }
379
+ dropIndex(table, index, oldIndexName = index.keyName) {
380
+ if (index.primary) {
381
+ return `alter table ${this.quote(table)} drop constraint ${this.quote(oldIndexName)}`;
382
+ }
383
+ return `drop index ${this.quote(oldIndexName)} on ${this.quote(table)}`;
384
+ }
385
+ getDropColumnsSQL(tableName, columns, schemaName) {
386
+ /* v8 ignore next */
387
+ const tableNameRaw = this.quote((schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName);
388
+ const drops = [];
389
+ const constraints = this.getDropDefaultsSQL(tableName, columns, schemaName);
390
+ for (const column of columns) {
391
+ drops.push(this.quote(column.name));
392
+ }
393
+ return `${constraints.join(';\n')};\nalter table ${tableNameRaw} drop column ${drops.join(', ')}`;
394
+ }
395
+ getDropDefaultsSQL(tableName, columns, schemaName) {
396
+ /* v8 ignore next */
397
+ const tableNameRaw = this.quote((schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') + tableName);
398
+ const constraints = [];
399
+ schemaName ??= this.platform.getDefaultSchemaName();
400
+ for (const column of columns) {
401
+ if (column.defaultConstraint) {
402
+ constraints.push(`alter table ${tableNameRaw} drop constraint ${this.quote(column.defaultConstraint)}`);
403
+ continue;
404
+ }
405
+ const i = globalThis.idx;
406
+ globalThis.idx++;
407
+ constraints.push(`declare @constraint${i} varchar(100) = (select default_constraints.name from sys.all_columns` +
408
+ ' join sys.tables on all_columns.object_id = tables.object_id' +
409
+ ' join sys.schemas on tables.schema_id = schemas.schema_id' +
410
+ ' join sys.default_constraints on all_columns.default_object_id = default_constraints.object_id' +
411
+ ` where schemas.name = '${schemaName}' and tables.name = '${tableName}' and all_columns.name = '${column.name}')` +
412
+ ` if @constraint${i} is not null exec('alter table ${tableNameRaw} drop constraint ' + @constraint${i})`);
413
+ }
414
+ return constraints;
415
+ }
416
+ getRenameColumnSQL(tableName, oldColumnName, to, schemaName) {
417
+ /* v8 ignore next */
418
+ const oldName = (schemaName && schemaName !== this.platform.getDefaultSchemaName() ? schemaName + '.' : '') +
419
+ tableName +
420
+ '.' +
421
+ oldColumnName;
422
+ const columnName = this.platform.quoteValue(to.name);
423
+ return `exec sp_rename ${this.platform.quoteValue(oldName)}, ${columnName}, 'COLUMN'`;
424
+ }
425
+ createTableColumn(column, table, changedProperties) {
426
+ const compositePK = table.getPrimaryKey()?.composite;
427
+ const primaryKey = !changedProperties && !this.hasNonDefaultPrimaryKeyName(table);
428
+ const columnType = column.generated ? `as ${column.generated}` : column.type;
429
+ const col = [this.quote(column.name)];
430
+ if (column.autoincrement &&
431
+ !column.generated &&
432
+ !compositePK &&
433
+ (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
434
+ col.push(column.mappedType.getColumnType({ autoincrement: true }, this.platform));
435
+ }
436
+ else {
437
+ col.push(columnType);
438
+ }
439
+ Utils.runIfNotEmpty(() => col.push('identity(1,1)'), column.autoincrement);
440
+ Utils.runIfNotEmpty(() => col.push('null'), column.nullable);
441
+ Utils.runIfNotEmpty(() => col.push('not null'), !column.nullable && !column.generated);
442
+ if (column.autoincrement &&
443
+ !column.generated &&
444
+ !compositePK &&
445
+ (!changedProperties || changedProperties.has('autoincrement') || changedProperties.has('type'))) {
446
+ Utils.runIfNotEmpty(() => col.push('primary key'), primaryKey && column.primary);
447
+ }
448
+ const useDefault = changedProperties
449
+ ? false
450
+ : column.default != null && column.default !== 'null' && !column.autoincrement;
451
+ const defaultName = this.platform.getConfig().getNamingStrategy().indexName(table.name, [column.name], 'default');
452
+ Utils.runIfNotEmpty(() => col.push(`constraint ${this.quote(defaultName)} default ${column.default}`), useDefault);
453
+ return col.join(' ');
454
+ }
455
+ alterTableColumn(column, table, changedProperties) {
456
+ const parts = [];
457
+ if (changedProperties.has('default')) {
458
+ const [constraint] = this.getDropDefaultsSQL(table.name, [column], table.schema);
459
+ parts.push(constraint);
460
+ }
461
+ if (changedProperties.has('type') || changedProperties.has('nullable')) {
462
+ const col = this.createTableColumn(column, table, changedProperties);
463
+ parts.push(`alter table ${table.getQuotedName()} alter column ${col}`);
464
+ }
465
+ if (changedProperties.has('default') && column.default != null) {
466
+ const defaultName = this.platform.getConfig().getNamingStrategy().indexName(table.name, [column.name], 'default');
467
+ parts.push(`alter table ${table.getQuotedName()} add constraint ${this.quote(defaultName)} default ${column.default} for ${this.quote(column.name)}`);
468
+ }
469
+ return parts;
470
+ }
471
+ getCreateIndexSQL(tableName, index, partialExpression = false) {
472
+ /* v8 ignore next */
473
+ if (index.expression && !partialExpression) {
474
+ return index.expression;
475
+ }
476
+ if (index.fillFactor != null && (index.fillFactor < 0 || index.fillFactor > 100)) {
477
+ throw new Error(`fillFactor must be between 0 and 100, got ${index.fillFactor} for index '${index.keyName}'`);
478
+ }
479
+ const keyName = this.quote(index.keyName);
480
+ // Only add clustered keyword when explicitly requested, otherwise omit (defaults to nonclustered)
481
+ const clustered = index.clustered ? 'clustered ' : '';
482
+ let sql = `create ${index.unique ? 'unique ' : ''}${clustered}index ${keyName} on ${this.quote(tableName)} `;
483
+ if (index.expression && partialExpression) {
484
+ return sql + `(${index.expression})` + this.getMsSqlIndexSuffix(index);
485
+ }
486
+ // Build column list with advanced options
487
+ const columns = this.getIndexColumns(index);
488
+ sql += `(${columns})`;
489
+ // Add INCLUDE clause for covering indexes
490
+ if (index.include?.length) {
491
+ sql += ` include (${index.include.map(c => this.quote(c)).join(', ')})`;
492
+ }
493
+ sql += this.getMsSqlIndexSuffix(index);
494
+ // Disabled indexes need to be created first, then disabled
495
+ if (index.disabled) {
496
+ sql += `;\nalter index ${keyName} on ${this.quote(tableName)} disable`;
497
+ }
498
+ return sql;
499
+ }
500
+ /**
501
+ * Build the column list for a MSSQL index.
502
+ */
503
+ getIndexColumns(index) {
504
+ if (index.columns?.length) {
505
+ return index.columns
506
+ .map(col => {
507
+ let colDef = this.quote(col.name);
508
+ // MSSQL supports sort order
509
+ if (col.sort) {
510
+ colDef += ` ${col.sort}`;
511
+ }
512
+ return colDef;
513
+ })
514
+ .join(', ');
515
+ }
516
+ return index.columnNames.map(c => this.quote(c)).join(', ');
517
+ }
518
+ /**
519
+ * Get MSSQL-specific index WITH options like fill factor.
520
+ */
521
+ getMsSqlIndexSuffix(index) {
522
+ const withOptions = [];
523
+ if (index.fillFactor != null) {
524
+ withOptions.push(`fillfactor = ${index.fillFactor}`);
525
+ }
526
+ if (withOptions.length > 0) {
527
+ return ` with (${withOptions.join(', ')})`;
528
+ }
529
+ return '';
530
+ }
531
+ createIndex(index, table, createPrimary = false) {
532
+ if (index.primary) {
533
+ return '';
534
+ }
535
+ if (index.expression) {
536
+ return index.expression;
537
+ }
538
+ const needsWhereClause = index.unique && index.columnNames.some(column => table.getColumn(column)?.nullable);
539
+ if (!needsWhereClause) {
540
+ return this.getCreateIndexSQL(table.getShortestName(), index);
541
+ }
542
+ // Generate without disabled suffix, insert WHERE clause, then re-add disabled
543
+ let sql = this.getCreateIndexSQL(table.getShortestName(), { ...index, disabled: false });
544
+ sql += ' where ' + index.columnNames.map(c => `${this.quote(c)} is not null`).join(' and ');
545
+ if (index.disabled) {
546
+ sql += `;\nalter index ${this.quote(index.keyName)} on ${table.getQuotedName()} disable`;
547
+ }
548
+ return sql;
549
+ }
550
+ dropForeignKey(tableName, constraintName) {
551
+ return `alter table ${this.quote(tableName)} drop constraint ${this.quote(constraintName)}`;
552
+ }
553
+ dropTableIfExists(name, schema) {
554
+ if (schema === this.platform.getDefaultSchemaName()) {
555
+ schema = undefined;
556
+ }
557
+ return `if object_id('${this.quote(schema, name)}', 'U') is not null drop table ${this.quote(schema, name)}`;
558
+ }
559
+ dropViewIfExists(name, schema) {
560
+ const viewName = this.quote(this.getTableName(name, schema));
561
+ return `if object_id('${viewName}', 'V') is not null drop view ${viewName}`;
562
+ }
563
+ getAddColumnsSQL(table, columns) {
564
+ const adds = columns
565
+ .map(column => {
566
+ return this.createTableColumn(column, table);
529
567
  })
530
- .join(', ');
531
- }
532
- return index.columnNames.map(c => this.quote(c)).join(', ');
533
- }
534
- /**
535
- * Get MSSQL-specific index WITH options like fill factor.
536
- */
537
- getMsSqlIndexSuffix(index) {
538
- const withOptions = [];
539
- if (index.fillFactor != null) {
540
- withOptions.push(`fillfactor = ${index.fillFactor}`);
541
- }
542
- if (withOptions.length > 0) {
543
- return ` with (${withOptions.join(', ')})`;
544
- }
545
- return '';
546
- }
547
- createIndex(index, table, createPrimary = false) {
548
- if (index.primary) {
549
- return '';
550
- }
551
- if (index.expression) {
552
- return index.expression;
553
- }
554
- const needsWhereClause = index.unique && index.columnNames.some(column => table.getColumn(column)?.nullable);
555
- if (!needsWhereClause) {
556
- return this.getCreateIndexSQL(table.getShortestName(), index);
557
- }
558
- // Generate without disabled suffix, insert WHERE clause, then re-add disabled
559
- let sql = this.getCreateIndexSQL(table.getShortestName(), { ...index, disabled: false });
560
- sql += ' where ' + index.columnNames.map(c => `${this.quote(c)} is not null`).join(' and ');
561
- if (index.disabled) {
562
- sql += `;\nalter index ${this.quote(index.keyName)} on ${table.getQuotedName()} disable`;
563
- }
564
- return sql;
565
- }
566
- dropForeignKey(tableName, constraintName) {
567
- return `alter table ${this.quote(tableName)} drop constraint ${this.quote(constraintName)}`;
568
- }
569
- dropTableIfExists(name, schema) {
570
- if (schema === this.platform.getDefaultSchemaName()) {
571
- schema = undefined;
572
- }
573
- return `if object_id('${this.quote(schema, name)}', 'U') is not null drop table ${this.quote(schema, name)}`;
574
- }
575
- dropViewIfExists(name, schema) {
576
- const viewName = this.quote(this.getTableName(name, schema));
577
- return `if object_id('${viewName}', 'V') is not null drop view ${viewName}`;
578
- }
579
- getAddColumnsSQL(table, columns) {
580
- const adds = columns
581
- .map(column => {
582
- return this.createTableColumn(column, table);
583
- })
584
- .join(', ');
585
- return [`alter table ${table.getQuotedName()} add ${adds}`];
586
- }
587
- appendComments(table) {
588
- const sql = [];
589
- const schema = this.platform.quoteValue(table.schema);
590
- const tableName = this.platform.quoteValue(table.name);
591
- if (table.comment) {
592
- const comment = this.platform.quoteValue(table.comment);
593
- sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, null, null))
568
+ .join(', ');
569
+ return [`alter table ${table.getQuotedName()} add ${adds}`];
570
+ }
571
+ appendComments(table) {
572
+ const sql = [];
573
+ const schema = this.platform.quoteValue(table.schema);
574
+ const tableName = this.platform.quoteValue(table.name);
575
+ if (table.comment) {
576
+ const comment = this.platform.quoteValue(table.comment);
577
+ sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, null, null))
594
578
  exec sys.sp_updateextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}
595
579
  else
596
580
  exec sys.sp_addextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}`);
597
- }
598
- for (const column of table.getColumns()) {
599
- if (column.comment) {
600
- const comment = this.platform.quoteValue(column.comment);
601
- const columnName = this.platform.quoteValue(column.name);
602
- sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}))
581
+ }
582
+ for (const column of table.getColumns()) {
583
+ if (column.comment) {
584
+ const comment = this.platform.quoteValue(column.comment);
585
+ const columnName = this.platform.quoteValue(column.name);
586
+ sql.push(`if exists(select * from sys.fn_listextendedproperty(N'MS_Description', N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}))
603
587
  exec sys.sp_updateextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}
604
588
  else
605
589
  exec sys.sp_addextendedproperty N'MS_Description', N${comment}, N'Schema', N${schema}, N'Table', N${tableName}, N'Column', N${columnName}`);
606
- }
607
- }
608
- return sql;
609
- }
610
- inferLengthFromColumnType(type) {
611
- const match = /^(\w+)\s*\(\s*(-?\d+|max)\s*\)/.exec(type);
612
- if (!match) {
613
- return;
614
- }
615
- if (match[2] === 'max') {
616
- return -1;
617
- }
618
- return +match[2];
619
- }
620
- wrap(val, type) {
621
- const stringType =
622
- type instanceof StringType ||
623
- type instanceof TextType ||
624
- type instanceof EnumType ||
625
- type instanceof UnicodeStringType;
626
- return typeof val === 'string' && val.length > 0 && stringType ? this.platform.quoteValue(val) : val;
627
- }
590
+ }
591
+ }
592
+ return sql;
593
+ }
594
+ inferLengthFromColumnType(type) {
595
+ const match = /^(\w+)\s*\(\s*(-?\d+|max)\s*\)/.exec(type);
596
+ if (!match) {
597
+ return;
598
+ }
599
+ if (match[2] === 'max') {
600
+ return -1;
601
+ }
602
+ return +match[2];
603
+ }
604
+ wrap(val, type) {
605
+ const stringType = type instanceof StringType ||
606
+ type instanceof TextType ||
607
+ type instanceof EnumType ||
608
+ type instanceof UnicodeStringType;
609
+ return typeof val === 'string' && val.length > 0 && stringType ? this.platform.quoteValue(val) : val;
610
+ }
628
611
  }