1
1
# frozen_string_literal: true
2
2
3
3
require "cases/helper_sqlserver"
4
+ require "stringio"
4
5
5
6
class SchemaDumperTestSQLServer < ActiveRecord ::TestCase
6
7
before { all_tables }
@@ -141,7 +142,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
141
142
it "honor nonstandard primary keys" do
142
143
generate_schema_for_table ( "movies" ) do |output |
143
144
match = output . match ( %r{create_table "movies"(.*)do} )
144
- assert_not_nil ( match , "nonstandardpk table not found" )
145
+ assert_not_nil ( match , "non-standard primary key table not found" )
145
146
assert_match %r(primary_key: "movieid") , match [ 1 ] , "non-standard primary key not preserved"
146
147
end
147
148
end
@@ -159,14 +160,32 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
159
160
_ ( output . scan ( 't.integer "unique_field"' ) . length ) . must_equal ( 1 )
160
161
end
161
162
163
+ it "schemas are dumped and tables names only include non-default schema" do
164
+ stream = StringIO . new
165
+ ActiveRecord ::SchemaDumper . dump ( ActiveRecord ::Base . connection_pool , stream )
166
+ generated_schema = stream . string
167
+
168
+ # Only generate non-default schemas. Default schema is 'dbo'.
169
+ assert_not_includes generated_schema , 'create_schema "dbo"'
170
+ assert_includes generated_schema , 'create_schema "test"'
171
+ assert_includes generated_schema , 'create_schema "test2"'
172
+
173
+ # Only non-default schemas should be included in table names. Default schema is 'dbo'.
174
+ assert_includes generated_schema , 'create_table "accounts"'
175
+ assert_includes generated_schema , 'create_table "test.aliens"'
176
+ assert_includes generated_schema , 'create_table "test2.sst_schema_test_multiple_schema"'
177
+ end
178
+
162
179
private
163
180
164
181
def generate_schema_for_table ( *table_names )
165
- require "stringio"
182
+ previous_ignore_tables = ActiveRecord ::SchemaDumper . ignore_tables
183
+ ActiveRecord ::SchemaDumper . ignore_tables = all_tables - table_names
166
184
167
185
stream = StringIO . new
168
186
ActiveRecord ::SchemaDumper . ignore_tables = all_tables - table_names
169
- ActiveRecord ::SchemaDumper . dump ( ActiveRecord ::Base . connection , stream )
187
+ ActiveRecord ::SchemaDumper . dump ( ActiveRecord ::Base . connection_pool , stream )
188
+
170
189
@generated_schema = stream . string
171
190
yield @generated_schema if block_given?
172
191
@schema_lines = Hash . new
@@ -177,6 +196,8 @@ def generate_schema_for_table(*table_names)
177
196
@schema_lines [ Regexp . last_match [ 1 ] ] = SchemaLine . new ( line )
178
197
end
179
198
@generated_schema
199
+ ensure
200
+ ActiveRecord ::SchemaDumper . ignore_tables = previous_ignore_tables
180
201
end
181
202
182
203
def line ( column_name )
0 commit comments