knex/src/dialects/redshift/schema/tablecompiler.js
Adam Cofer ee8cc35ecb Fix #2545, bring redshift wrapIdentifier custom hook up to date with postgres (#2551)
* Add a Redshift dialect that inherits from Postgres.

* Turn .index() and .dropIndex() into no-ops with warnings in the Redshift dialect.

* Update the Redshift dialect to be compatible with master.

* Update package.json

* Disable liftoff cli

* Remove the CLI

* Add lib to the repo

* Allow the escaping of named bindings.

* Update dist

* Update the Redshift dialect’s instantiation of the query and column compilers.

* Update the distribution

* Fix a merge conflict

* Take lib back out

* Trying to bring back in line with tgreisser/knex

* Add npm 5 package-lock

* Bring cli.js back in line

* Bring cli.js back in line

* Progress commit on redshift integration tests

* Revert "Progress commit on redshift integration tests"

This reverts commit 207e31635c638853dec54ce0580d34559ba5a54c.

* Progress commit

* Working not null on primary columns in createTable

* Working redshift unit tests

* Working unit and integration tests, still need to fix migration tests

* Brought datatypes more in line with what redshift actually supports

* Added query compiler unit tests

* Add a hacky returning clause for redshift ugh

* Working migration integration tests

* Working insert integration tests

* Allow multiple insert returning values

* Working select integration tests

* Working join integration tests

* Working aggregate integration tests

* All integration suite tests working

* Put docker index for reconnect tests back

* Redshift does not support insert...returning, there does not seem to be a way around that, therefore accept it and test accordingly

* Leave redshift integration tests in place, but do not run them by default

* Fix mysql order by test

* Fix more tests

* Change test db name to knex_test for consistency

* Address PR comments

* Sublime Text gitignore

* Redshift does not support adding more than one column in alter table

* Fix integration tests for redshift

* Linter

* Combine dialect test skip if clause
2018-04-05 01:43:39 +03:00

93 lines
3.2 KiB
JavaScript

/* eslint max-len: 0 */
// Redshift Table Builder & Compiler
// -------
import { warn } from '../../../helpers';
import inherits from 'inherits';
import { has } from 'lodash';
import TableCompiler_PG from '../../postgres/schema/tablecompiler';
function TableCompiler_Redshift() {
TableCompiler_PG.apply(this, arguments);
}
inherits(TableCompiler_Redshift, TableCompiler_PG);
TableCompiler_Redshift.prototype.index = function(columns, indexName, indexType) {
warn('Redshift does not support the creation of indexes.');
};
TableCompiler_Redshift.prototype.dropIndex = function(columns, indexName) {
warn('Redshift does not support the deletion of indexes.');
};
// TODO: have to disable setting not null on columns that already exist...
// Adds the "create" query to the query sequence.
TableCompiler_Redshift.prototype.createQuery = function(columns, ifNot) {
const createStatement = ifNot ? 'create table if not exists ' : 'create table ';
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
if (this.single.inherits) sql += ` like (${this.formatter.wrap(this.single.inherits)})`;
this.pushQuery({
sql,
bindings: columns.bindings
});
const hasComment = has(this.single, 'comment');
if (hasComment) this.comment(this.single.comment);
};
TableCompiler_Redshift.prototype.primary = function(columns, constraintName) {
const self = this;
constraintName = constraintName ? self.formatter.wrap(constraintName) : self.formatter.wrap(`${this.tableNameRaw}_pkey`);
if (columns.constructor !== Array){
columns = [columns];
}
const thiscolumns = self.grouped.columns;
if (thiscolumns) {
for (let i = 0; i < columns.length; i++){
let exists = thiscolumns.find(tcb => tcb.grouping === "columns" &&
tcb.builder &&
tcb.builder._method === "add" &&
tcb.builder._args &&
tcb.builder._args.indexOf(columns[i]) > -1);
if (exists) {
exists = exists.builder;
}
const nullable = !(exists &&
exists._modifiers &&
exists._modifiers["nullable"] &&
exists._modifiers["nullable"][0] === false);
if (nullable){
if (exists){
return warn("Redshift does not allow primary keys to contain nullable columns.");
} else {
return warn("Redshift does not allow primary keys to contain nonexistent columns.");
}
}
}
}
return self.pushQuery(`alter table ${self.tableName()} add constraint ${constraintName} primary key (${self.formatter.columnize(columns)})`);
};
// Compiles column add. Redshift can only add one column per ALTER TABLE, so core addColumns doesn't work. #2545
TableCompiler_Redshift.prototype.addColumns = function (columns, prefix, colCompilers) {
if (prefix === this.alterColumnsPrefix) {
TableCompiler_PG.prototype.addColumns.call(this, columns, prefix, colCompilers);
} else {
prefix = prefix || this.addColumnsPrefix;
colCompilers = colCompilers || this.getColumns();
for (const col of colCompilers) {
const quotedTableName = this.tableName();
const colCompiled = col.compileColumn();
this.pushQuery({
sql: `alter table ${quotedTableName} ${prefix}${colCompiled}`,
bindings: []
});
}
}
};
export default TableCompiler_Redshift;