dbhijacker 0.4.5 → 0.4.6
Sign up to get free protection for your applications and to get access to all the features.
- data/Gemfile.lock +1 -1
- data/hijacker.gemspec +1 -1
- data/lib/hijacker/database.rb +35 -2
- data/spec/hijacker/database_spec.rb +20 -4
- metadata +4 -4
data/Gemfile.lock
CHANGED
data/hijacker.gemspec
CHANGED
@@ -3,7 +3,7 @@
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = %q{dbhijacker}
|
5
5
|
s.homepage = "https://github.com/crystalcommerce/hijacker"
|
6
|
-
s.version = "0.4.
|
6
|
+
s.version = "0.4.6"
|
7
7
|
|
8
8
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
9
9
|
s.authors = ["Michael Xavier", "Donald Plummer", "Woody Peterson"]
|
data/lib/hijacker/database.rb
CHANGED
@@ -1,4 +1,31 @@
|
|
1
|
+
#Dummy exceptions if driver not loaded
|
2
|
+
if !defined?(Mysql2)
|
3
|
+
module Mysql2
|
4
|
+
class Error < StandardError
|
5
|
+
attr_accessor :errno
|
6
|
+
end
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
if !defined?(Mysql)
|
11
|
+
module Mysql
|
12
|
+
class Error < StandardError
|
13
|
+
attr_accessor :errno
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
1
18
|
class Hijacker::Database < ActiveRecord::Base
|
19
|
+
module MissingDatabaseError
|
20
|
+
MYSQL_UNKNOWN_DB_ERRNO = 1049
|
21
|
+
|
22
|
+
def self.===(e)
|
23
|
+
return true if e.is_a?(Hijacker::InvalidDatabase)
|
24
|
+
(e.is_a?(Mysql::Error) || e.is_a?(Mysql2::Error)) &&
|
25
|
+
e.errno == MYSQL_UNKNOWN_DB_ERRNO
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
2
29
|
establish_connection(Hijacker.root_config)
|
3
30
|
|
4
31
|
has_many :aliases, :class_name => "Hijacker::Alias"
|
@@ -12,6 +39,7 @@ class Hijacker::Database < ActiveRecord::Base
|
|
12
39
|
|
13
40
|
alias_attribute :name, :database
|
14
41
|
|
42
|
+
|
15
43
|
def sister?
|
16
44
|
master_id.present?
|
17
45
|
end
|
@@ -79,7 +107,7 @@ class Hijacker::Database < ActiveRecord::Base
|
|
79
107
|
sites.each do |db|
|
80
108
|
begin
|
81
109
|
Hijacker.connect_to_master(db)
|
82
|
-
rescue
|
110
|
+
rescue MissingDatabaseError
|
83
111
|
next
|
84
112
|
end
|
85
113
|
yield db
|
@@ -87,7 +115,7 @@ class Hijacker::Database < ActiveRecord::Base
|
|
87
115
|
ensure
|
88
116
|
begin
|
89
117
|
Hijacker.connect_to_master(original_database)
|
90
|
-
rescue
|
118
|
+
rescue MissingDatabaseError
|
91
119
|
end
|
92
120
|
end
|
93
121
|
end
|
@@ -118,4 +146,9 @@ class Hijacker::Database < ActiveRecord::Base
|
|
118
146
|
Hijacker::Database.connection.
|
119
147
|
execute("DELETE FROM `disabled_databases` WHERE `database_name` = '#{database}'")
|
120
148
|
end
|
149
|
+
|
150
|
+
private
|
151
|
+
def self.catch_missing_database(&block)
|
152
|
+
block.call
|
153
|
+
end
|
121
154
|
end
|
@@ -65,17 +65,33 @@ module Hijacker
|
|
65
65
|
it "connects to each of the database and reconnects to the original" do
|
66
66
|
original_db = Hijacker::Database.current
|
67
67
|
Hijacker.should_receive(:connect).exactly(Database.all.size + 1).times
|
68
|
-
Database.connect_each
|
69
|
-
# noop
|
70
|
-
end
|
68
|
+
Database.connect_each {}
|
71
69
|
|
72
70
|
Hijacker::Database.current.should == original_db
|
73
71
|
end
|
74
72
|
|
75
73
|
it "eats invalid database errors" do
|
76
|
-
Hijacker.stub
|
74
|
+
Hijacker.stub(:connect).and_raise(Hijacker::InvalidDatabase)
|
77
75
|
expect { Database.connect_each {|db| } }.not_to raise_error
|
78
76
|
end
|
77
|
+
|
78
|
+
it "eats mysql-specific errors for missing databases" do
|
79
|
+
[Mysql::Error, Mysql2::Error].each do |klass|
|
80
|
+
exception = klass.new("Unknown database 'fake'")
|
81
|
+
exception.errno = 1049
|
82
|
+
Hijacker.stub(:connect).and_raise(exception)
|
83
|
+
expect { Database.connect_each {} }.to_not raise_error
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "does not eat unrelated mysql-specific databases" do
|
88
|
+
[Mysql::Error, Mysql2::Error].each do |klass|
|
89
|
+
exception = klass.new("WAT")
|
90
|
+
exception.errno = 2000
|
91
|
+
Hijacker.stub(:connect).and_raise(exception)
|
92
|
+
expect { Database.connect_each {} }.to raise_error(klass)
|
93
|
+
end
|
94
|
+
end
|
79
95
|
end
|
80
96
|
end
|
81
97
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dbhijacker
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 3
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 6
|
10
|
+
version: 0.4.6
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Michael Xavier
|
@@ -195,7 +195,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
195
195
|
requirements: []
|
196
196
|
|
197
197
|
rubyforge_project:
|
198
|
-
rubygems_version: 1.8.
|
198
|
+
rubygems_version: 1.8.17
|
199
199
|
signing_key:
|
200
200
|
specification_version: 3
|
201
201
|
summary: One application, multiple client databases
|