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,109 +1,109 @@
|
|
|
1
|
-
// PostgreSQL Schema Compiler
|
|
2
|
-
// -------
|
|
3
|
-
|
|
4
|
-
const { inherits } = require('util');
|
|
5
|
-
const SchemaCompiler = require('../../../schema/compiler');
|
|
6
|
-
|
|
7
|
-
function SchemaCompiler_PG() {
|
|
8
|
-
SchemaCompiler.apply(this, arguments);
|
|
9
|
-
}
|
|
10
|
-
inherits(SchemaCompiler_PG, SchemaCompiler);
|
|
11
|
-
|
|
12
|
-
// Check whether the current table
|
|
13
|
-
SchemaCompiler_PG.prototype.hasTable = function (tableName) {
|
|
14
|
-
let sql = 'select * from information_schema.tables where table_name = ?';
|
|
15
|
-
const bindings = [tableName];
|
|
16
|
-
|
|
17
|
-
if (this.schema) {
|
|
18
|
-
sql += ' and table_schema = ?';
|
|
19
|
-
bindings.push(this.schema);
|
|
20
|
-
} else {
|
|
21
|
-
sql += ' and table_schema = current_schema()';
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
this.pushQuery({
|
|
25
|
-
sql,
|
|
26
|
-
bindings,
|
|
27
|
-
output(resp) {
|
|
28
|
-
return resp.rows.length > 0;
|
|
29
|
-
},
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// Compile the query to determine if a column exists in a table.
|
|
34
|
-
SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
|
|
35
|
-
let sql =
|
|
36
|
-
'select * from information_schema.columns where table_name = ? and column_name = ?';
|
|
37
|
-
const bindings = [tableName, columnName];
|
|
38
|
-
|
|
39
|
-
if (this.schema) {
|
|
40
|
-
sql += ' and table_schema = ?';
|
|
41
|
-
bindings.push(this.schema);
|
|
42
|
-
} else {
|
|
43
|
-
sql += ' and table_schema = current_schema()';
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
this.pushQuery({
|
|
47
|
-
sql,
|
|
48
|
-
bindings,
|
|
49
|
-
output(resp) {
|
|
50
|
-
return resp.rows.length > 0;
|
|
51
|
-
},
|
|
52
|
-
});
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
|
|
56
|
-
const name = this.schema ? `${this.schema}.${tableName}` : tableName;
|
|
57
|
-
return this.formatter.wrap(name);
|
|
58
|
-
};
|
|
59
|
-
|
|
60
|
-
// Compile a rename table command.
|
|
61
|
-
SchemaCompiler_PG.prototype.renameTable = function (from, to) {
|
|
62
|
-
this.pushQuery(
|
|
63
|
-
`alter table ${this.qualifiedTableName(
|
|
64
|
-
from
|
|
65
|
-
)} rename to ${this.formatter.wrap(to)}`
|
|
66
|
-
);
|
|
67
|
-
};
|
|
68
|
-
|
|
69
|
-
SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
|
|
70
|
-
this.pushQuery(`create schema ${this.formatter.wrap(schemaName)}`);
|
|
71
|
-
};
|
|
72
|
-
|
|
73
|
-
SchemaCompiler_PG.prototype.createSchemaIfNotExists = function (schemaName) {
|
|
74
|
-
this.pushQuery(
|
|
75
|
-
`create schema if not exists ${this.formatter.wrap(schemaName)}`
|
|
76
|
-
);
|
|
77
|
-
};
|
|
78
|
-
|
|
79
|
-
SchemaCompiler_PG.prototype.dropSchema = function (schemaName) {
|
|
80
|
-
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
|
|
81
|
-
};
|
|
82
|
-
|
|
83
|
-
SchemaCompiler_PG.prototype.dropSchemaIfExists = function (schemaName) {
|
|
84
|
-
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
SchemaCompiler_PG.prototype.dropExtension = function (extensionName) {
|
|
88
|
-
this.pushQuery(`drop extension ${this.formatter.wrap(extensionName)}`);
|
|
89
|
-
};
|
|
90
|
-
|
|
91
|
-
SchemaCompiler_PG.prototype.dropExtensionIfExists = function (extensionName) {
|
|
92
|
-
this.pushQuery(
|
|
93
|
-
`drop extension if exists ${this.formatter.wrap(extensionName)}`
|
|
94
|
-
);
|
|
95
|
-
};
|
|
96
|
-
|
|
97
|
-
SchemaCompiler_PG.prototype.createExtension = function (extensionName) {
|
|
98
|
-
this.pushQuery(`create extension ${this.formatter.wrap(extensionName)}`);
|
|
99
|
-
};
|
|
100
|
-
|
|
101
|
-
SchemaCompiler_PG.prototype.createExtensionIfNotExists = function (
|
|
102
|
-
extensionName
|
|
103
|
-
) {
|
|
104
|
-
this.pushQuery(
|
|
105
|
-
`create extension if not exists ${this.formatter.wrap(extensionName)}`
|
|
106
|
-
);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
module.exports = SchemaCompiler_PG;
|
|
1
|
+
// PostgreSQL Schema Compiler
|
|
2
|
+
// -------
|
|
3
|
+
|
|
4
|
+
const { inherits } = require('util');
|
|
5
|
+
const SchemaCompiler = require('../../../schema/compiler');
|
|
6
|
+
|
|
7
|
+
function SchemaCompiler_PG() {
|
|
8
|
+
SchemaCompiler.apply(this, arguments);
|
|
9
|
+
}
|
|
10
|
+
inherits(SchemaCompiler_PG, SchemaCompiler);
|
|
11
|
+
|
|
12
|
+
// Check whether the current table
|
|
13
|
+
SchemaCompiler_PG.prototype.hasTable = function (tableName) {
|
|
14
|
+
let sql = 'select * from information_schema.tables where table_name = ?';
|
|
15
|
+
const bindings = [tableName];
|
|
16
|
+
|
|
17
|
+
if (this.schema) {
|
|
18
|
+
sql += ' and table_schema = ?';
|
|
19
|
+
bindings.push(this.schema);
|
|
20
|
+
} else {
|
|
21
|
+
sql += ' and table_schema = current_schema()';
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
this.pushQuery({
|
|
25
|
+
sql,
|
|
26
|
+
bindings,
|
|
27
|
+
output(resp) {
|
|
28
|
+
return resp.rows.length > 0;
|
|
29
|
+
},
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Compile the query to determine if a column exists in a table.
|
|
34
|
+
SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
|
|
35
|
+
let sql =
|
|
36
|
+
'select * from information_schema.columns where table_name = ? and column_name = ?';
|
|
37
|
+
const bindings = [tableName, columnName];
|
|
38
|
+
|
|
39
|
+
if (this.schema) {
|
|
40
|
+
sql += ' and table_schema = ?';
|
|
41
|
+
bindings.push(this.schema);
|
|
42
|
+
} else {
|
|
43
|
+
sql += ' and table_schema = current_schema()';
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
this.pushQuery({
|
|
47
|
+
sql,
|
|
48
|
+
bindings,
|
|
49
|
+
output(resp) {
|
|
50
|
+
return resp.rows.length > 0;
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
|
|
56
|
+
const name = this.schema ? `${this.schema}.${tableName}` : tableName;
|
|
57
|
+
return this.formatter.wrap(name);
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// Compile a rename table command.
|
|
61
|
+
SchemaCompiler_PG.prototype.renameTable = function (from, to) {
|
|
62
|
+
this.pushQuery(
|
|
63
|
+
`alter table ${this.qualifiedTableName(
|
|
64
|
+
from
|
|
65
|
+
)} rename to ${this.formatter.wrap(to)}`
|
|
66
|
+
);
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
|
|
70
|
+
this.pushQuery(`create schema ${this.formatter.wrap(schemaName)}`);
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
SchemaCompiler_PG.prototype.createSchemaIfNotExists = function (schemaName) {
|
|
74
|
+
this.pushQuery(
|
|
75
|
+
`create schema if not exists ${this.formatter.wrap(schemaName)}`
|
|
76
|
+
);
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
SchemaCompiler_PG.prototype.dropSchema = function (schemaName) {
|
|
80
|
+
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
SchemaCompiler_PG.prototype.dropSchemaIfExists = function (schemaName) {
|
|
84
|
+
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
SchemaCompiler_PG.prototype.dropExtension = function (extensionName) {
|
|
88
|
+
this.pushQuery(`drop extension ${this.formatter.wrap(extensionName)}`);
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
SchemaCompiler_PG.prototype.dropExtensionIfExists = function (extensionName) {
|
|
92
|
+
this.pushQuery(
|
|
93
|
+
`drop extension if exists ${this.formatter.wrap(extensionName)}`
|
|
94
|
+
);
|
|
95
|
+
};
|
|
96
|
+
|
|
97
|
+
SchemaCompiler_PG.prototype.createExtension = function (extensionName) {
|
|
98
|
+
this.pushQuery(`create extension ${this.formatter.wrap(extensionName)}`);
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
SchemaCompiler_PG.prototype.createExtensionIfNotExists = function (
|
|
102
|
+
extensionName
|
|
103
|
+
) {
|
|
104
|
+
this.pushQuery(
|
|
105
|
+
`create extension if not exists ${this.formatter.wrap(extensionName)}`
|
|
106
|
+
);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
module.exports = SchemaCompiler_PG;
|
|
@@ -1,183 +1,183 @@
|
|
|
1
|
-
/* eslint max-len: 0 */
|
|
2
|
-
|
|
3
|
-
// PostgreSQL Table Builder & Compiler
|
|
4
|
-
// -------
|
|
5
|
-
|
|
6
|
-
const { inherits } = require('util');
|
|
7
|
-
const TableCompiler = require('../../../schema/tablecompiler');
|
|
8
|
-
|
|
9
|
-
const has = require('lodash/has');
|
|
10
|
-
|
|
11
|
-
function TableCompiler_PG() {
|
|
12
|
-
TableCompiler.apply(this, arguments);
|
|
13
|
-
}
|
|
14
|
-
inherits(TableCompiler_PG, TableCompiler);
|
|
15
|
-
|
|
16
|
-
// Compile a rename column command.
|
|
17
|
-
TableCompiler_PG.prototype.renameColumn = function (from, to) {
|
|
18
|
-
return this.pushQuery({
|
|
19
|
-
sql: `alter table ${this.tableName()} rename ${this.formatter.wrap(
|
|
20
|
-
from
|
|
21
|
-
)} to ${this.formatter.wrap(to)}`,
|
|
22
|
-
});
|
|
23
|
-
};
|
|
24
|
-
|
|
25
|
-
TableCompiler_PG.prototype.compileAdd = function (builder) {
|
|
26
|
-
const table = this.formatter.wrap(builder);
|
|
27
|
-
const columns = this.prefixArray('add column', this.getColumns(builder));
|
|
28
|
-
return this.pushQuery({
|
|
29
|
-
sql: `alter table ${table} ${columns.join(', ')}`,
|
|
30
|
-
});
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
// Adds the "create" query to the query sequence.
|
|
34
|
-
TableCompiler_PG.prototype.createQuery = function (columns, ifNot) {
|
|
35
|
-
const createStatement = ifNot
|
|
36
|
-
? 'create table if not exists '
|
|
37
|
-
: 'create table ';
|
|
38
|
-
let sql =
|
|
39
|
-
createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
|
40
|
-
if (this.single.inherits)
|
|
41
|
-
sql += ` inherits (${this.formatter.wrap(this.single.inherits)})`;
|
|
42
|
-
this.pushQuery({
|
|
43
|
-
sql,
|
|
44
|
-
bindings: columns.bindings,
|
|
45
|
-
});
|
|
46
|
-
const hasComment = has(this.single, 'comment');
|
|
47
|
-
if (hasComment) this.comment(this.single.comment);
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
TableCompiler_PG.prototype.addColumns = function (
|
|
51
|
-
columns,
|
|
52
|
-
prefix,
|
|
53
|
-
colCompilers
|
|
54
|
-
) {
|
|
55
|
-
if (prefix === this.alterColumnsPrefix) {
|
|
56
|
-
// alter columns
|
|
57
|
-
for (const col of colCompilers) {
|
|
58
|
-
const quotedTableName = this.tableName();
|
|
59
|
-
const type = col.getColumnType();
|
|
60
|
-
// We'd prefer to call this.formatter.wrapAsIdentifier here instead, however the context passed to
|
|
61
|
-
// `this` instance is not that of the column, but of the table. Thus, we unfortunately have to call
|
|
62
|
-
// `wrapIdentifier` here as well (it is already called once on the initial column operation) to give
|
|
63
|
-
// our `alter` operation the correct `queryContext`. Refer to issue #2606 and PR #2612.
|
|
64
|
-
const colName = this.client.wrapIdentifier(
|
|
65
|
-
col.getColumnName(),
|
|
66
|
-
col.columnBuilder.queryContext()
|
|
67
|
-
);
|
|
68
|
-
|
|
69
|
-
this.pushQuery({
|
|
70
|
-
sql: `alter table ${quotedTableName} alter column ${colName} drop default`,
|
|
71
|
-
bindings: [],
|
|
72
|
-
});
|
|
73
|
-
this.pushQuery({
|
|
74
|
-
sql: `alter table ${quotedTableName} alter column ${colName} drop not null`,
|
|
75
|
-
bindings: [],
|
|
76
|
-
});
|
|
77
|
-
this.pushQuery({
|
|
78
|
-
sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::${type})`,
|
|
79
|
-
bindings: [],
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
const defaultTo = col.modified['defaultTo'];
|
|
83
|
-
if (defaultTo) {
|
|
84
|
-
const modifier = col.defaultTo.apply(col, defaultTo);
|
|
85
|
-
this.pushQuery({
|
|
86
|
-
sql: `alter table ${quotedTableName} alter column ${colName} set ${modifier}`,
|
|
87
|
-
bindings: [],
|
|
88
|
-
});
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
const nullable = col.modified['nullable'];
|
|
92
|
-
if (nullable && nullable[0] === false) {
|
|
93
|
-
this.pushQuery({
|
|
94
|
-
sql: `alter table ${quotedTableName} alter column ${colName} set not null`,
|
|
95
|
-
bindings: [],
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
} else {
|
|
100
|
-
// base class implementation for normal add
|
|
101
|
-
TableCompiler.prototype.addColumns.call(this, columns, prefix);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
|
|
105
|
-
// Compiles the comment on the table.
|
|
106
|
-
TableCompiler_PG.prototype.comment = function (comment) {
|
|
107
|
-
this.pushQuery(
|
|
108
|
-
`comment on table ${this.tableName()} is '${this.single.comment}'`
|
|
109
|
-
);
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
// Indexes:
|
|
113
|
-
// -------
|
|
114
|
-
|
|
115
|
-
TableCompiler_PG.prototype.primary = function (columns, constraintName) {
|
|
116
|
-
constraintName = constraintName
|
|
117
|
-
? this.formatter.wrap(constraintName)
|
|
118
|
-
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
|
119
|
-
this.pushQuery(
|
|
120
|
-
`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
|
|
121
|
-
columns
|
|
122
|
-
)})`
|
|
123
|
-
);
|
|
124
|
-
};
|
|
125
|
-
TableCompiler_PG.prototype.unique = function (columns, indexName) {
|
|
126
|
-
indexName = indexName
|
|
127
|
-
? this.formatter.wrap(indexName)
|
|
128
|
-
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
129
|
-
this.pushQuery(
|
|
130
|
-
`alter table ${this.tableName()} add constraint ${indexName}` +
|
|
131
|
-
' unique (' +
|
|
132
|
-
this.formatter.columnize(columns) +
|
|
133
|
-
')'
|
|
134
|
-
);
|
|
135
|
-
};
|
|
136
|
-
TableCompiler_PG.prototype.index = function (columns, indexName, indexType) {
|
|
137
|
-
indexName = indexName
|
|
138
|
-
? this.formatter.wrap(indexName)
|
|
139
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
140
|
-
this.pushQuery(
|
|
141
|
-
`create index ${indexName} on ${this.tableName()}${
|
|
142
|
-
(indexType && ` using ${indexType}`) || ''
|
|
143
|
-
}` +
|
|
144
|
-
' (' +
|
|
145
|
-
this.formatter.columnize(columns) +
|
|
146
|
-
')'
|
|
147
|
-
);
|
|
148
|
-
};
|
|
149
|
-
TableCompiler_PG.prototype.dropPrimary = function (constraintName) {
|
|
150
|
-
constraintName = constraintName
|
|
151
|
-
? this.formatter.wrap(constraintName)
|
|
152
|
-
: this.formatter.wrap(this.tableNameRaw + '_pkey');
|
|
153
|
-
this.pushQuery(
|
|
154
|
-
`alter table ${this.tableName()} drop constraint ${constraintName}`
|
|
155
|
-
);
|
|
156
|
-
};
|
|
157
|
-
TableCompiler_PG.prototype.dropIndex = function (columns, indexName) {
|
|
158
|
-
indexName = indexName
|
|
159
|
-
? this.formatter.wrap(indexName)
|
|
160
|
-
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
161
|
-
indexName = this.schemaNameRaw
|
|
162
|
-
? `${this.formatter.wrap(this.schemaNameRaw)}.${indexName}`
|
|
163
|
-
: indexName;
|
|
164
|
-
this.pushQuery(`drop index ${indexName}`);
|
|
165
|
-
};
|
|
166
|
-
TableCompiler_PG.prototype.dropUnique = function (columns, indexName) {
|
|
167
|
-
indexName = indexName
|
|
168
|
-
? this.formatter.wrap(indexName)
|
|
169
|
-
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
170
|
-
this.pushQuery(
|
|
171
|
-
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
172
|
-
);
|
|
173
|
-
};
|
|
174
|
-
TableCompiler_PG.prototype.dropForeign = function (columns, indexName) {
|
|
175
|
-
indexName = indexName
|
|
176
|
-
? this.formatter.wrap(indexName)
|
|
177
|
-
: this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
178
|
-
this.pushQuery(
|
|
179
|
-
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
180
|
-
);
|
|
181
|
-
};
|
|
182
|
-
|
|
183
|
-
module.exports = TableCompiler_PG;
|
|
1
|
+
/* eslint max-len: 0 */
|
|
2
|
+
|
|
3
|
+
// PostgreSQL Table Builder & Compiler
|
|
4
|
+
// -------
|
|
5
|
+
|
|
6
|
+
const { inherits } = require('util');
|
|
7
|
+
const TableCompiler = require('../../../schema/tablecompiler');
|
|
8
|
+
|
|
9
|
+
const has = require('lodash/has');
|
|
10
|
+
|
|
11
|
+
function TableCompiler_PG() {
|
|
12
|
+
TableCompiler.apply(this, arguments);
|
|
13
|
+
}
|
|
14
|
+
inherits(TableCompiler_PG, TableCompiler);
|
|
15
|
+
|
|
16
|
+
// Compile a rename column command.
|
|
17
|
+
TableCompiler_PG.prototype.renameColumn = function (from, to) {
|
|
18
|
+
return this.pushQuery({
|
|
19
|
+
sql: `alter table ${this.tableName()} rename ${this.formatter.wrap(
|
|
20
|
+
from
|
|
21
|
+
)} to ${this.formatter.wrap(to)}`,
|
|
22
|
+
});
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
TableCompiler_PG.prototype.compileAdd = function (builder) {
|
|
26
|
+
const table = this.formatter.wrap(builder);
|
|
27
|
+
const columns = this.prefixArray('add column', this.getColumns(builder));
|
|
28
|
+
return this.pushQuery({
|
|
29
|
+
sql: `alter table ${table} ${columns.join(', ')}`,
|
|
30
|
+
});
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
// Adds the "create" query to the query sequence.
|
|
34
|
+
TableCompiler_PG.prototype.createQuery = function (columns, ifNot) {
|
|
35
|
+
const createStatement = ifNot
|
|
36
|
+
? 'create table if not exists '
|
|
37
|
+
: 'create table ';
|
|
38
|
+
let sql =
|
|
39
|
+
createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
|
40
|
+
if (this.single.inherits)
|
|
41
|
+
sql += ` inherits (${this.formatter.wrap(this.single.inherits)})`;
|
|
42
|
+
this.pushQuery({
|
|
43
|
+
sql,
|
|
44
|
+
bindings: columns.bindings,
|
|
45
|
+
});
|
|
46
|
+
const hasComment = has(this.single, 'comment');
|
|
47
|
+
if (hasComment) this.comment(this.single.comment);
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
TableCompiler_PG.prototype.addColumns = function (
|
|
51
|
+
columns,
|
|
52
|
+
prefix,
|
|
53
|
+
colCompilers
|
|
54
|
+
) {
|
|
55
|
+
if (prefix === this.alterColumnsPrefix) {
|
|
56
|
+
// alter columns
|
|
57
|
+
for (const col of colCompilers) {
|
|
58
|
+
const quotedTableName = this.tableName();
|
|
59
|
+
const type = col.getColumnType();
|
|
60
|
+
// We'd prefer to call this.formatter.wrapAsIdentifier here instead, however the context passed to
|
|
61
|
+
// `this` instance is not that of the column, but of the table. Thus, we unfortunately have to call
|
|
62
|
+
// `wrapIdentifier` here as well (it is already called once on the initial column operation) to give
|
|
63
|
+
// our `alter` operation the correct `queryContext`. Refer to issue #2606 and PR #2612.
|
|
64
|
+
const colName = this.client.wrapIdentifier(
|
|
65
|
+
col.getColumnName(),
|
|
66
|
+
col.columnBuilder.queryContext()
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
this.pushQuery({
|
|
70
|
+
sql: `alter table ${quotedTableName} alter column ${colName} drop default`,
|
|
71
|
+
bindings: [],
|
|
72
|
+
});
|
|
73
|
+
this.pushQuery({
|
|
74
|
+
sql: `alter table ${quotedTableName} alter column ${colName} drop not null`,
|
|
75
|
+
bindings: [],
|
|
76
|
+
});
|
|
77
|
+
this.pushQuery({
|
|
78
|
+
sql: `alter table ${quotedTableName} alter column ${colName} type ${type} using (${colName}::${type})`,
|
|
79
|
+
bindings: [],
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
const defaultTo = col.modified['defaultTo'];
|
|
83
|
+
if (defaultTo) {
|
|
84
|
+
const modifier = col.defaultTo.apply(col, defaultTo);
|
|
85
|
+
this.pushQuery({
|
|
86
|
+
sql: `alter table ${quotedTableName} alter column ${colName} set ${modifier}`,
|
|
87
|
+
bindings: [],
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
const nullable = col.modified['nullable'];
|
|
92
|
+
if (nullable && nullable[0] === false) {
|
|
93
|
+
this.pushQuery({
|
|
94
|
+
sql: `alter table ${quotedTableName} alter column ${colName} set not null`,
|
|
95
|
+
bindings: [],
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
} else {
|
|
100
|
+
// base class implementation for normal add
|
|
101
|
+
TableCompiler.prototype.addColumns.call(this, columns, prefix);
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
// Compiles the comment on the table.
|
|
106
|
+
TableCompiler_PG.prototype.comment = function (comment) {
|
|
107
|
+
this.pushQuery(
|
|
108
|
+
`comment on table ${this.tableName()} is '${this.single.comment}'`
|
|
109
|
+
);
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
// Indexes:
|
|
113
|
+
// -------
|
|
114
|
+
|
|
115
|
+
TableCompiler_PG.prototype.primary = function (columns, constraintName) {
|
|
116
|
+
constraintName = constraintName
|
|
117
|
+
? this.formatter.wrap(constraintName)
|
|
118
|
+
: this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
|
119
|
+
this.pushQuery(
|
|
120
|
+
`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(
|
|
121
|
+
columns
|
|
122
|
+
)})`
|
|
123
|
+
);
|
|
124
|
+
};
|
|
125
|
+
TableCompiler_PG.prototype.unique = function (columns, indexName) {
|
|
126
|
+
indexName = indexName
|
|
127
|
+
? this.formatter.wrap(indexName)
|
|
128
|
+
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
129
|
+
this.pushQuery(
|
|
130
|
+
`alter table ${this.tableName()} add constraint ${indexName}` +
|
|
131
|
+
' unique (' +
|
|
132
|
+
this.formatter.columnize(columns) +
|
|
133
|
+
')'
|
|
134
|
+
);
|
|
135
|
+
};
|
|
136
|
+
TableCompiler_PG.prototype.index = function (columns, indexName, indexType) {
|
|
137
|
+
indexName = indexName
|
|
138
|
+
? this.formatter.wrap(indexName)
|
|
139
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
140
|
+
this.pushQuery(
|
|
141
|
+
`create index ${indexName} on ${this.tableName()}${
|
|
142
|
+
(indexType && ` using ${indexType}`) || ''
|
|
143
|
+
}` +
|
|
144
|
+
' (' +
|
|
145
|
+
this.formatter.columnize(columns) +
|
|
146
|
+
')'
|
|
147
|
+
);
|
|
148
|
+
};
|
|
149
|
+
TableCompiler_PG.prototype.dropPrimary = function (constraintName) {
|
|
150
|
+
constraintName = constraintName
|
|
151
|
+
? this.formatter.wrap(constraintName)
|
|
152
|
+
: this.formatter.wrap(this.tableNameRaw + '_pkey');
|
|
153
|
+
this.pushQuery(
|
|
154
|
+
`alter table ${this.tableName()} drop constraint ${constraintName}`
|
|
155
|
+
);
|
|
156
|
+
};
|
|
157
|
+
TableCompiler_PG.prototype.dropIndex = function (columns, indexName) {
|
|
158
|
+
indexName = indexName
|
|
159
|
+
? this.formatter.wrap(indexName)
|
|
160
|
+
: this._indexCommand('index', this.tableNameRaw, columns);
|
|
161
|
+
indexName = this.schemaNameRaw
|
|
162
|
+
? `${this.formatter.wrap(this.schemaNameRaw)}.${indexName}`
|
|
163
|
+
: indexName;
|
|
164
|
+
this.pushQuery(`drop index ${indexName}`);
|
|
165
|
+
};
|
|
166
|
+
TableCompiler_PG.prototype.dropUnique = function (columns, indexName) {
|
|
167
|
+
indexName = indexName
|
|
168
|
+
? this.formatter.wrap(indexName)
|
|
169
|
+
: this._indexCommand('unique', this.tableNameRaw, columns);
|
|
170
|
+
this.pushQuery(
|
|
171
|
+
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
172
|
+
);
|
|
173
|
+
};
|
|
174
|
+
TableCompiler_PG.prototype.dropForeign = function (columns, indexName) {
|
|
175
|
+
indexName = indexName
|
|
176
|
+
? this.formatter.wrap(indexName)
|
|
177
|
+
: this._indexCommand('foreign', this.tableNameRaw, columns);
|
|
178
|
+
this.pushQuery(
|
|
179
|
+
`alter table ${this.tableName()} drop constraint ${indexName}`
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
module.exports = TableCompiler_PG;
|