mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Option not to line up column types and attributes in schema.rb
This commit is contained in:
parent
6eee673184
commit
dde3bdf214
4 changed files with 128 additions and 14 deletions
|
@ -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*
|
||||||
|
|
||||||
* Doing count on relations that contain LEFT OUTER JOIN Arel node no longer
|
* Doing count on relations that contain LEFT OUTER JOIN Arel node no longer
|
||||||
force a DISTINCT. This solves issues when using count after a left_joins.
|
force a DISTINCT. This solves issues when using count after a left_joins.
|
||||||
|
|
||||||
|
@ -57,8 +63,8 @@
|
||||||
*Xavier Noria*
|
*Xavier Noria*
|
||||||
|
|
||||||
* Using `group` with an attribute that has a custom type will properly cast
|
* Using `group` with an attribute that has a custom type will properly cast
|
||||||
the hash keys after calling a calculation method like `count`.
|
the hash keys after calling a calculation method like `count`.
|
||||||
|
|
||||||
Fixes #25595.
|
Fixes #25595.
|
||||||
|
|
||||||
*Sean Griffin*
|
*Sean Griffin*
|
||||||
|
@ -93,7 +99,7 @@
|
||||||
*Sean Griffin*
|
*Sean Griffin*
|
||||||
|
|
||||||
* Ensure hashes can be assigned to attributes created using `composed_of`.
|
* Ensure hashes can be assigned to attributes created using `composed_of`.
|
||||||
|
|
||||||
Fixes #25210.
|
Fixes #25210.
|
||||||
|
|
||||||
*Sean Griffin*
|
*Sean Griffin*
|
||||||
|
|
|
@ -16,6 +16,22 @@ module ActiveRecord
|
||||||
cattr_accessor :ignore_tables
|
cattr_accessor :ignore_tables
|
||||||
@@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
|
class << self
|
||||||
def dump(connection=ActiveRecord::Base.connection, stream=STDOUT, config = ActiveRecord::Base)
|
def dump(connection=ActiveRecord::Base.connection, stream=STDOUT, config = ActiveRecord::Base)
|
||||||
new(connection, generate_options(config)).dump(stream)
|
new(connection, generate_options(config)).dump(stream)
|
||||||
|
@ -146,20 +162,32 @@ HEADER
|
||||||
keys = @connection.migration_keys
|
keys = @connection.migration_keys
|
||||||
|
|
||||||
# figure out the lengths for each column based on above keys
|
# figure out the lengths for each column based on above keys
|
||||||
lengths = keys.map { |key|
|
lengths = if standardized_argument_widths
|
||||||
column_specs.map { |spec|
|
keys.map { |key|
|
||||||
spec[key] ? spec[key].length + 2 : 0
|
column_specs.map { |spec|
|
||||||
}.max
|
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
|
# the string we're going to sprintf our values against, with standardized column widths
|
||||||
format_string = lengths.map { |len| "%-#{len}s" }
|
format_string = if standardized_argument_widths
|
||||||
|
lengths.map { |len| "%-#{len}s" }
|
||||||
# find the max length for the 'type' column, which is special
|
else
|
||||||
type_length = column_specs.map { |column| column[:type].length }.max
|
["%s"] * keys.length
|
||||||
|
end
|
||||||
|
|
||||||
# add column type definition to our format string
|
# 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 *= ""
|
||||||
|
|
||||||
|
|
|
@ -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
|
assert_match %r{t\.time\s+"time_with_default",\s+default: '2000-01-01 07:17:04'}, output
|
||||||
end
|
end
|
||||||
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
|
||||||
|
|
|
@ -368,9 +368,11 @@ 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`.
|
* `ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans` controls whether Active Record will consider all `tinyint(1)` columns as booleans. Defaults to `true`.
|
||||||
|
|
||||||
The schema dumper adds one additional configuration option:
|
The schema dumper adds additional configuration options:
|
||||||
|
|
||||||
* `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.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
|
### Configuring Action Controller
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue