Skip to content

Commit 216661f

Browse files
committed
Added test
1 parent ff447ca commit 216661f

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

test/cases/schema_dumper_test_sqlserver.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# frozen_string_literal: true
22

33
require "cases/helper_sqlserver"
4+
require "stringio"
45

56
class SchemaDumperTestSQLServer < ActiveRecord::TestCase
67
before { all_tables }
@@ -141,7 +142,7 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
141142
it "honor nonstandard primary keys" do
142143
generate_schema_for_table("movies") do |output|
143144
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")
145146
assert_match %r(primary_key: "movieid"), match[1], "non-standard primary key not preserved"
146147
end
147148
end
@@ -159,15 +160,30 @@ class SchemaDumperTestSQLServer < ActiveRecord::TestCase
159160
_(output.scan('t.integer "unique_field"').length).must_equal(1)
160161
end
161162

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+
162178
private
163179

164180
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
166183

167184
stream = StringIO.new
168-
ActiveRecord::SchemaDumper.ignore_tables = all_tables - table_names
169185
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection_pool, stream)
170-
186+
171187
@generated_schema = stream.string
172188
yield @generated_schema if block_given?
173189
@schema_lines = Hash.new
@@ -178,6 +194,8 @@ def generate_schema_for_table(*table_names)
178194
@schema_lines[Regexp.last_match[1]] = SchemaLine.new(line)
179195
end
180196
@generated_schema
197+
ensure
198+
ActiveRecord::SchemaDumper.ignore_tables = previous_ignore_tables
181199
end
182200

183201
def line(column_name)

0 commit comments

Comments
 (0)