2015-05-09 13:58:18 -04:00
|
|
|
// PostgreSQL Schema Compiler
|
|
|
|
// -------
|
|
|
|
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
import inherits from 'inherits';
|
|
|
|
import SchemaCompiler from '../../../schema/compiler';
|
2015-05-09 13:58:18 -04:00
|
|
|
|
|
|
|
function SchemaCompiler_PG() {
|
|
|
|
SchemaCompiler.apply(this, arguments);
|
|
|
|
}
|
|
|
|
inherits(SchemaCompiler_PG, SchemaCompiler);
|
|
|
|
|
|
|
|
// Check whether the current table
|
|
|
|
SchemaCompiler_PG.prototype.hasTable = function(tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
let sql = 'select * from information_schema.tables where table_name = ?';
|
|
|
|
const bindings = [tableName];
|
2015-08-09 22:24:55 -03:00
|
|
|
|
|
|
|
if (this.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.schema);
|
|
|
|
} else {
|
2018-02-03 08:33:02 -05:00
|
|
|
sql += ' and table_schema = current_schema()';
|
2015-08-09 22:24:55 -03:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
|
|
|
bindings,
|
|
|
|
output(resp) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return resp.rows.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Compile the query to determine if a column exists in a table.
|
|
|
|
SchemaCompiler_PG.prototype.hasColumn = function(tableName, columnName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
let sql = 'select * from information_schema.columns where table_name = ? and column_name = ?';
|
|
|
|
const bindings = [tableName, columnName];
|
2015-08-09 22:24:55 -03:00
|
|
|
|
|
|
|
if (this.schema) {
|
|
|
|
sql += ' and table_schema = ?';
|
|
|
|
bindings.push(this.schema);
|
|
|
|
} else {
|
2018-02-03 08:33:02 -05:00
|
|
|
sql += ' and table_schema = current_schema()';
|
2015-08-09 22:24:55 -03:00
|
|
|
}
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
this.pushQuery({
|
2016-05-17 01:01:34 +10:00
|
|
|
sql,
|
|
|
|
bindings,
|
|
|
|
output(resp) {
|
2015-05-09 13:58:18 -04:00
|
|
|
return resp.rows.length > 0;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2015-08-09 22:24:55 -03:00
|
|
|
SchemaCompiler_PG.prototype.qualifiedTableName = function(tableName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
const name = this.schema ? `${this.schema}.${tableName}` : tableName;
|
2015-08-09 22:24:55 -03:00
|
|
|
return this.formatter.wrap(name);
|
|
|
|
};
|
|
|
|
|
2015-05-09 13:58:18 -04:00
|
|
|
// Compile a rename table command.
|
|
|
|
SchemaCompiler_PG.prototype.renameTable = function(from, to) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(
|
|
|
|
`alter table ${this.qualifiedTableName(from)} rename to ${this.formatter.wrap(to)}`
|
|
|
|
);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.createSchema = function(schemaName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create schema ${this.formatter.wrap(schemaName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.createSchemaIfNotExists = function(schemaName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create schema if not exists ${this.formatter.wrap(schemaName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.dropSchema = function(schemaName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.dropSchemaIfExists = function(schemaName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.dropExtension = function(extensionName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop extension ${this.formatter.wrap(extensionName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.dropExtensionIfExists = function(extensionName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`drop extension if exists ${this.formatter.wrap(extensionName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.createExtension = function(extensionName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create extension ${this.formatter.wrap(extensionName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
SchemaCompiler_PG.prototype.createExtensionIfNotExists = function(extensionName) {
|
2016-05-17 01:01:34 +10:00
|
|
|
this.pushQuery(`create extension if not exists ${this.formatter.wrap(extensionName)}`);
|
2015-05-09 13:58:18 -04:00
|
|
|
};
|
|
|
|
|
2016-05-17 01:01:34 +10:00
|
|
|
export default SchemaCompiler_PG;
|