Skip to content

Commit 3546db7

Browse files
committed
[Rails3] Last reorganization/cleanup. Rake task database statements.
1 parent fa613e7 commit 3546db7

File tree

2 files changed

+49
-53
lines changed

2 files changed

+49
-53
lines changed

lib/active_record/connection_adapters/sqlserver/database_statements.rb

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,54 @@ def run_with_isolation_level(isolation_level)
162162
end if block_given?
163163
end
164164

165+
# === SQLServer Specific (Rake/Test Helpers) ==================== #
166+
167+
def recreate_database
168+
remove_database_connections_and_rollback do
169+
do_execute "EXEC sp_MSforeachtable 'DROP TABLE ?'"
170+
end
171+
end
172+
173+
def recreate_database!(database=nil)
174+
current_db = current_database
175+
database ||= current_db
176+
this_db = database.to_s == current_db
177+
do_execute 'USE master' if this_db
178+
drop_database(database)
179+
create_database(database)
180+
ensure
181+
use_database(current_db) if this_db
182+
end
183+
184+
def drop_database(database)
185+
retry_count = 0
186+
max_retries = 1
187+
begin
188+
do_execute "DROP DATABASE #{quote_table_name(database)}"
189+
rescue ActiveRecord::StatementInvalid => err
190+
if err.message =~ /because it is currently in use/i
191+
raise if retry_count >= max_retries
192+
retry_count += 1
193+
remove_database_connections_and_rollback(database)
194+
retry
195+
else
196+
raise
197+
end
198+
end
199+
end
200+
201+
def create_database(database)
202+
do_execute "CREATE DATABASE #{quote_table_name(database)}"
203+
end
204+
205+
def current_database
206+
select_value 'SELECT DB_NAME()'
207+
end
208+
209+
def charset
210+
select_value "SELECT SERVERPROPERTY('SqlCharSetName')"
211+
end
212+
165213

166214
protected
167215

lib/active_record/connection_adapters/sqlserver_adapter.rb

Lines changed: 1 addition & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -323,59 +323,7 @@ def native_binary_database_type
323323
@@native_binary_database_type || ((sqlserver_2005? || sqlserver_2008?) ? 'varbinary(max)' : 'image')
324324
end
325325

326-
# RAKE UTILITY METHODS =====================================#
327-
328-
def recreate_database
329-
remove_database_connections_and_rollback do
330-
do_execute "EXEC sp_MSforeachtable 'DROP TABLE ?'"
331-
end
332-
end
333-
334-
def recreate_database!(database=nil)
335-
current_db = current_database
336-
database ||= current_db
337-
this_db = database.to_s == current_db
338-
do_execute 'USE master' if this_db
339-
drop_database(database)
340-
create_database(database)
341-
ensure
342-
use_database(current_db) if this_db
343-
end
344-
345-
# Remove existing connections and rollback any transactions if we received the message
346-
# 'Cannot drop the database 'test' because it is currently in use'
347-
def drop_database(database)
348-
retry_count = 0
349-
max_retries = 1
350-
begin
351-
do_execute "DROP DATABASE #{quote_table_name(database)}"
352-
rescue ActiveRecord::StatementInvalid => err
353-
if err.message =~ /because it is currently in use/i
354-
raise if retry_count >= max_retries
355-
retry_count += 1
356-
remove_database_connections_and_rollback(database)
357-
retry
358-
else
359-
raise
360-
end
361-
end
362-
end
363-
364-
def create_database(database)
365-
do_execute "CREATE DATABASE #{quote_table_name(database)}"
366-
end
367-
368-
def current_database
369-
select_value 'SELECT DB_NAME()'
370-
end
371-
372-
def charset
373-
select_value "SELECT SERVERPROPERTY('SqlCharSetName')"
374-
end
375-
376-
377-
378-
326+
379327
protected
380328

381329
# === SQLServer Specific (Connection Management) ================ #

0 commit comments

Comments
 (0)