diff options
Diffstat (limited to 'activerecord/lib/active_record/schema_dumper.rb')
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 75 |
1 files changed, 49 insertions, 26 deletions
diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index d769376d1a..be74922453 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -1,4 +1,4 @@ -require 'stringio' +require "stringio" module ActiveRecord # = Active Record Schema Dumper @@ -16,6 +16,22 @@ module ActiveRecord cattr_accessor :ignore_tables @@ignore_tables = [] + ## + # :singleton-method: + # Define whether column arguments are lined up in dump. + # Acceptable values are true or false. + # This setting is only used if ActiveRecord::Base.schema_format == :ruby + cattr_accessor :standardized_argument_widths + @@standardized_argument_widths = true + + ## + # :singleton-method: + # Define whether columns types are lined up in dump. + # Acceptable values are true or false. + # This setting is only used if ActiveRecord::Base.schema_format == :ruby + cattr_accessor :standardized_type_widths + @@standardized_type_widths = true + class << self def dump(connection=ActiveRecord::Base.connection, stream=STDOUT, config = ActiveRecord::Base) new(connection, generate_options(config)).dump(stream) @@ -105,18 +121,13 @@ HEADER tbl = StringIO.new # first dump primary key column - if @connection.respond_to?(:primary_keys) - pk = @connection.primary_keys(table) - pk = pk.first unless pk.size > 1 - else - pk = @connection.primary_key(table) - end + pk = @connection.primary_key(table) tbl.print " create_table #{remove_prefix_and_suffix(table).inspect}" case pk when String - tbl.print ", primary_key: #{pk.inspect}" unless pk == 'id' + tbl.print ", primary_key: #{pk.inspect}" unless pk == "id" pkcol = columns.detect { |c| c.name == pk } pkcolspec = @connection.column_spec_for_primary_key(pkcol) if pkcolspec.present? @@ -132,10 +143,10 @@ HEADER tbl.print ", force: :cascade" table_options = @connection.table_options(table) - tbl.print ", options: #{table_options.inspect}" unless table_options.blank? - - if comment = @connection.table_comment(table).presence - tbl.print ", comment: #{comment.inspect}" + if table_options.present? + table_options.each do |key, value| + tbl.print ", #{key}: #{value.inspect}" if value.present? + end end tbl.puts " do |t|" @@ -151,27 +162,39 @@ HEADER keys = @connection.migration_keys # figure out the lengths for each column based on above keys - lengths = keys.map { |key| - column_specs.map { |spec| - spec[key] ? spec[key].length + 2 : 0 - }.max - } + lengths = if standardized_argument_widths + keys.map { |key| + column_specs.map { |spec| + spec[key] ? spec[key].length + 2 : 0 + }.max + } + else + [0] * keys.length + end # the string we're going to sprintf our values against, with standardized column widths - format_string = lengths.map{ |len| "%-#{len}s" } - - # find the max length for the 'type' column, which is special - type_length = column_specs.map{ |column| column[:type].length }.max + format_string = if standardized_argument_widths + lengths.map { |len| "%-#{len}s" } + else + ["%s"] * keys.length + end # add column type definition to our format string - format_string.unshift " t.%-#{type_length}s " + if standardized_type_widths + # find the max length for the 'type' column, which is special + type_length = column_specs.map { |column| column[:type].length }.max + + format_string.unshift " t.%-#{type_length}s " + else + format_string.unshift " t.%s " + end - format_string *= '' + format_string *= "" column_specs.each do |colspec| - values = keys.zip(lengths).map{ |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len } + values = keys.zip(lengths).map { |key, len| colspec.key?(key) ? colspec[key] + ", " : " " * len } values.unshift colspec[:type] - tbl.print((format_string % values).gsub(/,\s*$/, '')) + tbl.print((format_string % values).gsub(/,\s*$/, "")) tbl.puts end @@ -218,7 +241,7 @@ HEADER index.columns.inspect, "name: #{index.name.inspect}", ] - index_parts << 'unique: true' if index.unique + index_parts << "unique: true" if index.unique index_lengths = (index.lengths || []).compact index_parts << "length: #{Hash[index.columns.zip(index.lengths)].inspect}" if index_lengths.any? |