Class: TableSync::Receiving::Model::Sequel
- Inherits:
-
Object
- Object
- TableSync::Receiving::Model::Sequel
- Defined in:
- lib/table_sync/receiving/model/sequel.rb
Instance Attribute Summary collapse
-
#schema ⇒ Object
readonly
Returns the value of attribute schema.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #after_commit(&block) ⇒ Object
- #columns ⇒ Object
- #destroy(data:, target_keys:, version_key:) ⇒ Object
-
#initialize(table_name) ⇒ Sequel
constructor
A new instance of Sequel.
- #primary_keys ⇒ Object
- #transaction(&block) ⇒ Object
- #upsert(data:, target_keys:, version_key:, default_values:) ⇒ Object
Constructor Details
#initialize(table_name) ⇒ Sequel
Returns a new instance of Sequel.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 7 def initialize(table_name) @raw_model = Class.new(::Sequel::Model(table_name)).tap(&:unrestrict_primary_key) model_naming = ::TableSync::NamingResolver::Sequel.new( table_name: table_name, db: @raw_model.db, ) @table = model_naming.table.to_sym @schema = model_naming.schema.to_sym end |
Instance Attribute Details
#schema ⇒ Object (readonly)
Returns the value of attribute schema.
5 6 7 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 5 def schema @schema end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
5 6 7 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 5 def table @table end |
Instance Method Details
#after_commit(&block) ⇒ Object
59 60 61 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 59 def after_commit(&block) db.after_commit(&block) end |
#columns ⇒ Object
19 20 21 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 19 def columns dataset.columns end |
#destroy(data:, target_keys:, version_key:) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 43 def destroy(data:, target_keys:, version_key:) sanitized_data = data.map { |attr| attr.select { |key, _value| target_keys.include?(key) } } sanitized_data = type_cast(sanitized_data) result = dataset.returning.where(::Sequel.|(*sanitized_data)).delete if result.size > data.size raise TableSync::DestroyError.new(data: data, target_keys: target_keys, result: result) end result end |
#primary_keys ⇒ Object
23 24 25 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 23 def primary_keys [raw_model.primary_key].flatten end |
#transaction(&block) ⇒ Object
55 56 57 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 55 def transaction(&block) db.transaction(&block) end |
#upsert(data:, target_keys:, version_key:, default_values:) ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/table_sync/receiving/model/sequel.rb', line 27 def upsert(data:, target_keys:, version_key:, default_values:) qualified_version = ::Sequel.qualify(raw_model.table_name, version_key) version_condition = ::Sequel.function(:coalesce, qualified_version, 0) < ::Sequel.qualify(:excluded, version_key) upd_spec = update_spec(data.first.keys - target_keys) data.map! { |d| default_values.merge(d) } insert_data = type_cast(data) dataset .returning .insert_conflict(target: target_keys, update: upd_spec, update_where: version_condition) .multi_insert(insert_data) end |