Class: Hijacker::Database
Defined Under Namespace
Modules: MissingDatabaseError
Class Method Summary
collapse
Instance Method Summary
collapse
Class Method Details
.connect_each(sites = all.map(&:database)) ⇒ Object
.connect_to_each_shared_site(&block) ⇒ Object
77
78
79
|
# File 'lib/hijacker/database.rb', line 77
def self.connect_to_each_shared_site(&block)
connect_each(find_shared_sites_for(Hijacker.current_client), &block)
end
|
.connect_to_each_sister_site(&block) ⇒ Object
81
82
83
84
85
|
# File 'lib/hijacker/database.rb', line 81
def self.connect_to_each_sister_site(&block)
sites = find_shared_sites_for(Hijacker.current_client)
sites.delete(Hijacker.current_client)
connect_each(sites, &block)
end
|
.count_each(options = {}, &blk) ⇒ Object
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
# File 'lib/hijacker/database.rb', line 121
def self.count_each(options = {}, &blk)
acc = {}
if options.fetch(:progress, true)
require 'progress'
Progress.start("Counting...", count) do
connect_each do |db|
count = blk.call
acc[db] = count if count > 0
Progress.step
end
end
else
connect_each do |db|
count = blk.call
acc[db] = count if count > 0
end
end
if options.fetch(:print, true)
width = acc.keys.map(&:length).max
acc.sort_by(&:last).each do |db, count|
puts("%#{width}s: %s" % [db, count])
end
end
acc
end
|
.current ⇒ Object
49
50
51
|
# File 'lib/hijacker/database.rb', line 49
def self.current
find(:first, :conditions => {:database => Hijacker.current_client})
end
|
.disabled_databases ⇒ Object
150
151
152
|
# File 'lib/hijacker/database.rb', line 150
def self.disabled_databases
Hijacker::Database.connection.select_values("SELECT `database_name` FROM `disabled_databases`")
end
|
.find_by_name(name) ⇒ Object
45
46
47
|
# File 'lib/hijacker/database.rb', line 45
def self.find_by_name(name)
find_by_database(name)
end
|
.find_master_and_sister_for(client) ⇒ Object
always returns a master, sister can be nil
65
66
67
68
69
70
71
|
# File 'lib/hijacker/database.rb', line 65
def self.find_master_and_sister_for(client)
master = self.find_master_for(client)
sister = master.nil? ? nil : client
master ||= client
return master, sister
end
|
.find_master_for(client) ⇒ Object
54
55
56
57
58
59
60
61
62
|
# File 'lib/hijacker/database.rb', line 54
def self.find_master_for(client)
@masters ||= {}
@masters[client] ||= self.connection.select_values(
"SELECT master.database "\
"FROM `databases` AS master, `databases` AS sister "\
"WHERE sister.database = #{ActiveRecord::Base.connection.quote(client)} "\
"AND sister.master_id = master.id"
).first
end
|
.find_shared_sites_for(client) ⇒ Object
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
# File 'lib/hijacker/database.rb', line 87
def self.find_shared_sites_for(client)
@shared_sites ||= {}
return @shared_sites[client] if @shared_sites[client].present?
current = self.find(:first, :conditions => {:database => client})
master_id = current.master_id || current.id
@shared_sites[client] = self.connection.select_values(
"SELECT `database`
FROM `databases`
WHERE master_id = '#{master_id}' OR id = '#{master_id}'
ORDER BY id"
)
end
|
.shared_sites ⇒ Object
73
74
75
|
# File 'lib/hijacker/database.rb', line 73
def self.shared_sites
self.find_shared_sites_for(Hijacker.current_client)
end
|
Instance Method Details
#disable! ⇒ Object
154
155
156
157
|
# File 'lib/hijacker/database.rb', line 154
def disable!
Hijacker::Database.connection.
execute("REPLACE INTO `disabled_databases` (`database_name`) VALUES ('#{database}')")
end
|
#enable! ⇒ Object
159
160
161
162
|
# File 'lib/hijacker/database.rb', line 159
def enable!
Hijacker::Database.connection.
execute("DELETE FROM `disabled_databases` WHERE `database_name` = '#{database}'")
end
|
#sister? ⇒ Boolean
41
42
43
|
# File 'lib/hijacker/database.rb', line 41
def sister?
master_id.present?
end
|