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,142 +1,142 @@
|
|
|
1
|
-
// MySQL Query Compiler
|
|
2
|
-
// ------
|
|
3
|
-
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
-
|
|
5
|
-
const identity = require('lodash/identity');
|
|
6
|
-
|
|
7
|
-
class QueryCompiler_MySQL extends QueryCompiler {
|
|
8
|
-
constructor(client, builder) {
|
|
9
|
-
super(client, builder);
|
|
10
|
-
|
|
11
|
-
const { returning } = this.single;
|
|
12
|
-
if (returning) {
|
|
13
|
-
this.client.logger.warn(
|
|
14
|
-
'.returning() is not supported by mysql and will not have any effect.'
|
|
15
|
-
);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
this._emptyInsertValue = '() values ()';
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
// Compiles an `insert` query, allowing for multiple
|
|
22
|
-
// inserts using a single query statement.
|
|
23
|
-
insert() {
|
|
24
|
-
let sql = super.insert();
|
|
25
|
-
if (sql === '') return sql;
|
|
26
|
-
|
|
27
|
-
const { ignore, merge, insert } = this.single;
|
|
28
|
-
if (ignore) sql = sql.replace('insert into', 'insert ignore into');
|
|
29
|
-
if (merge) {
|
|
30
|
-
sql += this._merge(merge.updates, insert);
|
|
31
|
-
const wheres = this.where();
|
|
32
|
-
if (wheres) {
|
|
33
|
-
throw new Error('.onConflict().merge().where() is not supported for mysql');
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
return sql;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
_merge(updates, insert) {
|
|
41
|
-
let sql = ' on duplicate key update ';
|
|
42
|
-
if (updates) {
|
|
43
|
-
const updateData = this._prepUpdate(updates);
|
|
44
|
-
return sql + updateData.join(',');
|
|
45
|
-
} else {
|
|
46
|
-
const insertData = this._prepInsert(insert);
|
|
47
|
-
if (typeof insertData === 'string') {
|
|
48
|
-
throw new Error(
|
|
49
|
-
'If using merge with a raw insert query, then updates must be provided'
|
|
50
|
-
);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
sql += insertData.columns
|
|
54
|
-
.map((column) => this.formatter.wrapAsIdentifier(column))
|
|
55
|
-
.map((column) => `${column} = values(${column})`)
|
|
56
|
-
.join(', ');
|
|
57
|
-
|
|
58
|
-
return sql;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
// Update method, including joins, wheres, order & limits.
|
|
63
|
-
update() {
|
|
64
|
-
const join = this.join();
|
|
65
|
-
const updates = this._prepUpdate(this.single.update);
|
|
66
|
-
const where = this.where();
|
|
67
|
-
const order = this.order();
|
|
68
|
-
const limit = this.limit();
|
|
69
|
-
return (
|
|
70
|
-
`update ${this.tableName}` +
|
|
71
|
-
(join ? ` ${join}` : '') +
|
|
72
|
-
' set ' +
|
|
73
|
-
updates.join(', ') +
|
|
74
|
-
(where ? ` ${where}` : '') +
|
|
75
|
-
(order ? ` ${order}` : '') +
|
|
76
|
-
(limit ? ` ${limit}` : '')
|
|
77
|
-
);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
forUpdate() {
|
|
81
|
-
return 'for update';
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
forShare() {
|
|
85
|
-
return 'lock in share mode';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// Only supported on MySQL 8.0+
|
|
89
|
-
skipLocked() {
|
|
90
|
-
return 'skip locked';
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
// Supported on MySQL 8.0+ and MariaDB 10.3.0+
|
|
94
|
-
noWait() {
|
|
95
|
-
return 'nowait';
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// Compiles a `columnInfo` query.
|
|
99
|
-
columnInfo() {
|
|
100
|
-
const column = this.single.columnInfo;
|
|
101
|
-
|
|
102
|
-
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
103
|
-
// need to run the identifiers through that function, but not format them as
|
|
104
|
-
// identifiers otherwise.
|
|
105
|
-
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
106
|
-
|
|
107
|
-
return {
|
|
108
|
-
sql:
|
|
109
|
-
'select * from information_schema.columns where table_name = ? and table_schema = ?',
|
|
110
|
-
bindings: [table, this.client.database()],
|
|
111
|
-
output(resp) {
|
|
112
|
-
const out = resp.reduce(function (columns, val) {
|
|
113
|
-
columns[val.COLUMN_NAME] = {
|
|
114
|
-
defaultValue: val.COLUMN_DEFAULT,
|
|
115
|
-
type: val.DATA_TYPE,
|
|
116
|
-
maxLength: val.CHARACTER_MAXIMUM_LENGTH,
|
|
117
|
-
nullable: val.IS_NULLABLE === 'YES',
|
|
118
|
-
};
|
|
119
|
-
return columns;
|
|
120
|
-
}, {});
|
|
121
|
-
return (column && out[column]) || out;
|
|
122
|
-
},
|
|
123
|
-
};
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
limit() {
|
|
127
|
-
const noLimit = !this.single.limit && this.single.limit !== 0;
|
|
128
|
-
if (noLimit && !this.single.offset) return '';
|
|
129
|
-
|
|
130
|
-
// Workaround for offset only.
|
|
131
|
-
// see: http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
|
|
132
|
-
const limit =
|
|
133
|
-
this.single.offset && noLimit
|
|
134
|
-
? '18446744073709551615'
|
|
135
|
-
: this.formatter.parameter(this.single.limit);
|
|
136
|
-
return `limit ${limit}`;
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
// Set the QueryBuilder & QueryCompiler on the client object,
|
|
141
|
-
// in case anyone wants to modify things to suit their own purposes.
|
|
142
|
-
module.exports = QueryCompiler_MySQL;
|
|
1
|
+
// MySQL Query Compiler
|
|
2
|
+
// ------
|
|
3
|
+
const QueryCompiler = require('../../../query/compiler');
|
|
4
|
+
|
|
5
|
+
const identity = require('lodash/identity');
|
|
6
|
+
|
|
7
|
+
class QueryCompiler_MySQL extends QueryCompiler {
|
|
8
|
+
constructor(client, builder) {
|
|
9
|
+
super(client, builder);
|
|
10
|
+
|
|
11
|
+
const { returning } = this.single;
|
|
12
|
+
if (returning) {
|
|
13
|
+
this.client.logger.warn(
|
|
14
|
+
'.returning() is not supported by mysql and will not have any effect.'
|
|
15
|
+
);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
this._emptyInsertValue = '() values ()';
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Compiles an `insert` query, allowing for multiple
|
|
22
|
+
// inserts using a single query statement.
|
|
23
|
+
insert() {
|
|
24
|
+
let sql = super.insert();
|
|
25
|
+
if (sql === '') return sql;
|
|
26
|
+
|
|
27
|
+
const { ignore, merge, insert } = this.single;
|
|
28
|
+
if (ignore) sql = sql.replace('insert into', 'insert ignore into');
|
|
29
|
+
if (merge) {
|
|
30
|
+
sql += this._merge(merge.updates, insert);
|
|
31
|
+
const wheres = this.where();
|
|
32
|
+
if (wheres) {
|
|
33
|
+
throw new Error('.onConflict().merge().where() is not supported for mysql');
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return sql;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
_merge(updates, insert) {
|
|
41
|
+
let sql = ' on duplicate key update ';
|
|
42
|
+
if (updates) {
|
|
43
|
+
const updateData = this._prepUpdate(updates);
|
|
44
|
+
return sql + updateData.join(',');
|
|
45
|
+
} else {
|
|
46
|
+
const insertData = this._prepInsert(insert);
|
|
47
|
+
if (typeof insertData === 'string') {
|
|
48
|
+
throw new Error(
|
|
49
|
+
'If using merge with a raw insert query, then updates must be provided'
|
|
50
|
+
);
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
sql += insertData.columns
|
|
54
|
+
.map((column) => this.formatter.wrapAsIdentifier(column))
|
|
55
|
+
.map((column) => `${column} = values(${column})`)
|
|
56
|
+
.join(', ');
|
|
57
|
+
|
|
58
|
+
return sql;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
// Update method, including joins, wheres, order & limits.
|
|
63
|
+
update() {
|
|
64
|
+
const join = this.join();
|
|
65
|
+
const updates = this._prepUpdate(this.single.update);
|
|
66
|
+
const where = this.where();
|
|
67
|
+
const order = this.order();
|
|
68
|
+
const limit = this.limit();
|
|
69
|
+
return (
|
|
70
|
+
`update ${this.tableName}` +
|
|
71
|
+
(join ? ` ${join}` : '') +
|
|
72
|
+
' set ' +
|
|
73
|
+
updates.join(', ') +
|
|
74
|
+
(where ? ` ${where}` : '') +
|
|
75
|
+
(order ? ` ${order}` : '') +
|
|
76
|
+
(limit ? ` ${limit}` : '')
|
|
77
|
+
);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
forUpdate() {
|
|
81
|
+
return 'for update';
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
forShare() {
|
|
85
|
+
return 'lock in share mode';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// Only supported on MySQL 8.0+
|
|
89
|
+
skipLocked() {
|
|
90
|
+
return 'skip locked';
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// Supported on MySQL 8.0+ and MariaDB 10.3.0+
|
|
94
|
+
noWait() {
|
|
95
|
+
return 'nowait';
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// Compiles a `columnInfo` query.
|
|
99
|
+
columnInfo() {
|
|
100
|
+
const column = this.single.columnInfo;
|
|
101
|
+
|
|
102
|
+
// The user may have specified a custom wrapIdentifier function in the config. We
|
|
103
|
+
// need to run the identifiers through that function, but not format them as
|
|
104
|
+
// identifiers otherwise.
|
|
105
|
+
const table = this.client.customWrapIdentifier(this.single.table, identity);
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
sql:
|
|
109
|
+
'select * from information_schema.columns where table_name = ? and table_schema = ?',
|
|
110
|
+
bindings: [table, this.client.database()],
|
|
111
|
+
output(resp) {
|
|
112
|
+
const out = resp.reduce(function (columns, val) {
|
|
113
|
+
columns[val.COLUMN_NAME] = {
|
|
114
|
+
defaultValue: val.COLUMN_DEFAULT,
|
|
115
|
+
type: val.DATA_TYPE,
|
|
116
|
+
maxLength: val.CHARACTER_MAXIMUM_LENGTH,
|
|
117
|
+
nullable: val.IS_NULLABLE === 'YES',
|
|
118
|
+
};
|
|
119
|
+
return columns;
|
|
120
|
+
}, {});
|
|
121
|
+
return (column && out[column]) || out;
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
limit() {
|
|
127
|
+
const noLimit = !this.single.limit && this.single.limit !== 0;
|
|
128
|
+
if (noLimit && !this.single.offset) return '';
|
|
129
|
+
|
|
130
|
+
// Workaround for offset only.
|
|
131
|
+
// see: http://stackoverflow.com/questions/255517/mysql-offset-infinite-rows
|
|
132
|
+
const limit =
|
|
133
|
+
this.single.offset && noLimit
|
|
134
|
+
? '18446744073709551615'
|
|
135
|
+
: this.formatter.parameter(this.single.limit);
|
|
136
|
+
return `limit ${limit}`;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
// Set the QueryBuilder & QueryCompiler on the client object,
|
|
141
|
+
// in case anyone wants to modify things to suit their own purposes.
|
|
142
|
+
module.exports = QueryCompiler_MySQL;
|
|
@@ -1,171 +1,171 @@
|
|
|
1
|
-
// MySQL Column Compiler
|
|
2
|
-
// -------
|
|
3
|
-
const { inherits } = require('util');
|
|
4
|
-
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
5
|
-
const { isObject } = require('../../../util/is');
|
|
6
|
-
|
|
7
|
-
const commentEscapeRegex = /(?<!\\)'/g;
|
|
8
|
-
|
|
9
|
-
function ColumnCompiler_MySQL() {
|
|
10
|
-
ColumnCompiler.apply(this, arguments);
|
|
11
|
-
this.modifiers = [
|
|
12
|
-
'unsigned',
|
|
13
|
-
'nullable',
|
|
14
|
-
'defaultTo',
|
|
15
|
-
'comment',
|
|
16
|
-
'collate',
|
|
17
|
-
'first',
|
|
18
|
-
'after',
|
|
19
|
-
];
|
|
20
|
-
}
|
|
21
|
-
inherits(ColumnCompiler_MySQL, ColumnCompiler);
|
|
22
|
-
|
|
23
|
-
// Types
|
|
24
|
-
// ------
|
|
25
|
-
|
|
26
|
-
Object.assign(ColumnCompiler_MySQL.prototype, {
|
|
27
|
-
increments: 'int unsigned not null auto_increment primary key',
|
|
28
|
-
|
|
29
|
-
bigincrements: 'bigint unsigned not null auto_increment primary key',
|
|
30
|
-
|
|
31
|
-
bigint: 'bigint',
|
|
32
|
-
|
|
33
|
-
double(precision, scale) {
|
|
34
|
-
if (!precision) return 'double';
|
|
35
|
-
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
36
|
-
},
|
|
37
|
-
|
|
38
|
-
integer(length) {
|
|
39
|
-
length = length ? `(${this._num(length, 11)})` : '';
|
|
40
|
-
return `int${length}`;
|
|
41
|
-
},
|
|
42
|
-
|
|
43
|
-
mediumint: 'mediumint',
|
|
44
|
-
|
|
45
|
-
smallint: 'smallint',
|
|
46
|
-
|
|
47
|
-
tinyint(length) {
|
|
48
|
-
length = length ? `(${this._num(length, 1)})` : '';
|
|
49
|
-
return `tinyint${length}`;
|
|
50
|
-
},
|
|
51
|
-
|
|
52
|
-
text(column) {
|
|
53
|
-
switch (column) {
|
|
54
|
-
case 'medium':
|
|
55
|
-
case 'mediumtext':
|
|
56
|
-
return 'mediumtext';
|
|
57
|
-
case 'long':
|
|
58
|
-
case 'longtext':
|
|
59
|
-
return 'longtext';
|
|
60
|
-
default:
|
|
61
|
-
return 'text';
|
|
62
|
-
}
|
|
63
|
-
},
|
|
64
|
-
|
|
65
|
-
mediumtext() {
|
|
66
|
-
return this.text('medium');
|
|
67
|
-
},
|
|
68
|
-
|
|
69
|
-
longtext() {
|
|
70
|
-
return this.text('long');
|
|
71
|
-
},
|
|
72
|
-
|
|
73
|
-
enu(allowed) {
|
|
74
|
-
return `enum('${allowed.join("', '")}')`;
|
|
75
|
-
},
|
|
76
|
-
|
|
77
|
-
datetime(precision) {
|
|
78
|
-
if (isObject(precision)) {
|
|
79
|
-
({ precision } = precision);
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
return typeof precision === 'number'
|
|
83
|
-
? `datetime(${precision})`
|
|
84
|
-
: 'datetime';
|
|
85
|
-
},
|
|
86
|
-
|
|
87
|
-
timestamp(precision) {
|
|
88
|
-
if (isObject(precision)) {
|
|
89
|
-
({ precision } = precision);
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
return typeof precision === 'number'
|
|
93
|
-
? `timestamp(${precision})`
|
|
94
|
-
: 'timestamp';
|
|
95
|
-
},
|
|
96
|
-
|
|
97
|
-
time(precision) {
|
|
98
|
-
if (isObject(precision)) {
|
|
99
|
-
({ precision } = precision);
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
return typeof precision === 'number' ? `time(${precision})` : 'time';
|
|
103
|
-
},
|
|
104
|
-
|
|
105
|
-
bit(length) {
|
|
106
|
-
return length ? `bit(${this._num(length)})` : 'bit';
|
|
107
|
-
},
|
|
108
|
-
|
|
109
|
-
binary(length) {
|
|
110
|
-
return length ? `varbinary(${this._num(length)})` : 'blob';
|
|
111
|
-
},
|
|
112
|
-
|
|
113
|
-
json() {
|
|
114
|
-
return 'json';
|
|
115
|
-
},
|
|
116
|
-
|
|
117
|
-
jsonb() {
|
|
118
|
-
return 'json';
|
|
119
|
-
},
|
|
120
|
-
|
|
121
|
-
// Modifiers
|
|
122
|
-
// ------
|
|
123
|
-
|
|
124
|
-
defaultTo(value) {
|
|
125
|
-
// MySQL defaults to null by default, but breaks down if you pass it explicitly
|
|
126
|
-
// Note that in MySQL versions up to 5.7, logic related to updating
|
|
127
|
-
// timestamps when no explicit value is passed is quite insane - https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
|
|
128
|
-
if (value === null || value === undefined) {
|
|
129
|
-
return;
|
|
130
|
-
}
|
|
131
|
-
if ((this.type === 'json' || this.type === 'jsonb') && isObject(value)) {
|
|
132
|
-
// Default value for json will work only it is an expression
|
|
133
|
-
return `default ('${JSON.stringify(value)}')`;
|
|
134
|
-
}
|
|
135
|
-
const defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(
|
|
136
|
-
this,
|
|
137
|
-
arguments
|
|
138
|
-
);
|
|
139
|
-
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
|
|
140
|
-
return defaultVal;
|
|
141
|
-
}
|
|
142
|
-
return '';
|
|
143
|
-
},
|
|
144
|
-
|
|
145
|
-
unsigned() {
|
|
146
|
-
return 'unsigned';
|
|
147
|
-
},
|
|
148
|
-
|
|
149
|
-
comment(comment) {
|
|
150
|
-
if (comment && comment.length > 255) {
|
|
151
|
-
this.client.logger.warn(
|
|
152
|
-
'Your comment is longer than the max comment length for MySQL'
|
|
153
|
-
);
|
|
154
|
-
}
|
|
155
|
-
return comment && `comment '${comment.replace(commentEscapeRegex, "\\'")}'`;
|
|
156
|
-
},
|
|
157
|
-
|
|
158
|
-
first() {
|
|
159
|
-
return 'first';
|
|
160
|
-
},
|
|
161
|
-
|
|
162
|
-
after(column) {
|
|
163
|
-
return `after ${this.formatter.wrap(column)}`;
|
|
164
|
-
},
|
|
165
|
-
|
|
166
|
-
collate(collation) {
|
|
167
|
-
return collation && `collate '${collation}'`;
|
|
168
|
-
},
|
|
169
|
-
});
|
|
170
|
-
|
|
171
|
-
module.exports = ColumnCompiler_MySQL;
|
|
1
|
+
// MySQL Column Compiler
|
|
2
|
+
// -------
|
|
3
|
+
const { inherits } = require('util');
|
|
4
|
+
const ColumnCompiler = require('../../../schema/columncompiler');
|
|
5
|
+
const { isObject } = require('../../../util/is');
|
|
6
|
+
|
|
7
|
+
const commentEscapeRegex = /(?<!\\)'/g;
|
|
8
|
+
|
|
9
|
+
function ColumnCompiler_MySQL() {
|
|
10
|
+
ColumnCompiler.apply(this, arguments);
|
|
11
|
+
this.modifiers = [
|
|
12
|
+
'unsigned',
|
|
13
|
+
'nullable',
|
|
14
|
+
'defaultTo',
|
|
15
|
+
'comment',
|
|
16
|
+
'collate',
|
|
17
|
+
'first',
|
|
18
|
+
'after',
|
|
19
|
+
];
|
|
20
|
+
}
|
|
21
|
+
inherits(ColumnCompiler_MySQL, ColumnCompiler);
|
|
22
|
+
|
|
23
|
+
// Types
|
|
24
|
+
// ------
|
|
25
|
+
|
|
26
|
+
Object.assign(ColumnCompiler_MySQL.prototype, {
|
|
27
|
+
increments: 'int unsigned not null auto_increment primary key',
|
|
28
|
+
|
|
29
|
+
bigincrements: 'bigint unsigned not null auto_increment primary key',
|
|
30
|
+
|
|
31
|
+
bigint: 'bigint',
|
|
32
|
+
|
|
33
|
+
double(precision, scale) {
|
|
34
|
+
if (!precision) return 'double';
|
|
35
|
+
return `double(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
36
|
+
},
|
|
37
|
+
|
|
38
|
+
integer(length) {
|
|
39
|
+
length = length ? `(${this._num(length, 11)})` : '';
|
|
40
|
+
return `int${length}`;
|
|
41
|
+
},
|
|
42
|
+
|
|
43
|
+
mediumint: 'mediumint',
|
|
44
|
+
|
|
45
|
+
smallint: 'smallint',
|
|
46
|
+
|
|
47
|
+
tinyint(length) {
|
|
48
|
+
length = length ? `(${this._num(length, 1)})` : '';
|
|
49
|
+
return `tinyint${length}`;
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
text(column) {
|
|
53
|
+
switch (column) {
|
|
54
|
+
case 'medium':
|
|
55
|
+
case 'mediumtext':
|
|
56
|
+
return 'mediumtext';
|
|
57
|
+
case 'long':
|
|
58
|
+
case 'longtext':
|
|
59
|
+
return 'longtext';
|
|
60
|
+
default:
|
|
61
|
+
return 'text';
|
|
62
|
+
}
|
|
63
|
+
},
|
|
64
|
+
|
|
65
|
+
mediumtext() {
|
|
66
|
+
return this.text('medium');
|
|
67
|
+
},
|
|
68
|
+
|
|
69
|
+
longtext() {
|
|
70
|
+
return this.text('long');
|
|
71
|
+
},
|
|
72
|
+
|
|
73
|
+
enu(allowed) {
|
|
74
|
+
return `enum('${allowed.join("', '")}')`;
|
|
75
|
+
},
|
|
76
|
+
|
|
77
|
+
datetime(precision) {
|
|
78
|
+
if (isObject(precision)) {
|
|
79
|
+
({ precision } = precision);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
return typeof precision === 'number'
|
|
83
|
+
? `datetime(${precision})`
|
|
84
|
+
: 'datetime';
|
|
85
|
+
},
|
|
86
|
+
|
|
87
|
+
timestamp(precision) {
|
|
88
|
+
if (isObject(precision)) {
|
|
89
|
+
({ precision } = precision);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return typeof precision === 'number'
|
|
93
|
+
? `timestamp(${precision})`
|
|
94
|
+
: 'timestamp';
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
time(precision) {
|
|
98
|
+
if (isObject(precision)) {
|
|
99
|
+
({ precision } = precision);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
return typeof precision === 'number' ? `time(${precision})` : 'time';
|
|
103
|
+
},
|
|
104
|
+
|
|
105
|
+
bit(length) {
|
|
106
|
+
return length ? `bit(${this._num(length)})` : 'bit';
|
|
107
|
+
},
|
|
108
|
+
|
|
109
|
+
binary(length) {
|
|
110
|
+
return length ? `varbinary(${this._num(length)})` : 'blob';
|
|
111
|
+
},
|
|
112
|
+
|
|
113
|
+
json() {
|
|
114
|
+
return 'json';
|
|
115
|
+
},
|
|
116
|
+
|
|
117
|
+
jsonb() {
|
|
118
|
+
return 'json';
|
|
119
|
+
},
|
|
120
|
+
|
|
121
|
+
// Modifiers
|
|
122
|
+
// ------
|
|
123
|
+
|
|
124
|
+
defaultTo(value) {
|
|
125
|
+
// MySQL defaults to null by default, but breaks down if you pass it explicitly
|
|
126
|
+
// Note that in MySQL versions up to 5.7, logic related to updating
|
|
127
|
+
// timestamps when no explicit value is passed is quite insane - https://dev.mysql.com/doc/refman/5.7/en/server-system-variables.html#sysvar_explicit_defaults_for_timestamp
|
|
128
|
+
if (value === null || value === undefined) {
|
|
129
|
+
return;
|
|
130
|
+
}
|
|
131
|
+
if ((this.type === 'json' || this.type === 'jsonb') && isObject(value)) {
|
|
132
|
+
// Default value for json will work only it is an expression
|
|
133
|
+
return `default ('${JSON.stringify(value)}')`;
|
|
134
|
+
}
|
|
135
|
+
const defaultVal = ColumnCompiler_MySQL.super_.prototype.defaultTo.apply(
|
|
136
|
+
this,
|
|
137
|
+
arguments
|
|
138
|
+
);
|
|
139
|
+
if (this.type !== 'blob' && this.type.indexOf('text') === -1) {
|
|
140
|
+
return defaultVal;
|
|
141
|
+
}
|
|
142
|
+
return '';
|
|
143
|
+
},
|
|
144
|
+
|
|
145
|
+
unsigned() {
|
|
146
|
+
return 'unsigned';
|
|
147
|
+
},
|
|
148
|
+
|
|
149
|
+
comment(comment) {
|
|
150
|
+
if (comment && comment.length > 255) {
|
|
151
|
+
this.client.logger.warn(
|
|
152
|
+
'Your comment is longer than the max comment length for MySQL'
|
|
153
|
+
);
|
|
154
|
+
}
|
|
155
|
+
return comment && `comment '${comment.replace(commentEscapeRegex, "\\'")}'`;
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
first() {
|
|
159
|
+
return 'first';
|
|
160
|
+
},
|
|
161
|
+
|
|
162
|
+
after(column) {
|
|
163
|
+
return `after ${this.formatter.wrap(column)}`;
|
|
164
|
+
},
|
|
165
|
+
|
|
166
|
+
collate(collation) {
|
|
167
|
+
return collation && `collate '${collation}'`;
|
|
168
|
+
},
|
|
169
|
+
});
|
|
170
|
+
|
|
171
|
+
module.exports = ColumnCompiler_MySQL;
|