Skip to content

Commit e50e2f9

Browse files
committed
Azure-Friendly DB create/drop. Fixes #442
Create allows edition options like: * MAXSIZE * EDITION * SERVICE_OBJECTIVE
1 parent 140957b commit e50e2f9

File tree

4 files changed

+53
-13
lines changed

4 files changed

+53
-13
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
#### Fixed
55

66
* Azure-Friendly Disable Referential Integrity. No more `sp_MSforeachtable` usage. Fixes #421
7+
* Azure-Friendly DB create/drop. Fixes #442
8+
- Create allows edition options like: MAXSIZE, EDITION, and SERVICE_OBJECTIVE.
79

810

911
## v4.2.7

lib/active_record/connection_adapters/sqlserver/database_tasks.rb

Lines changed: 35 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,9 @@ module DatabaseTasks
55

66
def create_database(database, options = {})
77
name = SQLServer::Utils.extract_identifiers(database)
8-
options = {collation: @connection_options[:collation]}.merge!(options.symbolize_keys)
9-
options = options.select { |_, v| v.present? }
10-
option_string = options.inject("") do |memo, (key, value)|
11-
memo += case key
12-
when :collation
13-
" COLLATE #{value}"
14-
else
15-
""
16-
end
17-
end
18-
do_execute "CREATE DATABASE #{name}#{option_string}"
8+
db_options = create_database_options(options)
9+
edition_options = create_database_edition_options(options)
10+
do_execute "CREATE DATABASE #{name} #{db_options} #{edition_options}"
1911
end
2012

2113
def drop_database(database)
@@ -35,6 +27,38 @@ def collation
3527
select_value "SELECT DATABASEPROPERTYEX(DB_NAME(), 'Collation')"
3628
end
3729

30+
31+
private
32+
33+
def create_database_options(options={})
34+
keys = [:collate]
35+
copts = @connection_options
36+
options = {
37+
collate: copts[:collation]
38+
}.merge(options.symbolize_keys).select { |_, v|
39+
v.present?
40+
}.slice(*keys).map { |k,v|
41+
"#{k.to_s.upcase} #{v}"
42+
}.join(' ')
43+
options
44+
end
45+
46+
def create_database_edition_options(options={})
47+
keys = [:maxsize, :edition, :service_objective]
48+
copts = @connection_options
49+
edition_options = {
50+
maxsize: copts[:azure_maxsize],
51+
edition: copts[:azure_edition],
52+
service_objective: copts[:azure_service_objective]
53+
}.merge(options.symbolize_keys).select { |_, v|
54+
v.present?
55+
}.slice(*keys).map { |k,v|
56+
"#{k.to_s.upcase} = #{v}"
57+
}.join(', ')
58+
edition_options = "( #{edition_options} )" if edition_options.present?
59+
edition_options
60+
end
61+
3862
end
3963
end
4064
end

test/cases/rake_test_sqlserver.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,24 @@ def disconnect!
2323
end
2424

2525
def reconnect
26-
ActiveRecord::Base.establish_connection(default_configuration)
27-
connection.drop_database(new_database) rescue nil
26+
config = default_configuration
27+
if connection_sqlserver_azure?
28+
ActiveRecord::Base.establish_connection(config.merge('database' => 'master'))
29+
connection.drop_database(new_database) rescue nil
30+
disconnect!
31+
ActiveRecord::Base.establish_connection(config)
32+
else
33+
ActiveRecord::Base.establish_connection(config)
34+
connection.drop_database(new_database) rescue nil
35+
end
2836
end
2937

3038
end
3139

3240
class SQLServerRakeCreateTest < SQLServerRakeTest
3341

42+
self.azure_skip = false
43+
3444
it 'establishes connection to database after create ' do
3545
db_tasks.create configuration
3646
connection.current_database.must_equal(new_database)
@@ -56,6 +66,8 @@ class SQLServerRakeCreateTest < SQLServerRakeTest
5666

5767
class SQLServerRakeDropTest < SQLServerRakeTest
5868

69+
self.azure_skip = false
70+
5971
it 'drops database and uses master' do
6072
db_tasks.create configuration
6173
db_tasks.drop configuration

test/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ connections:
2020
dataserver: <%= ENV['ACTIVERECORD_UNITTEST_DATASERVER'] %>
2121
tds_version: <%= ENV['ACTIVERECORD_UNITTEST_TDSVERSION'] %>
2222
azure: <%= !ENV['ACTIVERECORD_UNITTEST_AZURE'].nil? %>
23+
timeout: <%= ENV['ACTIVERECORD_UNITTEST_AZURE'].present? ? 20 : nil %>
2324
arunit2:
2425
<<: *default_connection_info
2526
database: activerecord_unittest2
2627
appname: SQLServerAdptrUnit2
2728
dataserver: <%= ENV['ACTIVERECORD_UNITTEST_DATASERVER'] %>
2829
tds_version: <%= ENV['ACTIVERECORD_UNITTEST_TDSVERSION'] %>
2930
azure: <%= !ENV['ACTIVERECORD_UNITTEST_AZURE'].nil? %>
31+
timeout: <%= ENV['ACTIVERECORD_UNITTEST_AZURE'].present? ? 20 : nil %>
3032

3133
odbc:
3234
arunit:

0 commit comments

Comments
 (0)