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-03-02 16:52:32 +01:00
|
|
|
import {has} from 'lodash'
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
|
|
|
|
// 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:
|
|
|
|
// -------
|
|
|
|
|
|
|
|
TableCompiler_PG.prototype.primary = function(columns) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`alter table ${this.tableName()} add 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) + ')');
|
|
|
|
};
|
|
|
|
TableCompiler_PG.prototype.dropPrimary = function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
const constraintName = this.formatter.wrap(this.tableNameRaw + '_pkey');
|
|
|
|
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);
|
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;
|