Merge branch 'master' into json

Conflicts:
	index.html
This commit is contained in:
Vaughan Rouesnel 2013-06-17 17:06:33 +10:00
commit 1efcfdf09d
6 changed files with 82 additions and 5 deletions

View File

@ -84,7 +84,7 @@ MysqlClient.grammar = {
MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar, {
// The possible column modifiers.
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After'],
modifiers: ['Unsigned', 'Nullable', 'Default', 'Increment', 'After', 'Comment'],
// Compile a create table command.
compileCreateTable: function(blueprint, command) {
@ -98,6 +98,11 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
sql += ' engine = ' + blueprint.isEngine;
}
if (blueprint.tableComment) {
var maxTableCommentLength = 60;
sql += " comment = '" + blueprint.tableComment + "'"
}
return sql;
},
@ -251,6 +256,14 @@ MysqlClient.schemaGrammar = _.extend({}, base.schemaGrammar, MysqlClient.grammar
if (column.isAfter) {
return ' after ' + this.wrap(column.isAfter);
}
},
// Get the SQL for a comment column modifier. (MySQL)
modifyComment: function(blueprint, column) {
var maxColumnCommentLength = 255;
if (_.isString(column.comment)) {
return " comment '" + column.comment + "'";
}
}
});

View File

@ -169,6 +169,25 @@ PostgresClient.schemaGrammar = _.extend({}, base.schemaGrammar, PostgresClient.g
return 'alter table ' + this.wrapTable(blueprint) + ' rename to ' + this.wrapTable(command.to);
},
// Compile a comment command.
compileComment: function(blueprint, command) {
var table = this.wrapTable(blueprint);
var comment;
if (command.comment == void 0) {
comment = 'NULL'
} else {
comment = "'" + command.comment + "'";
}
var identifier;
if (command.isTable) {
identifier = 'table ' + table;
} else {
var column = this.wrap(command.columnName);
identifier = 'column ' + table + '.' + column;
}
return 'comment on ' + identifier + ' is ' + comment;
},
// Create the column definition for a string type.
typeString: function(column) {
return "varchar(" + column.length + ")";

View File

@ -264,6 +264,7 @@
<li> <a href="#Schema-binary">binary</a></li>
<li> <a href="#Schema-enum">enum / enu</a></li>
<li> <a href="#Schema-json">json</a></li>
<li> <a href="#Schema-comment">comment</a></li>
<li><a href="#Chainable"><b>Chainable:</b></li>
<li> <a href="#Chainable-index">index</a></li>
<li> <a href="#Chainable-primary">primary</a></li>
@ -272,6 +273,7 @@
<li> <a href="#Chainable-unsigned">unsigned</a></li>
<li> <a href="#Chainable-nullable">nullable</a></li>
<li> <a href="#Chainable-after">after</a></li>
<li> <a href="#Chainable-comment">comment</a></li>
</ul>
<a class="toc_title" href="#Raw">
@ -1022,6 +1024,12 @@ Knex.Schema.table('users', function (table) {
applicable to MySql.
</p>
<p id="Schema-comment">
<b class="header">comment</b><code>table.comment(value)</code>
<br />
Sets the comment for a table.
</p>
<h3 id="Chainable">Chainable Methods:</h3>
<p>
@ -1070,6 +1078,12 @@ Knex.Schema.table('users', function (table) {
Sets the column to be inserted after another, only used in MySql alter tables.
</p>
<p id="Chainable-comment">
<b class="header">comment</b><code>column.comment(value)</code>
<br />
Sets the comment for a column.
</p>
<pre>
Knex.Schema.createTable('accounts', function() {
t.increments().primary();

30
knex.js
View File

@ -1083,6 +1083,26 @@
}
}
// Add table comments. (Postgres)
if (this.tableComment) {
this._addCommand('comment', {
comment: this.tableComment,
isTable: true
});
}
// Add column comments. (Postgres)
for (var i = 0, l = this.columns.length; i < l; i++) {
var column = this.columns[i];
if (_.has(column, 'comment')) {
this._addCommand('comment', {
comment: column.comment,
columnName: column.name,
isTable: false
});
}
}
var statements = [];
// Each type of command has a corresponding compiler function on the schema
@ -1166,6 +1186,10 @@
return this._indexCommand('foreign', columns, name);
},
comment: function(comment) {
this.tableComment = comment || null;
},
// Create a new auto-incrementing column on the table.
increments: function(column) {
return this._addColumn('integer', (column || 'id'), {autoIncrement: true, length: 11});
@ -1378,6 +1402,12 @@
after: function(name) {
this.isAfter = name;
return this;
},
// Adds a comment to this column.
comment: function(comment) {
this.comment = comment || null;
return this;
}
};

View File

@ -15,12 +15,13 @@ module.exports = function(Knex, resolver, error) {
return When.all([
Knex.Schema.createTable('test_table_one', function(table) {
table.engine('InnoDB');
table.comment('A table comment.')
table.increments('id');
table.string('first_name');
table.string('last_name');
table.string('email').unique().nullable();
table.integer('logins').defaultTo(1).index();
table.text('about');
table.integer('logins').defaultTo(1).index().comment();
table.text('about').comment('A comment.');
table.timestamps();
}),
Knex.Schema.createTable('test_table_two', function(t) {

View File

@ -16,11 +16,11 @@ module.exports = {
},
'schema.2': {
mysql: {
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null, `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 engine = InnoDB','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
sql: ['create table `test_table_one` (`id` int(11) not null auto_increment primary key, `first_name` varchar(255) not null, `last_name` varchar(255) not null, `email` varchar(255) null, `logins` int(11) not null default \'1\', `about` text not null comment \'A comment.\', `created_at` timestamp default 0 not null, `updated_at` timestamp default 0 not null) default character set utf8 engine = InnoDB comment = \'A table comment.\'','alter table `test_table_one` add unique test_table_one_email_unique(`email`)','alter table `test_table_one` add index test_table_one_logins_index(`logins`)'],
bindings: []
},
postgres: {
sql: ['create table "test_table_one" ("id" serial primary key not null, "first_name" varchar(255) not null, "last_name" varchar(255) not null, "email" varchar(255) null, "logins" integer not null default \'1\', "about" text not null, "created_at" timestamp not null, "updated_at" timestamp not null)','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")'],
sql: ['create table "test_table_one" ("id" serial primary key not null, "first_name" varchar(255) not null, "last_name" varchar(255) not null, "email" varchar(255) null, "logins" integer not null default \'1\', "about" text not null, "created_at" timestamp not null, "updated_at" timestamp not null)','alter table "test_table_one" add constraint test_table_one_email_unique unique ("email")','create index test_table_one_logins_index on "test_table_one" ("logins")','comment on table "test_table_one" is \'A table comment.\'','comment on column "test_table_one"."logins" is NULL','comment on column "test_table_one"."about" is \'A comment.\''],
bindings: []
},
sqlite3: {