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

Commit d1e7998b by Mick Hansen

merge master into 2.0

1 parent 4b1b89f3
......@@ -3,9 +3,10 @@ Notice: All 1.7.x changes are present in 2.0.x aswell
# v1.7.0-rc4
- fixes issue with postgres sync and enums [#1020](https://github.com/sequelize/sequelize/issues/1020)
- fixes various issues with limit and includes [#1322](https://github.com/sequelize/sequelize/pull/1322)
- fixes issues with migrations/queryInterface createTable and enums
#### Backwards compatability changes
- find/findAll will not always return primary keys regardless of `attributes` settings. (Motivation was to fix various issues with eager loading)
- find/findAll will now always return primary keys regardless of `attributes` settings. (Motivation was to fix various issues with eager loading)
# v1.7.0-rc3
- dropAllTables now takes an option parameter with `skip` as an option [#1280](https://github.com/sequelize/sequelize/pull/1280)
......
......@@ -91,7 +91,7 @@ var validateAttributes = function() {
, errors = {}
Utils._.each(this.model.rawAttributes, function(rawAttribute, field) {
var value = self.model.dataValues[field] || undefined
var value = self.model.dataValues[field]
, hasAllowedNull = ((rawAttribute === undefined || rawAttribute.allowNull === true) && ((value === null) || (value === undefined)))
, isSkipped = self.options.skip.length > 0 && self.options.skip.indexOf(field) !== -1
......
......@@ -710,7 +710,15 @@ module.exports = (function() {
pgEnum: function (tableName, attr, dataType, options) {
var enumName = this.pgEscapeAndQuote("enum_" + tableName + "_" + attr)
var sql = "CREATE TYPE " + enumName + " AS " + dataType.match(/^ENUM\(.+\)/)[0] + "; "
, values
if (dataType.values) {
values = "ENUM('"+dataType.values.join("', '")+"')";
} else {
values = dataType.toString().match(/^ENUM\(.+\)/)[0]
}
var sql = "CREATE TYPE " + enumName + " AS " + values + "; "
if (!!options && options.force === true) {
sql = this.pgEnumDrop(tableName, attr) + sql
}
......@@ -731,8 +739,8 @@ module.exports = (function() {
return sql
},
pgEnumDrop: function(tableName, attr) {
var enumName = this.pgEscapeAndQuote("enum_" + tableName + "_" + attr)
pgEnumDrop: function(tableName, attr, enumName) {
enumName = enumName || this.pgEscapeAndQuote("enum_" + tableName + "_" + attr)
return 'DROP TYPE IF EXISTS ' + enumName + '; '
},
......
......@@ -101,7 +101,7 @@ module.exports = (function() {
, getTableName = (!options || !options.schema || options.schema === "public" ? '' : options.schema + '_') + tableName
for (i = 0; i < keyLen; i++) {
if (attributes[keys[i]].toString().match(/^ENUM\(/)) {
if (attributes[keys[i]].toString().match(/^ENUM\(/) || attributes[keys[i]].toString() === "ENUM" || (attributes[keys[i]].type && attributes[keys[i]].type.toString() === "ENUM")) {
sql = self.QueryGenerator.pgListEnums(getTableName, keys[i], options)
chainer.add(self.sequelize.query(sql, null, { plain: true, raw: true, type: QueryTypes.SELECT, logging: options.logging }))
}
......@@ -116,7 +116,7 @@ module.exports = (function() {
daoTable = daoTable.length > 0 ? daoTable[0] : null
for (i = 0; i < keyLen; i++) {
if (attributes[keys[i]].toString().match(/^ENUM\(/)) {
if (attributes[keys[i]].toString().match(/^ENUM\(/) || attributes[keys[i]].toString() === "ENUM" || (attributes[keys[i]].type && attributes[keys[i]].type.toString() === "ENUM")) {
// If the enum type doesn't exist then create it
if (!results[enumIdx]) {
sql = self.QueryGenerator.pgEnum(getTableName, keys[i], attributes[keys[i]], options)
......@@ -288,6 +288,36 @@ module.exports = (function() {
}
}
QueryInterface.prototype.dropAllEnums = function(options) {
if (this.sequelize.getDialect() !== 'postgres') {
return new Utils.CustomEventEmitter(function (emitter) {
emitter.emit('success')
}).run()
}
options = options || {}
var self = this
, emitter = new Utils.CustomEventEmitter()
, chainer = new Utils.QueryChainer()
, sql = this.QueryGenerator.pgListEnums()
this.sequelize.query(sql, null, { plain: false, raw: true, type: QueryTypes.SELECT, logging: options.logging })
.proxy(emitter, {events: ['sql', 'error']})
.success(function (results) {
results.forEach(function (result) {
chainer.add(self.sequelize.query(
self.QueryGenerator.pgEnumDrop(null, null, result.enum_name),
null,
{logging: options.logging, raw: true}
))
})
chainer.run().proxy(emitter)
})
return emitter
}
QueryInterface.prototype.renameTable = function(before, after) {
var sql = this.QueryGenerator.renameTableQuery(before, after)
return queryAndEmit.call(this, sql, 'renameTable')
......
......@@ -2,6 +2,7 @@ var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/support')
, Migrator = require("../lib/migrator")
, DataTypes = require("../lib/data-types")
, dialect = Support.getTestDialect()
chai.Assertion.includeStack = true
......@@ -287,7 +288,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
})
describe('renameColumn', function() {
it("renames the signature column from user to sig", function(done) {
......@@ -308,7 +309,7 @@ describe(Support.getTestDialectTeaser("Migrator"), function() {
})
})
})
if (dialect.match(/^postgres/)) {
describe('function migrations', function() {
......
......@@ -142,4 +142,27 @@ describe(Support.getTestDialectTeaser("QueryInterface"), function () {
})
})
})
describe('createTable', function () {
it('should work with enums (1)', function (done) {
this.queryInterface.createTable('SomeTable', {
someEnum: DataTypes.ENUM('value1', 'value2', 'value3')
}).done(function (err) {
expect(err).not.to.be.ok
done()
})
})
it('should work with enums (2)', function (done) {
this.queryInterface.createTable('SomeTable', {
someEnum: {
type: DataTypes.ENUM,
values: ['value1', 'value2', 'value3']
}
}).done(function (err) {
expect(err).not.to.be.ok
done()
})
})
})
})
......@@ -83,9 +83,18 @@ var Support = {
.dropAllTables()
.success(function() {
sequelize.daoFactoryManager.daos = []
callback && callback()
sequelize
.getQueryInterface()
.dropAllEnums()
.success(callback)
.error(function (err) {
console.log(err)
})
})
.error(function(err) {
console.log(err)
})
.error(function(err) { console.log(err) })
},
getSupportedDialects: function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!