2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
// PostgreSQL Column Compiler
|
|
|
|
// -------
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import ColumnCompiler from '../../../schema/columncompiler';
|
|
|
|
import * as helpers from '../../../helpers';
|
2016-03-02 17:07:05 +01:00
|
|
|
|
2016-05-18 20:22:50 +10:00
|
|
|
import { assign } from 'lodash'
|
2016-03-02 17:07:05 +01:00
|
|
|
|
|
|
|
function ColumnCompiler_PG() {
|
|
|
|
ColumnCompiler.apply(this, arguments);
|
|
|
|
this.modifiers = ['nullable', 'defaultTo', 'comment']
|
|
|
|
}
|
|
|
|
inherits(ColumnCompiler_PG, ColumnCompiler);
|
|
|
|
|
|
|
|
assign(ColumnCompiler_PG.prototype, {
|
|
|
|
|
|
|
|
// Types
|
|
|
|
// ------
|
|
|
|
bigincrements: 'bigserial primary key',
|
|
|
|
bigint: 'bigint',
|
|
|
|
binary: 'bytea',
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
bit(column) {
|
|
|
|
return column.length !== false ? `bit(${column.length})` : 'bit';
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
bool: 'boolean',
|
|
|
|
|
|
|
|
// Create the column definition for an enum type.
|
|
|
|
// Using method "2" here: http://stackoverflow.com/a/10984951/525714
|
2016-05-17 01:01:34 +10:00
|
|
|
enu(allowed) {
|
|
|
|
return `text check (${this.formatter.wrap(this.args[0])} in ('${allowed.join("', '")}'))`;
|
2016-03-02 17:07:05 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
double: 'double precision',
|
2017-11-30 15:05:39 -06:00
|
|
|
decimal(precision, scale) {
|
|
|
|
if (precision === null) return 'decimal';
|
|
|
|
return `decimal(${this._num(precision, 8)}, ${this._num(scale, 2)})`;
|
|
|
|
},
|
2016-03-02 17:07:05 +01:00
|
|
|
floating: 'real',
|
|
|
|
increments: 'serial primary key',
|
2016-05-17 01:01:34 +10:00
|
|
|
json(jsonb) {
|
2016-03-02 17:07:05 +01:00
|
|
|
if (jsonb) helpers.deprecate('json(true)', 'jsonb()')
|
|
|
|
return jsonColumn(this.client, jsonb);
|
|
|
|
},
|
2016-05-17 01:01:34 +10:00
|
|
|
jsonb() {
|
2016-03-02 17:07:05 +01:00
|
|
|
return jsonColumn(this.client, true);
|
|
|
|
},
|
|
|
|
smallint: 'smallint',
|
|
|
|
tinyint: 'smallint',
|
2016-05-17 01:01:34 +10:00
|
|
|
datetime(without) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return without ? 'timestamp' : 'timestamptz';
|
|
|
|
},
|
2016-05-17 01:01:34 +10:00
|
|
|
timestamp(without) {
|
2016-03-02 17:07:05 +01:00
|
|
|
return without ? 'timestamp' : 'timestamptz';
|
|
|
|
},
|
|
|
|
uuid: 'uuid',
|
|
|
|
|
|
|
|
// Modifiers:
|
|
|
|
// ------
|
2016-05-17 01:01:34 +10:00
|
|
|
comment(comment) {
|
2017-09-28 01:54:20 -04:00
|
|
|
const columnName = this.args[0] || this.defaults('columnName');
|
2017-11-30 15:05:39 -06:00
|
|
|
|
2016-03-02 17:07:05 +01:00
|
|
|
this.pushAdditional(function() {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`comment on column ${this.tableCompiler.tableName()}.` +
|
2017-09-28 01:54:20 -04:00
|
|
|
this.formatter.wrap(columnName) + " is " + (comment ? `'${comment}'` : 'NULL'));
|
2016-03-02 17:07:05 +01:00
|
|
|
}, comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
function jsonColumn(client, jsonb) {
|
|
|
|
if (!client.version || parseFloat(client.version) >= 9.2) return jsonb ? 'jsonb' : 'json';
|
|
|
|
return 'text';
|
|
|
|
}
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default ColumnCompiler_PG;
|