Skip to content

Commit 420153a

Browse files
committed
added if_not_exists option with workaround
1 parent 6dcc1b7 commit 420153a

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

lib/active_record/connection_adapters/sqlserver/schema_creation.rb

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,26 @@ class SchemaCreation < AbstractAdapter::SchemaCreation
66
private
77

88
def visit_TableDefinition(o)
9+
if_not_exists = o.if_not_exists
10+
911
if o.as
1012
table_name = quote_table_name(o.temporary ? "##{o.name}" : o.name)
1113
query = o.as.respond_to?(:to_sql) ? o.as.to_sql : o.as
1214
projections, source = query.match(%r{SELECT\s+(.*)?\s+FROM\s+(.*)?}).captures
13-
select_into = "SELECT #{projections} INTO #{table_name} FROM #{source}"
15+
sql = "SELECT #{projections} INTO #{table_name} FROM #{source}"
1416
else
1517
o.instance_variable_set :@as, nil
16-
super
18+
o.instance_variable_set :@if_not_exists, false
19+
sql = super
20+
end
21+
22+
if if_not_exists
23+
o.instance_variable_set :@if_not_exists, true
24+
table_name = o.temporary ? "##{o.name}" : o.name
25+
sql = "IF NOT EXISTS (SELECT * FROM sysobjects WHERE name='#{table_name}' and xtype='U') #{sql}"
1726
end
27+
28+
sql
1829
end
1930

2031
def add_column_options!(sql, options)

0 commit comments

Comments
 (0)