不要怂,就是干,撸起袖子干!

Commit e879523f by Daniel Durante

Default schemas should now be utilized when describing tables (this really only …

…affected Postgres users for the most part) and fixed sqlite/mysql having trouble with dropping the correct schemas. Closes #672
1 parent c9df7e97
......@@ -195,7 +195,7 @@ module.exports = (function() {
}
DAOFactory.prototype.drop = function() {
return this.QueryInterface.dropTable(this.tableName)
return this.QueryInterface.dropTable(this.getTableName(this.tableName))
}
DAOFactory.prototype.dropSchema = function(schema) {
......@@ -570,8 +570,8 @@ module.exports = (function() {
return this.QueryInterface.bulkUpdate(this.tableName, attrValueHash, where, options)
}
DAOFactory.prototype.describe = function() {
return this.QueryInterface.describeTable(this.tableName)
DAOFactory.prototype.describe = function(schema) {
return this.QueryInterface.describeTable(this.tableName, schema || this.options.schema || undefined)
}
DAOFactory.prototype.dataset = function() {
......
......@@ -92,8 +92,9 @@ module.exports = (function() {
dropTableQuery: function(tableName, options) {
options = options || {}
var query = "DROP TABLE IF EXISTS <%= table %><%= cascade %>;"
var query = "DROP TABLE IF EXISTS <%= schema %><%= table %><%= cascade %>;"
return Utils._.template(query)({
schema: options.schema ? this.quoteIdentifiers(options.schema) + '.' : '',
table: this.quoteIdentifiers(tableName),
cascade: options.cascade? " CASCADE" : ""
})
......@@ -111,10 +112,16 @@ module.exports = (function() {
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"
},
describeTableQuery: function(tableName) {
var query = 'SELECT c.column_name as "Field", c.column_default as "Default", c.is_nullable as "Null", c.data_type as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special" FROM information_schema.columns c WHERE table_name = <%= table %>;'
describeTableQuery: function(tableName, schema) {
if (!schema) {
schema = 'public';
}
var query = 'SELECT c.column_name as "Field", c.column_default as "Default", c.is_nullable as "Null", c.data_type as "Type", (SELECT array_agg(e.enumlabel) FROM pg_catalog.pg_type t JOIN pg_catalog.pg_enum e ON t.oid=e.enumtypid WHERE t.typname=c.udt_name) AS "special" FROM information_schema.columns c WHERE table_name = <%= table %> AND table_schema = <%= schema %>'
return Utils._.template(query)({
table: this.escape(tableName)
table: this.escape(tableName),
schema: this.escape(schema)
})
},
......
......@@ -14,9 +14,9 @@ module.exports = (function() {
options: {},
addSchema: function(opts) {
var tableName = undefined
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaPrefix = (!!opts && !!opts.options && !!opts.options.schemaPrefix ? opts.options.schemaPrefix : undefined)
var tableName = undefined
var schema = (!!opts && !!opts.options && !!opts.options.schema ? opts.options.schema : undefined)
var schemaDelimiter = (!!opts && !!opts.options && !!opts.options.schemaDelimiter ? opts.options.schemaDelimiter : undefined)
if (!!opts && !!opts.tableName) {
tableName = opts.tableName
......@@ -29,7 +29,7 @@ module.exports = (function() {
return tableName
}
return this.quoteIdentifier(schema) + (!schemaPrefix ? '.' : schemaPrefix) + this.quoteIdentifier(tableName)
return this.quoteIdentifier(schema) + (!schemaDelimiter ? '.' : schemaDelimiter) + this.quoteIdentifier(tableName)
},
createSchema: function() {
......@@ -398,9 +398,13 @@ module.exports = (function() {
return Utils._.template(sql, { tableName: tableName, indexName: indexName })
},
describeTableQuery: function(tableName) {
describeTableQuery: function(tableName, schema, schemaDelimiter) {
var options = {}
options.schema = schema || null
options.schemaDelimiter = schemaDelimiter || null
var sql = "PRAGMA TABLE_INFO('<%= tableName %>');"
return Utils._.template(sql, { tableName: tableName })
return Utils._.template(sql, { tableName: this.addSchema({tableName: tableName, options: options})})
},
renameTableQuery: function(before, after) {
......
......@@ -137,22 +137,34 @@ module.exports = (function() {
}).run()
}
QueryInterface.prototype.describeTable = function(tableName) {
QueryInterface.prototype.describeTable = function(tableName, options) {
var self = this
, schema = null
, schemaDelimiter = null
if (typeof options === "string") {
schema = options
}
else if (typeof options === "object") {
schema = options.schema || null
schemaDelimiter = options.schemaDelimiter || null
}
return new Utils.CustomEventEmitter(function(emitter) {
var sql;
if (self.QueryGenerator.describeTableQuery) {
sql = self.QueryGenerator.describeTableQuery(tableName)
sql = self.QueryGenerator.describeTableQuery(tableName, schema, schemaDelimiter)
} else {
sql = 'DESCRIBE `' + tableName + '`;'
sql = 'DESCRIBE ' + self.QueryGenerator.addSchema({tableName: tableName, options: {schema: schema, schemaDelimiter: schemaDelimiter}}) + ';'
}
self.sequelize.query(sql, null, { raw: true }).success(function(data) {
emitter.emit('success', data)
}).error(function(err) {
emitter.emit('error', err)
}).on('sql', function(sql) {
emitter.emit('sql', sql)
})
}).run()
}
......
......@@ -2483,7 +2483,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
})
if (dialect === "mysql") {
if (dialect === "mysql" || dialect === "sqlite") {
it("should take schemaDelimiter into account if applicable", function(done){
var UserSpecialUnderscore = this.sequelize.define('UserSpecialUnderscore', {age: Sequelize.INTEGER}, {schema: 'hello', schemaDelimiter: '_'})
var UserSpecialDblUnderscore = this.sequelize.define('UserSpecialDblUnderscore', {age: Sequelize.INTEGER})
......@@ -2503,6 +2503,49 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
})
}
it("should describeTable using the default schema settings", function(done) {
var self = this
, UserPublic = this.sequelize.define('Public', {
username: Sequelize.STRING
})
var _done = _.after(2, function() {
done()
})
UserPublic.sync({ force: true }).success(function() {
UserPublic.schema('special').sync({ force: true }).success(function() {
self.sequelize.queryInterface.describeTable('Publics')
.on('sql', function(sql) {
if (dialect === "sqlite" || dialect === "mysql") {
expect(sql).to.not.contain('special')
_done()
}
})
.success(function(table) {
if (dialect === "postgres" || dialect === "postgres-native") {
expect(table.id.defaultValue).to.not.contain('special')
_done()
}
self.sequelize.queryInterface.describeTable('Publics', 'special')
.on('sql', function(sql) {
if (dialect === "sqlite" || dialect === "mysql") {
expect(sql).to.contain('special')
_done()
}
})
.success(function(table) {
if (dialect === "postgres" || dialect === "postgres-native") {
expect(table.id.defaultValue).to.contain('special')
_done()
}
})
})
})
})
})
it("should be able to create and update records under any valid schematic", function(done){
var self = this
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!