Skip to content

Commit 3a68ac3

Browse files
committed
[Rails5] Support 2014, 2012 drop table statement.
1 parent f1065a5 commit 3a68ac3

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/active_record/connection_adapters/sqlserver/schema_statements.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ def create_table(table_name, comment: nil, **options)
4242
res
4343
end
4444

45+
def drop_table(table_name, options = {})
46+
if options[:if_exists] && @version_year != 2016
47+
execute "IF EXISTS(SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = #{quote(table_name)}) DROP TABLE #{quote_table_name(table_name)}"
48+
else
49+
super
50+
end
51+
end
52+
4553
def indexes(table_name, name = nil)
4654
data = select("EXEC sp_helpindex #{quote(table_name)}", name) rescue []
4755
data.reduce([]) do |indexes, index|

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,7 @@ def connect
339339
dblib_connect(config)
340340
end
341341
@spid = _raw_select('SELECT @@SPID', fetch: :rows).first.first
342+
@version_year = version_year
342343
configure_connection
343344
end
344345

@@ -415,6 +416,13 @@ def initialize_dateformatter
415416
}
416417
end
417418

419+
def version_year
420+
vstring = _raw_select('SELECT @@version', fetch: :rows).first.first.to_s
421+
/SQL Server (\d+)/.match(vstring).to_a.last.to_s.to_i
422+
rescue Exception => e
423+
2016
424+
end
425+
418426
end
419427
end
420428
end

0 commit comments

Comments
 (0)