sharding_spec.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # frozen_string_literal: true
  2. require_relative 'spec_helper'
  3. describe "Sharding" do
  4. let(:processes) { Helpers::Pgcat.three_shard_setup("sharded_db", 5) }
  5. before do
  6. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  7. # Setup the sharding data
  8. 3.times do |i|
  9. conn.exec("SET SHARD TO '#{i}'")
  10. conn.exec("DELETE FROM data WHERE id > 0")
  11. end
  12. 18.times do |i|
  13. i = i + 1
  14. conn.exec("SET SHARDING KEY TO '#{i}'")
  15. conn.exec("INSERT INTO data (id, value) VALUES (#{i}, 'value_#{i}')")
  16. end
  17. end
  18. after do
  19. processes.all_databases.map(&:reset)
  20. processes.pgcat.shutdown
  21. end
  22. describe "automatic routing of extended protocol" do
  23. it "can do it" do
  24. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  25. conn.exec("SET SERVER ROLE TO 'auto'")
  26. 18.times do |i|
  27. result = conn.exec_params("SELECT * FROM data WHERE id = $1", [i + 1])
  28. expect(result.ntuples).to eq(1)
  29. end
  30. end
  31. it "can do it with multiple parameters" do
  32. conn = PG.connect(processes.pgcat.connection_string("sharded_db", "sharding_user"))
  33. conn.exec("SET SERVER ROLE TO 'auto'")
  34. 18.times do |i|
  35. result = conn.exec_params("SELECT * FROM data WHERE id = $1 AND id = $2", [i + 1, i + 1])
  36. expect(result.ntuples).to eq(1)
  37. end
  38. end
  39. end
  40. end