Skip to content

Commit 62bfe25

Browse files
committed
Support create table with query from relation or select statement.
HasOneAssociationsTest#test_has_one_does_not_use_order_by MigrationTest#test_create_table_with_query: 9 failures, 4 errors, 2 skips
1 parent da0156f commit 62bfe25

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Aliased `ActiveRecord::Type::SQLServer` to `ActiveRecord::ConnectionAdapters::SQLServer::Type`
88
* New `SQLServer::Utils::Name` object for decomposing and quoting SQL Server names/identifiers.
99
* Support for most all SQL Server types in schema statements and dumping.
10-
* Support create table with query from relation.
10+
* Support create table with query from relation or select statement.
1111

1212
#### Changed
1313

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,9 @@ def visit_ColumnDefinition(o)
1616

1717
def visit_TableDefinition(o)
1818
if o.as
19-
visitor = o.as.connection.visitor
20-
table_name = o.temporary ? "##{o.name}" : o.name
21-
select_into = "SELECT "
22-
select_into << "(#{visitor.accept(o.as.arel.projections, Arel::Collectors::PlainString.new).value}) "
23-
select_into << "INTO "
24-
select_into << "#{quote_table_name(table_name)} "
25-
select_into << "FROM "
26-
select_into << "#{visitor.accept(o.as.arel.froms[0], Arel::Collectors::PlainString.new).value}"
19+
table_name = quote_table_name(o.temporary ? "##{o.name}" : o.name)
20+
projections, source = @conn.to_sql(o.as).match(%r{SELECT\s+(.*)?\s+FROM\s+(.*)?}).captures
21+
select_into = "SELECT #{projections} INTO #{table_name} FROM #{source}"
2722
else
2823
o.instance_variable_set :@as, nil
2924
super

test/cases/coerced_tests.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,16 @@ def test_take_and_first_and_last_with_integer_should_use_sql_limit_coerced
291291

292292

293293

294+
class HasOneAssociationsTest < ActiveRecord::TestCase
295+
296+
# We use OFFSET/FETCH vs TOP. So we always have an order.
297+
coerce_tests! :test_has_one_does_not_use_order_by
298+
299+
end
300+
301+
302+
303+
294304
require 'models/company'
295305
class InheritanceTest < ActiveRecord::TestCase
296306

0 commit comments

Comments
 (0)