2 require 'active_record/schema_dumper'
6 class SchemaDumperTest
< ActiveRecord
::TestCase
9 ActiveRecord
::SchemaDumper.ignore_tables
= []
10 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
15 output
= standard_dump
16 assert_match
%r
{create_table
"accounts"}, output
17 assert_match
%r
{create_table
"authors"}, output
18 assert_no_match
%r
{create_table
"schema_migrations"}, output
21 def test_schema_dump_excludes_sqlite_sequence
22 output
= standard_dump
23 assert_no_match
%r
{create_table
"sqlite_sequence"}, output
26 def assert_line_up(lines
, pattern
, required
= false)
27 return assert(true) if lines
.empty
?
28 matches
= lines
.map
{ |line
| line
.match(pattern
) }
29 assert matches
.all
? if required
31 return assert(true) if matches
.empty
?
32 assert_equal
1, matches
.map
{ |match
| match
.offset(0).first
}.uniq
.length
35 def column_definition_lines(output
= standard_dump
)
36 output
.scan(/^( *)create_table.*?\n(.*?)^\1end/m
).map
{ |m
| m
.last
.split(/\n/) }
39 def test_types_line_up
40 column_definition_lines
.each
do |column_set
|
41 next if column_set
.empty
?
43 lengths
= column_set
.map
do |column
|
44 if match
= column
.match(/t\.(?:integer|decimal|float|datetime|timestamp|time|date|text|binary|string|boolean)\s+"/)
49 assert_equal
1, lengths
.uniq
.length
53 def test_arguments_line_up
54 column_definition_lines
.each
do |column_set
|
55 assert_line_up(column_set
, /:default => /)
56 assert_line_up(column_set
, /:limit => /)
57 assert_line_up(column_set
, /:null => /)
61 def test_no_dump_errors
62 output
= standard_dump
63 assert_no_match
%r
{\
# Could not dump table}, output
66 def test_schema_dump_includes_not_null_columns
69 ActiveRecord
::SchemaDumper.ignore_tables
= [/^[^r]/]
70 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
71 output
= stream
.string
72 assert_match
%r
{:null => false}, output
75 def test_schema_dump_includes_limit_constraint_for_integer_columns
78 ActiveRecord
::SchemaDumper.ignore_tables
= [/^(?!integer_limits)/]
79 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
80 output
= stream
.string
82 if current_adapter
?(:PostgreSQLAdapter)
83 assert_match
%r
{c_int_1
.*:limit => 2}, output
84 assert_match
%r
{c_int_2
.*:limit => 2}, output
86 # int 3 is 4 bytes in postgresql
87 assert_match
%r
{c_int_3
.*}, output
88 assert_no_match
%r
{c_int_3
.*:limit}, output
90 assert_match
%r
{c_int_4
.*}, output
91 assert_no_match
%r
{c_int_4
.*:limit}, output
92 elsif current_adapter
?(:MysqlAdapter)
93 assert_match
%r
{c_int_1
.*:limit => 1}, output
94 assert_match
%r
{c_int_2
.*:limit => 2}, output
95 assert_match
%r
{c_int_3
.*:limit => 3}, output
97 assert_match
%r
{c_int_4
.*}, output
98 assert_no_match
%r
{c_int_4
.*:limit}, output
99 elsif current_adapter
?(:SQLiteAdapter)
100 assert_match
%r
{c_int_1
.*:limit => 1}, output
101 assert_match
%r
{c_int_2
.*:limit => 2}, output
102 assert_match
%r
{c_int_3
.*:limit => 3}, output
103 assert_match
%r
{c_int_4
.*:limit => 4}, output
105 assert_match
%r
{c_int_without_limit
.*}, output
106 assert_no_match
%r
{c_int_without_limit
.*:limit}, output
108 if current_adapter
?(:SQLiteAdapter)
109 assert_match
%r
{c_int_5
.*:limit => 5}, output
110 assert_match
%r
{c_int_6
.*:limit => 6}, output
111 assert_match
%r
{c_int_7
.*:limit => 7}, output
112 assert_match
%r
{c_int_8
.*:limit => 8}, output
114 assert_match
%r
{c_int_5
.*:limit => 8}, output
115 assert_match
%r
{c_int_6
.*:limit => 8}, output
116 assert_match
%r
{c_int_7
.*:limit => 8}, output
117 assert_match
%r
{c_int_8
.*:limit => 8}, output
121 def test_schema_dump_with_string_ignored_table
122 stream
= StringIO
.new
124 ActiveRecord
::SchemaDumper.ignore_tables
= ['accounts']
125 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
126 output
= stream
.string
127 assert_no_match
%r
{create_table
"accounts"}, output
128 assert_match
%r
{create_table
"authors"}, output
129 assert_no_match
%r
{create_table
"schema_migrations"}, output
132 def test_schema_dump_with_regexp_ignored_table
133 stream
= StringIO
.new
135 ActiveRecord
::SchemaDumper.ignore_tables
= [/^account/]
136 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
137 output
= stream
.string
138 assert_no_match
%r
{create_table
"accounts"}, output
139 assert_match
%r
{create_table
"authors"}, output
140 assert_no_match
%r
{create_table
"schema_migrations"}, output
143 def test_schema_dump_illegal_ignored_table_value
144 stream
= StringIO
.new
145 ActiveRecord
::SchemaDumper.ignore_tables
= [5]
146 assert_raise(StandardError
) do
147 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
151 if current_adapter
?(:MysqlAdapter)
152 def test_schema_dump_should_not_add_default_value_for_mysql_text_field
153 output
= standard_dump
154 assert_match
%r
{t
.text\s
+"body",\s
+:null => false$
}, output
157 def test_mysql_schema_dump_should_honor_nonstandard_primary_keys
158 output
= standard_dump
159 match
= output
.match(%r
{create_table
"movies"(.*)do})
160 assert_not_nil(match
, "nonstandardpk table not found")
161 assert_match
%r(:primary_key => "movieid"), match
[1], "non-standard primary key not preserved"
164 def test_schema_dump_includes_length_for_mysql_blob_and_text_fields
165 output
= standard_dump
166 assert_match
%r
{t
.binary\s
+"tiny_blob",\s
+:limit => 255$
}, output
167 assert_match
%r
{t
.binary\s
+"normal_blob"$
}, output
168 assert_match
%r
{t
.binary\s
+"medium_blob",\s
+:limit => 16777215$
}, output
169 assert_match
%r
{t
.binary\s
+"long_blob",\s
+:limit => 2147483647$
}, output
170 assert_match
%r
{t
.text\s
+"tiny_text",\s
+:limit => 255$
}, output
171 assert_match
%r
{t
.text\s
+"normal_text"$
}, output
172 assert_match
%r
{t
.text\s
+"medium_text",\s
+:limit => 16777215$
}, output
173 assert_match
%r
{t
.text\s
+"long_text",\s
+:limit => 2147483647$
}, output
177 def test_schema_dump_includes_decimal_options
178 stream
= StringIO
.new
179 ActiveRecord
::SchemaDumper.ignore_tables
= [/^[^n]/]
180 ActiveRecord
::SchemaDumper.dump(ActiveRecord
::Base.connection
, stream
)
181 output
= stream
.string
182 assert_match
%r
{:precision => 3,[[:space:]]+:scale => 2,[[:space:]]+:default => 2.78}, output