aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
diff options
context:
space:
mode:
authorAndrey Novikov <envek@envek.name>2016-01-03 21:13:53 +0300
committerAndrey Novikov <envek@envek.name>2016-04-16 10:17:26 +0300
commitc690b9ce39c893077b48fa5b27af87e995f97e9b (patch)
tree925a00ba04b8a144041b6c823bd8bceacd6084f1 /activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
parent39e087cbf5628ecc351fc86a3a1e320be193bf29 (diff)
downloadrails-c690b9ce39c893077b48fa5b27af87e995f97e9b.tar.gz
rails-c690b9ce39c893077b48fa5b27af87e995f97e9b.tar.bz2
rails-c690b9ce39c893077b48fa5b27af87e995f97e9b.zip
Add support for specifying comments for tables, columns, and indexes.
Comments are specified in migrations, stored in database itself (in its schema), and dumped into db/schema.rb file. This allows to generate good documentation and explain columns and tables' purpose to everyone from new developers to database administrators. For PostgreSQL and MySQL only. SQLite does not support comments at the moment. See docs for PostgreSQL: http://www.postgresql.org/docs/current/static/sql-comment.html See docs for MySQL: http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb')
-rw-r--r--activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb21
1 files changed, 18 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb b/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
index 1e2c859af9..5ab81640e8 100644
--- a/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
+++ b/activerecord/lib/active_record/connection_adapters/mysql/schema_creation.rb
@@ -2,6 +2,9 @@ module ActiveRecord
module ConnectionAdapters
module MySQL
class SchemaCreation < AbstractAdapter::SchemaCreation
+ delegate :quote, to: :@conn
+ private :quote
+
private
def visit_DropForeignKey(name)
@@ -22,6 +25,13 @@ module ActiveRecord
add_column_position!(change_column_sql, column_options(o.column))
end
+ def add_table_options!(sql, options)
+ super
+ if options[:comment]
+ sql << "COMMENT #{quote(options[:comment])} "
+ end
+ end
+
def column_options(o)
column_options = super
column_options[:charset] = o.charset
@@ -35,7 +45,11 @@ module ActiveRecord
if options[:collation]
sql << " COLLATE #{options[:collation]}"
end
- super
+ new_sql = super
+ if options[:comment]
+ new_sql << " COMMENT #{quote(options[:comment])}"
+ end
+ new_sql
end
def add_column_position!(sql, options)
@@ -48,8 +62,9 @@ module ActiveRecord
end
def index_in_create(table_name, column_name, options)
- index_name, index_type, index_columns, _, _, index_using = @conn.add_index_options(table_name, column_name, options)
- "#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns}) "
+ index_name, index_type, index_columns, _, _, index_using, comment = @conn.add_index_options(table_name, column_name, options)
+ index_option = " COMMENT #{quote(comment)}" if comment
+ "#{index_type} INDEX #{quote_column_name(index_name)} #{index_using} (#{index_columns})#{index_option} "
end
end
end