dbhijacker 0.4.7 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. data/Gemfile.lock +1 -1
  2. data/hijacker.gemspec +1 -1
  3. data/lib/hijacker.rb +26 -0
  4. metadata +4 -4
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- dbhijacker (0.4.6)
4
+ dbhijacker (0.4.7)
5
5
  rails (~> 2.3.14)
6
6
 
7
7
  GEM
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.7"
6
+ s.version = "0.5.0"
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.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'hijacker/active_record_ext'
2
2
  require 'active_record'
3
3
  require 'action_controller'
4
+ require 'set'
4
5
 
5
6
  module Hijacker
6
7
  class UnparseableURL < StandardError;end
@@ -39,6 +40,10 @@ module Hijacker
39
40
  target_name = target_name.downcase
40
41
  sister_name = sister_name.downcase unless sister_name.nil?
41
42
 
43
+ unless database_exists?(target_name)
44
+ raise InvalidDatabase, 'database does not exist'
45
+ end
46
+
42
47
  if already_connected?(target_name, sister_name)
43
48
  return "Already connected to #{target_name}"
44
49
  end
@@ -77,6 +82,27 @@ module Hijacker
77
82
  end
78
83
  end
79
84
 
85
+ def self.database_exists?(database_name)
86
+ return true unless defined?(ActiveRecord::ConnectionAdapters::Mysql2Adapter) && ActiveRecord::Base.connection.is_a?(ActiveRecord::ConnectionAdapters::Mysql2Adapter)
87
+
88
+ @host_connections ||= {}
89
+ current_host = Host.connection.config['host']
90
+
91
+ begin
92
+ existing_dbs = Host.all.inject(Set.new) do |acc, host|
93
+ @host_connections[host.hostname] ||= Mysql2::Client.new(root_config.merge('host' => host.hostname))
94
+ @host_connections[host.hostname].query("SHOW DATABASES").each do |row|
95
+ acc << row['Database']
96
+ end
97
+ acc
98
+ end
99
+
100
+ existing_dbs.include?(database_name)
101
+ ensure
102
+ Host.establish_connection(root_config.merge('host' => current_host))
103
+ end
104
+ end
105
+
80
106
  # very small chance this will raise, but if it does, we will still handle it the
81
107
  # same as +Hijacker.connect+ so we don't lock up the app.
82
108
  #
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: 1
4
+ hash: 11
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 7
10
- version: 0.4.7
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Michael Xavier