2016-05-17 01:01:34 +10:00
|
|
|
/* eslint max-len: 0 */
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// PostgreSQL Table Builder & Compiler
|
|
|
|
// -------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import TableCompiler from '../../../schema/tablecompiler';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { has } from 'lodash'
|
2016-03-02 16:52:32 +01:00
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
function TableCompiler_PG() {
|
|
|
|
TableCompiler.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(TableCompiler_PG, TableCompiler);
|
|
|
|
|
|
|
|
// Compile a rename column command.
|
|
|
|
TableCompiler_PG.prototype.renameColumn = function(from, to) {
|
|
|
|
return this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${this.tableName()} rename ${this.formatter.wrap(from)} to ${this.formatter.wrap(to)}`
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
TableCompiler_PG.prototype.compileAdd = function(builder) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const table = this.formatter.wrap(builder);
|
|
|
|
const columns = this.prefixArray('add column', this.getColumns(builder));
|
2015-05-09 13:58:18 -04:00
|
|
|
return this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql: `alter table ${table} ${columns.join(', ')}`
|
2015-05-09 13:58:18 -04:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Adds the "create" query to the query sequence.
|
|
|
|
TableCompiler_PG.prototype.createQuery = function(columns, ifNot) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const createStatement = ifNot ? 'create table if not exists ' : 'create table ';
|
|
|
|
let sql = createStatement + this.tableName() + ' (' + columns.sql.join(', ') + ')';
|
|
|
|
if (this.single.inherits) sql += ` inherits (${this.formatter.wrap(this.single.inherits)})`;
|
2015-05-09 13:58:18 -04:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
2015-05-09 13:58:18 -04:00
|
|
|
bindings: columns.bindings
|
|
|
|
});
|
2016-05-17 01:01:34 +10:00
|
|
|
const hasComment = has(this.single, 'comment');
|
2015-05-09 13:58:18 -04:00
|
|
|
if (hasComment) this.comment(this.single.comment);
|
|
|
|
};
|
|
|
|
|
2017-02-16 20:34:59 +02:00
|
|
|
TableCompiler_PG.prototype.addColumns = function(columns, prefix, colCompilers) {
|
|
|
|
if (prefix === this.alterColumnsPrefix) {
|
|
|
|
// alter columns
|
|
|
|
for (const col of colCompilers) {
|
|
|
|
const quotedTableName = this.tableName();
|
|
|
|
const colName = col.getColumnName();
|
|
|
|
const type = col.getColumnType();
|
|
|
|
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${quotedTableName} alter column "${colName}" drop default`,
|
|
|
|
bindings: []
|
|
|
|
});
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${quotedTableName} alter column "${colName}" drop not null`,
|
|
|
|
bindings: []
|
|
|
|
});
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${quotedTableName} alter column "${colName}" type ${type} using ("${colName}"::${type})`,
|
|
|
|
bindings: []
|
|
|
|
});
|
|
|
|
|
|
|
|
const defaultTo = col.modified['defaultTo'];
|
|
|
|
if (defaultTo) {
|
|
|
|
const modifier = col.defaultTo.apply(col, defaultTo);
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${quotedTableName} alter column "${colName}" set ${modifier}`,
|
|
|
|
bindings: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const nullable = col.modified['nullable'];
|
|
|
|
if (nullable && nullable[0] === false) {
|
|
|
|
this.pushQuery({
|
|
|
|
sql: `alter table ${quotedTableName} alter column "${colName}" set not null`,
|
|
|
|
bindings: []
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// base class implementation for normal add
|
|
|
|
TableCompiler.prototype.addColumns.call(this, columns, prefix);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// Compiles the comment on the table.
|
|
|
|
TableCompiler_PG.prototype.comment = function(comment) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`comment on table ${this.tableName()} is '${this.single.comment || ''}'`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
// Indexes:
|
|
|
|
// -------
|
|
|
|
|
2016-05-19 21:30:17 +02:00
|
|
|
TableCompiler_PG.prototype.primary = function(columns, constraintName) {
|
2016-05-19 21:45:01 +02:00
|
|
|
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(`${this.tableNameRaw}_pkey`);
|
2016-05-19 21:30:17 +02:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add constraint ${constraintName} primary key (${this.formatter.columnize(columns)})`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.unique = function(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add constraint ${indexName}` +
|
2015-05-09 13:58:18 -04:00
|
|
|
' unique (' + this.formatter.columnize(columns) + ')');
|
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.index = function(columns, indexName, indexType) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create index ${indexName} on ${this.tableName()}${indexType && (` using ${indexType}`) || ''}` +
|
2015-05-09 13:58:18 -04:00
|
|
|
' (' + this.formatter.columnize(columns) + ')');
|
|
|
|
};
|
2016-05-19 21:30:17 +02:00
|
|
|
TableCompiler_PG.prototype.dropPrimary = function(constraintName) {
|
|
|
|
constraintName = constraintName ? this.formatter.wrap(constraintName) : this.formatter.wrap(this.tableNameRaw + '_pkey');
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${constraintName}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.dropIndex = function(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('index', this.tableNameRaw, columns);
|
2017-06-09 18:07:57 -04:00
|
|
|
indexName = this.schemaNameRaw ? `${this.formatter.wrap(this.schemaNameRaw)}.${indexName}` : indexName;
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop index ${indexName}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.dropUnique = function(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('unique', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.dropForeign = function(columns, indexName) {
|
2015-07-15 23:16:30 -03:00
|
|
|
indexName = indexName ? this.formatter.wrap(indexName) : this._indexCommand('foreign', this.tableNameRaw, columns);
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} drop constraint ${indexName}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default TableCompiler_PG;
|