mirror of
https://github.com/knex/knex.git
synced 2025-12-29 07:59:31 +00:00
add schema support for schema builder
This commit is contained in:
parent
402dbd9903
commit
3f8f50425f
@ -13,9 +13,19 @@ inherits(SchemaCompiler_PG, SchemaCompiler);
|
||||
|
||||
// Check whether the current table
|
||||
SchemaCompiler_PG.prototype.hasTable = function (tableName) {
|
||||
var sql = 'select * from information_schema.tables where table_name = ?';
|
||||
var bindings = [tableName];
|
||||
|
||||
if (this.schema) {
|
||||
sql += ' and table_schema = ?';
|
||||
bindings.push(this.schema);
|
||||
} else {
|
||||
sql += ' and table_schema = current_schema';
|
||||
}
|
||||
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.tables where table_name = ?',
|
||||
bindings: [tableName],
|
||||
sql: sql,
|
||||
bindings: bindings,
|
||||
output: function output(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
@ -24,18 +34,33 @@ SchemaCompiler_PG.prototype.hasTable = function (tableName) {
|
||||
|
||||
// Compile the query to determine if a column exists in a table.
|
||||
SchemaCompiler_PG.prototype.hasColumn = function (tableName, columnName) {
|
||||
var sql = 'select * from information_schema.columns where table_name = ? and column_name = ?';
|
||||
var bindings = [tableName, columnName];
|
||||
|
||||
if (this.schema) {
|
||||
sql += ' and table_schema = ?';
|
||||
bindings.push(this.schema);
|
||||
} else {
|
||||
sql += ' and table_schema = current_schema';
|
||||
}
|
||||
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.columns where table_name = ? and column_name = ?',
|
||||
bindings: [tableName, columnName],
|
||||
sql: sql,
|
||||
bindings: bindings,
|
||||
output: function output(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SchemaCompiler_PG.prototype.qualifiedTableName = function (tableName) {
|
||||
var name = this.schema ? this.schema + '.' + tableName : tableName;
|
||||
return this.formatter.wrap(name);
|
||||
};
|
||||
|
||||
// Compile a rename table command.
|
||||
SchemaCompiler_PG.prototype.renameTable = function (from, to) {
|
||||
this.pushQuery('alter table ' + this.formatter.wrap(from) + ' rename to ' + this.formatter.wrap(to));
|
||||
this.pushQuery('alter table ' + this.qualifiedTableName(from) + ' rename to ' + this.qualifiedTableName(to));
|
||||
};
|
||||
|
||||
SchemaCompiler_PG.prototype.createSchema = function (schemaName) {
|
||||
|
||||
@ -30,6 +30,11 @@ _.each(['createTable', 'createTableIfNotExists', 'createSchema', 'createSchemaIf
|
||||
|
||||
require('../interface')(SchemaBuilder);
|
||||
|
||||
SchemaBuilder.prototype.using = function (schemaName) {
|
||||
this._schema = schemaName;
|
||||
return this;
|
||||
};
|
||||
|
||||
SchemaBuilder.prototype.toString = function () {
|
||||
return this.toQuery();
|
||||
};
|
||||
|
||||
@ -9,6 +9,7 @@ var assign = require('lodash/object/assign');
|
||||
function SchemaCompiler(client, builder) {
|
||||
this.builder = builder;
|
||||
this.client = client;
|
||||
this.schema = builder._schema;
|
||||
this.formatter = client.formatter();
|
||||
this.sequence = [];
|
||||
}
|
||||
@ -26,11 +27,11 @@ assign(SchemaCompiler.prototype, {
|
||||
alterTable: buildTable('alter'),
|
||||
|
||||
dropTable: function dropTable(tableName) {
|
||||
this.pushQuery('drop table ' + this.formatter.wrap(tableName));
|
||||
this.pushQuery('drop table ' + this.formatter.wrap(prefixedTableName(this.schema, tableName)));
|
||||
},
|
||||
|
||||
dropTableIfExists: function dropTableIfExists(tableName) {
|
||||
this.pushQuery('drop table if exists ' + this.formatter.wrap(tableName));
|
||||
this.pushQuery('drop table if exists ' + this.formatter.wrap(prefixedTableName(this.schema, tableName)));
|
||||
},
|
||||
|
||||
raw: function raw(sql, bindings) {
|
||||
@ -50,11 +51,20 @@ assign(SchemaCompiler.prototype, {
|
||||
|
||||
function buildTable(type) {
|
||||
return function (tableName, fn) {
|
||||
var sql = this.client.tableBuilder(type, tableName, fn).toSQL();
|
||||
var builder = this.client.tableBuilder(type, tableName, fn);
|
||||
var sql;
|
||||
|
||||
builder.setSchema(this.schema);
|
||||
sql = builder.toSQL();
|
||||
|
||||
for (var i = 0, l = sql.length; i < l; i++) {
|
||||
this.sequence.push(sql[i]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function prefixedTableName(prefix, table) {
|
||||
return prefix ? prefix + '.' + table : table;
|
||||
}
|
||||
|
||||
module.exports = SchemaCompiler;
|
||||
@ -16,11 +16,16 @@ function TableBuilder(client, method, tableName, fn) {
|
||||
this.client = client;
|
||||
this._fn = fn;
|
||||
this._method = method;
|
||||
this._schemaName = undefined;
|
||||
this._tableName = tableName;
|
||||
this._statements = [];
|
||||
this._single = {};
|
||||
}
|
||||
|
||||
TableBuilder.prototype.setSchema = function (schemaName) {
|
||||
this._schemaName = schemaName;
|
||||
};
|
||||
|
||||
// Convert the current tableBuilder object "toSQL"
|
||||
// giving us additional methods if we're altering
|
||||
// rather than creating the table.
|
||||
|
||||
@ -10,6 +10,7 @@ var normalizeArr = require('../helpers').normalizeArr;
|
||||
function TableCompiler(client, tableBuilder) {
|
||||
this.client = client;
|
||||
this.method = tableBuilder._method;
|
||||
this.schemaNameRaw = tableBuilder._schemaName;
|
||||
this.tableNameRaw = tableBuilder._tableName;
|
||||
this.single = tableBuilder._single;
|
||||
this.grouped = _.groupBy(tableBuilder._statements, 'grouping');
|
||||
@ -118,7 +119,9 @@ TableCompiler.prototype.getColumns = function () {
|
||||
};
|
||||
|
||||
TableCompiler.prototype.tableName = function () {
|
||||
return this.formatter.wrap(this.tableNameRaw);
|
||||
var name = this.schemaNameRaw ? this.schemaNameRaw + '.' + this.tableNameRaw : this.tableNameRaw;
|
||||
|
||||
return this.formatter.wrap(name);
|
||||
};
|
||||
|
||||
// Generate all of the alter column statements necessary for the query.
|
||||
|
||||
@ -12,9 +12,19 @@ inherits(SchemaCompiler_PG, SchemaCompiler);
|
||||
|
||||
// Check whether the current table
|
||||
SchemaCompiler_PG.prototype.hasTable = function(tableName) {
|
||||
var sql = 'select * from information_schema.tables where table_name = ?';
|
||||
var bindings = [tableName];
|
||||
|
||||
if (this.schema) {
|
||||
sql += ' and table_schema = ?';
|
||||
bindings.push(this.schema);
|
||||
} else {
|
||||
sql += ' and table_schema = current_schema';
|
||||
}
|
||||
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.tables where table_name = ?',
|
||||
bindings: [tableName],
|
||||
sql: sql,
|
||||
bindings: bindings,
|
||||
output: function(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
@ -23,18 +33,33 @@ SchemaCompiler_PG.prototype.hasTable = function(tableName) {
|
||||
|
||||
// Compile the query to determine if a column exists in a table.
|
||||
SchemaCompiler_PG.prototype.hasColumn = function(tableName, columnName) {
|
||||
var sql = 'select * from information_schema.columns where table_name = ? and column_name = ?';
|
||||
var bindings = [tableName, columnName];
|
||||
|
||||
if (this.schema) {
|
||||
sql += ' and table_schema = ?';
|
||||
bindings.push(this.schema);
|
||||
} else {
|
||||
sql += ' and table_schema = current_schema';
|
||||
}
|
||||
|
||||
this.pushQuery({
|
||||
sql: 'select * from information_schema.columns where table_name = ? and column_name = ?',
|
||||
bindings: [tableName, columnName],
|
||||
sql: sql,
|
||||
bindings: bindings,
|
||||
output: function(resp) {
|
||||
return resp.rows.length > 0;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
SchemaCompiler_PG.prototype.qualifiedTableName = function(tableName) {
|
||||
var name = this.schema ? `${this.schema}.${tableName}` : tableName;
|
||||
return this.formatter.wrap(name);
|
||||
};
|
||||
|
||||
// Compile a rename table command.
|
||||
SchemaCompiler_PG.prototype.renameTable = function(from, to) {
|
||||
this.pushQuery('alter table ' + this.formatter.wrap(from) + ' rename to ' + this.formatter.wrap(to));
|
||||
this.pushQuery('alter table ' + this.qualifiedTableName(from) + ' rename to ' + this.qualifiedTableName(to));
|
||||
};
|
||||
|
||||
SchemaCompiler_PG.prototype.createSchema = function(schemaName) {
|
||||
|
||||
@ -48,6 +48,11 @@ _.each([
|
||||
|
||||
require('../interface')(SchemaBuilder)
|
||||
|
||||
SchemaBuilder.prototype.using = function(schemaName) {
|
||||
this._schema = schemaName;
|
||||
return this;
|
||||
}
|
||||
|
||||
SchemaBuilder.prototype.toString = function() {
|
||||
return this.toQuery()
|
||||
}
|
||||
|
||||
@ -8,6 +8,7 @@ var assign = require('lodash/object/assign');
|
||||
function SchemaCompiler(client, builder) {
|
||||
this.builder = builder
|
||||
this.client = client
|
||||
this.schema = builder._schema;
|
||||
this.formatter = client.formatter()
|
||||
this.sequence = []
|
||||
}
|
||||
@ -25,11 +26,11 @@ assign(SchemaCompiler.prototype, {
|
||||
alterTable: buildTable('alter'),
|
||||
|
||||
dropTable: function(tableName) {
|
||||
this.pushQuery('drop table ' + this.formatter.wrap(tableName))
|
||||
this.pushQuery('drop table ' + this.formatter.wrap(prefixedTableName(this.schema, tableName)));
|
||||
},
|
||||
|
||||
dropTableIfExists: function(tableName) {
|
||||
this.pushQuery('drop table if exists ' + this.formatter.wrap(tableName));
|
||||
this.pushQuery('drop table if exists ' + this.formatter.wrap(prefixedTableName(this.schema, tableName)));
|
||||
},
|
||||
|
||||
raw: function(sql, bindings) {
|
||||
@ -49,12 +50,20 @@ assign(SchemaCompiler.prototype, {
|
||||
|
||||
function buildTable(type) {
|
||||
return function(tableName, fn) {
|
||||
var sql = this.client.tableBuilder(type, tableName, fn).toSQL();
|
||||
var builder = this.client.tableBuilder(type, tableName, fn);
|
||||
var sql;
|
||||
|
||||
builder.setSchema(this.schema);
|
||||
sql = builder.toSQL();
|
||||
|
||||
for (var i = 0, l = sql.length; i < l; i++) {
|
||||
this.sequence.push(sql[i]);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
function prefixedTableName(prefix, table) {
|
||||
return prefix ? `${prefix}.${table}` : table;
|
||||
}
|
||||
|
||||
module.exports = SchemaCompiler;
|
||||
|
||||
@ -14,11 +14,16 @@ function TableBuilder(client, method, tableName, fn) {
|
||||
this.client = client
|
||||
this._fn = fn;
|
||||
this._method = method;
|
||||
this._schemaName = undefined;
|
||||
this._tableName = tableName;
|
||||
this._statements = [];
|
||||
this._single = {};
|
||||
}
|
||||
|
||||
TableBuilder.prototype.setSchema = function(schemaName) {
|
||||
this._schemaName = schemaName;
|
||||
};
|
||||
|
||||
// Convert the current tableBuilder object "toSQL"
|
||||
// giving us additional methods if we're altering
|
||||
// rather than creating the table.
|
||||
|
||||
@ -8,6 +8,7 @@ var normalizeArr = require('../helpers').normalizeArr
|
||||
function TableCompiler(client, tableBuilder) {
|
||||
this.client = client
|
||||
this.method = tableBuilder._method;
|
||||
this.schemaNameRaw = tableBuilder._schemaName;
|
||||
this.tableNameRaw = tableBuilder._tableName;
|
||||
this.single = tableBuilder._single;
|
||||
this.grouped = _.groupBy(tableBuilder._statements, 'grouping');
|
||||
@ -115,7 +116,11 @@ TableCompiler.prototype.getColumns = function() {
|
||||
};
|
||||
|
||||
TableCompiler.prototype.tableName = function() {
|
||||
return this.formatter.wrap(this.tableNameRaw);
|
||||
var name = this.schemaNameRaw ?
|
||||
`${this.schemaNameRaw}.${this.tableNameRaw}`
|
||||
: this.tableNameRaw;
|
||||
|
||||
return this.formatter.wrap(name);
|
||||
};
|
||||
|
||||
// Generate all of the alter column statements necessary for the query.
|
||||
|
||||
@ -24,6 +24,7 @@ module.exports = function(knex) {
|
||||
.dropTableIfExists('catch_test')
|
||||
.dropTableIfExists('test_table_two')
|
||||
.dropTableIfExists('test_table_three')
|
||||
.dropTableIfExists('test_table_four')
|
||||
.dropTableIfExists('datatype_test')
|
||||
.dropTableIfExists('composite_key_test')
|
||||
.dropTableIfExists('charset_collate_test')
|
||||
|
||||
@ -30,18 +30,38 @@ describe("PostgreSQL SchemaBuilder", function() {
|
||||
expect(tableSql[0].sql).to.equal('alter table "users" add column "id" serial primary key, add column "email" varchar(255)');
|
||||
});
|
||||
|
||||
it("alter table with schema", function() {
|
||||
tableSql = client.schemaBuilder().using('myschema').table('users', function(table) {
|
||||
table.increments('id');
|
||||
}).toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('alter table "myschema"."users" add column "id" serial primary key');
|
||||
});
|
||||
|
||||
it("drop table", function() {
|
||||
tableSql = client.schemaBuilder().dropTable('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table "users"');
|
||||
});
|
||||
|
||||
it("drop table with schema", function() {
|
||||
tableSql = client.schemaBuilder().using('myschema').dropTable('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table "myschema"."users"');
|
||||
});
|
||||
|
||||
it("drop table if exists", function() {
|
||||
tableSql = client.schemaBuilder().dropTableIfExists('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table if exists "users"');
|
||||
});
|
||||
|
||||
it("drop table if exists with schema", function() {
|
||||
tableSql = client.schemaBuilder().using('myschema').dropTableIfExists('users').toSQL();
|
||||
equal(1, tableSql.length);
|
||||
expect(tableSql[0].sql).to.equal('drop table if exists "myschema"."users"');
|
||||
});
|
||||
|
||||
it("drop column", function() {
|
||||
tableSql = client.schemaBuilder().table('users', function(table) {
|
||||
table.dropColumn('foo');
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user