boot.rb 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # frozen_string_literal: true
  2. require 'bundler/setup'
  3. SEQUEL_NO_ASSOCIATIONS = true
  4. Bundler.require(:default) # Load core modules
  5. def connect(dbtype)
  6. Bundler.require(dbtype) # Load database-specific modules
  7. opts = {}
  8. if dbtype == :mysql
  9. adapter = 'trilogy'
  10. opts[:ssl] = true
  11. opts[:ssl_mode] = 4 # Trilogy::SSL_PREFERRED_NOVERIFY
  12. opts[:tls_min_version] = 3 # Trilogy::TLS_VERSION_12
  13. else
  14. adapter = 'postgresql'
  15. end
  16. # Determine threading/thread pool size and timeout
  17. if defined?(Puma) && (threads = Puma.cli_config.options.fetch(:max_threads)) > 1
  18. opts[:max_connections] = threads
  19. opts[:pool_timeout] = 10
  20. else
  21. opts[:max_connections] = 512
  22. end
  23. Sequel.connect \
  24. '%{adapter}://%{host}/%{database}?user=%{user}&password=%{password}' % {
  25. adapter: adapter,
  26. host: 'tfb-database',
  27. database: 'hello_world',
  28. user: 'benchmarkdbuser',
  29. password: 'benchmarkdbpass'
  30. }, opts
  31. end
  32. DB = connect ENV.fetch('DBTYPE').to_sym
  33. # Define ORM models
  34. class World < Sequel::Model(:World)
  35. def_column_alias(:randomnumber, :randomNumber) if DB.database_type == :mysql
  36. def self.batch_update(worlds)
  37. if DB.database_type == :mysql
  38. worlds.map(&:save_changes)
  39. else
  40. ids = []
  41. sql = String.new("UPDATE world SET randomnumber = CASE id ")
  42. worlds.each do |world|
  43. sql << "when #{world[:id]} then #{world[:randomnumber]} "
  44. ids << world[:id]
  45. end
  46. sql << "ELSE randomnumber END WHERE id IN ( #{ids.join(',')})"
  47. DB.run(sql)
  48. end
  49. end
  50. end
  51. class Fortune < Sequel::Model(:Fortune)
  52. # Allow setting id to zero (0) per benchmark requirements
  53. unrestrict_primary_key
  54. end
  55. [World, Fortune].each(&:freeze)
  56. DB.freeze