lib/ahnnotate/tables.rb in ahnnotate-0.5.0 vs lib/ahnnotate/tables.rb in ahnnotate-0.5.1

- old
+ new

@@ -15,62 +15,69 @@ if !block_given? enum_for(:each) end @connection.public_send(data_sources_method_name).each do |table_name| - primary_key = ActiveRecord::Base.get_primary_key(table_name) + begin + original_stderr = $stderr + $stderr = StringIO.new - columns = @connection.columns(table_name).map do |c| - is_primary_key = - if primary_key.is_a?(Array) - primary_key.include?(c.name) - else - primary_key == c.name - end + primary_key = ActiveRecord::Base.get_primary_key(table_name) - Column.new( - name: c.name, - sql_type: c.sql_type, - nullable: c.null, - primary_key: is_primary_key, - default: c.default - ) - end + columns = @connection.columns(table_name).map do |c| + is_primary_key = + if primary_key.is_a?(Array) + primary_key.include?(c.name) + else + primary_key == c.name + end - indexes = @connection.indexes(table_name).map do |i| - comment = - if i.respond_to?(:comment) - i.comment - end + Column.new( + name: c.name, + sql_type: c.sql_type, + nullable: c.null, + primary_key: is_primary_key, + default: c.default + ) + end - Index.new( - name: i.name, - columns: i.columns, - unique: i.unique, - comment: comment - ) - end + indexes = @connection.indexes(table_name).map do |i| + comment = + if i.respond_to?(:comment) + i.comment + end - foreign_keys = - if @abilities.foreign_key? - @connection.foreign_keys(table_name).map do |fk| - ForeignKey.new( - name: fk.name, - from_column: fk.column, - to_table: fk.to_table, - to_column: fk.primary_key - ) - end - else - [] + Index.new( + name: i.name, + columns: i.columns, + unique: i.unique, + comment: comment + ) end - yield Table.new( - name: table_name, - columns: columns, - indexes: indexes, - foreign_keys: foreign_keys, - ) + foreign_keys = + if @abilities.foreign_key? + @connection.foreign_keys(table_name).map do |fk| + ForeignKey.new( + name: fk.name, + from_column: fk.column, + to_table: fk.to_table, + to_column: fk.primary_key + ) + end + else + [] + end + + yield Table.new( + name: table_name, + columns: columns, + indexes: indexes, + foreign_keys: foreign_keys, + ) + ensure + $stderr = original_stderr + end end end def data_sources_method_name @data_sources_method_name ||=