Skip to content

Commit

Permalink
avoid conflicts with Rails 5 comment support
Browse files Browse the repository at this point in the history
  • Loading branch information
pinnymz committed May 30, 2016
1 parent 2ce8373 commit a9646f9
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gemfiles/rails5.gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
source "http://rubygems.org"
gem "activerecord", '= 5.0.0.beta3'
gem "activerecord", '~> 5.0.0.rc1'

gemspec :path => '../'
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module MigrationComments::ActiveRecord::ConnectionAdapters::AbstractAdapter
module SchemaCreation
def column_options(o)
column_options = super(o)
column_options[:comment] = o.comment.comment_text if o.comment
column_options[:comment] = o.comment.comment_text if o.comment.respond_to?(:comment_text)
column_options
end

Expand All @@ -18,7 +18,7 @@ def add_column_options!(sql, options)
def visit_TableDefinition(o)
if @conn.inline_comments?
create_sql = "CREATE#{' TEMPORARY' if o.temporary} TABLE "
create_sql << "#{quote_table_name(o.name)}#{@conn.comment_sql(o.table_comment)} ("
create_sql << "#{quote_table_name(o.name)}#{@conn.comment_sql(o.comment)} ("
create_sql << o.columns.map { |c| accept c }.join(', ')
create_sql << ") #{o.options}"
create_sql
Expand All @@ -28,11 +28,16 @@ def visit_TableDefinition(o)
end

def visit_ColumnDefinition(o)
if @conn.inline_comments? && o.type.to_sym == :primary_key
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale)
column_sql = "#{quote_column_name(o.name)} #{sql_type}"
add_column_options!(column_sql, column_options(o))
column_sql
if @conn.inline_comments? && o.type.to_sym == :primary_key && o.comment
if ::ActiveRecord::VERSION::MAJOR >= 5
comment_sql = super(o)
comment_sql << @conn.comment_sql(o.comment)
else
sql_type = type_to_sql(o.type.to_sym, o.limit, o.precision, o.scale)
column_sql = "#{quote_column_name(o.name)} #{sql_type}"
add_column_options!(column_sql, column_options(o))
column_sql
end
else
super(o)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ module AlterTable
def add_column(name, type, options)
super(name, type, options)
if options.keys.include?(:comment)
column = @adds.last
added_entity = @adds.last
column = added_entity.respond_to?(:column) ? added_entity.column : added_entity
column.comment = CommentDefinition.new(@td, name, options[:comment])
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ def set_column_comment(table_name, column_name, comment_text)
change_column table_name, column_name, column.sql_type, options
end

if ::ActiveRecord::VERSION::MAJOR >= 5
def add_sql_comment!(sql, comment)
comment_text = comment.respond_to?(:comment_text) ? comment.comment_text : comment
super(sql, comment_text)
end
end

