Skip to content

Commit be71de1

Browse files
committed
Added support for create_table :as, which fixes tests MigrationTest#test_create_table_with_query and MigrationTest#test_create_table_with_query_from_relation
1 parent efbead1 commit be71de1

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,34 @@ def add_column_options!(sql, options)
2222
super
2323
end
2424
end
25+
26+
def visit_TableDefinition(o)
27+
quoted_name = "#{quote_table_name((o.temporary ? '#' : '') + o.name.to_s)} "
28+
29+
if o.as
30+
if o.as.is_a?(ActiveRecord::Relation)
31+
select = o.as.to_sql
32+
elsif o.as.is_a?(String)
33+
select = o.as
34+
else
35+
raise "Only able to generate a table from a SELECT statement passed as a String or ActiveRecord::Relation"
36+
end
37+
38+
create_sql = 'SELECT * INTO '
39+
create_sql << quoted_name
40+
create_sql << 'FROM ('
41+
create_sql << select
42+
create_sql << ') AS __sq'
43+
44+
else
45+
create_sql = "CREATE TABLE "
46+
create_sql << quoted_name
47+
create_sql << "(#{o.columns.map { |c| accept c }.join(', ')}) "
48+
create_sql << "#{o.options}"
49+
end
50+
51+
create_sql
52+
end
2553
end
2654
end
2755
end

0 commit comments

Comments
 (0)