Class: River::Driver::ActiveRecord
- Inherits:
-
Object
- Object
- River::Driver::ActiveRecord
- Defined in:
- lib/driver.rb
Overview
Provides an ActiveRecord driver for River that supports both PostgreSQL and SQLite.
Used in conjunction with a River client like:
ActiveRecord::Base.establish_connection("postgres://...")
client = River::Client.new(River::Driver::ActiveRecord.new)
Instance Method Summary collapse
-
#initialize ⇒ ActiveRecord
constructor
A new instance of ActiveRecord.
- #job_get_by_id(id) ⇒ Object
- #job_insert(insert_params) ⇒ Object
- #job_insert_many(insert_params_many) ⇒ Object
- #job_list ⇒ Object
- #rollback_exception ⇒ Object
- #transaction ⇒ Object
Constructor Details
#initialize ⇒ ActiveRecord
Returns a new instance of ActiveRecord.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/driver.rb', line 60 def initialize @is_sqlite = ::ActiveRecord::Base.connection.adapter_name.downcase.include?("sqlite") # It's Ruby, so we can only define a model after ActiveRecord's established a # connection because it's all dynamic. if !River::Driver::ActiveRecord.const_defined?(:RiverJob) River::Driver::ActiveRecord.const_set(:RiverJob, Class.new(::ActiveRecord::Base) do self.table_name = "river_job" # Unfortunately, Rails errors if you have a column called `errors` and # provides no way to remap names (beyond ignoring a column, which we # really don't want). This patch is in place so we can hydrate this # model at all without ActiveRecord self-immolating. def self.dangerous_attribute_method?(method_name) return false if method_name == "errors" super end # See comment above, but since we force allowed `errors` as an # attribute name, ActiveRecord would otherwise fail to save a row as # it checked for its own `errors` hash and finding no values. def errors = {} end) end end |
Instance Method Details
#job_get_by_id(id) ⇒ Object
86 87 88 89 90 91 92 93 94 |
# File 'lib/driver.rb', line 86 def job_get_by_id(id) if @is_sqlite row = sqlite_job_rows("WHERE id = ? LIMIT 1", [id]).first row ? sqlite_to_job_row_from_raw(row) : nil else data_set = RiverJob.where(id: id) data_set.first ? to_job_row_from_model(data_set.first) : nil end end |
#job_insert(insert_params) ⇒ Object
96 97 98 |
# File 'lib/driver.rb', line 96 def job_insert(insert_params) job_insert_many([insert_params]).first end |
#job_insert_many(insert_params_many) ⇒ Object
100 101 102 |
# File 'lib/driver.rb', line 100 def job_insert_many(insert_params_many) @is_sqlite ? sqlite_job_insert_many(insert_params_many) : postgres_job_insert_many(insert_params_many) end |
#job_list ⇒ Object
104 105 106 107 108 109 110 |
# File 'lib/driver.rb', line 104 def job_list if @is_sqlite sqlite_job_rows("ORDER BY id").map { |row| sqlite_to_job_row_from_raw(row) } else RiverJob.order(:id).all.map { |job| to_job_row_from_model(job) } end end |
#rollback_exception ⇒ Object
112 113 114 |
# File 'lib/driver.rb', line 112 def rollback_exception ::ActiveRecord::Rollback end |
#transaction ⇒ Object
116 117 118 |
# File 'lib/driver.rb', line 116 def transaction(&) ::ActiveRecord::Base.transaction(requires_new: true, &) end |