1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Include default column limits in schema.rb

Allows :limit defaults to be changed without pulling the rug out from
under old migrations that omitted :limit because it matched the default
at the time.
This commit is contained in:
Jeremy Kemper 2014-09-10 14:22:41 -07:00
parent da74eeb294
commit 2c76793f08
3 changed files with 23 additions and 8 deletions

View file

@ -1,3 +1,8 @@
* Include default column limits in schema.rb. Allows defaults to be changed
in the future without affecting old migrations that assumed old defaults.
*Jeremy Kemper*
* MySQL: schema.rb now includes TEXT and BLOB column limits. * MySQL: schema.rb now includes TEXT and BLOB column limits.
*Jeremy Kemper* *Jeremy Kemper*

View file

@ -19,12 +19,16 @@ module ActiveRecord
spec = {} spec = {}
spec[:name] = column.name.inspect spec[:name] = column.name.inspect
spec[:type] = column.type.to_s spec[:type] = column.type.to_s
spec[:limit] = column.limit.inspect if column.limit != types[column.type][:limit] spec[:null] = 'false' unless column.null
limit = column.limit || types[column.type][:limit]
spec[:limit] = limit.inspect if limit
spec[:precision] = column.precision.inspect if column.precision spec[:precision] = column.precision.inspect if column.precision
spec[:scale] = column.scale.inspect if column.scale spec[:scale] = column.scale.inspect if column.scale
spec[:null] = 'false' unless column.null
spec[:default] = schema_default(column) if column.has_default? default = schema_default(column) if column.has_default?
spec.delete(:default) if spec[:default].nil? spec[:default] = default unless default.nil?
spec spec
end end

View file

@ -100,7 +100,11 @@ class SchemaDumperTest < ActiveRecord::TestCase
def test_schema_dump_includes_limit_constraint_for_integer_columns def test_schema_dump_includes_limit_constraint_for_integer_columns
output = dump_all_table_schema([/^(?!integer_limits)/]) output = dump_all_table_schema([/^(?!integer_limits)/])
assert_match %r{c_int_without_limit}, output
if current_adapter?(:PostgreSQLAdapter) if current_adapter?(:PostgreSQLAdapter)
assert_no_match %r{c_int_without_limit.*limit:}, output
assert_match %r{c_int_1.*limit: 2}, output assert_match %r{c_int_1.*limit: 2}, output
assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_2.*limit: 2}, output
@ -111,6 +115,8 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_match %r{c_int_4.*}, output assert_match %r{c_int_4.*}, output
assert_no_match %r{c_int_4.*limit:}, output assert_no_match %r{c_int_4.*limit:}, output
elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter) elsif current_adapter?(:MysqlAdapter, :Mysql2Adapter)
assert_match %r{c_int_without_limit.*limit: 4}, output
assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_1.*limit: 1}, output
assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_2.*limit: 2}, output
assert_match %r{c_int_3.*limit: 3}, output assert_match %r{c_int_3.*limit: 3}, output
@ -118,13 +124,13 @@ class SchemaDumperTest < ActiveRecord::TestCase
assert_match %r{c_int_4.*}, output assert_match %r{c_int_4.*}, output
assert_no_match %r{c_int_4.*:limit}, output assert_no_match %r{c_int_4.*:limit}, output
elsif current_adapter?(:SQLite3Adapter) elsif current_adapter?(:SQLite3Adapter)
assert_no_match %r{c_int_without_limit.*limit:}, output
assert_match %r{c_int_1.*limit: 1}, output assert_match %r{c_int_1.*limit: 1}, output
assert_match %r{c_int_2.*limit: 2}, output assert_match %r{c_int_2.*limit: 2}, output
assert_match %r{c_int_3.*limit: 3}, output assert_match %r{c_int_3.*limit: 3}, output
assert_match %r{c_int_4.*limit: 4}, output assert_match %r{c_int_4.*limit: 4}, output
end end
assert_match %r{c_int_without_limit.*}, output
assert_no_match %r{c_int_without_limit.*limit:}, output
if current_adapter?(:SQLite3Adapter) if current_adapter?(:SQLite3Adapter)
assert_match %r{c_int_5.*limit: 5}, output assert_match %r{c_int_5.*limit: 5}, output
@ -354,7 +360,7 @@ class SchemaDumperTest < ActiveRecord::TestCase
match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n}) match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n})
assert_not_nil(match, "goofy_string_id table not found") assert_not_nil(match, "goofy_string_id table not found")
assert_match %r(id: false), match[1], "no table id not preserved" assert_match %r(id: false), match[1], "no table id not preserved"
assert_match %r{t.string[[:space:]]+"id",[[:space:]]+null: false$}, match[2], "non-primary key id column not preserved" assert_match %r{t.string\s+"id",.*?null: false$}, match[2], "non-primary key id column not preserved"
end end
def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added def test_schema_dump_keeps_id_false_when_id_is_false_and_unique_not_null_column_added
@ -433,7 +439,7 @@ class SchemaDumperDefaultsTest < ActiveRecord::TestCase
def test_schema_dump_defaults_with_universally_supported_types def test_schema_dump_defaults_with_universally_supported_types
output = dump_table_schema('defaults') output = dump_table_schema('defaults')
assert_match %r{t\.string\s+"string_with_default",\s+default: "Hello!"}, output assert_match %r{t\.string\s+"string_with_default",.*?default: "Hello!"}, output
assert_match %r{t\.date\s+"date_with_default",\s+default: '2014-06-05'}, output assert_match %r{t\.date\s+"date_with_default",\s+default: '2014-06-05'}, output
assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: '2014-06-05 07:17:04'}, output assert_match %r{t\.datetime\s+"datetime_with_default",\s+default: '2014-06-05 07:17:04'}, output
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