baza 0.0.19 → 0.0.20
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop_todo.yml +157 -0
- data/Gemfile +11 -9
- data/Gemfile.lock +41 -9
- data/README.md +1 -1
- data/Rakefile +19 -16
- data/VERSION +1 -1
- data/baza.gemspec +31 -7
- data/config/best_project_practice_rubocop.yml +2 -0
- data/config/best_project_practice_rubocop_todo.yml +157 -0
- data/lib/baza/base_sql_driver.rb +79 -20
- data/lib/baza/cloner.rb +2 -2
- data/lib/baza/column.rb +6 -0
- data/lib/baza/database.rb +33 -0
- data/lib/baza/database_model.rb +26 -0
- data/lib/baza/database_model_functionality.rb +10 -0
- data/lib/baza/database_model_name.rb +16 -0
- data/lib/baza/db.rb +210 -413
- data/lib/baza/dbtime.rb +20 -20
- data/lib/baza/driver.rb +3 -3
- data/lib/baza/drivers/active_record/columns.rb +1 -1
- data/lib/baza/drivers/active_record/indexes.rb +1 -1
- data/lib/baza/drivers/active_record/result.rb +3 -5
- data/lib/baza/drivers/active_record/tables.rb +2 -2
- data/lib/baza/drivers/active_record.rb +30 -16
- data/lib/baza/drivers/mysql/column.rb +51 -48
- data/lib/baza/drivers/mysql/columns.rb +8 -8
- data/lib/baza/drivers/mysql/database.rb +28 -0
- data/lib/baza/drivers/mysql/databases.rb +35 -0
- data/lib/baza/drivers/mysql/index.rb +32 -22
- data/lib/baza/drivers/mysql/result.rb +24 -24
- data/lib/baza/drivers/mysql/sqlspecs.rb +1 -1
- data/lib/baza/drivers/mysql/table.rb +80 -91
- data/lib/baza/drivers/mysql/tables.rb +30 -32
- data/lib/baza/drivers/mysql/unbuffered_result.rb +5 -5
- data/lib/baza/drivers/mysql.rb +127 -220
- data/lib/baza/drivers/mysql2/database.rb +2 -0
- data/lib/baza/drivers/mysql2/databases.rb +2 -0
- data/lib/baza/drivers/mysql2/result.rb +3 -5
- data/lib/baza/drivers/mysql2.rb +21 -106
- data/lib/baza/drivers/mysql_java/database.rb +2 -0
- data/lib/baza/drivers/mysql_java/databases.rb +2 -0
- data/lib/baza/drivers/mysql_java.rb +16 -12
- data/lib/baza/drivers/sqlite3/column.rb +33 -33
- data/lib/baza/drivers/sqlite3/columns.rb +6 -6
- data/lib/baza/drivers/sqlite3/database.rb +2 -0
- data/lib/baza/drivers/sqlite3/databases.rb +25 -0
- data/lib/baza/drivers/sqlite3/index.rb +20 -6
- data/lib/baza/drivers/sqlite3/result.rb +16 -17
- data/lib/baza/drivers/sqlite3/sqlspecs.rb +1 -1
- data/lib/baza/drivers/sqlite3/table.rb +99 -100
- data/lib/baza/drivers/sqlite3/tables.rb +30 -23
- data/lib/baza/drivers/sqlite3/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3.rb +27 -14
- data/lib/baza/drivers/sqlite3_java/database.rb +2 -0
- data/lib/baza/drivers/sqlite3_java/unbuffered_result.rb +2 -2
- data/lib/baza/drivers/sqlite3_java.rb +12 -10
- data/lib/baza/drivers/sqlite3_rhodes.rb +9 -7
- data/lib/baza/dump.rb +55 -55
- data/lib/baza/errors.rb +5 -0
- data/lib/baza/idquery.rb +25 -25
- data/lib/baza/index.rb +6 -0
- data/lib/baza/jdbc_driver.rb +6 -8
- data/lib/baza/jdbc_result.rb +1 -1
- data/lib/baza/model.rb +299 -309
- data/lib/baza/model_custom.rb +74 -72
- data/lib/baza/model_handler.rb +244 -255
- data/lib/baza/model_handler_sqlhelper.rb +122 -132
- data/lib/baza/mysql_base_driver.rb +112 -0
- data/lib/baza/query_buffer.rb +23 -23
- data/lib/baza/result_base.rb +2 -2
- data/lib/baza/revision.rb +67 -67
- data/lib/baza/row.rb +82 -84
- data/lib/baza/sqlspecs.rb +1 -1
- data/lib/baza/table.rb +31 -0
- data/lib/baza.rb +43 -10
- data/shippable.yml +1 -0
- data/spec/cloner_spec.rb +4 -4
- data/spec/drivers/active_record_mysql2_spec.rb +3 -3
- data/spec/drivers/active_record_mysql_spec.rb +2 -2
- data/spec/drivers/active_record_sqlite3_spec.rb +2 -2
- data/spec/drivers/mysql2_spec.rb +1 -0
- data/spec/drivers/mysql_spec.rb +1 -0
- data/spec/drivers/sqlite3_spec.rb +4 -5
- data/spec/info_active_record_example.rb +4 -6
- data/spec/info_active_record_mysql.rb +6 -7
- data/spec/info_active_record_mysql2.rb +2 -4
- data/spec/info_active_record_mysql2_shippable.rb +2 -4
- data/spec/info_active_record_mysql_shippable.rb +2 -4
- data/spec/info_active_record_sqlite3.rb +5 -6
- data/spec/info_mysql2_example.rb +1 -1
- data/spec/info_mysql2_shippable.rb +1 -1
- data/spec/info_mysql_example.rb +1 -1
- data/spec/info_mysql_shippable.rb +1 -1
- data/spec/info_sqlite3.rb +4 -3
- data/spec/model_handler_spec.rb +102 -115
- data/spec/spec_helper.rb +5 -5
- data/spec/support/driver_collection.rb +98 -95
- data/spec/support/driver_columns_collection.rb +18 -22
- data/spec/support/driver_databases_collection.rb +29 -0
- data/spec/support/driver_indexes_collection.rb +7 -6
- data/spec/support/driver_tables_collection.rb +38 -26
- metadata +49 -3
@@ -5,18 +5,16 @@ shared_examples_for "a baza driver" do
|
|
5
5
|
let(:db2) { driver2.db }
|
6
6
|
let(:db_with_type_translation) { constant.new(type_translation: true, debug: false).db }
|
7
7
|
let(:row) do
|
8
|
-
test_table.insert(text:
|
9
|
-
db.select(:test, text:
|
8
|
+
test_table.insert(text: "Kasper", number: 30, float: 4.5)
|
9
|
+
db.select(:test, text: "Kasper").fetch
|
10
10
|
end
|
11
11
|
let(:test_table) do
|
12
|
-
db.tables.create("test",
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
]
|
19
|
-
})
|
12
|
+
db.tables.create("test", columns: [
|
13
|
+
{name: "id", type: :int, autoincr: true, primarykey: true},
|
14
|
+
{name: "text", type: :varchar},
|
15
|
+
{name: "number", type: :int, default: 0},
|
16
|
+
{name: "float", type: :float, default: 0.0}
|
17
|
+
])
|
20
18
|
db.tables[:test]
|
21
19
|
end
|
22
20
|
|
@@ -59,11 +57,11 @@ shared_examples_for "a baza driver" do
|
|
59
57
|
rev.init_db(schema: schema, debug: false, db: db)
|
60
58
|
|
61
59
|
test_table = db.tables[:test_table]
|
62
|
-
test_table.columns.
|
63
|
-
test_table.columns.
|
60
|
+
expect(test_table.columns.map(&:name)).to include "age"
|
61
|
+
expect(test_table.columns.map(&:name)).to include "nickname"
|
64
62
|
end
|
65
63
|
|
66
|
-
it "
|
64
|
+
it "does id-queries" do
|
67
65
|
test_table
|
68
66
|
|
69
67
|
rows_count = 1250
|
@@ -74,21 +72,21 @@ shared_examples_for "a baza driver" do
|
|
74
72
|
end
|
75
73
|
|
76
74
|
block_ran = 0
|
77
|
-
idq = Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do |
|
75
|
+
idq = Baza::Idquery.new(db: db, debug: false, table: :test, query: "SELECT id FROM test") do |_data|
|
78
76
|
block_ran += 1
|
79
77
|
end
|
80
78
|
|
81
79
|
raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
|
82
80
|
|
83
81
|
block_ran = 0
|
84
|
-
db.select(:test, {}, idquery: true) do |
|
82
|
+
db.select(:test, {}, idquery: true) do |_data|
|
85
83
|
block_ran += 1
|
86
84
|
end
|
87
85
|
|
88
86
|
raise "Block with should have ran too little: #{block_ran}." if block_ran < rows_count
|
89
87
|
end
|
90
88
|
|
91
|
-
it
|
89
|
+
it "does unbuffered queries" do
|
92
90
|
test_table
|
93
91
|
|
94
92
|
10.times do |count|
|
@@ -104,10 +102,10 @@ shared_examples_for "a baza driver" do
|
|
104
102
|
expect(count_results).to eq 10
|
105
103
|
end
|
106
104
|
|
107
|
-
it "
|
105
|
+
it "does upserting" do
|
108
106
|
test_table.create_columns([{name: "nickname", type: :varchar}])
|
109
107
|
|
110
|
-
#Test upserting.
|
108
|
+
# Test upserting.
|
111
109
|
data = {text: "upsert - Kasper Johansen"}
|
112
110
|
data2 = {text: "upsert - Kasper Nielsen Johansen"}
|
113
111
|
sel = {nickname: "upsert - kaspernj"}
|
@@ -131,85 +129,94 @@ shared_examples_for "a baza driver" do
|
|
131
129
|
table.rows_count.should eq rows_count + 1
|
132
130
|
end
|
133
131
|
|
134
|
-
it "
|
132
|
+
it "dumps as SQL" do
|
135
133
|
dump = Baza::Dump.new(db: db, debug: false)
|
136
134
|
str_io = StringIO.new
|
137
135
|
dump.dump(str_io)
|
138
136
|
str_io.rewind
|
139
137
|
|
140
|
-
#Remember some numbers for validation.
|
138
|
+
# Remember some numbers for validation.
|
141
139
|
tables_count = db.tables.list.length
|
142
140
|
|
143
|
-
#Remove everything in the db.
|
141
|
+
# Remove everything in the db.
|
144
142
|
db.tables.list do |table|
|
145
143
|
table.drop unless table.native?
|
146
144
|
end
|
147
145
|
|
148
|
-
#Run the exported SQL.
|
146
|
+
# Run the exported SQL.
|
149
147
|
db.transaction do
|
150
148
|
str_io.each_line do |sql|
|
151
149
|
db.q(sql)
|
152
150
|
end
|
153
151
|
end
|
154
152
|
|
155
|
-
#Vaildate import.
|
153
|
+
# Vaildate import.
|
156
154
|
raise "Not same amount of tables: #{tables_count}, #{db.tables.list.length}" if tables_count != db.tables.list.length
|
157
155
|
end
|
158
156
|
|
159
|
-
it "
|
157
|
+
it "renames tables in revisions" do
|
160
158
|
test_table
|
161
159
|
|
162
|
-
Baza::Revision.new.init_db(
|
163
|
-
|
164
|
-
|
165
|
-
|
160
|
+
Baza::Revision.new.init_db(
|
161
|
+
db: db,
|
162
|
+
debug: false,
|
163
|
+
schema: {
|
164
|
+
tables: {
|
165
|
+
new_test_table: {
|
166
|
+
renames: [:test]
|
167
|
+
}
|
166
168
|
}
|
167
169
|
}
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
170
|
+
)
|
171
|
+
|
172
|
+
tables = db.tables.list.map(&:name)
|
173
|
+
|
174
|
+
expect(tables).to_not include "test"
|
175
|
+
expect(tables).to include "new_test_table"
|
172
176
|
end
|
173
177
|
|
174
|
-
it "
|
178
|
+
it "renames columns in revisions" do
|
175
179
|
test_table
|
176
180
|
|
177
|
-
Baza::Revision.new.init_db(
|
178
|
-
|
179
|
-
|
180
|
-
|
181
|
-
|
182
|
-
|
181
|
+
Baza::Revision.new.init_db(
|
182
|
+
db: db,
|
183
|
+
debug: false,
|
184
|
+
schema: {
|
185
|
+
tables: {
|
186
|
+
new_test_table: {
|
187
|
+
columns: [
|
188
|
+
{name: :new_name, type: :varchar, renames: [:text]}
|
189
|
+
]
|
190
|
+
}
|
183
191
|
}
|
184
192
|
}
|
185
|
-
|
186
|
-
|
187
|
-
|
188
|
-
|
193
|
+
)
|
194
|
+
|
195
|
+
columns = db.tables[:new_test_table].columns.map(&:name)
|
196
|
+
expect(columns).to_not include "text"
|
197
|
+
expect(columns).to include "new_name"
|
189
198
|
end
|
190
199
|
|
191
|
-
it "
|
200
|
+
it "generates proper sql" do
|
192
201
|
time = Time.new(1985, 6, 17, 10, 30)
|
193
|
-
db.insert(:test, {:
|
202
|
+
db.insert(:test, {date: time}, return_sql: true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17 10:30:00')")
|
194
203
|
|
195
204
|
date = Date.new(1985, 6, 17)
|
196
|
-
db.insert(:test, {:
|
205
|
+
db.insert(:test, {date: date}, return_sql: true).should eql("INSERT INTO `test` (`date`) VALUES ('1985-06-17')")
|
197
206
|
end
|
198
207
|
|
199
|
-
it "
|
208
|
+
it "is able to make new connections based on given objects" do
|
200
209
|
# Mysql doesn't support it...
|
201
210
|
unless db.opts.fetch(:type) == :mysql
|
202
|
-
new_db = Baza::Db.from_object(object: db.
|
211
|
+
new_db = Baza::Db.from_object(object: db.driver.conn)
|
203
212
|
end
|
204
213
|
end
|
205
214
|
|
206
|
-
it "
|
207
|
-
db.tables.create(:test_table,
|
208
|
-
:
|
209
|
-
|
210
|
-
|
211
|
-
]
|
212
|
-
})
|
215
|
+
it "is able to do ID-queries through the select-method" do
|
216
|
+
db.tables.create(:test_table, columns: [
|
217
|
+
{name: :idrow, type: :int, autoincr: true, primarykey: true},
|
218
|
+
{name: :name, type: :varchar}
|
219
|
+
])
|
213
220
|
|
214
221
|
count = 0
|
215
222
|
100.times do
|
@@ -229,26 +236,24 @@ shared_examples_for "a baza driver" do
|
|
229
236
|
row[:name].should eq "Kasper #{count_found}"
|
230
237
|
end
|
231
238
|
|
232
|
-
expect(count_found).to eq
|
239
|
+
expect(count_found).to eq 10_000
|
233
240
|
end
|
234
241
|
|
235
242
|
it "should be able to use query buffers" do
|
236
|
-
db.tables.create(:test_table,
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
]
|
241
|
-
})
|
243
|
+
db.tables.create(:test_table, columns: [
|
244
|
+
{name: :id, type: :int, autoincr: true, primarykey: true},
|
245
|
+
{name: :name, type: :varchar}
|
246
|
+
])
|
242
247
|
|
243
248
|
upsert = false
|
244
249
|
count_inserts = 0
|
245
250
|
db.q_buffer do |buffer|
|
246
251
|
2500.times do |count|
|
247
252
|
if upsert
|
248
|
-
buffer.upsert(:test_table, {name: "Kasper #{count}"},
|
253
|
+
buffer.upsert(:test_table, {name: "Kasper #{count}"}, name: "Kasper #{count}")
|
249
254
|
upsert = false
|
250
255
|
else
|
251
|
-
buffer.insert(:test_table,
|
256
|
+
buffer.insert(:test_table, name: "Kasper #{count}")
|
252
257
|
upsert = true
|
253
258
|
end
|
254
259
|
|
@@ -290,34 +295,31 @@ shared_examples_for "a baza driver" do
|
|
290
295
|
|
291
296
|
expect(count).to eq 2500
|
292
297
|
|
293
|
-
#Test the flush-async which flushes transactions in a thread asyncronous.
|
298
|
+
# Test the flush-async which flushes transactions in a thread asyncronous.
|
294
299
|
db.q_buffer(flush_async: true) do |buffer|
|
295
300
|
count = 0
|
296
301
|
db.select(:test_table) do |row|
|
297
302
|
count += 1
|
298
303
|
|
299
|
-
if count == 1000
|
300
|
-
time_start = Time.now.to_f
|
301
|
-
end
|
304
|
+
time_start = Time.now.to_f if count == 1000
|
302
305
|
|
303
306
|
buffer.delete(:test_table, id: row[:id])
|
304
307
|
|
305
|
-
|
306
|
-
|
308
|
+
next unless count == 1000
|
309
|
+
time_end = Time.now.to_f
|
307
310
|
|
308
|
-
|
309
|
-
|
310
|
-
end
|
311
|
+
time_spent = time_end - time_start
|
312
|
+
raise "Too much time spent: '#{time_spent}'." if time_spent > 0.01
|
311
313
|
end
|
312
314
|
end
|
313
315
|
|
314
316
|
expect(test_table.rows_count).to eq 0
|
315
317
|
end
|
316
318
|
|
317
|
-
describe
|
319
|
+
describe "results" do
|
318
320
|
before do
|
319
|
-
test_table.insert(text:
|
320
|
-
test_table.insert(text:
|
321
|
+
test_table.insert(text: "test 1")
|
322
|
+
test_table.insert(text: "test 2")
|
321
323
|
end
|
322
324
|
|
323
325
|
it '#to_a' do
|
@@ -342,37 +344,38 @@ shared_examples_for "a baza driver" do
|
|
342
344
|
end
|
343
345
|
end
|
344
346
|
|
345
|
-
it
|
346
|
-
test_table.insert(text:
|
347
|
-
expect(db.count(:test, text:
|
347
|
+
it "counts" do
|
348
|
+
test_table.insert(text: "test 1")
|
349
|
+
expect(db.count(:test, text: "test 1")).to eq 1
|
348
350
|
end
|
349
351
|
|
350
|
-
it
|
352
|
+
it "doesnt do type translation by default" do
|
351
353
|
expect(row.fetch(:text).class).to eq String
|
352
354
|
expect(row.fetch(:number).class).to eq String
|
353
355
|
expect(row.fetch(:float).class).to eq String
|
354
356
|
end
|
355
357
|
|
356
|
-
it
|
357
|
-
db_with_type_translation.tables.create(:test,
|
358
|
-
|
359
|
-
|
360
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
]
|
366
|
-
})
|
358
|
+
it "does type translation" do
|
359
|
+
db_with_type_translation.tables.create(:test, columns: [
|
360
|
+
{name: "id", type: :int, autoincr: true, primarykey: true},
|
361
|
+
{name: "text", type: :varchar},
|
362
|
+
{name: "number", type: :int},
|
363
|
+
{name: "float", type: :float},
|
364
|
+
{name: "created_at", type: :datetime},
|
365
|
+
{name: "date", type: :date}
|
366
|
+
])
|
367
367
|
|
368
|
-
db_with_type_translation.insert(:test, text:
|
368
|
+
db_with_type_translation.insert(:test, text: "Kasper", number: 30, float: 4.5, created_at: Time.now, date: Date.new(2015, 06, 17))
|
369
369
|
|
370
|
-
row = db_with_type_translation.select(:test, text:
|
370
|
+
row = db_with_type_translation.select(:test, text: "Kasper").fetch
|
371
371
|
|
372
372
|
expect(row.fetch(:text).class).to eq String
|
373
373
|
expect(row.fetch(:number).class).to eq Fixnum
|
374
374
|
expect(row.fetch(:float).class).to eq Float
|
375
|
-
|
376
|
-
|
375
|
+
|
376
|
+
unless db.driver.conn.class.name == "ActiveRecord::ConnectionAdapters::SQLite3Adapter"
|
377
|
+
expect(row.fetch(:created_at).class).to eq Time
|
378
|
+
expect(row.fetch(:date).class).to eq Date
|
379
|
+
end
|
377
380
|
end
|
378
381
|
end
|
@@ -1,15 +1,13 @@
|
|
1
1
|
shared_examples_for "a baza columns driver" do
|
2
|
-
let(:driver){ constant.new }
|
3
|
-
let(:db){ driver.db }
|
4
|
-
let(:test_table)
|
5
|
-
db.tables.create("test",
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
]
|
10
|
-
})
|
2
|
+
let(:driver) { constant.new }
|
3
|
+
let(:db) { driver.db }
|
4
|
+
let(:test_table) do
|
5
|
+
db.tables.create("test", columns: [
|
6
|
+
{name: "id", type: :int, autoincr: true, primarykey: true},
|
7
|
+
{name: "text", type: :varchar}
|
8
|
+
])
|
11
9
|
db.tables[:test]
|
12
|
-
|
10
|
+
end
|
13
11
|
|
14
12
|
before do
|
15
13
|
driver.before
|
@@ -20,19 +18,17 @@ shared_examples_for "a baza columns driver" do
|
|
20
18
|
end
|
21
19
|
|
22
20
|
it "renames columns for renamed tables" do
|
23
|
-
# Load up columns to make them set table-name.
|
24
|
-
test_table.columns.each do |name, column|
|
25
|
-
end
|
21
|
+
test_table.columns # Load up columns to make them set table-name.
|
26
22
|
|
27
23
|
test_table.rename("test2")
|
28
|
-
test_table.
|
24
|
+
test_table.column(:text).change(name: "text2")
|
29
25
|
|
30
26
|
table = db.tables[:test2]
|
31
|
-
column = table.
|
32
|
-
column.table.name.should eq
|
27
|
+
column = table.column(:text2)
|
28
|
+
column.table.name.should eq "test2"
|
33
29
|
end
|
34
30
|
|
35
|
-
it "
|
31
|
+
it "creates columns right" do
|
36
32
|
col_id = test_table.column(:id)
|
37
33
|
col_id.type.should eq :int
|
38
34
|
|
@@ -40,20 +36,20 @@ shared_examples_for "a baza columns driver" do
|
|
40
36
|
col_text.type.should eq :varchar
|
41
37
|
end
|
42
38
|
|
43
|
-
it "
|
39
|
+
it "is able to change columns" do
|
44
40
|
col_text = test_table.column(:text)
|
45
41
|
col_text.change(name: "text2", type: :int, default: 5)
|
46
42
|
|
47
43
|
col_text.type.should eq :int
|
48
44
|
col_text.default.should eq "5"
|
49
|
-
col_text.name.should eq
|
45
|
+
col_text.name.should eq "text2"
|
50
46
|
end
|
51
47
|
|
52
|
-
it "
|
48
|
+
it "is able to drop a column" do
|
53
49
|
test_table.column(:text).drop
|
54
50
|
|
55
|
-
expect
|
51
|
+
expect do
|
56
52
|
test_table.column(:text)
|
57
|
-
|
53
|
+
end.to raise_error(Baza::Errors::ColumnNotFound)
|
58
54
|
end
|
59
55
|
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
shared_examples_for "a baza databases driver" do
|
2
|
+
let(:driver) { constant.new }
|
3
|
+
let(:driver2) { constant.new }
|
4
|
+
let(:db) { driver.db }
|
5
|
+
let(:db2) { driver2.db }
|
6
|
+
let(:test_database) do
|
7
|
+
db.databases.create(name: "baza-test-create", if_not_exists: true)
|
8
|
+
db.databases["baza-test-create"]
|
9
|
+
end
|
10
|
+
|
11
|
+
it "renames database" do
|
12
|
+
begin
|
13
|
+
db.databases["renamed-db"].drop
|
14
|
+
rescue Baza::Errors::DatabaseNotFound
|
15
|
+
# Ignore - it shouldn't exist
|
16
|
+
end
|
17
|
+
|
18
|
+
test_database.name = "renamed-db"
|
19
|
+
test_database.save!
|
20
|
+
|
21
|
+
expect(test_database.name).to eq "renamed-db"
|
22
|
+
end
|
23
|
+
|
24
|
+
it "drops databases" do
|
25
|
+
test_database.drop
|
26
|
+
|
27
|
+
expect { db.databases["baza-test-create"] }.to raise_error(Baza::Errors::DatabaseNotFound)
|
28
|
+
end
|
29
|
+
end
|
@@ -2,7 +2,8 @@ shared_examples_for "a baza indexes driver" do
|
|
2
2
|
let(:driver) { constant.new }
|
3
3
|
let(:db) { driver.db }
|
4
4
|
let(:test_table) do
|
5
|
-
db.tables.create(
|
5
|
+
db.tables.create(
|
6
|
+
"test",
|
6
7
|
columns: [
|
7
8
|
{name: "id", type: :int, autoincr: true, primarykey: true},
|
8
9
|
{name: "text", type: :varchar},
|
@@ -12,7 +13,7 @@ shared_examples_for "a baza indexes driver" do
|
|
12
13
|
:text,
|
13
14
|
{name: :email, unique: true, columns: [:email]}
|
14
15
|
]
|
15
|
-
|
16
|
+
)
|
16
17
|
db.tables[:test]
|
17
18
|
end
|
18
19
|
|
@@ -35,13 +36,13 @@ shared_examples_for "a baza indexes driver" do
|
|
35
36
|
|
36
37
|
table = db.tables[:test2]
|
37
38
|
index = table.index(:index_on_text2)
|
38
|
-
index.table.name.
|
39
|
+
expect(index.table.name).to eq "test2"
|
39
40
|
end
|
40
41
|
|
41
|
-
it "
|
42
|
-
expect
|
42
|
+
it "raises an error when an index isn't found" do
|
43
|
+
expect do
|
43
44
|
test_table.index("index_that_doesnt_exist")
|
44
|
-
|
45
|
+
end.to raise_error(Baza::Errors::IndexNotFound)
|
45
46
|
end
|
46
47
|
|
47
48
|
describe "#unique?" do
|
@@ -1,17 +1,15 @@
|
|
1
1
|
shared_examples_for "a baza tables driver" do
|
2
|
-
let(:driver){ constant.new }
|
3
|
-
let(:driver2){ constant.new }
|
4
|
-
let(:db){ driver.db }
|
5
|
-
let(:db2){ driver2.db }
|
6
|
-
let(:test_table)
|
7
|
-
db.tables.create("test",
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
]
|
12
|
-
})
|
2
|
+
let(:driver) { constant.new }
|
3
|
+
let(:driver2) { constant.new }
|
4
|
+
let(:db) { driver.db }
|
5
|
+
let(:db2) { driver2.db }
|
6
|
+
let(:test_table) do
|
7
|
+
db.tables.create("test", columns: [
|
8
|
+
{name: "id", type: :int, autoincr: true, primarykey: true},
|
9
|
+
{name: "text", type: :varchar}
|
10
|
+
])
|
13
11
|
db.tables[:test]
|
14
|
-
|
12
|
+
end
|
15
13
|
|
16
14
|
before do
|
17
15
|
driver.before
|
@@ -21,36 +19,40 @@ shared_examples_for "a baza tables driver" do
|
|
21
19
|
driver.after
|
22
20
|
end
|
23
21
|
|
24
|
-
it "
|
25
|
-
test_table.name.
|
26
|
-
db.tables[:test].
|
22
|
+
it "creates tables" do
|
23
|
+
expect(test_table.name).to eq "test"
|
24
|
+
expect(db.tables[:test]).to_not eq nil
|
27
25
|
end
|
28
26
|
|
29
|
-
it "
|
27
|
+
it "#list" do
|
30
28
|
test_table
|
31
|
-
db.tables.list.
|
29
|
+
expect(db.tables.list).to include test_table
|
32
30
|
end
|
33
31
|
|
34
|
-
it "
|
32
|
+
it "#optimize" do
|
35
33
|
test_table.optimize
|
36
34
|
# FIXME: How to validate?
|
37
35
|
end
|
38
36
|
|
39
|
-
it "
|
37
|
+
it "#truncate" do
|
40
38
|
test_table
|
41
39
|
|
42
40
|
db.insert(:test, text: "test")
|
43
|
-
|
41
|
+
expect(db.select(:test).fetch.fetch(:id).to_i).to eq 1
|
42
|
+
expect(test_table.rows_count).to eq 1
|
44
43
|
|
45
44
|
# Throw out invalid encoding because it will make dumping fail.
|
46
45
|
db.tables[:test].truncate
|
47
|
-
test_table.rows_count.
|
46
|
+
expect(test_table.rows_count).to eq 0
|
47
|
+
|
48
|
+
db.insert(:test, text: "test")
|
49
|
+
expect(db.select(:test).fetch.fetch(:id).to_i).to eq 1
|
48
50
|
end
|
49
51
|
|
50
52
|
it "#clone" do
|
51
53
|
test_table
|
52
54
|
test_table.create_indexes([{name: "index_on_text", columns: ["text"]}])
|
53
|
-
test_table.indexes.length.
|
55
|
+
expect(test_table.indexes.length).to eq 1
|
54
56
|
|
55
57
|
test_table.insert(text: "test1")
|
56
58
|
test_table.insert(text: "test2")
|
@@ -58,9 +60,19 @@ shared_examples_for "a baza tables driver" do
|
|
58
60
|
test_table.clone("test2")
|
59
61
|
test_table2 = db.tables[:test2]
|
60
62
|
|
61
|
-
test_table2.columns.length.
|
62
|
-
test_table2.indexes.length.
|
63
|
-
test_table2.rows_count.
|
64
|
-
test_table2.rows_count.
|
63
|
+
expect(test_table2.columns.length).to eq test_table.columns.length
|
64
|
+
expect(test_table2.indexes.length).to eq test_table.indexes.length
|
65
|
+
expect(test_table2.rows_count).to eq test_table.rows_count
|
66
|
+
expect(test_table2.rows_count).to eq 2
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#reload" do
|
70
|
+
it "reloads the data on the table" do
|
71
|
+
test_table.reload
|
72
|
+
expect(test_table.name).to eq "test"
|
73
|
+
|
74
|
+
test_table.drop
|
75
|
+
expect { test_table.reload }.to raise_error(Baza::Errors::TableNotFound)
|
76
|
+
end
|
65
77
|
end
|
66
78
|
end
|