Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions lib/active_record/connection_adapters/sqlserver/schema_creation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@ class SchemaCreation < AbstractAdapter::SchemaCreation
private

def visit_TableDefinition(o)
if_not_exists = o.if_not_exists

if o.as
table_name = quote_table_name(o.temporary ? "##{o.name}" : o.name)
query = o.as.respond_to?(:to_sql) ? o.as.to_sql : o.as
projections, source = query.match(%r{SELECT\s+(.*)?\s+FROM\s+(.*)?}).captures
select_into = "SELECT #{projections} INTO #{table_name} FROM #{source}"
sql = "SELECT #{projections} INTO #{table_name} FROM #{source}"
else
o.instance_variable_set :@as, nil
super
o.instance_variable_set :@if_not_exists, false
sql = super
end

if if_not_exists
o.instance_variable_set :@if_not_exists, true
table_name = o.temporary ? "##{o.name}" : o.name
sql = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='#{table_name}' and xtype='U') #{sql}"
end

sql
end

def add_column_options!(sql, options)
Expand Down