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

Commit 36447ba5 by Shawn Woodtke Committed by sdepold

Fix issues found while attempting to use migration functionality with postgres.

1 parent 72291654
......@@ -92,6 +92,11 @@ module.exports = (function() {
return "SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';"
},
describeTableQuery: function(tableName) {
var query = 'SELECT column_name as "Field", column_default as "Default", is_nullable as "Null", data_type as "Type" FROM information_schema.columns WHERE table_name = <%= table %>;'
return Utils._.template(query)({ table: addQuotes(tableName, "'") })
},
addColumnQuery: function(tableName, attributes) {
var query = "ALTER TABLE <%= tableName %> ADD COLUMN <%= attributes %>;"
, attrString = Utils._.map(attributes, function(definition, attributeName) {
......@@ -110,15 +115,30 @@ module.exports = (function() {
},
changeColumnQuery: function(tableName, attributes) {
var query = "ALTER TABLE <%= tableName %> ALTER COLUMN <%= attributes %>;"
var attrString = Utils._.map(attributes, function(definition, attributeName) {
return Utils._.template('<%= attributeName %> <%= attributeName %> <%= definition %>')({
attributeName: addQuotes(attributeName),
definition: pgDataTypeMapping(tableName, attributeName, definition)
var query = "ALTER TABLE <%= tableName %> ALTER COLUMN <%= query %>;"
var sql = Utils._.map(attributes, function(definition, attributeName) {
var attrSql = ''
if (definition.indexOf('NOT NULL') > 0) {
attrSql += Utils._.template(query)({
tableName: addQuotes(tableName),
query: addQuotes(attributeName) + ' SET NOT NULL'
})
definition = definition.replace('NOT NULL', '').trim()
} else {
attrSql += Utils._.template(query)({
tableName: addQuotes(tableName),
query: addQuotes(attributeName) + ' DROP NOT NULL'
})
}
attrSql += Utils._.template(query)({
tableName: addQuotes(tableName),
query: addQuotes(attributeName) + ' TYPE ' + definition
})
}).join(', ')
return attrSql;
}).join('')
return Utils._.template(query)({ tableName: addQuotes(tableName), attributes: attrString })
return sql
},
renameColumnQuery: function(tableName, attrBefore, attributes) {
......@@ -272,7 +292,8 @@ module.exports = (function() {
},
showIndexQuery: function(tableName, options) {
throw new Error('showIndexQuery is not implemented for PostgreSQL')
var query = "SELECT relname FROM pg_class WHERE oid IN ( SELECT indexrelid FROM pg_index, pg_class WHERE pg_class.relname='<%= tableName %>' AND pg_class.oid=pg_index.indrelid);"
return Utils._.template(query)({ tableName: tableName });
},
removeIndexQuery: function(tableName, indexNameOrAttributes) {
......
......@@ -33,6 +33,8 @@ module.exports = (function() {
if (self.sql.indexOf('SELECT table_name FROM information_schema.tables') == 0) {
results.push(Utils._.values(row))
} else if (self.sql.indexOf('SELECT relname FROM pg_class WHERE oid IN') == 0) {
results.push(Utils._.values(row))
} else if (self.sql.indexOf('SELECT') == 0) {
// transform results into real model instances
// return the first real model instance if options.plain is set (e.g. Model.find)
......
......@@ -80,7 +80,13 @@ module.exports = (function() {
var self = this
return new Utils.CustomEventEmitter(function(emitter) {
self.sequelize.query('DESCRIBE `' + tableName + '`;', null, { raw: true }).success(function(data) {
var sql;
if (self.QueryGenerator.describeTableQuery) {
sql = self.QueryGenerator.describeTableQuery(tableName)
} else {
sql = 'DESCRIBE `' + tableName + '`;'
}
self.sequelize.query(sql, null, { raw: true }).success(function(data) {
emitter.emit('success', data)
}).error(function(err) {
emitter.emit('failure', err)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!