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,238 +1,238 @@
|
|
|
1
|
-
const { inherits } = require('util');
|
|
2
|
-
const TableCompiler = require('../../../schema/tablecompiler');
|
|
3
|
-
|
|
4
|
-
const filter = require('lodash/filter');
|
|
5
|
-
const values = require('lodash/values');
|
|
6
|
-
|
|
7
|
-
// Table Compiler
|
|
8
|
-
// -------
|
|
9
|
-
|
|
10
|
-
function TableCompiler_SQLite3() {
|
|
11
|
-
TableCompiler.apply(this, arguments);
|
|
12
|
-
this.primaryKey = void 0;
|
|
13
|
-
}
|
|
14
|
-
inherits(TableCompiler_SQLite3, TableCompiler);
|
|
15
|
-
|
|
16
|
-
// Create a new table.
|
|
17
|
-
TableCompiler_SQLite3.prototype.createQuery = function (columns, ifNot) {
|
|
18
|
-
const createStatement = ifNot
|
|
19
|
-
? 'create table if not exists '
|
|
20
|
-
: 'create table ';
|
|
21
|
-
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ');
|
|
22
|
-
|
|
23
|
-
// SQLite forces primary keys to be added when the table is initially created
|
|
24
|
-
// so we will need to check for a primary key commands and add the columns
|
|
25
|
-
// to the table's declaration here so they can be created on the tables.
|
|
26
|
-
sql += this.foreignKeys() || '';
|
|
27
|
-
sql += this.primaryKeys() || '';
|
|
28
|
-
sql += ')';
|
|
29
|
-
|
|
30
|
-
this.pushQuery(sql);
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
TableCompiler_SQLite3.prototype.addColumns = function (columns, prefix) {
|
|
34
|
-
if (prefix) {
|
|
35
|
-
throw new Error('Sqlite does not support alter column.');
|
|
36
|
-
}
|
|
37
|
-
for (let i = 0, l = columns.sql.length; i < l; i++) {
|
|
38
|
-
this.pushQuery({
|
|
39
|
-
sql: `alter table ${this.tableName()} add column ${columns.sql[i]}`,
|
|
40
|
-
bindings: columns.bindings[i],
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
// Compile a drop unique key command.
|
|
46
|
-
TableCompiler_SQLite3.prototype.dropUnique = function (columns, indexName) {
|
|
47
|
-
indexName = indexName
|
|
48
|
-
? this.formatter.wrap(indexName)
|
|
49
|
-
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
50
|
-
this.pushQuery(`drop index ${indexName}`);
|
|
51
|
-
};
|
|
52
|
-
|
|
53
|
-
// Compile a drop foreign key command.
|
|
54
|
-
TableCompiler_SQLite3.prototype.dropForeign = function (columns, indexName) {
|
|
55
|
-
const compiler = this;
|
|
56
|
-
|
|
57
|
-
this.pushQuery({
|
|
58
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
59
|
-
output(pragma) {
|
|
60
|
-
return compiler.client
|
|
61
|
-
.ddl(compiler, pragma, this.connection)
|
|
62
|
-
.dropForeign(columns, indexName);
|
|
63
|
-
},
|
|
64
|
-
});
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
// Compile a drop primary key command.
|
|
68
|
-
TableCompiler_SQLite3.prototype.dropPrimary = function (constraintName) {
|
|
69
|
-
const compiler = this;
|
|
70
|
-
|
|
71
|
-
this.pushQuery({
|
|
72
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
73
|
-
output(pragma) {
|
|
74
|
-
return compiler.client
|
|
75
|
-
.ddl(compiler, pragma, this.connection)
|
|
76
|
-
.dropPrimary(constraintName);
|
|
77
|
-
},
|
|
78
|
-
});
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
TableCompiler_SQLite3.prototype.dropIndex = function (columns, indexName) {
|
|
82
|
-
indexName = indexName
|
|
83
|
-
? this.formatter.wrap(indexName)
|
|
84
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
85
|
-
this.pushQuery(`drop index ${indexName}`);
|
|
86
|
-
};
|
|
87
|
-
|
|
88
|
-
// Compile a unique key command.
|
|
89
|
-
TableCompiler_SQLite3.prototype.unique = function (columns, indexName) {
|
|
90
|
-
indexName = indexName
|
|
91
|
-
? this.formatter.wrap(indexName)
|
|
92
|
-
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
93
|
-
columns = this.formatter.columnize(columns);
|
|
94
|
-
this.pushQuery(
|
|
95
|
-
`create unique index ${indexName} on ${this.tableName()} (${columns})`
|
|
96
|
-
);
|
|
97
|
-
};
|
|
98
|
-
|
|
99
|
-
// Compile a plain index key command.
|
|
100
|
-
TableCompiler_SQLite3.prototype.index = function (columns, indexName) {
|
|
101
|
-
indexName = indexName
|
|
102
|
-
? this.formatter.wrap(indexName)
|
|
103
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
104
|
-
columns = this.formatter.columnize(columns);
|
|
105
|
-
this.pushQuery(
|
|
106
|
-
`create index ${indexName} on ${this.tableName()} (${columns})`
|
|
107
|
-
);
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Add a primary key to an existing table.
|
|
112
|
-
*
|
|
113
|
-
* @NOTE The `createQuery` method above handles table creation. Don't do anything regarding table
|
|
114
|
-
* creation in this method
|
|
115
|
-
*
|
|
116
|
-
* @param {string | string[]} columns - Column name(s) to assign as primary keys
|
|
117
|
-
* @param {string} [constraintName] - Custom name for the PK constraint
|
|
118
|
-
*/
|
|
119
|
-
TableCompiler_SQLite3.prototype.primary = function (columns, constraintName) {
|
|
120
|
-
const compiler = this;
|
|
121
|
-
|
|
122
|
-
columns = this.formatter.columnize(columns);
|
|
123
|
-
columns = Array.isArray(columns) ? columns : [columns];
|
|
124
|
-
|
|
125
|
-
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
126
|
-
this.pushQuery({
|
|
127
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
128
|
-
output(pragma) {
|
|
129
|
-
return compiler.client
|
|
130
|
-
.ddl(compiler, pragma, this.connection)
|
|
131
|
-
.primary(columns, constraintName);
|
|
132
|
-
},
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Add a foreign key constraint to an existing table
|
|
139
|
-
*
|
|
140
|
-
* @NOTE The `createQuery` method above handles foreign key constraints on table creation. Don't do
|
|
141
|
-
* anything regarding table creation in this method
|
|
142
|
-
*
|
|
143
|
-
* @param {object} foreignInfo - Information about the current column foreign setup
|
|
144
|
-
* @param {string | string[]} [foreignInfo.column] - Column in the current constraint
|
|
145
|
-
* @param {string | undefined} foreignInfo.keyName - Name of the foreign key constraint
|
|
146
|
-
* @param {string} foreignInfo.references - What column it references in the other table
|
|
147
|
-
* @param {string} foreignInfo.inTable - What table is referenced in this constraint
|
|
148
|
-
* @param {string} [foreignInfo.onUpdate] - What to do on updates
|
|
149
|
-
* @param {string} [foreignInfo.onDelete] - What to do on deletions
|
|
150
|
-
*/
|
|
151
|
-
TableCompiler_SQLite3.prototype.foreign = function (foreignInfo) {
|
|
152
|
-
const compiler = this;
|
|
153
|
-
|
|
154
|
-
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
155
|
-
foreignInfo.column = this.formatter.columnize(foreignInfo.column);
|
|
156
|
-
foreignInfo.column = Array.isArray(foreignInfo.column)
|
|
157
|
-
? foreignInfo.column
|
|
158
|
-
: [foreignInfo.column];
|
|
159
|
-
foreignInfo.inTable = this.formatter.columnize(foreignInfo.inTable);
|
|
160
|
-
foreignInfo.references = this.formatter.columnize(foreignInfo.references);
|
|
161
|
-
|
|
162
|
-
this.pushQuery({
|
|
163
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
164
|
-
output(pragma) {
|
|
165
|
-
return compiler.client
|
|
166
|
-
.ddl(compiler, pragma, this.connection)
|
|
167
|
-
.foreign(foreignInfo);
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
}
|
|
171
|
-
};
|
|
172
|
-
|
|
173
|
-
TableCompiler_SQLite3.prototype.primaryKeys = function () {
|
|
174
|
-
const pks = filter(this.grouped.alterTable || [], { method: 'primary' });
|
|
175
|
-
if (pks.length > 0 && pks[0].args.length > 0) {
|
|
176
|
-
const columns = pks[0].args[0];
|
|
177
|
-
let constraintName = pks[0].args[1] || '';
|
|
178
|
-
if (constraintName) {
|
|
179
|
-
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
180
|
-
}
|
|
181
|
-
return `,${constraintName} primary key (${this.formatter.columnize(
|
|
182
|
-
columns
|
|
183
|
-
)})`;
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
|
|
187
|
-
TableCompiler_SQLite3.prototype.foreignKeys = function () {
|
|
188
|
-
let sql = '';
|
|
189
|
-
const foreignKeys = filter(this.grouped.alterTable || [], {
|
|
190
|
-
method: 'foreign',
|
|
191
|
-
});
|
|
192
|
-
for (let i = 0, l = foreignKeys.length; i < l; i++) {
|
|
193
|
-
const foreign = foreignKeys[i].args[0];
|
|
194
|
-
const column = this.formatter.columnize(foreign.column);
|
|
195
|
-
const references = this.formatter.columnize(foreign.references);
|
|
196
|
-
const foreignTable = this.formatter.wrap(foreign.inTable);
|
|
197
|
-
let constraintName = foreign.keyName || '';
|
|
198
|
-
if (constraintName) {
|
|
199
|
-
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
200
|
-
}
|
|
201
|
-
sql += `,${constraintName} foreign key(${column}) references ${foreignTable}(${references})`;
|
|
202
|
-
if (foreign.onDelete) sql += ` on delete ${foreign.onDelete}`;
|
|
203
|
-
if (foreign.onUpdate) sql += ` on update ${foreign.onUpdate}`;
|
|
204
|
-
}
|
|
205
|
-
return sql;
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
TableCompiler_SQLite3.prototype.createTableBlock = function () {
|
|
209
|
-
return this.getColumns().concat().join(',');
|
|
210
|
-
};
|
|
211
|
-
|
|
212
|
-
// Compile a rename column command... very complex in sqlite
|
|
213
|
-
TableCompiler_SQLite3.prototype.renameColumn = function (from, to) {
|
|
214
|
-
const compiler = this;
|
|
215
|
-
this.pushQuery({
|
|
216
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
217
|
-
output(pragma) {
|
|
218
|
-
return compiler.client
|
|
219
|
-
.ddl(compiler, pragma, this.connection)
|
|
220
|
-
.renameColumn(from, to);
|
|
221
|
-
},
|
|
222
|
-
});
|
|
223
|
-
};
|
|
224
|
-
|
|
225
|
-
TableCompiler_SQLite3.prototype.dropColumn = function () {
|
|
226
|
-
const compiler = this;
|
|
227
|
-
const columns = values(arguments);
|
|
228
|
-
this.pushQuery({
|
|
229
|
-
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
230
|
-
output(pragma) {
|
|
231
|
-
return compiler.client
|
|
232
|
-
.ddl(compiler, pragma, this.connection)
|
|
233
|
-
.dropColumn(columns);
|
|
234
|
-
},
|
|
235
|
-
});
|
|
236
|
-
};
|
|
237
|
-
|
|
238
|
-
module.exports = TableCompiler_SQLite3;
|
|
1
|
+
const { inherits } = require('util');
|
|
2
|
+
const TableCompiler = require('../../../schema/tablecompiler');
|
|
3
|
+
|
|
4
|
+
const filter = require('lodash/filter');
|
|
5
|
+
const values = require('lodash/values');
|
|
6
|
+
|
|
7
|
+
// Table Compiler
|
|
8
|
+
// -------
|
|
9
|
+
|
|
10
|
+
function TableCompiler_SQLite3() {
|
|
11
|
+
TableCompiler.apply(this, arguments);
|
|
12
|
+
this.primaryKey = void 0;
|
|
13
|
+
}
|
|
14
|
+
inherits(TableCompiler_SQLite3, TableCompiler);
|
|
15
|
+
|
|
16
|
+
// Create a new table.
|
|
17
|
+
TableCompiler_SQLite3.prototype.createQuery = function (columns, ifNot) {
|
|
18
|
+
const createStatement = ifNot
|
|
19
|
+
? 'create table if not exists '
|
|
20
|
+
: 'create table ';
|
|
21
|
+
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ');
|
|
22
|
+
|
|
23
|
+
// SQLite forces primary keys to be added when the table is initially created
|
|
24
|
+
// so we will need to check for a primary key commands and add the columns
|
|
25
|
+
// to the table's declaration here so they can be created on the tables.
|
|
26
|
+
sql += this.foreignKeys() || '';
|
|
27
|
+
sql += this.primaryKeys() || '';
|
|
28
|
+
sql += ')';
|
|
29
|
+
|
|
30
|
+
this.pushQuery(sql);
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
TableCompiler_SQLite3.prototype.addColumns = function (columns, prefix) {
|
|
34
|
+
if (prefix) {
|
|
35
|
+
throw new Error('Sqlite does not support alter column.');
|
|
36
|
+
}
|
|
37
|
+
for (let i = 0, l = columns.sql.length; i < l; i++) {
|
|
38
|
+
this.pushQuery({
|
|
39
|
+
sql: `alter table ${this.tableName()} add column ${columns.sql[i]}`,
|
|
40
|
+
bindings: columns.bindings[i],
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
// Compile a drop unique key command.
|
|
46
|
+
TableCompiler_SQLite3.prototype.dropUnique = function (columns, indexName) {
|
|
47
|
+
indexName = indexName
|
|
48
|
+
? this.formatter.wrap(indexName)
|
|
49
|
+
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
50
|
+
this.pushQuery(`drop index ${indexName}`);
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Compile a drop foreign key command.
|
|
54
|
+
TableCompiler_SQLite3.prototype.dropForeign = function (columns, indexName) {
|
|
55
|
+
const compiler = this;
|
|
56
|
+
|
|
57
|
+
this.pushQuery({
|
|
58
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
59
|
+
output(pragma) {
|
|
60
|
+
return compiler.client
|
|
61
|
+
.ddl(compiler, pragma, this.connection)
|
|
62
|
+
.dropForeign(columns, indexName);
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
// Compile a drop primary key command.
|
|
68
|
+
TableCompiler_SQLite3.prototype.dropPrimary = function (constraintName) {
|
|
69
|
+
const compiler = this;
|
|
70
|
+
|
|
71
|
+
this.pushQuery({
|
|
72
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
73
|
+
output(pragma) {
|
|
74
|
+
return compiler.client
|
|
75
|
+
.ddl(compiler, pragma, this.connection)
|
|
76
|
+
.dropPrimary(constraintName);
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
TableCompiler_SQLite3.prototype.dropIndex = function (columns, indexName) {
|
|
82
|
+
indexName = indexName
|
|
83
|
+
? this.formatter.wrap(indexName)
|
|
84
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
85
|
+
this.pushQuery(`drop index ${indexName}`);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// Compile a unique key command.
|
|
89
|
+
TableCompiler_SQLite3.prototype.unique = function (columns, indexName) {
|
|
90
|
+
indexName = indexName
|
|
91
|
+
? this.formatter.wrap(indexName)
|
|
92
|
+
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
93
|
+
columns = this.formatter.columnize(columns);
|
|
94
|
+
this.pushQuery(
|
|
95
|
+
`create unique index ${indexName} on ${this.tableName()} (${columns})`
|
|
96
|
+
);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
// Compile a plain index key command.
|
|
100
|
+
TableCompiler_SQLite3.prototype.index = function (columns, indexName) {
|
|
101
|
+
indexName = indexName
|
|
102
|
+
? this.formatter.wrap(indexName)
|
|
103
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
104
|
+
columns = this.formatter.columnize(columns);
|
|
105
|
+
this.pushQuery(
|
|
106
|
+
`create index ${indexName} on ${this.tableName()} (${columns})`
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Add a primary key to an existing table.
|
|
112
|
+
*
|
|
113
|
+
* @NOTE The `createQuery` method above handles table creation. Don't do anything regarding table
|
|
114
|
+
* creation in this method
|
|
115
|
+
*
|
|
116
|
+
* @param {string | string[]} columns - Column name(s) to assign as primary keys
|
|
117
|
+
* @param {string} [constraintName] - Custom name for the PK constraint
|
|
118
|
+
*/
|
|
119
|
+
TableCompiler_SQLite3.prototype.primary = function (columns, constraintName) {
|
|
120
|
+
const compiler = this;
|
|
121
|
+
|
|
122
|
+
columns = this.formatter.columnize(columns);
|
|
123
|
+
columns = Array.isArray(columns) ? columns : [columns];
|
|
124
|
+
|
|
125
|
+
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
126
|
+
this.pushQuery({
|
|
127
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
128
|
+
output(pragma) {
|
|
129
|
+
return compiler.client
|
|
130
|
+
.ddl(compiler, pragma, this.connection)
|
|
131
|
+
.primary(columns, constraintName);
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* Add a foreign key constraint to an existing table
|
|
139
|
+
*
|
|
140
|
+
* @NOTE The `createQuery` method above handles foreign key constraints on table creation. Don't do
|
|
141
|
+
* anything regarding table creation in this method
|
|
142
|
+
*
|
|
143
|
+
* @param {object} foreignInfo - Information about the current column foreign setup
|
|
144
|
+
* @param {string | string[]} [foreignInfo.column] - Column in the current constraint
|
|
145
|
+
* @param {string | undefined} foreignInfo.keyName - Name of the foreign key constraint
|
|
146
|
+
* @param {string} foreignInfo.references - What column it references in the other table
|
|
147
|
+
* @param {string} foreignInfo.inTable - What table is referenced in this constraint
|
|
148
|
+
* @param {string} [foreignInfo.onUpdate] - What to do on updates
|
|
149
|
+
* @param {string} [foreignInfo.onDelete] - What to do on deletions
|
|
150
|
+
*/
|
|
151
|
+
TableCompiler_SQLite3.prototype.foreign = function (foreignInfo) {
|
|
152
|
+
const compiler = this;
|
|
153
|
+
|
|
154
|
+
if (this.method !== 'create' && this.method !== 'createIfNot') {
|
|
155
|
+
foreignInfo.column = this.formatter.columnize(foreignInfo.column);
|
|
156
|
+
foreignInfo.column = Array.isArray(foreignInfo.column)
|
|
157
|
+
? foreignInfo.column
|
|
158
|
+
: [foreignInfo.column];
|
|
159
|
+
foreignInfo.inTable = this.formatter.columnize(foreignInfo.inTable);
|
|
160
|
+
foreignInfo.references = this.formatter.columnize(foreignInfo.references);
|
|
161
|
+
|
|
162
|
+
this.pushQuery({
|
|
163
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
164
|
+
output(pragma) {
|
|
165
|
+
return compiler.client
|
|
166
|
+
.ddl(compiler, pragma, this.connection)
|
|
167
|
+
.foreign(foreignInfo);
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
TableCompiler_SQLite3.prototype.primaryKeys = function () {
|
|
174
|
+
const pks = filter(this.grouped.alterTable || [], { method: 'primary' });
|
|
175
|
+
if (pks.length > 0 && pks[0].args.length > 0) {
|
|
176
|
+
const columns = pks[0].args[0];
|
|
177
|
+
let constraintName = pks[0].args[1] || '';
|
|
178
|
+
if (constraintName) {
|
|
179
|
+
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
180
|
+
}
|
|
181
|
+
return `,${constraintName} primary key (${this.formatter.columnize(
|
|
182
|
+
columns
|
|
183
|
+
)})`;
|
|
184
|
+
}
|
|
185
|
+
};
|
|
186
|
+
|
|
187
|
+
TableCompiler_SQLite3.prototype.foreignKeys = function () {
|
|
188
|
+
let sql = '';
|
|
189
|
+
const foreignKeys = filter(this.grouped.alterTable || [], {
|
|
190
|
+
method: 'foreign',
|
|
191
|
+
});
|
|
192
|
+
for (let i = 0, l = foreignKeys.length; i < l; i++) {
|
|
193
|
+
const foreign = foreignKeys[i].args[0];
|
|
194
|
+
const column = this.formatter.columnize(foreign.column);
|
|
195
|
+
const references = this.formatter.columnize(foreign.references);
|
|
196
|
+
const foreignTable = this.formatter.wrap(foreign.inTable);
|
|
197
|
+
let constraintName = foreign.keyName || '';
|
|
198
|
+
if (constraintName) {
|
|
199
|
+
constraintName = ' constraint ' + this.formatter.wrap(constraintName);
|
|
200
|
+
}
|
|
201
|
+
sql += `,${constraintName} foreign key(${column}) references ${foreignTable}(${references})`;
|
|
202
|
+
if (foreign.onDelete) sql += ` on delete ${foreign.onDelete}`;
|
|
203
|
+
if (foreign.onUpdate) sql += ` on update ${foreign.onUpdate}`;
|
|
204
|
+
}
|
|
205
|
+
return sql;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
TableCompiler_SQLite3.prototype.createTableBlock = function () {
|
|
209
|
+
return this.getColumns().concat().join(',');
|
|
210
|
+
};
|
|
211
|
+
|
|
212
|
+
// Compile a rename column command... very complex in sqlite
|
|
213
|
+
TableCompiler_SQLite3.prototype.renameColumn = function (from, to) {
|
|
214
|
+
const compiler = this;
|
|
215
|
+
this.pushQuery({
|
|
216
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
217
|
+
output(pragma) {
|
|
218
|
+
return compiler.client
|
|
219
|
+
.ddl(compiler, pragma, this.connection)
|
|
220
|
+
.renameColumn(from, to);
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
};
|
|
224
|
+
|
|
225
|
+
TableCompiler_SQLite3.prototype.dropColumn = function () {
|
|
226
|
+
const compiler = this;
|
|
227
|
+
const columns = values(arguments);
|
|
228
|
+
this.pushQuery({
|
|
229
|
+
sql: `PRAGMA table_info(${this.tableName()})`,
|
|
230
|
+
output(pragma) {
|
|
231
|
+
return compiler.client
|
|
232
|
+
.ddl(compiler, pragma, this.connection)
|
|
233
|
+
.dropColumn(columns);
|
|
234
|
+
},
|
|
235
|
+
});
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
module.exports = TableCompiler_SQLite3;
|