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,60 +1,60 @@
|
|
|
1
|
-
// MySQL Schema Compiler
|
|
2
|
-
// -------
|
|
3
|
-
const { inherits } = require('util');
|
|
4
|
-
const SchemaCompiler = require('../../../schema/compiler');
|
|
5
|
-
|
|
6
|
-
const some = require('lodash/some');
|
|
7
|
-
|
|
8
|
-
function SchemaCompiler_MySQL(client, builder) {
|
|
9
|
-
SchemaCompiler.call(this, client, builder);
|
|
10
|
-
}
|
|
11
|
-
inherits(SchemaCompiler_MySQL, SchemaCompiler);
|
|
12
|
-
|
|
13
|
-
Object.assign(SchemaCompiler_MySQL.prototype, {
|
|
14
|
-
// Rename a table on the schema.
|
|
15
|
-
renameTable(tableName, to) {
|
|
16
|
-
this.pushQuery(
|
|
17
|
-
`rename table ${this.formatter.wrap(tableName)} to ${this.formatter.wrap(
|
|
18
|
-
to
|
|
19
|
-
)}`
|
|
20
|
-
);
|
|
21
|
-
},
|
|
22
|
-
|
|
23
|
-
// Check whether a table exists on the query.
|
|
24
|
-
hasTable(tableName) {
|
|
25
|
-
let sql = 'select * from information_schema.tables where table_name = ?';
|
|
26
|
-
const bindings = [tableName];
|
|
27
|
-
|
|
28
|
-
if (this.schema) {
|
|
29
|
-
sql += ' and table_schema = ?';
|
|
30
|
-
bindings.push(this.schema);
|
|
31
|
-
} else {
|
|
32
|
-
sql += ' and table_schema = database()';
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
this.pushQuery({
|
|
36
|
-
sql,
|
|
37
|
-
bindings,
|
|
38
|
-
output: function output(resp) {
|
|
39
|
-
return resp.length > 0;
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
},
|
|
43
|
-
|
|
44
|
-
// Check whether a column exists on the schema.
|
|
45
|
-
hasColumn(tableName, column) {
|
|
46
|
-
this.pushQuery({
|
|
47
|
-
sql: `show columns from ${this.formatter.wrap(tableName)}`,
|
|
48
|
-
output(resp) {
|
|
49
|
-
return some(resp, (row) => {
|
|
50
|
-
return (
|
|
51
|
-
this.client.wrapIdentifier(row.Field) ===
|
|
52
|
-
this.client.wrapIdentifier(column)
|
|
53
|
-
);
|
|
54
|
-
});
|
|
55
|
-
},
|
|
56
|
-
});
|
|
57
|
-
},
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
module.exports = SchemaCompiler_MySQL;
|
|
1
|
+
// MySQL Schema Compiler
|
|
2
|
+
// -------
|
|
3
|
+
const { inherits } = require('util');
|
|
4
|
+
const SchemaCompiler = require('../../../schema/compiler');
|
|
5
|
+
|
|
6
|
+
const some = require('lodash/some');
|
|
7
|
+
|
|
8
|
+
function SchemaCompiler_MySQL(client, builder) {
|
|
9
|
+
SchemaCompiler.call(this, client, builder);
|
|
10
|
+
}
|
|
11
|
+
inherits(SchemaCompiler_MySQL, SchemaCompiler);
|
|
12
|
+
|
|
13
|
+
Object.assign(SchemaCompiler_MySQL.prototype, {
|
|
14
|
+
// Rename a table on the schema.
|
|
15
|
+
renameTable(tableName, to) {
|
|
16
|
+
this.pushQuery(
|
|
17
|
+
`rename table ${this.formatter.wrap(tableName)} to ${this.formatter.wrap(
|
|
18
|
+
to
|
|
19
|
+
)}`
|
|
20
|
+
);
|
|
21
|
+
},
|
|
22
|
+
|
|
23
|
+
// Check whether a table exists on the query.
|
|
24
|
+
hasTable(tableName) {
|
|
25
|
+
let sql = 'select * from information_schema.tables where table_name = ?';
|
|
26
|
+
const bindings = [tableName];
|
|
27
|
+
|
|
28
|
+
if (this.schema) {
|
|
29
|
+
sql += ' and table_schema = ?';
|
|
30
|
+
bindings.push(this.schema);
|
|
31
|
+
} else {
|
|
32
|
+
sql += ' and table_schema = database()';
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
this.pushQuery({
|
|
36
|
+
sql,
|
|
37
|
+
bindings,
|
|
38
|
+
output: function output(resp) {
|
|
39
|
+
return resp.length > 0;
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
// Check whether a column exists on the schema.
|
|
45
|
+
hasColumn(tableName, column) {
|
|
46
|
+
this.pushQuery({
|
|
47
|
+
sql: `show columns from ${this.formatter.wrap(tableName)}`,
|
|
48
|
+
output(resp) {
|
|
49
|
+
return some(resp, (row) => {
|
|
50
|
+
return (
|
|
51
|
+
this.client.wrapIdentifier(row.Field) ===
|
|
52
|
+
this.client.wrapIdentifier(column)
|
|
53
|
+
);
|
|
54
|
+
});
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
module.exports = SchemaCompiler_MySQL;
|
|
@@ -1,262 +1,262 @@
|
|
|
1
|
-
/* eslint max-len:0*/
|
|
2
|
-
|
|
3
|
-
// MySQL Table Builder & Compiler
|
|
4
|
-
// -------
|
|
5
|
-
const { inherits } = require('util');
|
|
6
|
-
const TableCompiler = require('../../../schema/tablecompiler');
|
|
7
|
-
|
|
8
|
-
// Table Compiler
|
|
9
|
-
// ------
|
|
10
|
-
|
|
11
|
-
function TableCompiler_MySQL() {
|
|
12
|
-
TableCompiler.apply(this, arguments);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
inherits(TableCompiler_MySQL, TableCompiler);
|
|
16
|
-
|
|
17
|
-
Object.assign(TableCompiler_MySQL.prototype, {
|
|
18
|
-
createQuery(columns, ifNot) {
|
|
19
|
-
const createStatement = ifNot
|
|
20
|
-
? 'create table if not exists '
|
|
21
|
-
: 'create table ';
|
|
22
|
-
const { client } = this;
|
|
23
|
-
let conn = {};
|
|
24
|
-
let sql =
|
|
25
|
-
createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
|
26
|
-
|
|
27
|
-
// Check if the connection settings are set.
|
|
28
|
-
if (client.connectionSettings) {
|
|
29
|
-
conn = client.connectionSettings;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
const charset = this.single.charset || conn.charset || '';
|
|
33
|
-
const collation = this.single.collate || conn.collate || '';
|
|
34
|
-
const engine = this.single.engine || '';
|
|
35
|
-
|
|
36
|
-
// var conn = builder.client.connectionSettings;
|
|
37
|
-
if (charset) sql += ` default character set ${charset}`;
|
|
38
|
-
if (collation) sql += ` collate ${collation}`;
|
|
39
|
-
if (engine) sql += ` engine = ${engine}`;
|
|
40
|
-
|
|
41
|
-
if (this.single.comment) {
|
|
42
|
-
const comment = this.single.comment || '';
|
|
43
|
-
if (comment.length > 60)
|
|
44
|
-
this.client.logger.warn(
|
|
45
|
-
'The max length for a table comment is 60 characters'
|
|
46
|
-
);
|
|
47
|
-
sql += ` comment = '${comment}'`;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
this.pushQuery(sql);
|
|
51
|
-
},
|
|
52
|
-
|
|
53
|
-
addColumnsPrefix: 'add ',
|
|
54
|
-
|
|
55
|
-
alterColumnsPrefix: 'modify ',
|
|
56
|
-
|
|
57
|
-
dropColumnPrefix: 'drop ',
|
|
58
|
-
|
|
59
|
-
// Compiles the comment on the table.
|
|
60
|
-
comment(comment) {
|
|
61
|
-
this.pushQuery(`alter table ${this.tableName()} comment = '${comment}'`);
|
|
62
|
-
},
|
|
63
|
-
|
|
64
|
-
changeType() {
|
|
65
|
-
// alter table + table + ' modify ' + wrapped + '// type';
|
|
66
|
-
},
|
|
67
|
-
|
|
68
|
-
// Renames a column on the table.
|
|
69
|
-
renameColumn(from, to) {
|
|
70
|
-
const compiler = this;
|
|
71
|
-
const table = this.tableName();
|
|
72
|
-
const wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
|
|
73
|
-
|
|
74
|
-
this.pushQuery({
|
|
75
|
-
sql:
|
|
76
|
-
`show fields from ${table} where field = ` +
|
|
77
|
-
this.formatter.parameter(from),
|
|
78
|
-
output(resp) {
|
|
79
|
-
const column = resp[0];
|
|
80
|
-
const runner = this;
|
|
81
|
-
return compiler.getFKRefs(runner).then(([refs]) =>
|
|
82
|
-
new Promise((resolve, reject) => {
|
|
83
|
-
try {
|
|
84
|
-
if (!refs.length) {
|
|
85
|
-
resolve();
|
|
86
|
-
}
|
|
87
|
-
resolve(compiler.dropFKRefs(runner, refs));
|
|
88
|
-
} catch (e) {
|
|
89
|
-
reject(e);
|
|
90
|
-
}
|
|
91
|
-
})
|
|
92
|
-
.then(function () {
|
|
93
|
-
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;
|
|
94
|
-
|
|
95
|
-
if (String(column.Null).toUpperCase() !== 'YES') {
|
|
96
|
-
sql += ` NOT NULL`;
|
|
97
|
-
} else {
|
|
98
|
-
// This doesn't matter for most cases except Timestamp, where this is important
|
|
99
|
-
sql += ` NULL`;
|
|
100
|
-
}
|
|
101
|
-
if (column.Default !== void 0 && column.Default !== null) {
|
|
102
|
-
sql += ` DEFAULT '${column.Default}'`;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
return runner.query({
|
|
106
|
-
sql,
|
|
107
|
-
});
|
|
108
|
-
})
|
|
109
|
-
.then(function () {
|
|
110
|
-
if (!refs.length) {
|
|
111
|
-
return;
|
|
112
|
-
}
|
|
113
|
-
return compiler.createFKRefs(
|
|
114
|
-
runner,
|
|
115
|
-
refs.map(function (ref) {
|
|
116
|
-
if (ref.REFERENCED_COLUMN_NAME === from) {
|
|
117
|
-
ref.REFERENCED_COLUMN_NAME = to;
|
|
118
|
-
}
|
|
119
|
-
if (ref.COLUMN_NAME === from) {
|
|
120
|
-
ref.COLUMN_NAME = to;
|
|
121
|
-
}
|
|
122
|
-
return ref;
|
|
123
|
-
})
|
|
124
|
-
);
|
|
125
|
-
})
|
|
126
|
-
);
|
|
127
|
-
},
|
|
128
|
-
});
|
|
129
|
-
},
|
|
130
|
-
|
|
131
|
-
getFKRefs(runner) {
|
|
132
|
-
const formatter = this.client.formatter(this.tableBuilder);
|
|
133
|
-
const sql =
|
|
134
|
-
'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, ' +
|
|
135
|
-
' KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, ' +
|
|
136
|
-
' RC.UPDATE_RULE, RC.DELETE_RULE ' +
|
|
137
|
-
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ' +
|
|
138
|
-
'JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ' +
|
|
139
|
-
' USING(CONSTRAINT_NAME)' +
|
|
140
|
-
'WHERE KCU.REFERENCED_TABLE_NAME = ' +
|
|
141
|
-
formatter.parameter(this.tableNameRaw) +
|
|
142
|
-
' ' +
|
|
143
|
-
' AND KCU.CONSTRAINT_SCHEMA = ' +
|
|
144
|
-
formatter.parameter(this.client.database()) +
|
|
145
|
-
' ' +
|
|
146
|
-
' AND RC.CONSTRAINT_SCHEMA = ' +
|
|
147
|
-
formatter.parameter(this.client.database());
|
|
148
|
-
|
|
149
|
-
return runner.query({
|
|
150
|
-
sql,
|
|
151
|
-
bindings: formatter.bindings,
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
|
|
155
|
-
dropFKRefs(runner, refs) {
|
|
156
|
-
const formatter = this.client.formatter(this.tableBuilder);
|
|
157
|
-
|
|
158
|
-
return Promise.all(
|
|
159
|
-
refs.map(function (ref) {
|
|
160
|
-
const constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
161
|
-
const tableName = formatter.wrap(ref.TABLE_NAME);
|
|
162
|
-
return runner.query({
|
|
163
|
-
sql: `alter table ${tableName} drop foreign key ${constraintName}`,
|
|
164
|
-
});
|
|
165
|
-
})
|
|
166
|
-
);
|
|
167
|
-
},
|
|
168
|
-
createFKRefs(runner, refs) {
|
|
169
|
-
const formatter = this.client.formatter(this.tableBuilder);
|
|
170
|
-
|
|
171
|
-
return Promise.all(
|
|
172
|
-
refs.map(function (ref) {
|
|
173
|
-
const tableName = formatter.wrap(ref.TABLE_NAME);
|
|
174
|
-
const keyName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
175
|
-
const column = formatter.columnize(ref.COLUMN_NAME);
|
|
176
|
-
const references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
|
|
177
|
-
const inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
|
178
|
-
const onUpdate = ` ON UPDATE ${ref.UPDATE_RULE}`;
|
|
179
|
-
const onDelete = ` ON DELETE ${ref.DELETE_RULE}`;
|
|
180
|
-
|
|
181
|
-
return runner.query({
|
|
182
|
-
sql:
|
|
183
|
-
`alter table ${tableName} add constraint ${keyName} ` +
|
|
184
|
-
'foreign key (' +
|
|
185
|
-
column +
|
|
186
|
-
') references ' +
|
|
187
|
-
inTable +
|
|
188
|
-
' (' +
|
|
189
|
-
references +
|
|
190
|
-
')' +
|
|
191
|
-
onUpdate +
|
|
192
|
-
onDelete,
|
|
193
|
-
});
|
|
194
|
-
})
|
|
195
|
-
);
|
|
196
|
-
},
|
|
197
|
-
index(columns, indexName, indexType) {
|
|
198
|
-
indexName = indexName
|
|
199
|
-
? this.formatter.wrap(indexName)
|
|
200
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
201
|
-
this.pushQuery(
|
|
202
|
-
`alter table ${this.tableName()} add${
|
|
203
|
-
indexType ? ` ${indexType}` : ''
|
|
204
|
-
} index ${indexName}(${this.formatter.columnize(columns)})`
|
|
205
|
-
);
|
|
206
|
-
},
|
|
207
|
-
|
|
208
|
-
primary(columns, constraintName) {
|
|
209
|
-
constraintName = constraintName
|
|
210
|
-
? this.formatter.wrap(constraintName)
|
|
211
|
-
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
|
212
|
-
this.pushQuery(
|
|
213
|
-
`alter table ${this.tableName()} add primary key ${constraintName}(${this.formatter.columnize(
|
|
214
|
-
columns
|
|
215
|
-
)})`
|
|
216
|
-
);
|
|
217
|
-
},
|
|
218
|
-
|
|
219
|
-
unique(columns, indexName) {
|
|
220
|
-
indexName = indexName
|
|
221
|
-
? this.formatter.wrap(indexName)
|
|
222
|
-
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
223
|
-
this.pushQuery(
|
|
224
|
-
`alter table ${this.tableName()} add unique ${indexName}(${this.formatter.columnize(
|
|
225
|
-
columns
|
|
226
|
-
)})`
|
|
227
|
-
);
|
|
228
|
-
},
|
|
229
|
-
|
|
230
|
-
// Compile a drop index command.
|
|
231
|
-
dropIndex(columns, indexName) {
|
|
232
|
-
indexName = indexName
|
|
233
|
-
? this.formatter.wrap(indexName)
|
|
234
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
235
|
-
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
|
236
|
-
},
|
|
237
|
-
|
|
238
|
-
// Compile a drop foreign key command.
|
|
239
|
-
dropForeign(columns, indexName) {
|
|
240
|
-
indexName = indexName
|
|
241
|
-
? this.formatter.wrap(indexName)
|
|
242
|
-
: this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
243
|
-
this.pushQuery(
|
|
244
|
-
`alter table ${this.tableName()} drop foreign key ${indexName}`
|
|
245
|
-
);
|
|
246
|
-
},
|
|
247
|
-
|
|
248
|
-
// Compile a drop primary key command.
|
|
249
|
-
dropPrimary() {
|
|
250
|
-
this.pushQuery(`alter table ${this.tableName()} drop primary key`);
|
|
251
|
-
},
|
|
252
|
-
|
|
253
|
-
// Compile a drop unique key command.
|
|
254
|
-
dropUnique(column, indexName) {
|
|
255
|
-
indexName = indexName
|
|
256
|
-
? this.formatter.wrap(indexName)
|
|
257
|
-
: this._indexCommand('unique', this.tableNameRaw, column);
|
|
258
|
-
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
|
259
|
-
},
|
|
260
|
-
});
|
|
261
|
-
|
|
262
|
-
module.exports = TableCompiler_MySQL;
|
|
1
|
+
/* eslint max-len:0*/
|
|
2
|
+
|
|
3
|
+
// MySQL Table Builder & Compiler
|
|
4
|
+
// -------
|
|
5
|
+
const { inherits } = require('util');
|
|
6
|
+
const TableCompiler = require('../../../schema/tablecompiler');
|
|
7
|
+
|
|
8
|
+
// Table Compiler
|
|
9
|
+
// ------
|
|
10
|
+
|
|
11
|
+
function TableCompiler_MySQL() {
|
|
12
|
+
TableCompiler.apply(this, arguments);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
inherits(TableCompiler_MySQL, TableCompiler);
|
|
16
|
+
|
|
17
|
+
Object.assign(TableCompiler_MySQL.prototype, {
|
|
18
|
+
createQuery(columns, ifNot) {
|
|
19
|
+
const createStatement = ifNot
|
|
20
|
+
? 'create table if not exists '
|
|
21
|
+
: 'create table ';
|
|
22
|
+
const { client } = this;
|
|
23
|
+
let conn = {};
|
|
24
|
+
let sql =
|
|
25
|
+
createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
|
26
|
+
|
|
27
|
+
// Check if the connection settings are set.
|
|
28
|
+
if (client.connectionSettings) {
|
|
29
|
+
conn = client.connectionSettings;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const charset = this.single.charset || conn.charset || '';
|
|
33
|
+
const collation = this.single.collate || conn.collate || '';
|
|
34
|
+
const engine = this.single.engine || '';
|
|
35
|
+
|
|
36
|
+
// var conn = builder.client.connectionSettings;
|
|
37
|
+
if (charset) sql += ` default character set ${charset}`;
|
|
38
|
+
if (collation) sql += ` collate ${collation}`;
|
|
39
|
+
if (engine) sql += ` engine = ${engine}`;
|
|
40
|
+
|
|
41
|
+
if (this.single.comment) {
|
|
42
|
+
const comment = this.single.comment || '';
|
|
43
|
+
if (comment.length > 60)
|
|
44
|
+
this.client.logger.warn(
|
|
45
|
+
'The max length for a table comment is 60 characters'
|
|
46
|
+
);
|
|
47
|
+
sql += ` comment = '${comment}'`;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
this.pushQuery(sql);
|
|
51
|
+
},
|
|
52
|
+
|
|
53
|
+
addColumnsPrefix: 'add ',
|
|
54
|
+
|
|
55
|
+
alterColumnsPrefix: 'modify ',
|
|
56
|
+
|
|
57
|
+
dropColumnPrefix: 'drop ',
|
|
58
|
+
|
|
59
|
+
// Compiles the comment on the table.
|
|
60
|
+
comment(comment) {
|
|
61
|
+
this.pushQuery(`alter table ${this.tableName()} comment = '${comment}'`);
|
|
62
|
+
},
|
|
63
|
+
|
|
64
|
+
changeType() {
|
|
65
|
+
// alter table + table + ' modify ' + wrapped + '// type';
|
|
66
|
+
},
|
|
67
|
+
|
|
68
|
+
// Renames a column on the table.
|
|
69
|
+
renameColumn(from, to) {
|
|
70
|
+
const compiler = this;
|
|
71
|
+
const table = this.tableName();
|
|
72
|
+
const wrapped = this.formatter.wrap(from) + ' ' + this.formatter.wrap(to);
|
|
73
|
+
|
|
74
|
+
this.pushQuery({
|
|
75
|
+
sql:
|
|
76
|
+
`show fields from ${table} where field = ` +
|
|
77
|
+
this.formatter.parameter(from),
|
|
78
|
+
output(resp) {
|
|
79
|
+
const column = resp[0];
|
|
80
|
+
const runner = this;
|
|
81
|
+
return compiler.getFKRefs(runner).then(([refs]) =>
|
|
82
|
+
new Promise((resolve, reject) => {
|
|
83
|
+
try {
|
|
84
|
+
if (!refs.length) {
|
|
85
|
+
resolve();
|
|
86
|
+
}
|
|
87
|
+
resolve(compiler.dropFKRefs(runner, refs));
|
|
88
|
+
} catch (e) {
|
|
89
|
+
reject(e);
|
|
90
|
+
}
|
|
91
|
+
})
|
|
92
|
+
.then(function () {
|
|
93
|
+
let sql = `alter table ${table} change ${wrapped} ${column.Type}`;
|
|
94
|
+
|
|
95
|
+
if (String(column.Null).toUpperCase() !== 'YES') {
|
|
96
|
+
sql += ` NOT NULL`;
|
|
97
|
+
} else {
|
|
98
|
+
// This doesn't matter for most cases except Timestamp, where this is important
|
|
99
|
+
sql += ` NULL`;
|
|
100
|
+
}
|
|
101
|
+
if (column.Default !== void 0 && column.Default !== null) {
|
|
102
|
+
sql += ` DEFAULT '${column.Default}'`;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return runner.query({
|
|
106
|
+
sql,
|
|
107
|
+
});
|
|
108
|
+
})
|
|
109
|
+
.then(function () {
|
|
110
|
+
if (!refs.length) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
return compiler.createFKRefs(
|
|
114
|
+
runner,
|
|
115
|
+
refs.map(function (ref) {
|
|
116
|
+
if (ref.REFERENCED_COLUMN_NAME === from) {
|
|
117
|
+
ref.REFERENCED_COLUMN_NAME = to;
|
|
118
|
+
}
|
|
119
|
+
if (ref.COLUMN_NAME === from) {
|
|
120
|
+
ref.COLUMN_NAME = to;
|
|
121
|
+
}
|
|
122
|
+
return ref;
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
})
|
|
126
|
+
);
|
|
127
|
+
},
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
|
|
131
|
+
getFKRefs(runner) {
|
|
132
|
+
const formatter = this.client.formatter(this.tableBuilder);
|
|
133
|
+
const sql =
|
|
134
|
+
'SELECT KCU.CONSTRAINT_NAME, KCU.TABLE_NAME, KCU.COLUMN_NAME, ' +
|
|
135
|
+
' KCU.REFERENCED_TABLE_NAME, KCU.REFERENCED_COLUMN_NAME, ' +
|
|
136
|
+
' RC.UPDATE_RULE, RC.DELETE_RULE ' +
|
|
137
|
+
'FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS KCU ' +
|
|
138
|
+
'JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS AS RC ' +
|
|
139
|
+
' USING(CONSTRAINT_NAME)' +
|
|
140
|
+
'WHERE KCU.REFERENCED_TABLE_NAME = ' +
|
|
141
|
+
formatter.parameter(this.tableNameRaw) +
|
|
142
|
+
' ' +
|
|
143
|
+
' AND KCU.CONSTRAINT_SCHEMA = ' +
|
|
144
|
+
formatter.parameter(this.client.database()) +
|
|
145
|
+
' ' +
|
|
146
|
+
' AND RC.CONSTRAINT_SCHEMA = ' +
|
|
147
|
+
formatter.parameter(this.client.database());
|
|
148
|
+
|
|
149
|
+
return runner.query({
|
|
150
|
+
sql,
|
|
151
|
+
bindings: formatter.bindings,
|
|
152
|
+
});
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
dropFKRefs(runner, refs) {
|
|
156
|
+
const formatter = this.client.formatter(this.tableBuilder);
|
|
157
|
+
|
|
158
|
+
return Promise.all(
|
|
159
|
+
refs.map(function (ref) {
|
|
160
|
+
const constraintName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
161
|
+
const tableName = formatter.wrap(ref.TABLE_NAME);
|
|
162
|
+
return runner.query({
|
|
163
|
+
sql: `alter table ${tableName} drop foreign key ${constraintName}`,
|
|
164
|
+
});
|
|
165
|
+
})
|
|
166
|
+
);
|
|
167
|
+
},
|
|
168
|
+
createFKRefs(runner, refs) {
|
|
169
|
+
const formatter = this.client.formatter(this.tableBuilder);
|
|
170
|
+
|
|
171
|
+
return Promise.all(
|
|
172
|
+
refs.map(function (ref) {
|
|
173
|
+
const tableName = formatter.wrap(ref.TABLE_NAME);
|
|
174
|
+
const keyName = formatter.wrap(ref.CONSTRAINT_NAME);
|
|
175
|
+
const column = formatter.columnize(ref.COLUMN_NAME);
|
|
176
|
+
const references = formatter.columnize(ref.REFERENCED_COLUMN_NAME);
|
|
177
|
+
const inTable = formatter.wrap(ref.REFERENCED_TABLE_NAME);
|
|
178
|
+
const onUpdate = ` ON UPDATE ${ref.UPDATE_RULE}`;
|
|
179
|
+
const onDelete = ` ON DELETE ${ref.DELETE_RULE}`;
|
|
180
|
+
|
|
181
|
+
return runner.query({
|
|
182
|
+
sql:
|
|
183
|
+
`alter table ${tableName} add constraint ${keyName} ` +
|
|
184
|
+
'foreign key (' +
|
|
185
|
+
column +
|
|
186
|
+
') references ' +
|
|
187
|
+
inTable +
|
|
188
|
+
' (' +
|
|
189
|
+
references +
|
|
190
|
+
')' +
|
|
191
|
+
onUpdate +
|
|
192
|
+
onDelete,
|
|
193
|
+
});
|
|
194
|
+
})
|
|
195
|
+
);
|
|
196
|
+
},
|
|
197
|
+
index(columns, indexName, indexType) {
|
|
198
|
+
indexName = indexName
|
|
199
|
+
? this.formatter.wrap(indexName)
|
|
200
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
201
|
+
this.pushQuery(
|
|
202
|
+
`alter table ${this.tableName()} add${
|
|
203
|
+
indexType ? ` ${indexType}` : ''
|
|
204
|
+
} index ${indexName}(${this.formatter.columnize(columns)})`
|
|
205
|
+
);
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
primary(columns, constraintName) {
|
|
209
|
+
constraintName = constraintName
|
|
210
|
+
? this.formatter.wrap(constraintName)
|
|
211
|
+
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
|
212
|
+
this.pushQuery(
|
|
213
|
+
`alter table ${this.tableName()} add primary key ${constraintName}(${this.formatter.columnize(
|
|
214
|
+
columns
|
|
215
|
+
)})`
|
|
216
|
+
);
|
|
217
|
+
},
|
|
218
|
+
|
|
219
|
+
unique(columns, indexName) {
|
|
220
|
+
indexName = indexName
|
|
221
|
+
? this.formatter.wrap(indexName)
|
|
222
|
+
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
223
|
+
this.pushQuery(
|
|
224
|
+
`alter table ${this.tableName()} add unique ${indexName}(${this.formatter.columnize(
|
|
225
|
+
columns
|
|
226
|
+
)})`
|
|
227
|
+
);
|
|
228
|
+
},
|
|
229
|
+
|
|
230
|
+
// Compile a drop index command.
|
|
231
|
+
dropIndex(columns, indexName) {
|
|
232
|
+
indexName = indexName
|
|
233
|
+
? this.formatter.wrap(indexName)
|
|
234
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
235
|
+
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
|
236
|
+
},
|
|
237
|
+
|
|
238
|
+
// Compile a drop foreign key command.
|
|
239
|
+
dropForeign(columns, indexName) {
|
|
240
|
+
indexName = indexName
|
|
241
|
+
? this.formatter.wrap(indexName)
|
|
242
|
+
: this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
243
|
+
this.pushQuery(
|
|
244
|
+
`alter table ${this.tableName()} drop foreign key ${indexName}`
|
|
245
|
+
);
|
|
246
|
+
},
|
|
247
|
+
|
|
248
|
+
// Compile a drop primary key command.
|
|
249
|
+
dropPrimary() {
|
|
250
|
+
this.pushQuery(`alter table ${this.tableName()} drop primary key`);
|
|
251
|
+
},
|
|
252
|
+
|
|
253
|
+
// Compile a drop unique key command.
|
|
254
|
+
dropUnique(column, indexName) {
|
|
255
|
+
indexName = indexName
|
|
256
|
+
? this.formatter.wrap(indexName)
|
|
257
|
+
: this._indexCommand('unique', this.tableNameRaw, column);
|
|
258
|
+
this.pushQuery(`alter table ${this.tableName()} drop index ${indexName}`);
|
|
259
|
+
},
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
module.exports = TableCompiler_MySQL;
|