def retrieve_table_comment(table_name)
select_value(table_comment_sql(table_name)).presence
end
Expand All @@ -31,7 +38,7 @@ def create_table(table_name, options={})
local_table_definition = nil
super(table_name, options) do |td|
local_table_definition = td
local_table_definition.comment options[:comment] if options.has_key?(:comment)
local_table_definition.comment = options[:comment] if options.has_key?(:comment)
yield td if block_given?
end
comments = local_table_definition.collect_comments(table_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ def set_column_comment(table_name, column_name, comment_text)
execute comment_sql(CommentDefinition.new(table_name, column_name, comment_text))
end

if ::ActiveRecord::VERSION::MAJOR >= 5
def change_column_comment(table_name, column_name, comment)
comment_text = comment.respond_to?(:comment_text) ? comment.comment_text : comment
super(table_name, column_name, comment_text)
end
end

def retrieve_table_comment(table_name)
select_value(table_comment_sql(table_name)).presence
end
Expand All @@ -32,7 +39,7 @@ def create_table(table_name, options = {})
local_table_definition = nil
super(table_name, options) do |td|
local_table_definition = td
local_table_definition.comment options[:comment] if options.has_key?(:comment)
local_table_definition.comment = options[:comment] if options.has_key?(:comment)
yield td if block_given?
end
comments = local_table_definition.collect_comments(table_name)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def retrieve_column_comments(table_name, *column_names)

def create_table(table_name, options = {})
super(table_name, options) do |td|
td.comment options[:comment] if options.has_key?(:comment)
td.comment = options[:comment] if options.has_key?(:comment)
yield td if block_given?
end
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
module MigrationComments::ActiveRecord::ConnectionAdapters
module TableDefinition
attr_accessor :table_comment
attr_accessor :comment

def comment(text)
self.table_comment = CommentDefinition.new(nil, nil, text)
self
def comment=(text)
@comment = text.respond_to?(:comment_text) ? text : CommentDefinition.new(nil, nil, text)
end

def column(name, type, options = {})
Expand All @@ -17,7 +16,7 @@ def column(name, type, options = {})
end

def collect_comments(table_name)
comments = [table_comment] + columns.map(&:comment)
comments = [comment] + columns.map(&:comment)
comments.compact!
comments.each{|comment| comment.table_name = table_name }
end
Expand Down
3 changes: 2 additions & 1 deletion lib/migration_comments/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module SchemaDumper
include MigrationComments::SchemaFormatter

def table(table, stream)
return super if ::ActiveRecord::VERSION::MAJOR >= 5 && @connection.class.name !~ /SQLite/
tbl_stream = StringIO.new
super(table, tbl_stream)
tbl_stream.rewind
Expand Down Expand Up @@ -38,7 +39,7 @@ def append_comments(table, stream)
elsif table_line == index && table_comment.present?
block_init = " do |t|"
line.chomp!(block_init) << ", " << render_comment(table_comment) << block_init
elsif col_names[index]
elsif col_names[index] && ::ActiveRecord::VERSION::MAJOR < 5
comment = column_comments[col_names[index]]
line << ',' << ' ' * (len - line.length) << render_comment(comment) unless comment.blank?
end
Expand Down
30 changes: 22 additions & 8 deletions test/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,32 @@ def test_dump
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, dest)
dest.rewind
result = dest.read
expected = if ENV['DB'] == 'mysql'
expected = if ENV['DB'] == 'mysql' && ::ActiveRecord::VERSION::MAJOR >= 5
<<-EOS
create_table "sample", #{render_kv_pair(:force, :cascade)}, #{render_kv_pair(:comment, "a table comment")} do |t|
create_table "sample", force: :cascade, options: __OPTIONS__, comment: "a table comment" do |t|
t.string "field1", #{render_kv_pair(:comment, %{a \"comment\" \\ that ' needs; escaping''})}
t.integer "field2"
t.string "field3", #{render_kv_pair(:default, "")}, #{render_kv_pair(:null, false)}, #{render_kv_pair(:comment, "third column comment")}
end
EOS
elsif ENV['DB'] == 'mysql'
<<-EOS
create_table "sample", force: :cascade, comment: "a table comment" do |t|
t.string "field1", #{render_kv_pair(:limit, 255)}, #{render_kv_pair(:comment, %{a \"comment\" \\ that ' needs; escaping''})}
t.integer "field2", #{render_kv_pair(:limit, 4)}
t.string "field3", #{render_kv_pair(:limit, 255)}, #{render_kv_pair(:default, "")}, #{render_kv_pair(:null, false)}, #{render_kv_pair(:comment, "third column comment")}
end
EOS
else
<<-EOS
create_table "sample", #{render_kv_pair(:force, :cascade)}, #{render_kv_pair(:comment, "a table comment")} do |t|
create_table "sample", force: :cascade, comment: "a table comment" do |t|
t.string "field1", #{render_kv_pair(:comment, %{a \"comment\" \\ that ' needs; escaping''})}
t.integer "field2"
t.string "field3", #{render_kv_pair(:default, "")}, #{render_kv_pair(:null, false)}, #{render_kv_pair(:comment, "third column comment")}
end
EOS
end
assert_match(/#{Regexp.escape(expected).gsub(/__SPACES__/, " +")}/, result)
assert_match(/#{Regexp.escape(expected).gsub(/__OPTIONS__/, %Q("ENGINE=InnoDB[^"]*"))}/, result)
end

def test_dump_with_no_columns
Expand All @@ -44,12 +52,18 @@ def test_dump_with_no_columns
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, dest)
dest.rewind
result = dest.read
expected = <<EOS
create_table "sample", #{render_kv_pair(:force, :cascade)}, #{render_kv_pair(:comment, "a table comment")} do |t|
expected = if ENV['DB'] == 'mysql' && ::ActiveRecord::VERSION::MAJOR >= 5
<<-EOS
create_table "sample", force: :cascade, options: __OPTIONS__, comment: "a table comment" do |t|
EOS
else
<<-EOS
create_table "sample", force: :cascade, comment: "a table comment" do |t|
end
EOS
EOS
end

assert_match(/#{Regexp.escape expected}/, result)
assert_match(/#{Regexp.escape(expected).gsub(/__OPTIONS__/, %Q("ENGINE=InnoDB[^"]*"))}/, result)
end

def test_schema_dump_with_custom_type_error_for_pg
Expand Down

0 comments on commit a9646f9

Please sign in to comment.