2005-09-23 13:29:33 +00:00
|
|
|
require 'abstract_unit'
|
2008-01-18 07:31:37 +00:00
|
|
|
require 'active_record/schema_dumper'
|
2005-09-23 13:29:33 +00:00
|
|
|
require 'stringio'
|
|
|
|
|
|
|
|
if ActiveRecord::Base.connection.respond_to?(:tables)
|
|
|
|
|
2008-01-05 13:34:15 +00:00
|
|
|
class SchemaDumperTest < ActiveSupport::TestCase
|
2006-04-26 06:15:51 +00:00
|
|
|
def standard_dump
|
2005-10-29 18:40:49 +00:00
|
|
|
stream = StringIO.new
|
2006-07-08 20:35:56 +00:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = []
|
2005-10-29 18:40:49 +00:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
2006-04-26 06:15:51 +00:00
|
|
|
stream.string
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-04-26 06:15:51 +00:00
|
|
|
def test_schema_dump
|
|
|
|
output = standard_dump
|
2005-10-29 18:40:49 +00:00
|
|
|
assert_match %r{create_table "accounts"}, output
|
|
|
|
assert_match %r{create_table "authors"}, output
|
|
|
|
assert_no_match %r{create_table "schema_info"}, output
|
2005-09-23 13:29:33 +00:00
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-11-14 03:32:16 +00:00
|
|
|
def test_schema_dump_excludes_sqlite_sequence
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{create_table "sqlite_sequence"}, output
|
|
|
|
end
|
|
|
|
|
2006-04-26 06:15:51 +00: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 21:26:53 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
column_definition_lines.each do |column_set|
|
|
|
|
next if column_set.empty?
|
|
|
|
|
2008-01-18 07:30:42 +00:00
|
|
|
lengths = column_set.map do |column|
|
2007-05-11 21:26:53 +00:00
|
|
|
if match = column.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
|
|
|
|
match[0].length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal 1, lengths.uniq.length
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-04-26 06:15:51 +00:00
|
|
|
def test_arguments_line_up
|
2007-05-11 21:26:53 +00:00
|
|
|
column_definition_lines.each do |column_set|
|
2006-04-26 06:15:51 +00:00
|
|
|
assert_line_up(column_set, /:default => /)
|
|
|
|
assert_line_up(column_set, /:limit => /)
|
|
|
|
assert_line_up(column_set, /:null => /)
|
|
|
|
end
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-04-26 06:15:51 +00:00
|
|
|
def test_no_dump_errors
|
|
|
|
output = standard_dump
|
|
|
|
assert_no_match %r{\# Could not dump table}, output
|
|
|
|
end
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-03-18 21:27:40 +00:00
|
|
|
def test_schema_dump_includes_not_null_columns
|
|
|
|
stream = StringIO.new
|
2008-01-18 07:30:42 +00:00
|
|
|
|
2006-11-13 19:23:32 +00:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
|
2006-03-18 21:27:40 +00:00
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
|
|
|
assert_match %r{:null => false}, output
|
|
|
|
end
|
2005-12-24 16:55:55 +00:00
|
|
|
|
|
|
|
def test_schema_dump_with_string_ignored_table
|
|
|
|
stream = StringIO.new
|
2008-01-18 07:30:42 +00:00
|
|
|
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = ['accounts']
|
2005-12-24 16:55:55 +00:00
|
|
|
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_info"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_dump_with_regexp_ignored_table
|
|
|
|
stream = StringIO.new
|
2008-01-18 07:30:42 +00:00
|
|
|
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^account/]
|
2005-12-24 16:55:55 +00:00
|
|
|
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_info"}, output
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def test_schema_dump_illegal_ignored_table_value
|
2008-01-18 07:30:42 +00:00
|
|
|
stream = StringIO.new
|
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [5]
|
2005-12-24 16:55:55 +00:00
|
|
|
assert_raise(StandardError) do
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
end
|
|
|
|
end
|
2006-07-08 20:35:56 +00:00
|
|
|
|
2007-01-28 15:45:06 +00:00
|
|
|
if current_adapter?(:MysqlAdapter)
|
|
|
|
def test_schema_dump_should_not_add_default_value_for_mysql_text_field
|
|
|
|
output = standard_dump
|
2007-09-04 02:44:10 +00:00
|
|
|
assert_match %r{t.text\s+"body",\s+:default => "",\s+:null => false$}, output
|
2007-01-28 15:45:06 +00:00
|
|
|
end
|
2007-10-25 06:42:05 +00:00
|
|
|
|
|
|
|
def test_mysql_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
|
2007-01-28 15:45:06 +00:00
|
|
|
end
|
|
|
|
|
2006-07-08 20:35:56 +00:00
|
|
|
def test_schema_dump_includes_decimal_options
|
2008-01-18 07:30:42 +00:00
|
|
|
stream = StringIO.new
|
2006-07-08 20:35:56 +00:00
|
|
|
ActiveRecord::SchemaDumper.ignore_tables = [/^[^n]/]
|
|
|
|
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
|
|
|
|
output = stream.string
|
2006-07-24 17:14:48 +00:00
|
|
|
assert_match %r{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output
|
2006-07-08 20:35:56 +00:00
|
|
|
end
|
2005-09-23 13:29:33 +00:00
|
|
|
end
|
2005-10-29 18:40:49 +00:00
|
|
|
|
2005-09-23 13:29:33 +00:00
|
|
|
end
|