-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add #travel_to and a few more features #11
Changes from all commits
850d61f
4cf7252
026c17a
e5cc17f
f17f043
72fa3ad
8160e24
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# frozen_string_literal: true | ||
|
||
module IronTrail | ||
module CollectionProxyMixin | ||
def travel_to(ts) | ||
arel_table = arel.ast.cores.first.source.left | ||
|
||
change_record = scope | ||
.order(arel_table[:created_at] => :desc) | ||
.where(arel_table[:created_at].lteq(ts)) | ||
.first | ||
|
||
change_record.reify | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||
# frozen_string_literal: true | ||||||
|
||||||
module IronTrail | ||||||
module Reifier | ||||||
def self.reify(trail) | ||||||
source_attributes = (trail.delete_operation? ? trail.rec_old : trail.rec_new) | ||||||
klass = model_from_table_name(trail.rec_table, source_attributes['type']) | ||||||
|
||||||
record = klass.where(id: trail.rec_id).first || klass.new | ||||||
|
||||||
source_attributes.each do |name, value| | ||||||
if record.has_attribute?(name) | ||||||
record[name.to_sym] = value | ||||||
elsif record.respond_to?("#{name}=") | ||||||
record.send("#{name}=", value) | ||||||
else | ||||||
ghost = record.instance_variable_get(:@irontrail_reified_ghost_attributes) | ||||||
unless ghost | ||||||
ghost = HashWithIndifferentAccess.new | ||||||
record.instance_variable_set(:@irontrail_reified_ghost_attributes, ghost) | ||||||
end | ||||||
ghost[name] = value | ||||||
end | ||||||
end | ||||||
|
||||||
record | ||||||
end | ||||||
|
||||||
def self.model_from_table_name(table_name, sti_type=nil) | ||||||
index = ActiveRecord::Base.descendants.reject(&:abstract_class).chunk(&:table_name).to_h do |key, val| | ||||||
v = \ | ||||||
if val.length == 1 | ||||||
val[0] | ||||||
else | ||||||
val.to_h { |k| [k.to_s, k] } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Double-checking if this logic is correct, seems wrong to me considering the rest of the method. Also, I am unsure if you are and how hard it is to cover this path.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is correct. I have to yet add This basically deals with tables that are mapped by multiple models, that is, activerecord Single Table Inheritance. So the mapping here becomes a hash mapping the model name to the model itself. The index = {
'some_sti_table' => {
'ModelA' => ModelA,
'ModelB' => ModelB
},
'some_other_table' => SomeOtherTable
} |
||||||
end | ||||||
|
||||||
[key, v] | ||||||
end | ||||||
|
||||||
klass = index[table_name] | ||||||
raise "Cannot infer model from table named '#{table_name}'" unless klass | ||||||
|
||||||
return klass unless klass.is_a?(Hash) | ||||||
klass = klass[sti_type] | ||||||
|
||||||
return klass if klass | ||||||
|
||||||
raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'" | ||||||
end | ||||||
end | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
|
||
# An STI model | ||
class MatrixPill < ApplicationRecord | ||
include IronTrail::Model | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,13 +43,13 @@ | |
::IronTrailSpecMigrator.new.migrate | ||
|
||
Time.now.tap do |date| | ||
partition_name = "irontrail_chgn_#{date.strftime('%Y%m')}" | ||
partition_name = "irontrail_chgn_infinite" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the goal isn't to test |
||
next if ActiveRecord::Base.connection.table_exists?(partition_name) | ||
|
||
IrontrailChange.create_partition( | ||
name: partition_name, | ||
start_range: date, | ||
end_range: date.next_month | ||
start_range: Time.parse('2000-01-01T00:00:00Z'), | ||
end_range: Time.parse('2100-01-01T00:00:00Z') | ||
) | ||
end | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder what YJIT thinks of this. Anyway, not a concern :)