110 lines
2.9 KiB
JavaScript
Raw Normal View History

2015-05-09 13:58:18 -04:00
// PostgreSQL Schema Compiler
// -------
const inherits = require('inherits');
const SchemaCompiler = require('../../../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
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.hasTable = function (tableName) {
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 {
Add redshift support without changing cli or package.json (#2233) * 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
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({
sql,
bindings,
output(resp) {
2015-05-09 13:58:18 -04:00
return resp.rows.length > 0;
},
2015-05-09 13:58:18 -04:00
});
};
// Compile the query to determine if a column exists in a table.
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
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 {
Add redshift support without changing cli or package.json (#2233) * 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
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({
sql,
bindings,
output(resp) {
2015-05-09 13:58:18 -04:00
return resp.rows.length > 0;
},
2015-05-09 13:58:18 -04:00
});
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
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.
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.renameTable = function (from, to) {
this.pushQuery(
`alter table ${this.qualifiedTableName(
from
)} rename to ${this.formatter.wrap(to)}`
);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
this.pushQuery(`create schema ${this.formatter.wrap(schemaName)}`);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.createSchemaIfNotExists = function (schemaName) {
this.pushQuery(
`create schema if not exists ${this.formatter.wrap(schemaName)}`
);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.dropSchema = function (schemaName) {
this.pushQuery(`drop schema ${this.formatter.wrap(schemaName)}`);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.dropSchemaIfExists = function (schemaName) {
this.pushQuery(`drop schema if exists ${this.formatter.wrap(schemaName)}`);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.dropExtension = function (extensionName) {
this.pushQuery(`drop extension ${this.formatter.wrap(extensionName)}`);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.dropExtensionIfExists = function (extensionName) {
this.pushQuery(
`drop extension if exists ${this.formatter.wrap(extensionName)}`
);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.createExtension = function (extensionName) {
this.pushQuery(`create extension ${this.formatter.wrap(extensionName)}`);
2015-05-09 13:58:18 -04:00
};
2020-04-19 00:40:23 +02:00
SchemaCompiler_PG.prototype.createExtensionIfNotExists = function (
extensionName
) {
this.pushQuery(
`create extension if not exists ${this.formatter.wrap(extensionName)}`
);
2015-05-09 13:58:18 -04:00
};
module.exports = SchemaCompiler_PG;