2008-01-21 12:20:51 -05:00
|
|
|
require "cases/helper"
|
2005-09-23 09:29:33 -04:00
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
class SchemaDumperTest < ActiveRecord::TestCase
|
|
|
|
def standard_dump
|
|
|
|
stream = StringIO.new
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = []
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
stream.string
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_excludes_sqlite_sequence
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{create_table "sqlite_sequence"}, output
|
|
|
|
end
|
2006-11-13 22:32:16 -05:00
|
|
|
|
2009-04-20 16:48:02 -04:00
|
|
|
def test_schema_dump_includes_camelcase_table_name
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{create_table "CamelCase"}, output
|
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def assert_line_up(lines, pattern, required = false)
|
|
|
|
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
|
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def column_definition_lines(output = standard_dump)
|
|
|
|
output.scan(/^( *)create_table.*?\n(.*?)^\1end/m).map{ |m| m.last.split(/\n/) }
|
|
|
|
end
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_types_line_up
|
|
|
|
column_definition_lines.each do |column_set|
|
|
|
|
next if column_set.empty?
|
2007-05-11 17:26:53 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
lengths = column_set.map do |column|
|
|
|
|
if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
|
|
|
|
match[0].length
|
2007-05-11 17:26:53 -04:00
|
|
|
end
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
assert_equal 1, lengths.uniq.length
|
2006-04-26 02:15:51 -04:00
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_arguments_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 => /)
|
2006-04-26 02:15:51 -04:00
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_no_dump_errors
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{\# Could not dump table}, output
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_not_null_columns
|
|
|
|
stream = StringIO.new
|
2005-12-24 11:55:55 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_match %r{:null => false}, output
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-06-14 22:55:56 -04:00
|
|
|
def test_schema_dump_includes_limit_constraint_for_integer_columns
|
|
|
|
stream = StringIO.new
|
|
|
|
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^(?!integer_limits)/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
|
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
assert_match %r{c_int_1.*:limit => 2}, output
|
|
|
|
assert_match %r{c_int_2.*:limit => 2}, output
|
|
|
|
|
|
|
|
# int 3 is 4 bytes in postgresql
|
|
|
|
assert_match %r{c_int_3.*}, output
|
|
|
|
assert_no_match %r{c_int_3.*:limit}, output
|
|
|
|
|
|
|
|
assert_match %r{c_int_4.*}, output
|
|
|
|
assert_no_match %r{c_int_4.*:limit}, output
|
2010-08-02 04:37:57 -04:00
|
|
|
elsif current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
2008-06-24 02:42:06 -04:00
|
|
|
assert_match %r{c_int_1.*:limit => 1}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
assert_match %r{c_int_2.*:limit => 2}, output
|
|
|
|
assert_match %r{c_int_3.*:limit => 3}, output
|
|
|
|
|
|
|
|
assert_match %r{c_int_4.*}, output
|
|
|
|
assert_no_match %r{c_int_4.*:limit}, output
|
2010-10-19 13:18:54 -04:00
|
|
|
elsif current_adapter?(:SQLite3Adapter)
|
2008-06-14 22:55:56 -04:00
|
|
|
assert_match %r{c_int_1.*:limit => 1}, output
|
|
|
|
assert_match %r{c_int_2.*:limit => 2}, output
|
|
|
|
assert_match %r{c_int_3.*:limit => 3}, output
|
|
|
|
assert_match %r{c_int_4.*:limit => 4}, output
|
|
|
|
end
|
|
|
|
assert_match %r{c_int_without_limit.*}, output
|
|
|
|
assert_no_match %r{c_int_without_limit.*:limit}, output
|
|
|
|
|
2010-10-19 13:18:54 -04:00
|
|
|
if current_adapter?(:SQLite3Adapter)
|
2008-06-14 22:55:56 -04:00
|
|
|
assert_match %r{c_int_5.*:limit => 5}, output
|
|
|
|
assert_match %r{c_int_6.*:limit => 6}, output
|
|
|
|
assert_match %r{c_int_7.*:limit => 7}, output
|
|
|
|
assert_match %r{c_int_8.*:limit => 8}, output
|
2009-03-22 18:20:42 -04:00
|
|
|
elsif current_adapter?(:OracleAdapter)
|
|
|
|
assert_match %r{c_int_5.*:limit => 5}, output
|
|
|
|
assert_match %r{c_int_6.*:limit => 6}, output
|
|
|
|
assert_match %r{c_int_7.*:limit => 7}, output
|
|
|
|
assert_match %r{c_int_8.*:limit => 8}, output
|
2008-06-14 22:55:56 -04:00
|
|
|
else
|
|
|
|
assert_match %r{c_int_5.*:limit => 8}, output
|
|
|
|
assert_match %r{c_int_6.*:limit => 8}, output
|
|
|
|
assert_match %r{c_int_7.*:limit => 8}, output
|
|
|
|
assert_match %r{c_int_8.*:limit => 8}, output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_with_string_ignored_table
|
|
|
|
stream = StringIO.new
|
|
|
|
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_schema_dump_with_regexp_ignored_table
|
|
|
|
stream = StringIO.new
|
2005-12-24 11:55:55 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_no_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_migrations"}, output
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_illegal_ignored_table_value
|
|
|
|
stream = StringIO.new
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [5]
|
|
|
|
assert_raise(StandardError) do
|
2005-12-24 11:55:55 -05:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
end
|
2008-05-06 19:08:23 -04:00
|
|
|
end
|
2005-12-24 11:55:55 -05:00
|
|
|
|
2009-04-20 11:47:31 -04:00
|
|
|
def test_schema_dumps_index_columns_in_right_order
|
|
|
|
index_definition = standard_dump.split(/\n/).grep(/add_index.*companies/).first.strip
|
|
|
|
assert_equal 'add_index "companies", ["firm_id", "type", "rating", "ruby_type"], :name => "company_index"', index_definition
|
|
|
|
end
|
2009-08-09 04:56:25 -04:00
|
|
|
|
2009-07-05 03:59:25 -04:00
|
|
|
def test_schema_dump_should_honor_nonstandard_primary_keys
|
|
|
|
output = standard_dump
|
|
|
|
match = output.match(%r{create_table "movies"(.*)do})
|
|
|
|
assert_not_nil(match, "nonstandardpk table not found")
|
|
|
|
assert_match %r(:primary_key => "movieid"), match[1], "non-standard primary key not preserved"
|
|
|
|
end
|
2009-04-20 11:47:31 -04:00
|
|
|
|
2010-08-02 04:37:57 -04:00
|
|
|
if current_adapter?(:MysqlAdapter) or current_adapter?(:Mysql2Adapter)
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{t.text\s+"body",\s+:null => false$}, output
|
2005-12-24 11:55:55 -05:00
|
|
|
end
|
2006-07-08 16:35:56 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
|
|
|
|
output = standard_dump
|
|
|
|
assert_match %r{t.binary\s+"tiny_blob",\s+:limit => 255$}, output
|
|
|
|
assert_match %r{t.binary\s+"normal_blob"$}, output
|
|
|
|
assert_match %r{t.binary\s+"medium_blob",\s+:limit => 16777215$}, output
|
|
|
|
assert_match %r{t.binary\s+"long_blob",\s+:limit => 2147483647$}, output
|
|
|
|
assert_match %r{t.text\s+"tiny_text",\s+:limit => 255$}, output
|
|
|
|
assert_match %r{t.text\s+"normal_text"$}, output
|
|
|
|
assert_match %r{t.text\s+"medium_text",\s+:limit => 16777215$}, output
|
|
|
|
assert_match %r{t.text\s+"long_text",\s+:limit => 2147483647$}, output
|
2006-07-08 16:35:56 -04:00
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2005-10-29 14:40:49 -04:00
|
|
|
|
2008-05-06 19:08:23 -04:00
|
|
|
def test_schema_dump_includes_decimal_options
|
|
|
|
stream = StringIO.new
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
|
|
|
|
end
|
2009-05-30 03:44:50 -04:00
|
|
|
|
2009-08-09 04:56:25 -04:00
|
|
|
if current_adapter?(:PostgreSQLAdapter)
|
|
|
|
def test_schema_dump_includes_xml_shorthand_definition
|
|
|
|
output = standard_dump
|
|
|
|
if %r{create_table "postgresql_xml_data_type"} =~ output
|
|
|
|
assert_match %r{t.xml "data"}, output
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-05-30 03:44:50 -04:00
|
|
|
def test_schema_dump_keeps_large_precision_integer_columns_as_decimal
|
|
|
|
output = standard_dump
|
2009-06-05 11:43:11 -04:00
|
|
|
# Oracle supports precision up to 38 and it identifies decimals with scale 0 as integers
|
|
|
|
if current_adapter?(:OracleAdapter)
|
|
|
|
assert_match %r{t.integer\s+"atoms_in_universe",\s+:precision => 38,\s+:scale => 0}, output
|
|
|
|
else
|
|
|
|
assert_match %r{t.decimal\s+"atoms_in_universe",\s+:precision => 55,\s+:scale => 0}, output
|
|
|
|
end
|
2009-05-30 03:44:50 -04:00
|
|
|
end
|
2009-08-08 18:29:20 -04:00
|
|
|
|
|
|
|
def test_schema_dump_keeps_id_column_when_id_is_false_and_id_column_added
|
|
|
|
output = standard_dump
|
|
|
|
match = output.match(%r{create_table "goofy_string_id"(.*)do.*\n(.*)\n})
|
|
|
|
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{t.string[[:space:]]+"id",[[:space:]]+:null => false$}, match[2], "non-primary key id column not preserved"
|
|
|
|
end
|
2005-09-23 09:29:33 -04:00
|
|
|
end
|
2009-08-09 04:56:25 -04:00
|
|
|
|