Skip to content

Commit 5b61f94

Browse files
committed
Support non-dbo schemas in dumper
1 parent b9b0912 commit 5b61f94

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

lib/active_record/connection_adapters/sqlserver/schema_dumper.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,17 @@ def schema_collation(column)
3939
def default_primary_key?(column)
4040
super && column.is_identity?
4141
end
42+
43+
def schemas(stream)
44+
schema_names = @connection.schema_names - ["guest"]
45+
46+
if schema_names.any?
47+
schema_names.sort.each do |name|
48+
stream.puts " create_schema #{name.inspect}"
49+
end
50+
stream.puts
51+
end
52+
end
4253
end
4354
end
4455
end

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -393,19 +393,33 @@ def drop_schema(schema_name)
393393
execute "DROP SCHEMA [#{schema_name}]"
394394
end
395395

396+
# Returns an array of schema names.
397+
def schema_names
398+
sql = <<~SQL.squish
399+
SELECT name
400+
FROM sys.schemas
401+
WHERE
402+
name NOT LIKE 'db_%' AND
403+
name NOT IN ('INFORMATION_SCHEMA', 'sys')
404+
SQL
405+
406+
query_values(sql, "SCHEMA")
407+
end
408+
396409
private
397410

398411
def data_source_sql(name = nil, type: nil)
399-
scope = quoted_scope name, type: type
412+
scope = quoted_scope(name, type: type)
400413

401-
table_name = lowercase_schema_reflection_sql 'TABLE_NAME'
402-
database = scope[:database].present? ? "#{scope[:database]}." : ""
414+
table_schema = lowercase_schema_reflection_sql('TABLE_SCHEMA')
415+
table_name = lowercase_schema_reflection_sql('TABLE_NAME')
416+
database = scope[:database].present? ? "#{scope[:database]}." : ""
403417
table_catalog = scope[:database].present? ? quote(scope[:database]) : "DB_NAME()"
404418

405-
sql = "SELECT #{table_name}"
419+
sql = "SELECT CONCAT(#{table_schema}, '.', #{table_name})"
406420
sql += " FROM #{database}INFORMATION_SCHEMA.TABLES WITH (NOLOCK)"
407421
sql += " WHERE TABLE_CATALOG = #{table_catalog}"
408-
sql += " AND TABLE_SCHEMA = #{quote(scope[:schema])}"
422+
sql += " AND TABLE_SCHEMA = #{quote(scope[:schema])}" if scope[:schema]
409423
sql += " AND TABLE_NAME = #{quote(scope[:name])}" if scope[:name]
410424
sql += " AND TABLE_TYPE = #{quote(scope[:type])}" if scope[:type]
411425
sql += " ORDER BY #{table_name}"
@@ -414,9 +428,10 @@ def data_source_sql(name = nil, type: nil)
414428

415429
def quoted_scope(name = nil, type: nil)
416430
identifier = SQLServer::Utils.extract_identifiers(name)
431+
417432
{}.tap do |scope|
418433
scope[:database] = identifier.database if identifier.database
419-
scope[:schema] = identifier.schema || "dbo"
434+
scope[:schema] = identifier.schema || "dbo" if name.present?
420435
scope[:name] = identifier.object if identifier.object
421436
scope[:type] = type if type
422437
end

0 commit comments

Comments
 (0)