sequel 3.7.0 → 3.8.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +50 -0
- data/doc/advanced_associations.rdoc +4 -3
- data/doc/release_notes/3.7.0.txt +2 -2
- data/doc/release_notes/3.8.0.txt +151 -0
- data/lib/sequel/adapters/jdbc.rb +10 -2
- data/lib/sequel/adapters/jdbc/h2.rb +14 -0
- data/lib/sequel/adapters/mysql.rb +36 -26
- data/lib/sequel/adapters/postgres.rb +27 -19
- data/lib/sequel/adapters/shared/mssql.rb +12 -4
- data/lib/sequel/adapters/shared/mysql.rb +16 -0
- data/lib/sequel/connection_pool.rb +178 -57
- data/lib/sequel/database.rb +60 -12
- data/lib/sequel/database/schema_generator.rb +1 -2
- data/lib/sequel/dataset.rb +10 -1
- data/lib/sequel/dataset/actions.rb +4 -8
- data/lib/sequel/dataset/convenience.rb +9 -2
- data/lib/sequel/dataset/query.rb +2 -5
- data/lib/sequel/dataset/sql.rb +0 -1
- data/lib/sequel/exceptions.rb +3 -3
- data/lib/sequel/metaprogramming.rb +4 -18
- data/lib/sequel/model/associations.rb +2 -2
- data/lib/sequel/model/base.rb +15 -18
- data/lib/sequel/model/default_inflections.rb +0 -1
- data/lib/sequel/model/plugins.rb +3 -2
- data/lib/sequel/plugins/boolean_readers.rb +3 -2
- data/lib/sequel/plugins/identity_map.rb +9 -0
- data/lib/sequel/plugins/validation_helpers.rb +8 -1
- data/lib/sequel/sql.rb +21 -11
- data/lib/sequel/version.rb +1 -1
- data/spec/adapters/mssql_spec.rb +7 -1
- data/spec/adapters/mysql_spec.rb +22 -0
- data/spec/core/connection_pool_spec.rb +211 -3
- data/spec/core/core_sql_spec.rb +7 -0
- data/spec/core/database_spec.rb +159 -7
- data/spec/core/dataset_spec.rb +33 -0
- data/spec/core/spec_helper.rb +1 -0
- data/spec/extensions/boolean_readers_spec.rb +6 -0
- data/spec/extensions/identity_map_spec.rb +29 -1
- data/spec/extensions/inflector_spec.rb +0 -1
- data/spec/extensions/validation_helpers_spec.rb +23 -0
- data/spec/integration/type_test.rb +1 -1
- metadata +131 -129
data/spec/adapters/mysql_spec.rb
CHANGED
@@ -62,6 +62,17 @@ context "MySQL", '#create_table' do
|
|
62
62
|
@db.create_table(:dolls){File :name, :default=>'blah'}
|
63
63
|
@db.sqls.should == ["CREATE TABLE dolls (name blob)"]
|
64
64
|
end
|
65
|
+
|
66
|
+
specify "should respect the size option for File type" do
|
67
|
+
@db.create_table(:dolls) do
|
68
|
+
File :n1
|
69
|
+
File :n2, :size=>:tiny
|
70
|
+
File :n3, :size=>:medium
|
71
|
+
File :n4, :size=>:long
|
72
|
+
File :n5, :size=>255
|
73
|
+
end
|
74
|
+
@db.schema(:dolls).map{|k, v| v[:db_type]}.should == %w"blob tinyblob mediumblob longblob blob"
|
75
|
+
end
|
65
76
|
end
|
66
77
|
|
67
78
|
context "A MySQL database" do
|
@@ -942,6 +953,17 @@ if MYSQL_DB.class.adapter_scheme == :mysql
|
|
942
953
|
@ds.all.should == [{:a=>10}, {:a=>15}, {:b=>20}, {:b=>25}]
|
943
954
|
end
|
944
955
|
|
956
|
+
specify "should work with Database#run" do
|
957
|
+
proc{MYSQL_DB.run('SELECT * FROM a; SELECT * FROM b')}.should_not raise_error
|
958
|
+
proc{MYSQL_DB.run('SELECT * FROM a; SELECT * FROM b')}.should_not raise_error
|
959
|
+
end
|
960
|
+
|
961
|
+
specify "should work with Database#run and other statements" do
|
962
|
+
proc{MYSQL_DB.run('UPDATE a SET a = 1; SELECT * FROM a; DELETE FROM b')}.should_not raise_error
|
963
|
+
MYSQL_DB[:a].select_order_map(:a).should == [1, 1]
|
964
|
+
MYSQL_DB[:b].all.should == []
|
965
|
+
end
|
966
|
+
|
945
967
|
specify "should split results returned into arrays if split_multiple_result_sets is used" do
|
946
968
|
@ds.split_multiple_result_sets.all.should == [[{:a=>10}, {:a=>15}], [{:b=>20}, {:b=>25}]]
|
947
969
|
end
|
@@ -86,7 +86,7 @@ context "A connection pool handling connections" do
|
|
86
86
|
|
87
87
|
specify "#make_new should not make more than max_size connections" do
|
88
88
|
50.times{Thread.new{@cpool.hold{sleep 0.001}}}
|
89
|
-
@cpool.created_count.should
|
89
|
+
@cpool.created_count.should <= @max_size
|
90
90
|
end
|
91
91
|
|
92
92
|
specify ":disconnection_proc option should set the disconnection proc to use" do
|
@@ -383,7 +383,7 @@ context "ConnectionPool#disconnect" do
|
|
383
383
|
@pool.size.should == 0
|
384
384
|
end
|
385
385
|
|
386
|
-
specify "should
|
386
|
+
specify "should disconnect connections in use as soon as they are no longer in use" do
|
387
387
|
threads = []
|
388
388
|
stop = nil
|
389
389
|
5.times {|i| threads << Thread.new {@pool.hold {|c| while !stop;sleep 0.01;end}}; sleep 0.01}
|
@@ -402,8 +402,9 @@ context "ConnectionPool#disconnect" do
|
|
402
402
|
conns = []
|
403
403
|
@pool.disconnect {|c| conns << c}
|
404
404
|
conns.size.should == 4
|
405
|
+
@pool.size.should == 1
|
405
406
|
end
|
406
|
-
@pool.size.should ==
|
407
|
+
@pool.size.should == 0
|
407
408
|
end
|
408
409
|
end
|
409
410
|
|
@@ -413,6 +414,10 @@ context "A connection pool with multiple servers" do
|
|
413
414
|
@pool = Sequel::ConnectionPool.new(CONNECTION_POOL_DEFAULTS.merge(:servers=>{:read_only=>{}})){|server| "#{server}#{@invoked_counts[server] += 1}"}
|
414
415
|
end
|
415
416
|
|
417
|
+
specify "#servers should return symbols for all servers" do
|
418
|
+
@pool.servers.sort_by{|s| s.to_s}.should == [:default, :read_only]
|
419
|
+
end
|
420
|
+
|
416
421
|
specify "should use the :default server by default" do
|
417
422
|
@pool.size.should == 0
|
418
423
|
@pool.hold do |c|
|
@@ -423,6 +428,18 @@ context "A connection pool with multiple servers" do
|
|
423
428
|
@pool.size.should == 1
|
424
429
|
@invoked_counts.should == {:default=>1}
|
425
430
|
end
|
431
|
+
|
432
|
+
specify "should use the :default server an invalid server is used" do
|
433
|
+
@pool.hold do |c1|
|
434
|
+
c1.should == "default1"
|
435
|
+
@pool.hold(:blah) do |c2|
|
436
|
+
c2.should == c1
|
437
|
+
@pool.hold(:blah2) do |c3|
|
438
|
+
c2.should == c3
|
439
|
+
end
|
440
|
+
end
|
441
|
+
end
|
442
|
+
end
|
426
443
|
|
427
444
|
specify "should use the requested server if server is given" do
|
428
445
|
@pool.size(:read_only).should == 0
|
@@ -464,6 +481,144 @@ context "A connection pool with multiple servers" do
|
|
464
481
|
@pool.hold(:read_only){|c| c.should == 'read_only2'}
|
465
482
|
@pool.hold{|c| c.should == 'default2'}
|
466
483
|
end
|
484
|
+
|
485
|
+
specify "#add_servers should add new servers to the pool" do
|
486
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}){|s| s}
|
487
|
+
|
488
|
+
pool.hold{}
|
489
|
+
pool.hold(:server2){}
|
490
|
+
pool.hold(:server3){}
|
491
|
+
pool.hold(:server1) do
|
492
|
+
pool.allocated.length.should == 0
|
493
|
+
pool.allocated(:server1).length.should == 1
|
494
|
+
pool.allocated(:server2).should == nil
|
495
|
+
pool.allocated(:server3).should == nil
|
496
|
+
pool.available_connections.length.should == 1
|
497
|
+
pool.available_connections(:server1).length.should == 0
|
498
|
+
pool.available_connections(:server2).should == nil
|
499
|
+
pool.available_connections(:server3).should == nil
|
500
|
+
|
501
|
+
pool.add_servers([:server2, :server3])
|
502
|
+
pool.hold(:server2){}
|
503
|
+
pool.hold(:server3) do
|
504
|
+
pool.allocated.length.should == 0
|
505
|
+
pool.allocated(:server1).length.should == 1
|
506
|
+
pool.allocated(:server2).length.should == 0
|
507
|
+
pool.allocated(:server3).length.should == 1
|
508
|
+
pool.available_connections.length.should == 1
|
509
|
+
pool.available_connections(:server1).length.should == 0
|
510
|
+
pool.available_connections(:server2).length.should == 1
|
511
|
+
pool.available_connections(:server3).length.should == 0
|
512
|
+
end
|
513
|
+
end
|
514
|
+
end
|
515
|
+
|
516
|
+
specify "#add_servers should ignore existing keys" do
|
517
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}){|s| s}
|
518
|
+
|
519
|
+
pool.allocated.length.should == 0
|
520
|
+
pool.allocated(:server1).length.should == 0
|
521
|
+
pool.available_connections.length.should == 0
|
522
|
+
pool.available_connections(:server1).length.should == 0
|
523
|
+
pool.hold do |c1|
|
524
|
+
c1.should == :default
|
525
|
+
pool.allocated.length.should == 1
|
526
|
+
pool.allocated(:server1).length.should == 0
|
527
|
+
pool.available_connections.length.should == 0
|
528
|
+
pool.available_connections(:server1).length.should == 0
|
529
|
+
pool.hold(:server1) do |c2|
|
530
|
+
c2.should == :server1
|
531
|
+
pool.allocated.length.should == 1
|
532
|
+
pool.allocated(:server1).length.should == 1
|
533
|
+
pool.available_connections.length.should == 0
|
534
|
+
pool.available_connections(:server1).length.should == 0
|
535
|
+
pool.add_servers([:default, :server1])
|
536
|
+
pool.allocated.length.should == 1
|
537
|
+
pool.allocated(:server1).length.should == 1
|
538
|
+
pool.available_connections.length.should == 0
|
539
|
+
pool.available_connections(:server1).length.should == 0
|
540
|
+
end
|
541
|
+
pool.allocated.length.should == 1
|
542
|
+
pool.allocated(:server1).length.should == 0
|
543
|
+
pool.available_connections.length.should == 0
|
544
|
+
pool.available_connections(:server1).length.should == 1
|
545
|
+
pool.add_servers([:default, :server1])
|
546
|
+
pool.allocated.length.should == 1
|
547
|
+
pool.allocated(:server1).length.should == 0
|
548
|
+
pool.available_connections.length.should == 0
|
549
|
+
pool.available_connections(:server1).length.should == 1
|
550
|
+
end
|
551
|
+
pool.allocated.length.should == 0
|
552
|
+
pool.allocated(:server1).length.should == 0
|
553
|
+
pool.available_connections.length.should == 1
|
554
|
+
pool.available_connections(:server1).length.should == 1
|
555
|
+
pool.add_servers([:default, :server1])
|
556
|
+
pool.allocated.length.should == 0
|
557
|
+
pool.allocated(:server1).length.should == 0
|
558
|
+
pool.available_connections.length.should == 1
|
559
|
+
pool.available_connections(:server1).length.should == 1
|
560
|
+
end
|
561
|
+
|
562
|
+
specify "#remove_servers should disconnect available connections immediately" do
|
563
|
+
pool = Sequel::ConnectionPool.new(:max_connections=>5, :servers=>{:server1=>{}}){|s| s}
|
564
|
+
threads = []
|
565
|
+
stop = nil
|
566
|
+
5.times {|i| threads << Thread.new{pool.hold(:server1){|c| sleep 0.05}}}
|
567
|
+
sleep 0.1
|
568
|
+
threads.each {|t| t.join}
|
569
|
+
|
570
|
+
pool.size(:server1).should == 5
|
571
|
+
pool.remove_servers([:server1])
|
572
|
+
pool.size(:server1).should == 0
|
573
|
+
end
|
574
|
+
|
575
|
+
specify "#remove_servers should disconnect connections in use as soon as they are returned to the pool" do
|
576
|
+
dc = []
|
577
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}, :disconnection_proc=>proc{|c| dc << c}){|s| s}
|
578
|
+
c1 = nil
|
579
|
+
pool.hold(:server1) do |c|
|
580
|
+
pool.size(:server1).should == 1
|
581
|
+
dc.should == []
|
582
|
+
pool.remove_servers([:server1])
|
583
|
+
pool.size(:server1).should == 0
|
584
|
+
dc.should == []
|
585
|
+
c1 = c
|
586
|
+
end
|
587
|
+
pool.size(:server1).should == 0
|
588
|
+
dc.should == [c1]
|
589
|
+
end
|
590
|
+
|
591
|
+
specify "#remove_servers should remove server related data structures immediately" do
|
592
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}){|s| s}
|
593
|
+
pool.available_connections(:server1).should == []
|
594
|
+
pool.allocated(:server1).should == {}
|
595
|
+
pool.remove_servers([:server1])
|
596
|
+
pool.available_connections(:server1).should == nil
|
597
|
+
pool.allocated(:server1).should == nil
|
598
|
+
end
|
599
|
+
|
600
|
+
specify "#remove_servers should not allow the removal of the default server" do
|
601
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}){|s| s}
|
602
|
+
proc{pool.remove_servers([:server1])}.should_not raise_error
|
603
|
+
proc{pool.remove_servers([:default])}.should raise_error(Sequel::Error)
|
604
|
+
end
|
605
|
+
|
606
|
+
specify "#remove_servers should ignore servers that have already been removed" do
|
607
|
+
dc = []
|
608
|
+
pool = Sequel::ConnectionPool.new(:servers=>{:server1=>{}}, :disconnection_proc=>proc{|c| dc << c}){|s| s}
|
609
|
+
c1 = nil
|
610
|
+
pool.hold(:server1) do |c|
|
611
|
+
pool.size(:server1).should == 1
|
612
|
+
dc.should == []
|
613
|
+
pool.remove_servers([:server1])
|
614
|
+
pool.remove_servers([:server1])
|
615
|
+
pool.size(:server1).should == 0
|
616
|
+
dc.should == []
|
617
|
+
c1 = c
|
618
|
+
end
|
619
|
+
pool.size(:server1).should == 0
|
620
|
+
dc.should == [c1]
|
621
|
+
end
|
467
622
|
end
|
468
623
|
|
469
624
|
context "SingleThreadedPool" do
|
@@ -493,10 +648,63 @@ context "A single threaded pool with multiple servers" do
|
|
493
648
|
@pool = Sequel::SingleThreadedPool.new(CONNECTION_POOL_DEFAULTS.merge(:disconnection_proc=>proc{|c| @max_size=3}, :servers=>{:read_only=>{}})){|server| server}
|
494
649
|
end
|
495
650
|
|
651
|
+
specify "#servers should return symbols for all servers" do
|
652
|
+
@pool.servers.sort_by{|s| s.to_s}.should == [:default, :read_only]
|
653
|
+
end
|
654
|
+
|
655
|
+
specify "#add_servers should add new servers to the pool" do
|
656
|
+
@pool.hold(:blah){|c| c.should == :default}
|
657
|
+
@pool.add_servers([:blah])
|
658
|
+
@pool.hold(:blah){|c| c.should == :blah}
|
659
|
+
end
|
660
|
+
|
661
|
+
specify "#add_servers should ignore keys already existing" do
|
662
|
+
@pool.hold{|c| c.should == :default}
|
663
|
+
@pool.hold(:read_only){|c| c.should == :read_only}
|
664
|
+
@pool.add_servers([:default, :read_only])
|
665
|
+
@pool.conn.should == :default
|
666
|
+
@pool.conn(:read_only).should == :read_only
|
667
|
+
end
|
668
|
+
|
669
|
+
specify "#remove_servers should remove servers from the pool" do
|
670
|
+
@pool.hold(:read_only){|c| c.should == :read_only}
|
671
|
+
@pool.remove_servers([:read_only])
|
672
|
+
@pool.hold(:read_only){|c| c.should == :default}
|
673
|
+
end
|
674
|
+
|
675
|
+
specify "#remove_servers should not allow the removal of the default server" do
|
676
|
+
proc{@pool.remove_servers([:default])}.should raise_error(Sequel::Error)
|
677
|
+
end
|
678
|
+
|
679
|
+
specify "#remove_servers should disconnect connection immediately" do
|
680
|
+
@pool.hold(:read_only){|c| c.should == :read_only}
|
681
|
+
@pool.conn(:read_only).should == :read_only
|
682
|
+
@pool.remove_servers([:read_only])
|
683
|
+
@pool.conn(:read_only).should == nil
|
684
|
+
@pool.hold{}
|
685
|
+
@pool.conn(:read_only).should == :default
|
686
|
+
end
|
687
|
+
|
688
|
+
specify "#remove_servers should ignore keys that do not exist" do
|
689
|
+
proc{@pool.remove_servers([:blah])}.should_not raise_error
|
690
|
+
end
|
691
|
+
|
496
692
|
specify "should use the :default server by default" do
|
497
693
|
@pool.hold{|c| c.should == :default}
|
498
694
|
@pool.conn.should == :default
|
499
695
|
end
|
696
|
+
|
697
|
+
specify "should use the :default server an invalid server is used" do
|
698
|
+
@pool.hold do |c1|
|
699
|
+
c1.should == :default
|
700
|
+
@pool.hold(:blah) do |c2|
|
701
|
+
c2.should == c1
|
702
|
+
@pool.hold(:blah2) do |c3|
|
703
|
+
c2.should == c3
|
704
|
+
end
|
705
|
+
end
|
706
|
+
end
|
707
|
+
end
|
500
708
|
|
501
709
|
specify "should use the requested server if server is given" do
|
502
710
|
@pool.hold(:read_only){|c| c.should == :read_only}
|
data/spec/core/core_sql_spec.rb
CHANGED
@@ -408,3 +408,10 @@ context "Sequel::SQL::OrderedExpression" do
|
|
408
408
|
@oe.invert.invert.descending.should == true
|
409
409
|
end
|
410
410
|
end
|
411
|
+
|
412
|
+
context "Expression" do
|
413
|
+
specify "should ==" do
|
414
|
+
:column.qualify(:table).cast(:type).*(:numeric_column).asc.should == :column.qualify(:table).cast(:type).*(:numeric_column).asc
|
415
|
+
:other_column.qualify(:table).cast(:type).*(:numeric_column).asc.should_not == :column.qualify(:table).cast(:type).*(:numeric_column).asc
|
416
|
+
end
|
417
|
+
end
|
data/spec/core/database_spec.rb
CHANGED
@@ -191,7 +191,7 @@ context "Database#disconnect" do
|
|
191
191
|
specify "should call pool.disconnect" do
|
192
192
|
d = Sequel::Database.new
|
193
193
|
p = d.pool
|
194
|
-
p.should_receive(:disconnect).once.and_return(2)
|
194
|
+
p.should_receive(:disconnect).once.with({}).and_return(2)
|
195
195
|
d.disconnect.should == 2
|
196
196
|
end
|
197
197
|
end
|
@@ -951,12 +951,30 @@ context "A single threaded database" do
|
|
951
951
|
@db.pool.hold {|c| c.should == 1234568}
|
952
952
|
end
|
953
953
|
|
954
|
-
specify "should
|
955
|
-
db
|
956
|
-
|
957
|
-
|
958
|
-
|
959
|
-
|
954
|
+
specify "should disconnect correctly" do
|
955
|
+
def @db.disconnect_connection(c); @dc = c end
|
956
|
+
def @db.dc; @dc end
|
957
|
+
x = nil
|
958
|
+
@db.pool.hold{|c| x = c}
|
959
|
+
@db.pool.conn.should == x
|
960
|
+
proc{@db.disconnect}.should_not raise_error
|
961
|
+
@db.pool.conn.should == nil
|
962
|
+
@db.dc.should == x
|
963
|
+
end
|
964
|
+
|
965
|
+
specify "should convert an Exception on connection into a DatabaseConnectionError" do
|
966
|
+
db = Sequel::Database.new(:single_threaded => true){raise Exception}
|
967
|
+
proc {db.pool.hold {|c|}}.should raise_error(Sequel::DatabaseConnectionError)
|
968
|
+
end
|
969
|
+
|
970
|
+
specify "should raise a DatabaseConnectionError if the connection proc returns nil" do
|
971
|
+
db = Sequel::Database.new(:single_threaded => true){nil}
|
972
|
+
proc {db.pool.hold {|c|}}.should raise_error(Sequel::DatabaseConnectionError)
|
973
|
+
end
|
974
|
+
|
975
|
+
specify "should convert an Exceptions during use into RuntimeErrors" do
|
976
|
+
db = Sequel::Database.new(:single_threaded => true){Object.new}
|
977
|
+
proc {db.synchronize{raise Exception}}.should raise_error(RuntimeError)
|
960
978
|
end
|
961
979
|
end
|
962
980
|
|
@@ -1232,6 +1250,140 @@ context "Database#server_opts" do
|
|
1232
1250
|
end
|
1233
1251
|
end
|
1234
1252
|
|
1253
|
+
context "Database#add_servers" do
|
1254
|
+
before do
|
1255
|
+
@db = MockDatabase.new(:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}})
|
1256
|
+
def @db.connect(server)
|
1257
|
+
server_opts(server)
|
1258
|
+
end
|
1259
|
+
def @db.disconnect_connection(c)
|
1260
|
+
end
|
1261
|
+
end
|
1262
|
+
|
1263
|
+
specify "should add new servers to the connection pool" do
|
1264
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1265
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1266
|
+
@db.synchronize(:server2){|c| c[:host].should == 1}
|
1267
|
+
|
1268
|
+
@db.add_servers(:server2=>{:host=>6})
|
1269
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1270
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1271
|
+
@db.synchronize(:server2){|c| c[:host].should == 6}
|
1272
|
+
|
1273
|
+
@db.disconnect
|
1274
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1275
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1276
|
+
@db.synchronize(:server2){|c| c[:host].should == 6}
|
1277
|
+
end
|
1278
|
+
|
1279
|
+
specify "should replace options for future connections to existing servers" do
|
1280
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1281
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1282
|
+
@db.synchronize(:server2){|c| c[:host].should == 1}
|
1283
|
+
|
1284
|
+
@db.add_servers(:default=>proc{{:host=>4}}, :server1=>{:host=>8})
|
1285
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1286
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1287
|
+
@db.synchronize(:server2){|c| c[:host].should == 1}
|
1288
|
+
|
1289
|
+
@db.disconnect
|
1290
|
+
@db.synchronize{|c| c[:host].should == 4}
|
1291
|
+
@db.synchronize(:server1){|c| c[:host].should == 8}
|
1292
|
+
@db.synchronize(:server2){|c| c[:host].should == 4}
|
1293
|
+
end
|
1294
|
+
end
|
1295
|
+
|
1296
|
+
context "Database#remove_servers" do
|
1297
|
+
before do
|
1298
|
+
@db = MockDatabase.new(:host=>1, :database=>2, :servers=>{:server1=>{:host=>3}, :server2=>{:host=>4}})
|
1299
|
+
def @db.connect(server)
|
1300
|
+
server_opts(server)
|
1301
|
+
end
|
1302
|
+
def @db.disconnect_connection(c)
|
1303
|
+
end
|
1304
|
+
end
|
1305
|
+
|
1306
|
+
specify "should remove servers from the connection pool" do
|
1307
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1308
|
+
@db.synchronize(:server1){|c| c[:host].should == 3}
|
1309
|
+
@db.synchronize(:server2){|c| c[:host].should == 4}
|
1310
|
+
|
1311
|
+
@db.remove_servers(:server1, :server2)
|
1312
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1313
|
+
@db.synchronize(:server1){|c| c[:host].should == 1}
|
1314
|
+
@db.synchronize(:server2){|c| c[:host].should == 1}
|
1315
|
+
end
|
1316
|
+
|
1317
|
+
specify "should accept arrays of symbols" do
|
1318
|
+
@db.remove_servers([:server1, :server2])
|
1319
|
+
@db.synchronize{|c| c[:host].should == 1}
|
1320
|
+
@db.synchronize(:server1){|c| c[:host].should == 1}
|
1321
|
+
@db.synchronize(:server2){|c| c[:host].should == 1}
|
1322
|
+
end
|
1323
|
+
|
1324
|
+
specify "should allow removal while connections are still open" do
|
1325
|
+
@db.synchronize do |c1|
|
1326
|
+
c1[:host].should == 1
|
1327
|
+
@db.synchronize(:server1) do |c2|
|
1328
|
+
c2[:host].should == 3
|
1329
|
+
@db.synchronize(:server2) do |c3|
|
1330
|
+
c3[:host].should == 4
|
1331
|
+
@db.remove_servers(:server1, :server2)
|
1332
|
+
@db.synchronize(:server1) do |c4|
|
1333
|
+
c4.should_not == c2
|
1334
|
+
c4.should == c1
|
1335
|
+
c4[:host].should == 1
|
1336
|
+
@db.synchronize(:server2) do |c5|
|
1337
|
+
c5.should_not == c3
|
1338
|
+
c5.should == c1
|
1339
|
+
c5[:host].should == 1
|
1340
|
+
end
|
1341
|
+
end
|
1342
|
+
c3[:host].should == 4
|
1343
|
+
end
|
1344
|
+
c2[:host].should == 3
|
1345
|
+
end
|
1346
|
+
c1[:host].should == 1
|
1347
|
+
end
|
1348
|
+
end
|
1349
|
+
end
|
1350
|
+
|
1351
|
+
context "Database#each_server" do
|
1352
|
+
before do
|
1353
|
+
@db = Sequel.connect(:adapter=>:mock, :host=>1, :database=>2, :servers=>{:server1=>{:host=>3}, :server2=>{:host=>4}})
|
1354
|
+
def @db.connect(server)
|
1355
|
+
server_opts(server)
|
1356
|
+
end
|
1357
|
+
def @db.disconnect_connection(c)
|
1358
|
+
end
|
1359
|
+
end
|
1360
|
+
|
1361
|
+
specify "should yield a separate database object for each server" do
|
1362
|
+
hosts = []
|
1363
|
+
@db.each_server do |db|
|
1364
|
+
db.should be_a_kind_of(Sequel::Database)
|
1365
|
+
db.should_not == @db
|
1366
|
+
db.opts[:database].should == 2
|
1367
|
+
hosts << db.opts[:host]
|
1368
|
+
end
|
1369
|
+
hosts.sort.should == [1, 3, 4]
|
1370
|
+
end
|
1371
|
+
|
1372
|
+
specify "should disconnect and remove entry from Sequel::DATABASES after use" do
|
1373
|
+
dbs = []
|
1374
|
+
dcs = []
|
1375
|
+
@db.each_server do |db|
|
1376
|
+
dbs << db
|
1377
|
+
Sequel::DATABASES.should include(db)
|
1378
|
+
db.meta_def(:disconnect){dcs << db}
|
1379
|
+
end
|
1380
|
+
dbs.each do |db|
|
1381
|
+
Sequel::DATABASES.should_not include(db)
|
1382
|
+
end
|
1383
|
+
dbs.should == dcs
|
1384
|
+
end
|
1385
|
+
end
|
1386
|
+
|
1235
1387
|
context "Database#raise_error" do
|
1236
1388
|
specify "should reraise if the exception class is not in opts[:classes]" do
|
1237
1389
|
e = Class.new(StandardError)
|