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,15 +160,30 @@ 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 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
+ assert_not_includes generated_schema , 'create_schema "dbo"'
169
+ assert_includes generated_schema , 'create_schema "test"'
170
+ assert_includes generated_schema , 'create_schema "test2"'
171
+
172
+ # Only non-default schemas should be included in table names. Default schema is 'dbo'.
173
+ assert_includes generated_schema , 'create_table "accounts"'
174
+ assert_includes generated_schema , 'create_table "test.aliens"'
175
+ assert_includes generated_schema , 'create_table "test2.sst_schema_test_mulitple_schema"'
176
+ end
177
+
162
178
private
163
179
164
180
def generate_schema_for_table ( *table_names )
165
- require "stringio"
181
+ previous_ignore_tables = ActiveRecord ::SchemaDumper . ignore_tables
182
+ ActiveRecord ::SchemaDumper . ignore_tables = all_tables - table_names
166
183
167
184
stream = StringIO . new
168
- ActiveRecord ::SchemaDumper . ignore_tables = all_tables - table_names
169
185
ActiveRecord ::SchemaDumper . dump ( ActiveRecord ::Base . connection_pool , stream )
170
-
186
+
171
187
@generated_schema = stream . string
172
188
yield @generated_schema if block_given?
173
189
@schema_lines = Hash . new
@@ -178,6 +194,8 @@ def generate_schema_for_table(*table_names)
178
194
@schema_lines [ Regexp . last_match [ 1 ] ] = SchemaLine . new ( line )
179
195
end
180
196
@generated_schema
197
+ ensure
198
+ ActiveRecord ::SchemaDumper . ignore_tables = previous_ignore_tables
181
199
end
182
200
183
201
def line ( column_name )
0 commit comments