Skip to content

Commit

Permalink
Remove the SchemaDumper options and change the default behavior
Browse files Browse the repository at this point in the history
Now the schema dumper by default doesn't align the types and arguments
in the ruby format anymore.
  • Loading branch information
rafaelfranca committed Aug 22, 2016
1 parent d25a5ce commit df84e98
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 137 deletions.
4 changes: 1 addition & 3 deletions activerecord/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
* Option to remove standardized column types/arguments spaces in schema dump
with `ActiveRecord::SchemaDumper.standardized_argument_widths` and
`ActiveRecord::SchemaDumper.standardized_type_widths` methods.
* Remove standardized column types/arguments spaces in schema dump.

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 14, 2017

Author Member

To avoid diffs just aligning the columns

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 15, 2017

Author Member

Yes there was, and there were discussions but remember that the core team can do what they think it is better for the framework. 😉

This comment has been minimized.

Copy link
@rafaelfranca

rafaelfranca Jun 15, 2017

Author Member

It is this one #25675. I just removed the config and made the new behavior the default


*Tim Petricola*

Expand Down
41 changes: 3 additions & 38 deletions activerecord/lib/active_record/schema_dumper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,6 @@ class SchemaDumper #:nodoc:
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)
Expand Down Expand Up @@ -162,32 +146,13 @@ def table(table, stream)
keys = @connection.migration_keys

# figure out the lengths for each column based on above keys
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
lengths = [0] * keys.length

# the string we're going to sprintf our values against, with standardized column widths
format_string = if standardized_argument_widths
lengths.map { |len| "%-#{len}s" }
else
["%s"] * keys.length
end
format_string = ["%s"] * keys.length

# add column type definition to our format string
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.unshift " t.%s "

format_string *= ""

Expand Down
105 changes: 12 additions & 93 deletions activerecord/test/cases/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,38 +70,35 @@ def test_schema_dump_includes_camelcase_table_name
assert_match %r{create_table "CamelCase"}, output
end

def assert_line_up(lines, pattern, required = false)
def assert_no_line_up(lines, pattern)
return assert(true) if lines.empty?
matches = lines.map { |line| line.match(pattern) }
assert matches.all? if required
matches.compact!
return assert(true) if matches.empty?
assert_equal 1, matches.map { |match| match.offset(0).first }.uniq.length
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_types_line_up
def test_types_no_line_up
column_definition_lines.each do |column_set|
next if column_set.empty?

lengths = column_set.map do |column|
if match = column.match(/\bt\.\w+\s+(?="\w+?")/)
match[0].length
end
end.compact

assert_equal 1, lengths.uniq.length
assert column_set.all? { |column| !column.match(/\bt\.\w+\s{2,}/) }
end
end

def test_arguments_line_up
def test_arguments_no_line_up
column_definition_lines.each do |column_set|
assert_line_up(column_set, /default: /)
assert_line_up(column_set, /limit: /)
assert_line_up(column_set, /null: /)
assert_no_line_up(column_set, /default: /)
assert_no_line_up(column_set, /limit: /)
assert_no_line_up(column_set, /null: /)
end
end

Expand Down Expand Up @@ -442,81 +439,3 @@ def test_schema_dump_defaults_with_universally_supported_types
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
4 changes: 1 addition & 3 deletions guides/source/configuring.md
Original file line number Diff line number Diff line change
Expand Up @@ -368,11 +368,9 @@ The MySQL adapter adds one additional configuration option:

* `ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns as booleans. Defaults to `true`.

The schema dumper adds additional configuration options:
The schema dumper adds one additional configuration option:

* `ActiveRecord::SchemaDumper.ignore_tables` accepts an array of tables that should _not_ be included in any generated schema file. This setting is ignored unless `config.active_record.schema_format == :ruby`.
* `ActiveRecord::SchemaDumper.standardized_argument_widths` configures whether colum arguments should be lined up or not in dump. By default this is `true`. This setting is ignored unless `config.active_record.schema_format == :ruby`.
* `ActiveRecord::SchemaDumper.standardized_type_widths` configures whether colum types should be lined up or not in dump. By default this is `true`. This setting is ignored unless `config.active_record.schema_format == :ruby`.

### Configuring Action Controller

Expand Down

0 comments on commit df84e98

Please sign in to comment.