diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-08-19 01:28:39 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2016-08-19 01:28:39 -0300 |
commit | e87158f16b2a85beed690620a5f5e6d57ee505e3 (patch) | |
tree | e6641f3ca01784aaafe012d5be1e260b17a9704e /activerecord | |
parent | 15175fdad7e2203e3d20b197f7b9aeb14c29d4d2 (diff) | |
parent | 84cc8fd5d8fa15e868e4873a5f83e5c2b0c2fee1 (diff) | |
download | rails-e87158f16b2a85beed690620a5f5e6d57ee505e3.tar.gz rails-e87158f16b2a85beed690620a5f5e6d57ee505e3.tar.bz2 rails-e87158f16b2a85beed690620a5f5e6d57ee505e3.zip |
Merge pull request #25675 from TimPetricola/schema-no-standardized-column-widths
Option not to line up column attributes in schema.rb
Diffstat (limited to 'activerecord')
-rw-r--r-- | activerecord/CHANGELOG.md | 6 | ||||
-rw-r--r-- | activerecord/lib/active_record/schema_dumper.rb | 48 | ||||
-rw-r--r-- | activerecord/test/cases/schema_dumper_test.rb | 78 |
3 files changed, 122 insertions, 10 deletions
diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index fa57c1a005..fd2c5dd9bb 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -1,3 +1,9 @@ +* Option to remove standardized column types/arguments spaces in schema dump + with `ActiveRecord::SchemaDumper.standardized_argument_widths` and + `ActiveRecord::SchemaDumper.standardized_type_widths` methods. + + *Tim Petricola* + * Avoid loading records from database when they are already loaded using the `pluck` method on a collection. diff --git a/activerecord/lib/active_record/schema_dumper.rb b/activerecord/lib/active_record/schema_dumper.rb index 01f788a424..be74922453 100644 --- a/activerecord/lib/active_record/schema_dumper.rb +++ b/activerecord/lib/active_record/schema_dumper.rb @@ -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) @@ -146,20 +162,32 @@ 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 *= "" diff --git a/activerecord/test/cases/schema_dumper_test.rb b/activerecord/test/cases/schema_dumper_test.rb index 33baf84ef2..23cfe46d7f 100644 --- a/activerecord/test/cases/schema_dumper_test.rb +++ b/activerecord/test/cases/schema_dumper_test.rb @@ -442,3 +442,81 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase assert_match %r{t\.time\s+"time_with_default",\s+default: '2000-01-01 07:17:04'}, output end end + +class SchemaDumperNoStandardizedArgumentWidthsTest < ActiveRecord::TestCase + include SchemaDumpingHelper + + setup do + ActiveRecord::SchemaDumper.standardized_argument_widths = false + ActiveRecord::SchemaMigration.create_table + end + + teardown do + ActiveRecord::SchemaDumper.standardized_argument_widths = true + end + + def standard_dump + @@standard_dump ||= perform_schema_dump + end + + def perform_schema_dump + dump_all_table_schema [] + end + + def assert_no_line_up(lines, pattern) + return assert(true) if lines.empty? + matches = lines.map { |line| line.match(pattern) } + matches.compact! + return assert(true) if matches.empty? + line_matches = lines.map { |line| [line, line.match(pattern)] }.select { |line, match| match } + assert line_matches.all? { |line, match| + start = match.offset(0).first + line[start - 2..start - 1] == ", " + } + end + + def column_definition_lines(output = standard_dump) + output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) } + end + + def test_arguments_no_line_up + column_definition_lines.each do |column_set| + assert_no_line_up(column_set, /default: /) + assert_no_line_up(column_set, /limit: /) + assert_no_line_up(column_set, /null: /) + end + end +end + +class SchemaDumperNoStandardizedTypeWidthsTest < ActiveRecord::TestCase + include SchemaDumpingHelper + + setup do + ActiveRecord::SchemaDumper.standardized_type_widths = false + ActiveRecord::SchemaMigration.create_table + end + + teardown do + ActiveRecord::SchemaDumper.standardized_type_widths = true + end + + def standard_dump + @@standard_dump ||= perform_schema_dump + end + + def perform_schema_dump + dump_all_table_schema [] + end + + def column_definition_lines(output = standard_dump) + output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map { |m| m.last.split(/\n/) } + end + + def test_types_no_line_up + column_definition_lines.each do |column_set| + next if column_set.empty? + + assert column_set.all? { |column| !column.match(/\bt\.\w+\s{2,}/) } + end + end +end |