knex 0.21.20 → 0.21.21
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.
- package/CHANGELOG.md +6 -0
- package/CONTRIBUTING.md +184 -184
- package/LICENSE +22 -22
- package/README.md +95 -95
- package/bin/cli.js +414 -414
- package/bin/utils/cli-config-utils.js +151 -151
- package/bin/utils/constants.js +7 -7
- package/bin/utils/migrationsLister.js +37 -37
- package/knex.js +8 -8
- package/lib/client.js +413 -413
- package/lib/config-resolver.js +61 -61
- package/lib/constants.js +44 -44
- package/lib/dialects/mssql/index.js +390 -390
- package/lib/dialects/mssql/query/compiler.js +444 -444
- package/lib/dialects/mssql/schema/columncompiler.js +103 -103
- package/lib/dialects/mssql/schema/compiler.js +59 -59
- package/lib/dialects/mssql/schema/tablecompiler.js +245 -245
- package/lib/dialects/mssql/transaction.js +97 -97
- package/lib/dialects/mysql/index.js +191 -191
- package/lib/dialects/mysql/query/compiler.js +142 -142
- package/lib/dialects/mysql/schema/columncompiler.js +171 -171
- package/lib/dialects/mysql/schema/compiler.js +60 -60
- package/lib/dialects/mysql/schema/tablecompiler.js +262 -262
- package/lib/dialects/mysql/transaction.js +48 -48
- package/lib/dialects/mysql2/index.js +35 -35
- package/lib/dialects/mysql2/transaction.js +46 -46
- package/lib/dialects/oracle/DEAD_CODE.md +5 -5
- package/lib/dialects/oracle/formatter.js +20 -20
- package/lib/dialects/oracle/index.js +79 -79
- package/lib/dialects/oracle/query/compiler.js +327 -327
- package/lib/dialects/oracle/schema/columnbuilder.js +18 -18
- package/lib/dialects/oracle/schema/columncompiler.js +139 -139
- package/lib/dialects/oracle/schema/compiler.js +81 -81
- package/lib/dialects/oracle/schema/tablecompiler.js +165 -165
- package/lib/dialects/oracle/schema/trigger.js +126 -126
- package/lib/dialects/oracle/utils.js +86 -86
- package/lib/dialects/oracledb/index.js +489 -489
- package/lib/dialects/oracledb/query/compiler.js +363 -363
- package/lib/dialects/oracledb/schema/columncompiler.js +35 -35
- package/lib/dialects/oracledb/transaction.js +76 -76
- package/lib/dialects/oracledb/utils.js +14 -14
- package/lib/dialects/postgres/index.js +319 -319
- package/lib/dialects/postgres/query/compiler.js +206 -206
- package/lib/dialects/postgres/schema/columncompiler.js +125 -125
- package/lib/dialects/postgres/schema/compiler.js +109 -109
- package/lib/dialects/postgres/schema/tablecompiler.js +183 -183
- package/lib/dialects/redshift/index.js +73 -73
- package/lib/dialects/redshift/query/compiler.js +119 -119
- package/lib/dialects/redshift/schema/columnbuilder.js +20 -20
- package/lib/dialects/redshift/schema/columncompiler.js +60 -60
- package/lib/dialects/redshift/schema/compiler.js +14 -14
- package/lib/dialects/redshift/schema/tablecompiler.js +123 -123
- package/lib/dialects/redshift/transaction.js +18 -18
- package/lib/dialects/sqlite3/formatter.js +21 -21
- package/lib/dialects/sqlite3/index.js +169 -169
- package/lib/dialects/sqlite3/query/compiler.js +222 -222
- package/lib/dialects/sqlite3/schema/columncompiler.js +27 -27
- package/lib/dialects/sqlite3/schema/compiler.js +49 -49
- package/lib/dialects/sqlite3/schema/ddl.js +525 -525
- package/lib/dialects/sqlite3/schema/tablecompiler.js +238 -238
- package/lib/formatter.js +295 -295
- package/lib/functionhelper.js +14 -14
- package/lib/helpers.js +92 -92
- package/lib/index.js +3 -3
- package/lib/interface.js +115 -115
- package/lib/knex.js +42 -42
- package/lib/logger.js +76 -76
- package/lib/migrate/MigrationGenerator.js +82 -82
- package/lib/migrate/Migrator.js +611 -611
- package/lib/migrate/configuration-merger.js +60 -60
- package/lib/migrate/migrate-stub.js +17 -17
- package/lib/migrate/migration-list-resolver.js +36 -36
- package/lib/migrate/sources/fs-migrations.js +99 -99
- package/lib/migrate/stub/cjs.stub +15 -15
- package/lib/migrate/stub/coffee.stub +13 -13
- package/lib/migrate/stub/eg.stub +14 -14
- package/lib/migrate/stub/js.stub +15 -15
- package/lib/migrate/stub/knexfile-coffee.stub +34 -34
- package/lib/migrate/stub/knexfile-eg.stub +43 -43
- package/lib/migrate/stub/knexfile-js.stub +44 -44
- package/lib/migrate/stub/knexfile-ls.stub +35 -35
- package/lib/migrate/stub/knexfile-ts.stub +44 -44
- package/lib/migrate/stub/ls.stub +14 -14
- package/lib/migrate/stub/ts.stub +21 -21
- package/lib/migrate/table-creator.js +67 -67
- package/lib/migrate/table-resolver.js +27 -27
- package/lib/query/builder.js +1372 -1372
- package/lib/query/compiler.js +889 -889
- package/lib/query/constants.js +13 -13
- package/lib/query/joinclause.js +263 -263
- package/lib/query/methods.js +92 -92
- package/lib/query/string.js +190 -190
- package/lib/raw.js +188 -188
- package/lib/ref.js +39 -39
- package/lib/runner.js +285 -285
- package/lib/schema/builder.js +82 -82
- package/lib/schema/columnbuilder.js +117 -117
- package/lib/schema/columncompiler.js +177 -177
- package/lib/schema/compiler.js +101 -101
- package/lib/schema/helpers.js +51 -51
- package/lib/schema/tablebuilder.js +288 -288
- package/lib/schema/tablecompiler.js +296 -296
- package/lib/seed/Seeder.js +203 -203
- package/lib/seed/seed-stub.js +13 -13
- package/lib/seed/stub/coffee.stub +9 -9
- package/lib/seed/stub/eg.stub +11 -11
- package/lib/seed/stub/js.stub +13 -13
- package/lib/seed/stub/ls.stub +11 -11
- package/lib/seed/stub/ts.stub +13 -13
- package/lib/transaction.js +363 -363
- package/lib/util/batchInsert.js +59 -59
- package/lib/util/delay.js +6 -6
- package/lib/util/fake-client.js +9 -9
- package/lib/util/finally-mixin.js +13 -13
- package/lib/util/fs.js +76 -76
- package/lib/util/import-file.js +13 -13
- package/lib/util/is-module-type.js +14 -14
- package/lib/util/is.js +32 -32
- package/lib/util/make-knex.js +338 -338
- package/lib/util/nanoid.js +29 -29
- package/lib/util/noop.js +1 -1
- package/lib/util/parse-connection.js +66 -66
- package/lib/util/save-async-stack.js +14 -14
- package/lib/util/template.js +52 -52
- package/lib/util/timeout.js +29 -29
- package/lib/util/timestamp.js +16 -16
- package/package.json +1 -1
- package/scripts/build.js +125 -125
- package/scripts/docker-compose.yml +111 -111
- package/scripts/next-release-howto.md +24 -24
- package/scripts/release.sh +34 -34
- package/scripts/runkit-example.js +34 -34
- package/scripts/stress-test/README.txt +18 -18
- package/scripts/stress-test/docker-compose.yml +47 -47
- package/scripts/stress-test/knex-stress-test.js +196 -196
- package/scripts/stress-test/mysql2-random-hanging-every-now-and-then.js +145 -145
- package/scripts/stress-test/mysql2-sudden-exit-without-error.js +100 -100
- package/scripts/stress-test/reconnect-test-mysql-based-drivers.js +184 -184
- package/types/index.d.ts +2249 -2249
- package/types/result.d.ts +27 -27
- package/types/tables.d.ts +4 -4
|
@@ -1,296 +1,296 @@
|
|
|
1
|
-
/* eslint max-len:0 */
|
|
2
|
-
|
|
3
|
-
// Table Compiler
|
|
4
|
-
// -------
|
|
5
|
-
const { pushAdditional, pushQuery, unshiftQuery } = require('./helpers');
|
|
6
|
-
const helpers = require('../helpers');
|
|
7
|
-
const groupBy = require('lodash/groupBy');
|
|
8
|
-
const indexOf = require('lodash/indexOf');
|
|
9
|
-
const isEmpty = require('lodash/isEmpty');
|
|
10
|
-
const tail = require('lodash/tail');
|
|
11
|
-
|
|
12
|
-
function TableCompiler(client, tableBuilder) {
|
|
13
|
-
this.client = client;
|
|
14
|
-
this.tableBuilder = tableBuilder;
|
|
15
|
-
this._commonBuilder = this.tableBuilder;
|
|
16
|
-
this.method = tableBuilder._method;
|
|
17
|
-
this.schemaNameRaw = tableBuilder._schemaName;
|
|
18
|
-
this.tableNameRaw = tableBuilder._tableName;
|
|
19
|
-
this.single = tableBuilder._single;
|
|
20
|
-
this.grouped = groupBy(tableBuilder._statements, 'grouping');
|
|
21
|
-
this.formatter = client.formatter(tableBuilder);
|
|
22
|
-
this.sequence = [];
|
|
23
|
-
this._formatting = client.config && client.config.formatting;
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
TableCompiler.prototype.pushQuery = pushQuery;
|
|
27
|
-
|
|
28
|
-
TableCompiler.prototype.pushAdditional = pushAdditional;
|
|
29
|
-
|
|
30
|
-
TableCompiler.prototype.unshiftQuery = unshiftQuery;
|
|
31
|
-
|
|
32
|
-
// Convert the tableCompiler toSQL
|
|
33
|
-
TableCompiler.prototype.toSQL = function () {
|
|
34
|
-
this[this.method]();
|
|
35
|
-
return this.sequence;
|
|
36
|
-
};
|
|
37
|
-
|
|
38
|
-
TableCompiler.prototype.lowerCase = true;
|
|
39
|
-
|
|
40
|
-
// Column Compilation
|
|
41
|
-
// -------
|
|
42
|
-
|
|
43
|
-
// If this is a table "creation", we need to first run through all
|
|
44
|
-
// of the columns to build them into a single string,
|
|
45
|
-
// and then run through anything else and push it to the query sequence.
|
|
46
|
-
TableCompiler.prototype.createAlterTableMethods = null;
|
|
47
|
-
TableCompiler.prototype.create = function (ifNot) {
|
|
48
|
-
const columnBuilders = this.getColumns();
|
|
49
|
-
const columns = columnBuilders.map((col) => col.toSQL());
|
|
50
|
-
const columnTypes = this.getColumnTypes(columns);
|
|
51
|
-
if (this.createAlterTableMethods) {
|
|
52
|
-
this.alterTableForCreate(columnTypes);
|
|
53
|
-
}
|
|
54
|
-
this.createQuery(columnTypes, ifNot);
|
|
55
|
-
this.columnQueries(columns);
|
|
56
|
-
delete this.single.comment;
|
|
57
|
-
this.alterTable();
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// Only create the table if it doesn't exist.
|
|
61
|
-
TableCompiler.prototype.createIfNot = function () {
|
|
62
|
-
this.create(true);
|
|
63
|
-
};
|
|
64
|
-
|
|
65
|
-
// If we're altering the table, we need to one-by-one
|
|
66
|
-
// go through and handle each of the queries associated
|
|
67
|
-
// with altering the table's schema.
|
|
68
|
-
TableCompiler.prototype.alter = function () {
|
|
69
|
-
const addColBuilders = this.getColumns();
|
|
70
|
-
const addColumns = addColBuilders.map((col) => col.toSQL());
|
|
71
|
-
const alterColBuilders = this.getColumns('alter');
|
|
72
|
-
const alterColumns = alterColBuilders.map((col) => col.toSQL());
|
|
73
|
-
const addColumnTypes = this.getColumnTypes(addColumns);
|
|
74
|
-
const alterColumnTypes = this.getColumnTypes(alterColumns);
|
|
75
|
-
|
|
76
|
-
this.addColumns(addColumnTypes);
|
|
77
|
-
this.alterColumns(alterColumnTypes, alterColBuilders);
|
|
78
|
-
this.columnQueries(addColumns);
|
|
79
|
-
this.columnQueries(alterColumns);
|
|
80
|
-
this.alterTable();
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
TableCompiler.prototype.foreign = function (foreignData) {
|
|
84
|
-
if (foreignData.inTable && foreignData.references) {
|
|
85
|
-
const keyName = foreignData.keyName
|
|
86
|
-
? this.formatter.wrap(foreignData.keyName)
|
|
87
|
-
: this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
|
|
88
|
-
const column = this.formatter.columnize(foreignData.column);
|
|
89
|
-
const references = this.formatter.columnize(foreignData.references);
|
|
90
|
-
const inTable = this.formatter.wrap(foreignData.inTable);
|
|
91
|
-
const onUpdate = foreignData.onUpdate
|
|
92
|
-
? (this.lowerCase ? ' on update ' : ' ON UPDATE ') + foreignData.onUpdate
|
|
93
|
-
: '';
|
|
94
|
-
const onDelete = foreignData.onDelete
|
|
95
|
-
? (this.lowerCase ? ' on delete ' : ' ON DELETE ') + foreignData.onDelete
|
|
96
|
-
: '';
|
|
97
|
-
if (this.lowerCase) {
|
|
98
|
-
this.pushQuery(
|
|
99
|
-
(!this.forCreate ? `alter table ${this.tableName()} add ` : '') +
|
|
100
|
-
'constraint ' +
|
|
101
|
-
keyName +
|
|
102
|
-
' ' +
|
|
103
|
-
'foreign key (' +
|
|
104
|
-
column +
|
|
105
|
-
') references ' +
|
|
106
|
-
inTable +
|
|
107
|
-
' (' +
|
|
108
|
-
references +
|
|
109
|
-
')' +
|
|
110
|
-
onUpdate +
|
|
111
|
-
onDelete
|
|
112
|
-
);
|
|
113
|
-
} else {
|
|
114
|
-
this.pushQuery(
|
|
115
|
-
(!this.forCreate ? `ALTER TABLE ${this.tableName()} ADD ` : '') +
|
|
116
|
-
'CONSTRAINT ' +
|
|
117
|
-
keyName +
|
|
118
|
-
' ' +
|
|
119
|
-
'FOREIGN KEY (' +
|
|
120
|
-
column +
|
|
121
|
-
') REFERENCES ' +
|
|
122
|
-
inTable +
|
|
123
|
-
' (' +
|
|
124
|
-
references +
|
|
125
|
-
')' +
|
|
126
|
-
onUpdate +
|
|
127
|
-
onDelete
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
// Get all of the column sql & bindings individually for building the table queries.
|
|
134
|
-
TableCompiler.prototype.getColumnTypes = (columns) =>
|
|
135
|
-
columns.reduce(
|
|
136
|
-
function (memo, columnSQL) {
|
|
137
|
-
const column = columnSQL[0];
|
|
138
|
-
memo.sql.push(column.sql);
|
|
139
|
-
memo.bindings.concat(column.bindings);
|
|
140
|
-
return memo;
|
|
141
|
-
},
|
|
142
|
-
{ sql: [], bindings: [] }
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
// Adds all of the additional queries from the "column"
|
|
146
|
-
TableCompiler.prototype.columnQueries = function (columns) {
|
|
147
|
-
const queries = columns.reduce(function (memo, columnSQL) {
|
|
148
|
-
const column = tail(columnSQL);
|
|
149
|
-
if (!isEmpty(column)) return memo.concat(column);
|
|
150
|
-
return memo;
|
|
151
|
-
}, []);
|
|
152
|
-
for (const q of queries) {
|
|
153
|
-
this.pushQuery(q);
|
|
154
|
-
}
|
|
155
|
-
};
|
|
156
|
-
|
|
157
|
-
// Add a new column.
|
|
158
|
-
TableCompiler.prototype.addColumnsPrefix = 'add column ';
|
|
159
|
-
|
|
160
|
-
// All of the columns to "add" for the query
|
|
161
|
-
TableCompiler.prototype.addColumns = function (columns, prefix) {
|
|
162
|
-
prefix = prefix || this.addColumnsPrefix;
|
|
163
|
-
|
|
164
|
-
if (columns.sql.length > 0) {
|
|
165
|
-
const columnSql = columns.sql.map((column) => {
|
|
166
|
-
return prefix + column;
|
|
167
|
-
});
|
|
168
|
-
this.pushQuery({
|
|
169
|
-
sql:
|
|
170
|
-
(this.lowerCase ? 'alter table ' : 'ALTER TABLE ') +
|
|
171
|
-
this.tableName() +
|
|
172
|
-
' ' +
|
|
173
|
-
columnSql.join(', '),
|
|
174
|
-
bindings: columns.bindings,
|
|
175
|
-
});
|
|
176
|
-
}
|
|
177
|
-
};
|
|
178
|
-
|
|
179
|
-
// Alter column
|
|
180
|
-
TableCompiler.prototype.alterColumnsPrefix = 'alter column ';
|
|
181
|
-
|
|
182
|
-
TableCompiler.prototype.alterColumns = function (columns, colBuilders) {
|
|
183
|
-
if (columns.sql.length > 0) {
|
|
184
|
-
this.addColumns(columns, this.alterColumnsPrefix, colBuilders);
|
|
185
|
-
}
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
// Compile the columns as needed for the current create or alter table
|
|
189
|
-
TableCompiler.prototype.getColumns = function (method) {
|
|
190
|
-
const columns = this.grouped.columns || [];
|
|
191
|
-
method = method || 'add';
|
|
192
|
-
|
|
193
|
-
const queryContext = this.tableBuilder.queryContext();
|
|
194
|
-
|
|
195
|
-
return columns
|
|
196
|
-
.filter((column) => column.builder._method === method)
|
|
197
|
-
.map((column) => {
|
|
198
|
-
// pass queryContext down to columnBuilder but do not overwrite it if already set
|
|
199
|
-
if (
|
|
200
|
-
queryContext !== undefined &&
|
|
201
|
-
column.builder.queryContext() === undefined
|
|
202
|
-
) {
|
|
203
|
-
column.builder.queryContext(queryContext);
|
|
204
|
-
}
|
|
205
|
-
return this.client.columnCompiler(this, column.builder);
|
|
206
|
-
});
|
|
207
|
-
};
|
|
208
|
-
|
|
209
|
-
TableCompiler.prototype.tableName = function () {
|
|
210
|
-
const name = this.schemaNameRaw
|
|
211
|
-
? `${this.schemaNameRaw}.${this.tableNameRaw}`
|
|
212
|
-
: this.tableNameRaw;
|
|
213
|
-
|
|
214
|
-
return this.formatter.wrap(name);
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
// Generate all of the alter column statements necessary for the query.
|
|
218
|
-
TableCompiler.prototype.alterTable = function () {
|
|
219
|
-
const alterTable = this.grouped.alterTable || [];
|
|
220
|
-
for (let i = 0, l = alterTable.length; i < l; i++) {
|
|
221
|
-
const statement = alterTable[i];
|
|
222
|
-
if (this[statement.method]) {
|
|
223
|
-
this[statement.method].apply(this, statement.args);
|
|
224
|
-
} else {
|
|
225
|
-
this.client.logger.error(`Debug: ${statement.method} does not exist`);
|
|
226
|
-
}
|
|
227
|
-
}
|
|
228
|
-
for (const item in this.single) {
|
|
229
|
-
if (typeof this[item] === 'function') this[item](this.single[item]);
|
|
230
|
-
}
|
|
231
|
-
};
|
|
232
|
-
|
|
233
|
-
TableCompiler.prototype.alterTableForCreate = function (columnTypes) {
|
|
234
|
-
this.forCreate = true;
|
|
235
|
-
const savedSequence = this.sequence;
|
|
236
|
-
const alterTable = this.grouped.alterTable || [];
|
|
237
|
-
this.grouped.alterTable = [];
|
|
238
|
-
for (let i = 0, l = alterTable.length; i < l; i++) {
|
|
239
|
-
const statement = alterTable[i];
|
|
240
|
-
if (indexOf(this.createAlterTableMethods, statement.method) < 0) {
|
|
241
|
-
this.grouped.alterTable.push(statement);
|
|
242
|
-
continue;
|
|
243
|
-
}
|
|
244
|
-
if (this[statement.method]) {
|
|
245
|
-
this.sequence = [];
|
|
246
|
-
this[statement.method].apply(this, statement.args);
|
|
247
|
-
columnTypes.sql.push(this.sequence[0].sql);
|
|
248
|
-
} else {
|
|
249
|
-
this.client.logger.error(`Debug: ${statement.method} does not exist`);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
this.sequence = savedSequence;
|
|
253
|
-
this.forCreate = false;
|
|
254
|
-
};
|
|
255
|
-
|
|
256
|
-
// Drop the index on the current table.
|
|
257
|
-
TableCompiler.prototype.dropIndex = function (value) {
|
|
258
|
-
this.pushQuery(`drop index${value}`);
|
|
259
|
-
};
|
|
260
|
-
|
|
261
|
-
// Drop the unique
|
|
262
|
-
TableCompiler.prototype.dropUnique = TableCompiler.prototype.dropForeign = function () {
|
|
263
|
-
throw new Error('Method implemented in the dialect driver');
|
|
264
|
-
};
|
|
265
|
-
|
|
266
|
-
TableCompiler.prototype.dropColumnPrefix = 'drop column ';
|
|
267
|
-
TableCompiler.prototype.dropColumn = function () {
|
|
268
|
-
const columns = helpers.normalizeArr.apply(null, arguments);
|
|
269
|
-
const drops = (Array.isArray(columns) ? columns : [columns]).map((column) => {
|
|
270
|
-
return this.dropColumnPrefix + this.formatter.wrap(column);
|
|
271
|
-
});
|
|
272
|
-
this.pushQuery(
|
|
273
|
-
(this.lowerCase ? 'alter table ' : 'ALTER TABLE ') +
|
|
274
|
-
this.tableName() +
|
|
275
|
-
' ' +
|
|
276
|
-
drops.join(', ')
|
|
277
|
-
);
|
|
278
|
-
};
|
|
279
|
-
|
|
280
|
-
// If no name was specified for this index, we will create one using a basic
|
|
281
|
-
// convention of the table name, followed by the columns, followed by an
|
|
282
|
-
// index type, such as primary or index, which makes the index unique.
|
|
283
|
-
TableCompiler.prototype._indexCommand = function (type, tableName, columns) {
|
|
284
|
-
if (!Array.isArray(columns)) columns = columns ? [columns] : [];
|
|
285
|
-
const table = tableName.replace(/\.|-/g, '_');
|
|
286
|
-
const indexName = (
|
|
287
|
-
table +
|
|
288
|
-
'_' +
|
|
289
|
-
columns.join('_') +
|
|
290
|
-
'_' +
|
|
291
|
-
type
|
|
292
|
-
).toLowerCase();
|
|
293
|
-
return this.formatter.wrap(indexName);
|
|
294
|
-
};
|
|
295
|
-
|
|
296
|
-
module.exports = TableCompiler;
|
|
1
|
+
/* eslint max-len:0 */
|
|
2
|
+
|
|
3
|
+
// Table Compiler
|
|
4
|
+
// -------
|
|
5
|
+
const { pushAdditional, pushQuery, unshiftQuery } = require('./helpers');
|
|
6
|
+
const helpers = require('../helpers');
|
|
7
|
+
const groupBy = require('lodash/groupBy');
|
|
8
|
+
const indexOf = require('lodash/indexOf');
|
|
9
|
+
const isEmpty = require('lodash/isEmpty');
|
|
10
|
+
const tail = require('lodash/tail');
|
|
11
|
+
|
|
12
|
+
function TableCompiler(client, tableBuilder) {
|
|
13
|
+
this.client = client;
|
|
14
|
+
this.tableBuilder = tableBuilder;
|
|
15
|
+
this._commonBuilder = this.tableBuilder;
|
|
16
|
+
this.method = tableBuilder._method;
|
|
17
|
+
this.schemaNameRaw = tableBuilder._schemaName;
|
|
18
|
+
this.tableNameRaw = tableBuilder._tableName;
|
|
19
|
+
this.single = tableBuilder._single;
|
|
20
|
+
this.grouped = groupBy(tableBuilder._statements, 'grouping');
|
|
21
|
+
this.formatter = client.formatter(tableBuilder);
|
|
22
|
+
this.sequence = [];
|
|
23
|
+
this._formatting = client.config && client.config.formatting;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
TableCompiler.prototype.pushQuery = pushQuery;
|
|
27
|
+
|
|
28
|
+
TableCompiler.prototype.pushAdditional = pushAdditional;
|
|
29
|
+
|
|
30
|
+
TableCompiler.prototype.unshiftQuery = unshiftQuery;
|
|
31
|
+
|
|
32
|
+
// Convert the tableCompiler toSQL
|
|
33
|
+
TableCompiler.prototype.toSQL = function () {
|
|
34
|
+
this[this.method]();
|
|
35
|
+
return this.sequence;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
TableCompiler.prototype.lowerCase = true;
|
|
39
|
+
|
|
40
|
+
// Column Compilation
|
|
41
|
+
// -------
|
|
42
|
+
|
|
43
|
+
// If this is a table "creation", we need to first run through all
|
|
44
|
+
// of the columns to build them into a single string,
|
|
45
|
+
// and then run through anything else and push it to the query sequence.
|
|
46
|
+
TableCompiler.prototype.createAlterTableMethods = null;
|
|
47
|
+
TableCompiler.prototype.create = function (ifNot) {
|
|
48
|
+
const columnBuilders = this.getColumns();
|
|
49
|
+
const columns = columnBuilders.map((col) => col.toSQL());
|
|
50
|
+
const columnTypes = this.getColumnTypes(columns);
|
|
51
|
+
if (this.createAlterTableMethods) {
|
|
52
|
+
this.alterTableForCreate(columnTypes);
|
|
53
|
+
}
|
|
54
|
+
this.createQuery(columnTypes, ifNot);
|
|
55
|
+
this.columnQueries(columns);
|
|
56
|
+
delete this.single.comment;
|
|
57
|
+
this.alterTable();
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Only create the table if it doesn't exist.
|
|
61
|
+
TableCompiler.prototype.createIfNot = function () {
|
|
62
|
+
this.create(true);
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
// If we're altering the table, we need to one-by-one
|
|
66
|
+
// go through and handle each of the queries associated
|
|
67
|
+
// with altering the table's schema.
|
|
68
|
+
TableCompiler.prototype.alter = function () {
|
|
69
|
+
const addColBuilders = this.getColumns();
|
|
70
|
+
const addColumns = addColBuilders.map((col) => col.toSQL());
|
|
71
|
+
const alterColBuilders = this.getColumns('alter');
|
|
72
|
+
const alterColumns = alterColBuilders.map((col) => col.toSQL());
|
|
73
|
+
const addColumnTypes = this.getColumnTypes(addColumns);
|
|
74
|
+
const alterColumnTypes = this.getColumnTypes(alterColumns);
|
|
75
|
+
|
|
76
|
+
this.addColumns(addColumnTypes);
|
|
77
|
+
this.alterColumns(alterColumnTypes, alterColBuilders);
|
|
78
|
+
this.columnQueries(addColumns);
|
|
79
|
+
this.columnQueries(alterColumns);
|
|
80
|
+
this.alterTable();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
TableCompiler.prototype.foreign = function (foreignData) {
|
|
84
|
+
if (foreignData.inTable && foreignData.references) {
|
|
85
|
+
const keyName = foreignData.keyName
|
|
86
|
+
? this.formatter.wrap(foreignData.keyName)
|
|
87
|
+
: this._indexCommand('foreign', this.tableNameRaw, foreignData.column);
|
|
88
|
+
const column = this.formatter.columnize(foreignData.column);
|
|
89
|
+
const references = this.formatter.columnize(foreignData.references);
|
|
90
|
+
const inTable = this.formatter.wrap(foreignData.inTable);
|
|
91
|
+
const onUpdate = foreignData.onUpdate
|
|
92
|
+
? (this.lowerCase ? ' on update ' : ' ON UPDATE ') + foreignData.onUpdate
|
|
93
|
+
: '';
|
|
94
|
+
const onDelete = foreignData.onDelete
|
|
95
|
+
? (this.lowerCase ? ' on delete ' : ' ON DELETE ') + foreignData.onDelete
|
|
96
|
+
: '';
|
|
97
|
+
if (this.lowerCase) {
|
|
98
|
+
this.pushQuery(
|
|
99
|
+
(!this.forCreate ? `alter table ${this.tableName()} add ` : '') +
|
|
100
|
+
'constraint ' +
|
|
101
|
+
keyName +
|
|
102
|
+
' ' +
|
|
103
|
+
'foreign key (' +
|
|
104
|
+
column +
|
|
105
|
+
') references ' +
|
|
106
|
+
inTable +
|
|
107
|
+
' (' +
|
|
108
|
+
references +
|
|
109
|
+
')' +
|
|
110
|
+
onUpdate +
|
|
111
|
+
onDelete
|
|
112
|
+
);
|
|
113
|
+
} else {
|
|
114
|
+
this.pushQuery(
|
|
115
|
+
(!this.forCreate ? `ALTER TABLE ${this.tableName()} ADD ` : '') +
|
|
116
|
+
'CONSTRAINT ' +
|
|
117
|
+
keyName +
|
|
118
|
+
' ' +
|
|
119
|
+
'FOREIGN KEY (' +
|
|
120
|
+
column +
|
|
121
|
+
') REFERENCES ' +
|
|
122
|
+
inTable +
|
|
123
|
+
' (' +
|
|
124
|
+
references +
|
|
125
|
+
')' +
|
|
126
|
+
onUpdate +
|
|
127
|
+
onDelete
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// Get all of the column sql & bindings individually for building the table queries.
|
|
134
|
+
TableCompiler.prototype.getColumnTypes = (columns) =>
|
|
135
|
+
columns.reduce(
|
|
136
|
+
function (memo, columnSQL) {
|
|
137
|
+
const column = columnSQL[0];
|
|
138
|
+
memo.sql.push(column.sql);
|
|
139
|
+
memo.bindings.concat(column.bindings);
|
|
140
|
+
return memo;
|
|
141
|
+
},
|
|
142
|
+
{ sql: [], bindings: [] }
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
// Adds all of the additional queries from the "column"
|
|
146
|
+
TableCompiler.prototype.columnQueries = function (columns) {
|
|
147
|
+
const queries = columns.reduce(function (memo, columnSQL) {
|
|
148
|
+
const column = tail(columnSQL);
|
|
149
|
+
if (!isEmpty(column)) return memo.concat(column);
|
|
150
|
+
return memo;
|
|
151
|
+
}, []);
|
|
152
|
+
for (const q of queries) {
|
|
153
|
+
this.pushQuery(q);
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
// Add a new column.
|
|
158
|
+
TableCompiler.prototype.addColumnsPrefix = 'add column ';
|
|
159
|
+
|
|
160
|
+
// All of the columns to "add" for the query
|
|
161
|
+
TableCompiler.prototype.addColumns = function (columns, prefix) {
|
|
162
|
+
prefix = prefix || this.addColumnsPrefix;
|
|
163
|
+
|
|
164
|
+
if (columns.sql.length > 0) {
|
|
165
|
+
const columnSql = columns.sql.map((column) => {
|
|
166
|
+
return prefix + column;
|
|
167
|
+
});
|
|
168
|
+
this.pushQuery({
|
|
169
|
+
sql:
|
|
170
|
+
(this.lowerCase ? 'alter table ' : 'ALTER TABLE ') +
|
|
171
|
+
this.tableName() +
|
|
172
|
+
' ' +
|
|
173
|
+
columnSql.join(', '),
|
|
174
|
+
bindings: columns.bindings,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
// Alter column
|
|
180
|
+
TableCompiler.prototype.alterColumnsPrefix = 'alter column ';
|
|
181
|
+
|
|
182
|
+
TableCompiler.prototype.alterColumns = function (columns, colBuilders) {
|
|
183
|
+
if (columns.sql.length > 0) {
|
|
184
|
+
this.addColumns(columns, this.alterColumnsPrefix, colBuilders);
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
// Compile the columns as needed for the current create or alter table
|
|
189
|
+
TableCompiler.prototype.getColumns = function (method) {
|
|
190
|
+
const columns = this.grouped.columns || [];
|
|
191
|
+
method = method || 'add';
|
|
192
|
+
|
|
193
|
+
const queryContext = this.tableBuilder.queryContext();
|
|
194
|
+
|
|
195
|
+
return columns
|
|
196
|
+
.filter((column) => column.builder._method === method)
|
|
197
|
+
.map((column) => {
|
|
198
|
+
// pass queryContext down to columnBuilder but do not overwrite it if already set
|
|
199
|
+
if (
|
|
200
|
+
queryContext !== undefined &&
|
|
201
|
+
column.builder.queryContext() === undefined
|
|
202
|
+
) {
|
|
203
|
+
column.builder.queryContext(queryContext);
|
|
204
|
+
}
|
|
205
|
+
return this.client.columnCompiler(this, column.builder);
|
|
206
|
+
});
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
TableCompiler.prototype.tableName = function () {
|
|
210
|
+
const name = this.schemaNameRaw
|
|
211
|
+
? `${this.schemaNameRaw}.${this.tableNameRaw}`
|
|
212
|
+
: this.tableNameRaw;
|
|
213
|
+
|
|
214
|
+
return this.formatter.wrap(name);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
// Generate all of the alter column statements necessary for the query.
|
|
218
|
+
TableCompiler.prototype.alterTable = function () {
|
|
219
|
+
const alterTable = this.grouped.alterTable || [];
|
|
220
|
+
for (let i = 0, l = alterTable.length; i < l; i++) {
|
|
221
|
+
const statement = alterTable[i];
|
|
222
|
+
if (this[statement.method]) {
|
|
223
|
+
this[statement.method].apply(this, statement.args);
|
|
224
|
+
} else {
|
|
225
|
+
this.client.logger.error(`Debug: ${statement.method} does not exist`);
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
for (const item in this.single) {
|
|
229
|
+
if (typeof this[item] === 'function') this[item](this.single[item]);
|
|
230
|
+
}
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
TableCompiler.prototype.alterTableForCreate = function (columnTypes) {
|
|
234
|
+
this.forCreate = true;
|
|
235
|
+
const savedSequence = this.sequence;
|
|
236
|
+
const alterTable = this.grouped.alterTable || [];
|
|
237
|
+
this.grouped.alterTable = [];
|
|
238
|
+
for (let i = 0, l = alterTable.length; i < l; i++) {
|
|
239
|
+
const statement = alterTable[i];
|
|
240
|
+
if (indexOf(this.createAlterTableMethods, statement.method) < 0) {
|
|
241
|
+
this.grouped.alterTable.push(statement);
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
if (this[statement.method]) {
|
|
245
|
+
this.sequence = [];
|
|
246
|
+
this[statement.method].apply(this, statement.args);
|
|
247
|
+
columnTypes.sql.push(this.sequence[0].sql);
|
|
248
|
+
} else {
|
|
249
|
+
this.client.logger.error(`Debug: ${statement.method} does not exist`);
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
this.sequence = savedSequence;
|
|
253
|
+
this.forCreate = false;
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
// Drop the index on the current table.
|
|
257
|
+
TableCompiler.prototype.dropIndex = function (value) {
|
|
258
|
+
this.pushQuery(`drop index${value}`);
|
|
259
|
+
};
|
|
260
|
+
|
|
261
|
+
// Drop the unique
|
|
262
|
+
TableCompiler.prototype.dropUnique = TableCompiler.prototype.dropForeign = function () {
|
|
263
|
+
throw new Error('Method implemented in the dialect driver');
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
TableCompiler.prototype.dropColumnPrefix = 'drop column ';
|
|
267
|
+
TableCompiler.prototype.dropColumn = function () {
|
|
268
|
+
const columns = helpers.normalizeArr.apply(null, arguments);
|
|
269
|
+
const drops = (Array.isArray(columns) ? columns : [columns]).map((column) => {
|
|
270
|
+
return this.dropColumnPrefix + this.formatter.wrap(column);
|
|
271
|
+
});
|
|
272
|
+
this.pushQuery(
|
|
273
|
+
(this.lowerCase ? 'alter table ' : 'ALTER TABLE ') +
|
|
274
|
+
this.tableName() +
|
|
275
|
+
' ' +
|
|
276
|
+
drops.join(', ')
|
|
277
|
+
);
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
// If no name was specified for this index, we will create one using a basic
|
|
281
|
+
// convention of the table name, followed by the columns, followed by an
|
|
282
|
+
// index type, such as primary or index, which makes the index unique.
|
|
283
|
+
TableCompiler.prototype._indexCommand = function (type, tableName, columns) {
|
|
284
|
+
if (!Array.isArray(columns)) columns = columns ? [columns] : [];
|
|
285
|
+
const table = tableName.replace(/\.|-/g, '_');
|
|
286
|
+
const indexName = (
|
|
287
|
+
table +
|
|
288
|
+
'_' +
|
|
289
|
+
columns.join('_') +
|
|
290
|
+
'_' +
|
|
291
|
+
type
|
|
292
|
+
).toLowerCase();
|
|
293
|
+
return this.formatter.wrap(indexName);
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
module.exports = TableCompiler;
|