minus5_mssql 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -46,7 +46,6 @@ module Minus5
46
46
  def execute(sql)
47
47
  @connection.execute(sql)
48
48
  rescue TinyTds::Error => e
49
- print "execute error #{e}\n"
50
49
  connect
51
50
  execute(sql)
52
51
  end
@@ -75,7 +74,7 @@ module Minus5
75
74
  return results if results[0].kind_of?(Hash)
76
75
  data = {} #parent indexed by primary_key
77
76
  results[0].each{ |row| data[row[options[:primary_key]]] = row }
78
- options[:relations].each_with_index do |relation, index|index
77
+ options[:relations].each_with_index do |relation, index|
79
78
  result = results[index+1] #child result
80
79
  result.each do |row|
81
80
  #find parent row by foreign key and insert child row in collection
@@ -109,21 +108,42 @@ module Minus5
109
108
  create table #{schema}.#{table} (#{columns_def})
110
109
  SQL
111
110
  end
112
- private
113
111
 
112
+ def host
113
+ return @params[:host]
114
+ end
115
+ private
116
+
114
117
  def connect
115
- #print "connecting to #{@params[:host]} "
118
+ if mirror_defined?
119
+ unless (connect! rescue false)
120
+ to_mirror
121
+ connect!
122
+ end
123
+ else
124
+ connect!
125
+ end
126
+ end
127
+
128
+ #raises error on failed connection
129
+ def connect!
116
130
  @connection = TinyTds::Client.new(@params)
117
- #print "successful\n"
118
- rescue TinyTds::Error => e
119
- raise unless @params[:mirror_host]
120
- #print "#{e.to_s}\n"
121
- to_mirror
122
- connect
131
+ on_connect
132
+ true
133
+ end
134
+
135
+ def on_connect
136
+ execute "set ANSI_NULLS on"
137
+ execute "set ANSI_WARNINGS on"
138
+ end
139
+
140
+ def mirror_defined?
141
+ !@params[:mirror_host].nil?
123
142
  end
124
143
 
125
144
  # Switch host and mirror_host in @params
126
145
  def to_mirror
146
+ return unless mirror_defined?
127
147
  host = @params[:host]
128
148
  @params[:host] = @params[:mirror_host]
129
149
  @params[:mirror_host] = host
@@ -44,13 +44,16 @@ class DbMirroring < Test::Unit::TestCase
44
44
  end
45
45
  end
46
46
 
47
- def test_read
47
+ def test_mirror_failovering
48
48
  rows = @reader.read
49
+ first_server = server_name
49
50
  data_test rows
50
- pp rows
51
51
  @reader.failover
52
52
  rows = @reader.read
53
+ second_server = server_name
53
54
  data_test rows
55
+ assert second_server != first_server
56
+ print "\nfailovering from #{first_server} to #{second_server}\n"
54
57
  end
55
58
 
56
59
  def data_test(rows)
@@ -61,6 +64,10 @@ class DbMirroring < Test::Unit::TestCase
61
64
 
62
65
  private
63
66
 
67
+ def server_name
68
+ @reader.select_value "select @@servername"
69
+ end
70
+
64
71
  def setup_table
65
72
  @reader.execute "
66
73
  if not exists(select * from sys.objects where name = 'people')
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minus5_mssql
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 0
10
- version: 0.2.0
9
+ - 1
10
+ version: 0.2.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Igor Anic
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-07-05 00:00:00 +02:00
18
+ date: 2011-07-08 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency