baza 0.0.13 → 0.0.14
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/Gemfile +7 -4
- data/Gemfile.lock +84 -48
- data/README.md +186 -0
- data/VERSION +1 -1
- data/baza.gemspec +37 -22
- data/include/db.rb +153 -166
- data/include/dbtime.rb +2 -2
- data/include/driver.rb +9 -0
- data/include/drivers/active_record/active_record.rb +64 -27
- data/include/drivers/mysql/mysql_columns.rb +33 -33
- data/include/drivers/mysql/mysql_indexes.rb +26 -17
- data/include/drivers/mysql/mysql_tables.rb +140 -132
- data/include/drivers/sqlite3/sqlite3_indexes.rb +37 -9
- data/include/drivers/sqlite3/sqlite3_tables.rb +181 -152
- data/include/query_buffer.rb +22 -22
- data/include/revision.rb +70 -70
- data/lib/baza.rb +3 -1
- data/shippable.yml +11 -0
- data/spec/include/drivers/active_record_spec.rb +8 -0
- data/spec/include/drivers/mysql_spec.rb +47 -0
- data/spec/include/drivers/sqlite3_spec.rb +60 -0
- data/spec/info_active_record.rb +37 -0
- data/spec/info_active_record_example.rb +37 -0
- data/spec/info_active_record_shippable.rb +36 -0
- data/spec/info_mysql_example.rb +24 -6
- data/spec/info_mysql_shippable.rb +23 -0
- data/spec/info_sqlite3.rb +23 -15
- data/spec/model_handler_spec.rb +84 -84
- data/spec/spec_helper.rb +6 -6
- data/spec/support/driver_collection.rb +288 -0
- data/spec/support/driver_columns_collection.rb +49 -0
- data/spec/support/driver_indexes_collection.rb +49 -0
- data/spec/support/driver_tables_collection.rb +73 -0
- metadata +100 -102
- data/README.rdoc +0 -136
- data/spec/baza_spec.rb +0 -410
- data/spec/db_spec_encoding_test_file.txt +0 -1
data/spec/baza_spec.rb
DELETED
@@ -1,410 +0,0 @@
|
|
1
|
-
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
2
|
-
|
3
|
-
describe "Baza" do
|
4
|
-
require "baza"
|
5
|
-
require "knjrbfw"
|
6
|
-
require "knj/os"
|
7
|
-
require "rubygems"
|
8
|
-
require "tmpdir"
|
9
|
-
|
10
|
-
drivers = []
|
11
|
-
Baza::Db.drivers.each do |driver_data|
|
12
|
-
name = driver_data[:name].to_s
|
13
|
-
const_name = "Info#{name.slice(0, 1).upcase}#{name.slice(1, name.length)}"
|
14
|
-
require "#{File.dirname(__FILE__)}/info_#{driver_data[:name]}.rb"
|
15
|
-
raise "Constant was not defined: '#{const_name}'." if !Baza.const_defined?(const_name)
|
16
|
-
|
17
|
-
drivers << {
|
18
|
-
:const => Baza.const_get(const_name),
|
19
|
-
}
|
20
|
-
end
|
21
|
-
|
22
|
-
drivers.each do |driver|
|
23
|
-
it "should be able to handle various encodings" do
|
24
|
-
#I never got this test to actually fail... :-(
|
25
|
-
debug = false
|
26
|
-
|
27
|
-
driver[:const].sample_db do |db|
|
28
|
-
db.tables.create("test", {
|
29
|
-
:columns => [
|
30
|
-
{:name => "id", :type => :int, :autoincr => true, :primarykey => true},
|
31
|
-
{:name => "text", :type => :varchar}
|
32
|
-
]
|
33
|
-
})
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
#Get a list of tables and check the list for errors.
|
38
|
-
list = db.tables.list
|
39
|
-
raise "Table not found: 'test'." if !list.key?(:test)
|
40
|
-
|
41
|
-
list[:test].name.should eql(:test)
|
42
|
-
|
43
|
-
|
44
|
-
#Test revision to create tables, indexes and insert rows.
|
45
|
-
schema = {
|
46
|
-
:tables => {
|
47
|
-
:test_table => {
|
48
|
-
:columns => [
|
49
|
-
{:name => "id", :type => :int, :autoincr => true, :primarykey => true},
|
50
|
-
{:name => "name", :type => :varchar},
|
51
|
-
{:name => "age", :type => :int},
|
52
|
-
{:name => "nickname", :type => :varchar}
|
53
|
-
],
|
54
|
-
:indexes => [
|
55
|
-
"name"
|
56
|
-
],
|
57
|
-
:rows => [
|
58
|
-
{
|
59
|
-
:find_by => {"id" => 1},
|
60
|
-
:data => {"id" => 1, "name" => "trala"}
|
61
|
-
}
|
62
|
-
]
|
63
|
-
}
|
64
|
-
}
|
65
|
-
}
|
66
|
-
|
67
|
-
rev = Baza::Revision.new
|
68
|
-
rev.init_db(:schema => schema, :debug => debug, :db => db)
|
69
|
-
|
70
|
-
test_table = db.tables[:test_table]
|
71
|
-
|
72
|
-
|
73
|
-
#Test wrong encoding.
|
74
|
-
cont = File.read("#{File.dirname(__FILE__)}/db_spec_encoding_test_file.txt")
|
75
|
-
cont.force_encoding("ASCII-8BIT")
|
76
|
-
|
77
|
-
db.insert("test", {
|
78
|
-
"text" => cont
|
79
|
-
})
|
80
|
-
|
81
|
-
|
82
|
-
#Throw out invalid encoding because it will make dumping fail.
|
83
|
-
db.tables[:test].truncate
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
#Test IDQueries.
|
88
|
-
rows_count = 1250
|
89
|
-
db.transaction do
|
90
|
-
0.upto(rows_count) do |count|
|
91
|
-
db.insert(:test_table, {:name => "User #{count}"})
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
block_ran = 0
|
96
|
-
idq = Baza::Idquery.new(:db => db, :debug => debug, :table => :test_table, :query => "SELECT id FROM test_table") do |data|
|
97
|
-
block_ran += 1
|
98
|
-
end
|
99
|
-
|
100
|
-
raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
|
101
|
-
|
102
|
-
block_ran = 0
|
103
|
-
db.select(:test_table, {}, {:idquery => true}) do |data|
|
104
|
-
block_ran += 1
|
105
|
-
end
|
106
|
-
|
107
|
-
raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
|
108
|
-
|
109
|
-
|
110
|
-
#Test upserting.
|
111
|
-
data = {:name => "upsert - Kasper Johansen"}
|
112
|
-
data2 = {:name => "upsert - Kasper Nielsen Johansen"}
|
113
|
-
sel = {:nickname => "upsert - kaspernj"}
|
114
|
-
|
115
|
-
table = db.tables[:test_table]
|
116
|
-
table.reload
|
117
|
-
rows_count = table.rows_count
|
118
|
-
|
119
|
-
db.upsert(:test_table, data, sel)
|
120
|
-
row = db.select(:test_table, sel).fetch
|
121
|
-
row[:name].should eql("upsert - Kasper Johansen")
|
122
|
-
|
123
|
-
table.reload
|
124
|
-
table.rows_count.should eql(rows_count + 1)
|
125
|
-
|
126
|
-
db.upsert(:test_table, data2, sel)
|
127
|
-
row = db.select(:test_table, sel).fetch
|
128
|
-
row[:name].should eql("upsert - Kasper Nielsen Johansen")
|
129
|
-
|
130
|
-
table.reload
|
131
|
-
table.rows_count.should eql(rows_count + 1)
|
132
|
-
|
133
|
-
|
134
|
-
#Test dumping.
|
135
|
-
dump = Baza::Dump.new(:db => db, :debug => false)
|
136
|
-
str_io = StringIO.new
|
137
|
-
dump.dump(str_io)
|
138
|
-
str_io.rewind
|
139
|
-
|
140
|
-
|
141
|
-
#Remember some numbers for validation.
|
142
|
-
tables_count = db.tables.list.length
|
143
|
-
|
144
|
-
|
145
|
-
#Remove everything in the db.
|
146
|
-
db.tables.list do |table|
|
147
|
-
table.drop unless table.native?
|
148
|
-
end
|
149
|
-
|
150
|
-
|
151
|
-
#Run the exported SQL.
|
152
|
-
db.transaction do
|
153
|
-
str_io.each_line do |sql|
|
154
|
-
db.q(sql)
|
155
|
-
end
|
156
|
-
end
|
157
|
-
|
158
|
-
|
159
|
-
#Vaildate import.
|
160
|
-
raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
#Test revision table renaming.
|
165
|
-
Baza::Revision.new.init_db(:db => db, :debug => debug, :schema => {
|
166
|
-
:tables => {
|
167
|
-
:new_test_table => {
|
168
|
-
:renames => [:test_table]
|
169
|
-
}
|
170
|
-
}
|
171
|
-
})
|
172
|
-
tables = db.tables.list
|
173
|
-
raise "Didnt expect table 'test_table' to exist but it did." if tables.key?(:test_table)
|
174
|
-
raise "Expected 'new_test_table' to exist but it didnt." if !tables.key?(:new_test_table)
|
175
|
-
|
176
|
-
|
177
|
-
#Test revision for column renaming.
|
178
|
-
Baza::Revision.new.init_db(:db => db, :debug => debug, :schema => {
|
179
|
-
:tables => {
|
180
|
-
:new_test_table => {
|
181
|
-
:columns => [
|
182
|
-
{:name => :new_name, :type => :varchar, :renames => [:name]}
|
183
|
-
]
|
184
|
-
}
|
185
|
-
}
|
186
|
-
})
|
187
|
-
columns = db.tables[:new_test_table].columns
|
188
|
-
raise "Didnt expect 'name' to exist but it did." if columns.key?(:name)
|
189
|
-
raise "Expected 'new_name'-column to exist but it didnt." if !columns.key?(:new_name)
|
190
|
-
end
|
191
|
-
end
|
192
|
-
|
193
|
-
it "should generate proper sql" do
|
194
|
-
driver[:const].sample_db do |db|
|
195
|
-
time = Time.new(1985, 6, 17, 10, 30)
|
196
|
-
db.insert(:test, {:date => time}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
|
197
|
-
|
198
|
-
date = Date.new(1985, 6, 17)
|
199
|
-
db.insert(:test, {:date => date}, :return_sql => true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
|
200
|
-
end
|
201
|
-
end
|
202
|
-
|
203
|
-
it "should copy database structure and data" do
|
204
|
-
driver[:const].sample_db do |db1|
|
205
|
-
driver[:const].sample_db do |db2|
|
206
|
-
db1.tables.create(:test_table, {
|
207
|
-
:columns => [
|
208
|
-
{:name => "id", :type => :int, :autoincr => true, :primarykey => true},
|
209
|
-
{:name => "testname", :type => :varchar}
|
210
|
-
],
|
211
|
-
:indexes => [
|
212
|
-
"testname"
|
213
|
-
]
|
214
|
-
})
|
215
|
-
|
216
|
-
table1 = db1.tables["test_table"]
|
217
|
-
cols1 = table1.columns
|
218
|
-
|
219
|
-
100.times do |count|
|
220
|
-
table1.insert(:testname => "TestRow#{count}")
|
221
|
-
end
|
222
|
-
|
223
|
-
begin
|
224
|
-
table2 = db2.tables[:test_table]
|
225
|
-
raise "Expected not-found exception."
|
226
|
-
rescue Errno::ENOENT
|
227
|
-
#expected
|
228
|
-
end
|
229
|
-
|
230
|
-
db1.copy_to(db2)
|
231
|
-
|
232
|
-
table2 = db2.tables[:test_table]
|
233
|
-
|
234
|
-
cols2 = table2.columns
|
235
|
-
cols2.length.should eql(cols1.length)
|
236
|
-
|
237
|
-
table2.rows_count.should eql(table1.rows_count)
|
238
|
-
|
239
|
-
db1.select(:test_table) do |row1|
|
240
|
-
found = 0
|
241
|
-
db2.select(:test_table, row1) do |row2|
|
242
|
-
found += 1
|
243
|
-
|
244
|
-
row1.each do |key, val|
|
245
|
-
row2[key].should eql(val)
|
246
|
-
end
|
247
|
-
end
|
248
|
-
|
249
|
-
found.should eql(1)
|
250
|
-
end
|
251
|
-
|
252
|
-
table1.indexes.length.should eql(1)
|
253
|
-
table2.indexes.length.should eql(table1.indexes.length)
|
254
|
-
end
|
255
|
-
end
|
256
|
-
end
|
257
|
-
|
258
|
-
it "should be able to make new connections based on given objects" do
|
259
|
-
driver[:const].sample_db do |db|
|
260
|
-
db2 = Baza::Db.from_object(:object => db.conn.conn)
|
261
|
-
end
|
262
|
-
end
|
263
|
-
|
264
|
-
it "should be able to do ID-queries through the select-method" do
|
265
|
-
driver[:const].sample_db do |db|
|
266
|
-
db.tables.create(:test_table, {
|
267
|
-
:columns => [
|
268
|
-
{:name => :idrow, :type => :int, :autoincr => true, :primarykey => true},
|
269
|
-
{:name => :name, :type => :varchar}
|
270
|
-
]
|
271
|
-
})
|
272
|
-
|
273
|
-
count = 0
|
274
|
-
100.times do
|
275
|
-
arr = []
|
276
|
-
100.times do
|
277
|
-
count += 1
|
278
|
-
arr << {:name => "Kasper #{count}"}
|
279
|
-
end
|
280
|
-
|
281
|
-
db.insert_multi(:test_table, arr)
|
282
|
-
end
|
283
|
-
|
284
|
-
count_found = 0
|
285
|
-
db.select(:test_table, nil, :idquery => :idrow) do |row|
|
286
|
-
count_found += 1
|
287
|
-
|
288
|
-
row[:name].should eql("Kasper #{count_found}")
|
289
|
-
end
|
290
|
-
|
291
|
-
count_found.should eql(10000)
|
292
|
-
end
|
293
|
-
end
|
294
|
-
|
295
|
-
it "should be able to use query buffers" do
|
296
|
-
driver[:const].sample_db do |db|
|
297
|
-
db.tables.create(:test_table, {
|
298
|
-
:columns => [
|
299
|
-
{:name => :id, :type => :int, :autoincr => true, :primarykey => true},
|
300
|
-
{:name => :name, :type => :varchar}
|
301
|
-
]
|
302
|
-
})
|
303
|
-
|
304
|
-
upsert = false
|
305
|
-
db.q_buffer do |buffer|
|
306
|
-
2500.times do |count|
|
307
|
-
if upsert
|
308
|
-
buffer.upsert(:test_table, {:name => "Kasper #{count}"}, {:name => "Kasper #{count}"})
|
309
|
-
upsert = false
|
310
|
-
else
|
311
|
-
buffer.insert(:test_table, {:name => "Kasper #{count}"})
|
312
|
-
upsert = true
|
313
|
-
end
|
314
|
-
end
|
315
|
-
end
|
316
|
-
|
317
|
-
test_table = db.tables[:test_table]
|
318
|
-
test_table.rows_count.should eql(2500)
|
319
|
-
|
320
|
-
db.q_buffer do |buffer|
|
321
|
-
count = 0
|
322
|
-
upsert = false
|
323
|
-
|
324
|
-
db.select(:test_table, {}, :orderby => :id) do |row|
|
325
|
-
row[:name].should eql("Kasper #{count}")
|
326
|
-
|
327
|
-
if upsert
|
328
|
-
buffer.upsert(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
|
329
|
-
upsert = false
|
330
|
-
else
|
331
|
-
buffer.update(:test_table, {:name => "Kasper #{count}-#{count}"}, {:id => row[:id]})
|
332
|
-
upsert = true
|
333
|
-
end
|
334
|
-
|
335
|
-
count += 1
|
336
|
-
end
|
337
|
-
end
|
338
|
-
|
339
|
-
count = 0
|
340
|
-
db.select(:test_table, {}, :orderby => :id) do |row|
|
341
|
-
row[:name].should eql("Kasper #{count}-#{count}")
|
342
|
-
count += 1
|
343
|
-
end
|
344
|
-
|
345
|
-
#Test the flush-async which flushes transactions in a thread asyncronous.
|
346
|
-
db.q_buffer(:flush_async => true) do |buffer|
|
347
|
-
count = 0
|
348
|
-
db.select(:test_table) do |row|
|
349
|
-
count += 1
|
350
|
-
|
351
|
-
if count == 1000
|
352
|
-
time_start = Time.now.to_f
|
353
|
-
end
|
354
|
-
|
355
|
-
buffer.delete(:test_table, {:id => row[:id]})
|
356
|
-
|
357
|
-
if count == 1000
|
358
|
-
time_end = Time.now.to_f
|
359
|
-
|
360
|
-
time_spent = time_end - time_start
|
361
|
-
raise "Too much time spent: '#{time_spent}'." if time_spent > 0.01
|
362
|
-
end
|
363
|
-
end
|
364
|
-
end
|
365
|
-
|
366
|
-
test_table.rows_count.should eql(0)
|
367
|
-
end
|
368
|
-
end
|
369
|
-
end
|
370
|
-
|
371
|
-
it "should be able to connect to mysql and do various stuff" do
|
372
|
-
Baza::InfoSqlite3.sample_db do |db3|
|
373
|
-
Baza::InfoMysql.sample_db do |db1|
|
374
|
-
#create table.
|
375
|
-
db1.tables.create(:test_table, {
|
376
|
-
:columns => [
|
377
|
-
{:name => :id, :type => :int, :autoincr => true, :primarykey => true},
|
378
|
-
{:name => :name, :type => :varchar, :maxlength => 100}
|
379
|
-
]
|
380
|
-
})
|
381
|
-
|
382
|
-
test_table = db1.tables[:test_table]
|
383
|
-
|
384
|
-
col_id = test_table.column(:id)
|
385
|
-
col_name = test_table.column(:name)
|
386
|
-
|
387
|
-
#Test various operations actually work.
|
388
|
-
test_table.optimize
|
389
|
-
|
390
|
-
#object_from test
|
391
|
-
db2 = Baza::Db.from_object(db1.conn.conn)
|
392
|
-
|
393
|
-
#Test dumping to SQLite.
|
394
|
-
db2.copy_to(db3)
|
395
|
-
|
396
|
-
table_sqlite = db3.tables[:test_table]
|
397
|
-
table_sqlite.columns.length.should eql(test_table.columns.length)
|
398
|
-
|
399
|
-
col_id_sqlite = table_sqlite.column(:id)
|
400
|
-
col_id_sqlite.type.should eql(:int)
|
401
|
-
col_id_sqlite.autoincr?.should eql(true)
|
402
|
-
col_id_sqlite.primarykey?.should eql(true)
|
403
|
-
|
404
|
-
col_name_sqlite = table_sqlite.column(:name)
|
405
|
-
col_name_sqlite.type.should eql(:varchar)
|
406
|
-
col_name_sqlite.maxlength.to_i.should eql(100)
|
407
|
-
end
|
408
|
-
end
|
409
|
-
end
|
410
|
-
end
|
@@ -1 +0,0 @@
|
|
1
|
-
��pl��p���
|