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

Commit 1163c545 by Jan Aagaard Meier

Merge branch 'comments'

2 parents 590766b7 8d086786
...@@ -43,7 +43,7 @@ module.exports = (function() { ...@@ -43,7 +43,7 @@ module.exports = (function() {
charset: null charset: null
}, options || {}) }, options || {})
var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>) ENGINE=<%= engine %> <%= charset %>" var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)<%= comment %> ENGINE=<%= engine %> <%= charset %>"
, primaryKeys = [] , primaryKeys = []
, foreignKeys = {} , foreignKeys = {}
, attrStr = [] , attrStr = []
...@@ -69,6 +69,7 @@ module.exports = (function() { ...@@ -69,6 +69,7 @@ module.exports = (function() {
var values = { var values = {
table: this.quoteIdentifier(tableName), table: this.quoteIdentifier(tableName),
attributes: attrStr.join(", "), attributes: attrStr.join(", "),
comment: options.comment && Utils._.isString(options.comment) ? " COMMENT " + this.escape(options.comment) : "",
engine: options.engine, engine: options.engine,
charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "") charset: (options.charset ? "DEFAULT CHARSET=" + options.charset : "")
} }
...@@ -504,6 +505,10 @@ module.exports = (function() { ...@@ -504,6 +505,10 @@ module.exports = (function() {
} }
if (dataType.comment && Utils._.isString(dataType.comment) && dataType.comment.length) {
template += " COMMENT " + Utils.escape(dataType.comment)
}
result[name] = template result[name] = template
} else { } else {
result[name] = dataType result[name] = dataType
......
...@@ -48,10 +48,22 @@ module.exports = (function() { ...@@ -48,10 +48,22 @@ module.exports = (function() {
primaryKeys[tableName] = [] primaryKeys[tableName] = []
tables[tableName] = {} tables[tableName] = {}
var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)" var query = "CREATE TABLE IF NOT EXISTS <%= table %> (<%= attributes%>)<%= comments %>"
, comments = ""
, attrStr = [] , attrStr = []
, i
if (options.comment && Utils._.isString(options.comment)) {
comments += "; COMMENT ON TABLE <%= table %> IS " + this.escape(options.comment)
}
for (var attr in attributes) { for (var attr in attributes) {
if ((i = attributes[attr].indexOf('COMMENT')) !== -1) {
// Move comment to a seperate query
comments += "; " + attributes[attr].substring(i)
attributes[attr] = attributes[attr].substring(0, i)
}
var dataType = this.pgDataTypeMapping(tableName, attr, attributes[attr]) var dataType = this.pgDataTypeMapping(tableName, attr, attributes[attr])
attrStr.push(this.quoteIdentifier(attr) + " " + dataType) attrStr.push(this.quoteIdentifier(attr) + " " + dataType)
...@@ -62,7 +74,8 @@ module.exports = (function() { ...@@ -62,7 +74,8 @@ module.exports = (function() {
var values = { var values = {
table: this.quoteIdentifiers(tableName), table: this.quoteIdentifiers(tableName),
attributes: attrStr.join(", ") attributes: attrStr.join(", "),
comments: Utils._.template(comments, { table: this.quoteIdentifiers(tableName)})
} }
var pks = primaryKeys[tableName].map(function(pk){ var pks = primaryKeys[tableName].map(function(pk){
...@@ -574,7 +587,13 @@ module.exports = (function() { ...@@ -574,7 +587,13 @@ module.exports = (function() {
template += " ON UPDATE <%= onUpdateAction %>" template += " ON UPDATE <%= onUpdateAction %>"
replacements.onUpdateAction = dataType.onUpdate.toUpperCase() replacements.onUpdateAction = dataType.onUpdate.toUpperCase()
} }
}
if (dataType.comment && Utils._.isString(dataType.comment)) {
template += " COMMENT ON COLUMN <%= tableName %>.<%= columnName %> IS <%= comment %>"
replacements.columnName = this.quoteIdentifier(name)
replacements.tableName = '<%= table %>' // Hacky, table name will be inserted by create table
replacements.comment = this.escape(dataType.comment)
} }
result[name] = Utils._.template(template)(replacements) result[name] = Utils._.template(template)(replacements)
......
...@@ -31,6 +31,13 @@ describe('DAOFactory', function() { ...@@ -31,6 +31,13 @@ describe('DAOFactory', function() {
expect(User.attributes).toEqual({username:"VARCHAR(255) NOT NULL",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"}) expect(User.attributes).toEqual({username:"VARCHAR(255) NOT NULL",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
}) })
it("handles extended attributes (comment)", function() {
var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, comment: 'This be\'s a comment'}
}, { timestamps: false })
expect(User.attributes).toEqual({username:"VARCHAR(255) COMMENT 'This be\\'s a comment'",id:"INTEGER NOT NULL auto_increment PRIMARY KEY"})
})
it("handles extended attributes (primaryKey)", function() { it("handles extended attributes (primaryKey)", function() {
var User = sequelize.define('User' + config.rand(), { var User = sequelize.define('User' + config.rand(), {
username: {type: Sequelize.STRING, primaryKey: true} username: {type: Sequelize.STRING, primaryKey: true}
......
...@@ -45,6 +45,10 @@ describe('QueryGenerator', function() { ...@@ -45,6 +45,10 @@ describe('QueryGenerator', function() {
expectation: {id: 'INTEGER UNIQUE'} expectation: {id: 'INTEGER UNIQUE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', comment: "I'm a comment!" }}],
expectation: {id: "INTEGER COMMENT 'I\\'m a comment!'" }
},
{
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: 'Bar'}}],
expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'} expectation: {id: 'INTEGER REFERENCES `Bar` (`id`)'}
}, },
...@@ -72,6 +76,14 @@ describe('QueryGenerator', function() { ...@@ -72,6 +76,14 @@ describe('QueryGenerator', function() {
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB;" expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=InnoDB;"
}, },
{ {
arguments: ['myTable', {title: "INTEGER COMMENT 'I\\'m a comment!'"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` INTEGER COMMENT 'I\\'m a comment!') ENGINE=InnoDB;",
},
{
arguments: ['myTable', {title: "INTEGER"}, {comment: "I'm a comment!"}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` INTEGER) COMMENT 'I\\'m a comment!' ENGINE=InnoDB;",
},
{
arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {engine: 'MyISAM'}], arguments: ['myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}, {engine: 'MyISAM'}],
expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=MyISAM;" expectation: "CREATE TABLE IF NOT EXISTS `myTable` (`title` VARCHAR(255), `name` VARCHAR(255)) ENGINE=MyISAM;"
}, },
......
...@@ -49,6 +49,10 @@ describe('QueryGenerator', function() { ...@@ -49,6 +49,10 @@ describe('QueryGenerator', function() {
expectation: {id: 'INTEGER UNIQUE'} expectation: {id: 'INTEGER UNIQUE'}
}, },
{ {
arguments: [{id: {type: 'INTEGER', comment: "I'm a comment!" }}],
expectation: {id: "INTEGER COMMENT ON COLUMN <%= table %>.\"id\" IS 'I''m a comment!'" }
},
{
arguments: [{id: {type: 'INTEGER', references: 'Bar'}}], arguments: [{id: {type: 'INTEGER', references: 'Bar'}}],
expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'} expectation: {id: 'INTEGER REFERENCES "Bar" ("id")'}
}, },
...@@ -104,6 +108,14 @@ describe('QueryGenerator', function() { ...@@ -104,6 +108,14 @@ describe('QueryGenerator', function() {
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));", expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));",
}, },
{ {
arguments: ['myTable', {title: "INTEGER COMMENT ON COLUMN <%= table %>.\"title\" IS 'I''m a comment!'"}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" INTEGER ); COMMENT ON COLUMN \"myTable\".\"title\" IS 'I''m a comment!';",
},
{
arguments: ['myTable', {title: "INTEGER"}, {comment: "I'm a comment!"}],
expectation: "CREATE TABLE IF NOT EXISTS \"myTable\" (\"title\" INTEGER); COMMENT ON TABLE \"myTable\" IS 'I''m a comment!';",
},
{
arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}], arguments: ['mySchema.myTable', {title: 'VARCHAR(255)', name: 'VARCHAR(255)'}],
expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));" expectation: "CREATE TABLE IF NOT EXISTS \"mySchema\".\"myTable\" (\"title\" VARCHAR(255), \"name\" VARCHAR(255));"
}, },
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!