@@ -10,7 +10,7 @@ module Tasks
10
10
class SQLServerDatabaseTasks
11
11
DEFAULT_COLLATION = "SQL_Latin1_General_CP1_CI_AS"
12
12
13
- delegate :lease_connection , : establish_connection, to : ActiveRecord ::Base
13
+ delegate :establish_connection , to : ActiveRecord ::Base
14
14
15
15
def self . using_database_configurations?
16
16
true
@@ -23,8 +23,10 @@ def initialize(configuration)
23
23
24
24
def create ( master_established = false )
25
25
establish_master_connection unless master_established
26
- lease_connection . create_database configuration . database , configuration_hash . merge ( collation : default_collation )
27
- establish_connection configuration
26
+ with_connection do |connection |
27
+ connection . create_database ( configuration . database , configuration_hash . merge ( collation : default_collation ) )
28
+ end
29
+ establish_connection ( configuration )
28
30
rescue ActiveRecord ::StatementInvalid => e
29
31
if /database .* already exists/i === e . message
30
32
raise DatabaseAlreadyExists
@@ -35,15 +37,15 @@ def create(master_established = false)
35
37
36
38
def drop
37
39
establish_master_connection
38
- lease_connection . drop_database configuration . database
40
+ with_connection { | connection | connection . drop_database ( configuration . database ) }
39
41
end
40
42
41
43
def charset
42
- lease_connection . charset
44
+ with_connection { | connection | connection . charset }
43
45
end
44
46
45
47
def collation
46
- lease_connection . collation
48
+ with_connection { | connection | connection . collation }
47
49
end
48
50
49
51
def purge
@@ -56,34 +58,38 @@ def clear_active_connections!
56
58
ActiveRecord ::Base . connection_handler . clear_active_connections! ( :all )
57
59
end
58
60
59
- def structure_dump ( filename , extra_flags )
60
- server_arg = "-S #{ Shellwords . escape ( configuration_hash [ :host ] ) } "
61
- server_arg += ":#{ Shellwords . escape ( configuration_hash [ :port ] ) } " if configuration_hash [ :port ]
62
- command = [
63
- "defncopy-ttds" ,
64
- server_arg ,
65
- "-D #{ Shellwords . escape ( configuration_hash [ :database ] ) } " ,
66
- "-U #{ Shellwords . escape ( configuration_hash [ :username ] ) } " ,
67
- "-P #{ Shellwords . escape ( configuration_hash [ :password ] ) } " ,
68
- "-o #{ Shellwords . escape ( filename ) } " ,
69
- ]
70
- table_args = lease_connection . tables . map { |t | Shellwords . escape ( t ) }
71
- command . concat ( table_args )
72
- view_args = lease_connection . views . map { |v | Shellwords . escape ( v ) }
73
- command . concat ( view_args )
74
- raise "Error dumping database" unless Kernel . system ( command . join ( " " ) )
75
-
76
- dump = File . read ( filename )
77
- dump . gsub! ( /^USE .*$\n GO\n / , "" ) # Strip db USE statements
78
- dump . gsub! ( /^GO\n / , "" ) # Strip db GO statements
79
- dump . gsub! ( /nvarchar\( 8000\) / , "nvarchar(4000)" ) # Fix nvarchar(8000) column defs
80
- dump . gsub! ( /nvarchar\( -1\) / , "nvarchar(max)" ) # Fix nvarchar(-1) column defs
81
- dump . gsub! ( /text\( \d +\) / , "text" ) # Fix text(16) column defs
82
- File . open ( filename , "w" ) { |file | file . puts dump }
61
+ def structure_dump ( filename , _extra_flags )
62
+ with_connection do |connection |
63
+ server_arg = "-S #{ Shellwords . escape ( configuration_hash [ :host ] ) } "
64
+ server_arg += ":#{ Shellwords . escape ( configuration_hash [ :port ] ) } " if configuration_hash [ :port ]
65
+ command = [
66
+ "defncopy-ttds" ,
67
+ server_arg ,
68
+ "-D #{ Shellwords . escape ( configuration_hash [ :database ] ) } " ,
69
+ "-U #{ Shellwords . escape ( configuration_hash [ :username ] ) } " ,
70
+ "-P #{ Shellwords . escape ( configuration_hash [ :password ] ) } " ,
71
+ "-o #{ Shellwords . escape ( filename ) } " ,
72
+ ]
73
+ table_args = connection . tables . map { |t | Shellwords . escape ( t ) }
74
+ command . concat ( table_args )
75
+ view_args = connection . views . map { |v | Shellwords . escape ( v ) }
76
+ command . concat ( view_args )
77
+ raise "Error dumping database" unless Kernel . system ( command . join ( " " ) )
78
+
79
+ dump = File . read ( filename )
80
+ dump . gsub! ( /^USE .*$\n GO\n / , "" ) # Strip db USE statements
81
+ dump . gsub! ( /^GO\n / , "" ) # Strip db GO statements
82
+ dump . gsub! ( /nvarchar\( 8000\) / , "nvarchar(4000)" ) # Fix nvarchar(8000) column defs
83
+ dump . gsub! ( /nvarchar\( -1\) / , "nvarchar(max)" ) # Fix nvarchar(-1) column defs
84
+ dump . gsub! ( /text\( \d +\) / , "text" ) # Fix text(16) column defs
85
+ File . open ( filename , "w" ) { |file | file . puts dump }
86
+ end
83
87
end
84
88
85
- def structure_load ( filename , extra_flags )
86
- lease_connection . execute File . read ( filename )
89
+ def structure_load ( filename , _extra_flags )
90
+ with_connection do |connection |
91
+ connection . execute File . read ( filename )
92
+ end
87
93
end
88
94
89
95
private
0 commit comments