Class: Rmap::Database
- Inherits:
-
Object
show all
- Defined in:
- lib/rmap/database.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(connection = {}, &block) ⇒ Database
Returns a new instance of Database.
28
29
30
31
32
33
34
|
# File 'lib/rmap/database.rb', line 28
def initialize(connection={}, &block)
self.connection = connection
if !block.nil?
instance_eval(&block)
end
@scopes = {}
end
|
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args) ⇒ Object
85
86
87
|
# File 'lib/rmap/database.rb', line 85
def method_missing name, *args
table(name)
end
|
Class Method Details
.create(database, connection = {}) ⇒ Object
7
8
9
10
11
12
|
# File 'lib/rmap/database.rb', line 7
def self.create(database, connection = {})
connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
Mysql2::Client.new(connection).query("create database `#{database}`");
connection[:database] = database
self.new(connection)
end
|
.drop(database, connection = {}) ⇒ Object
14
15
16
17
|
# File 'lib/rmap/database.rb', line 14
def self.drop(database, connection = {})
connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
Mysql2::Client.new(connection).query("drop database `#{database}`");
end
|
.exists?(database, connection = {}) ⇒ Boolean
24
25
26
|
# File 'lib/rmap/database.rb', line 24
def self.exists?(database, connection = {})
list(connection).include? database.to_s
end
|
.list(connection = {}) ⇒ Object
19
20
21
22
|
# File 'lib/rmap/database.rb', line 19
def self.list(connection = {})
connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
Mysql2::Client.new(connection).query("show databases", :as => :array).map{|a| a[0]}
end
|
Instance Method Details
#bindings ⇒ Object
64
65
66
|
# File 'lib/rmap/database.rb', line 64
def bindings
binding
end
|
#client ⇒ Object
42
43
44
45
46
47
|
# File 'lib/rmap/database.rb', line 42
def client
if @client.nil?
@client = Mysql2::Client.new(@connection)
end
@client
end
|
#close ⇒ Object
57
58
59
60
61
62
|
# File 'lib/rmap/database.rb', line 57
def close
if !@client.nil?
@client.close
end
@client = nil
end
|
#commit_transaction ⇒ Object
105
106
107
|
# File 'lib/rmap/database.rb', line 105
def commit_transaction
client.query("commit")
end
|
#connected? ⇒ Boolean
49
50
51
52
53
54
55
|
# File 'lib/rmap/database.rb', line 49
def connected?
begin
client.ping && !client.query("select database()", :as => :array).first.first.nil?
rescue Exception => e
false
end
end
|
#connection=(connection) ⇒ Object
36
37
38
39
40
|
# File 'lib/rmap/database.rb', line 36
def connection=(connection)
@connection = {:host => "localhost", :username => "root", :password => ""}.merge connection
close
@table_names = nil
end
|
#create(name) ⇒ Object
89
90
91
92
|
# File 'lib/rmap/database.rb', line 89
def create(name)
@table_names = nil
client.query("create table `#{name}`(id int unsigned not null auto_increment primary key) engine = InnoDB")
end
|
#current_migration ⇒ Object
113
114
115
116
117
118
119
120
121
122
123
124
125
|
# File 'lib/rmap/database.rb', line 113
def current_migration
if !table? :rmap_vars
rmap_vars.add :key, :string
rmap_vars.add :value, :binary
end
if rmap_vars.key_eq(:current_migration).count == 0
rmap_vars.insert(:key => :current_migration, :value => 0)
0
else
rmap_vars.key_eq(:current_migration).first.value.to_i
end
end
|
#migrate(options = {}) ⇒ Object
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
|
# File 'lib/rmap/database.rb', line 127
def migrate(options = {})
migrations_dir = !options[:migrations_dir].nil? ? (options[:migrations_dir].match(/\/\Z/) ? options[:migrations_dir] : "#{options[:migrations_dir]}/") : "#{Rmap::CONF_ROOT}/migrations/"
current_migration = self.current_migration
migrations = Dir.new(migrations_dir).to_a.find_all{|file| ::File.file? "#{migrations_dir}#{file}" }.map{ |file| Rmap::Migration.new("#{migrations_dir}#{file}") }.sort {|l,r| l.schema_version <=> r.schema_version}
if migrations.count == 0
raise "There are currently no migrations."
end
if options[:to].nil?
to = migrations.last.schema_version
if to == current_migration
raise "There are no are more migrations that can be applied."
end
elsif options[:to].to_s == 'previous'
if current_migration == 0
raise "No migrations have been applied."
elsif current_migration == migrations.first.schema_version
to = 0
else
migrations.each_with_index do |migration, i|
if current_migration == migration.schema_version
to = migrations[i - 1]
break
end
end
end
elsif options[:to].to_s == 'next'
if current_migration == 0
to = migrations.first.schema_version
elsif current_migration == migrations.last.schema_version
raise "There are no are more migrations that can be applied."
end
migrations.each_with_index do |migration, i|
if current_migration == migration.schema_version
to = migrations[i + 1]
break
end
end
else
found = false
to = options[:to].to_i
if to == current_migration
raise "Already at migration #{to}"
end
if to != 0
migrations.each do |migration, i|
if migration.schema_version == to
found = true
break
end
end
if !found
raise "No such migration '#{to}' exists"
end
end
end
if to > current_migration
migrations.each do |migration|
if migration.schema_version <= current_migration
next
end
if migration.schema_version <= to
run &migration.up_block
else
break
end
end
else
migrations.reverse.each do |migration|
if migration.schema_version > current_migration
next
end
if migration.schema_version > to
run &migration.down_block
else
break
end
end
end
rmap_vars.key_eq(:current_migration).value = to
self
end
|
#rollback_transaction ⇒ Object
109
110
111
|
# File 'lib/rmap/database.rb', line 109
def rollback_transaction
client.query("rollback")
end
|
#run(file_path = nil, &block) ⇒ Object
68
69
70
71
72
73
74
75
|
# File 'lib/rmap/database.rb', line 68
def run(file_path = nil, &block)
if !file_path.nil?
instance_eval(::File.open(file_path).read, file_path)
end
if !block.nil?
instance_eval(&block)
end
end
|
#start_transaction ⇒ Object
101
102
103
|
# File 'lib/rmap/database.rb', line 101
def start_transaction
client.query("start transaction")
end
|
#table(name) ⇒ Object
81
82
83
|
# File 'lib/rmap/database.rb', line 81
def table(name)
Table.new(self, name.to_s)
end
|
#table?(name) ⇒ Boolean
77
78
79
|
# File 'lib/rmap/database.rb', line 77
def table?(name)
table_names.include?(name.to_s)
end
|
#table_names ⇒ Object
94
95
96
97
98
99
|
# File 'lib/rmap/database.rb', line 94
def table_names
if @table_names.nil?
@table_names = client.query("show tables", :as => :array).map{|a| a[0]}
end
@table_names
end
|