Skip to content
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

[Proposal] #force_update with valid_from valid_to #93

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

osyo-manga
Copy link
Collaborator

Summary

Allow force_update to specify valid_from valid_to when updating with #force_update.
force_update { _1.update(valid_from: valid_from, valid_to: valid_to) } Then all records between valid_from ~ valid_to will be overwritten with record.
This can be used to delete history, etc.
See test code for specific behavior.

Sample code

def puts_histories(record)
  puts Company.ignore_valid_datetime.bitemporal_for(record.id).order(:valid_from).pluck(:id, :valid_from, :valid_to, :name).map { |id, valid_from, valid_to, name|
    "| #{"%02d" % id} | #{valid_from} ~ #{valid_to} | #{name} |"
  }
  puts
  puts "---" * 10
  puts
end

company = nil
ActiveRecord::Bitemporal.valid_at("2020/01/01") {
  company = Company.create(name: "A")
}

ActiveRecord::Bitemporal.valid_at("2020/02/01") {
  company.update!(name: "B")
}

ActiveRecord::Bitemporal.valid_at("2020/03/01") {
  company.update!(name: "C")
}

ActiveRecord::Bitemporal.valid_at("2020/04/01") {
  company.update!(name: "D")
}

puts_histories company


# Overwrite 2020/01/10 ~ 2020/04/10 with X
company = Company.find_at_time("2020/02/01", company.id)
company.force_update { |record|
  record.update!(valid_from: "2020/01/20", valid_to: "2020/04/10", name: "X")
}

puts_histories company


# Delete X history
company = Company.find_at_time("2020/01/01", company.id)
company.force_update { |record|
  record.update!(valid_to: "2020/04/10")
}

puts_histories company
__END__
output:

| 02 | 2020-01-01 00:00:00 UTC ~ 2020-02-01 00:00:00 UTC | A |
| 04 | 2020-02-01 00:00:00 UTC ~ 2020-03-01 00:00:00 UTC | B |
| 06 | 2020-03-01 00:00:00 UTC ~ 2020-04-01 00:00:00 UTC | C |
| 07 | 2020-04-01 00:00:00 UTC ~ 9999-12-31 00:00:00 UTC | D |

------------------------------

| 08 | 2020-01-01 00:00:00 UTC ~ 2020-01-20 00:00:00 UTC | A |
| 09 | 2020-01-20 00:00:00 UTC ~ 2020-04-10 00:00:00 UTC | X |
| 10 | 2020-04-10 00:00:00 UTC ~ 9999-12-31 00:00:00 UTC | D |

------------------------------

| 11 | 2020-01-01 00:00:00 UTC ~ 2020-04-10 00:00:00 UTC | A |
| 10 | 2020-04-10 00:00:00 UTC ~ 9999-12-31 00:00:00 UTC | D |

------------------------------

@auto-assign auto-assign bot requested review from wakasa51 and yono June 16, 2022 07:42
@osyo-manga osyo-manga marked this pull request as draft June 16, 2022 09:31
@osyo-manga osyo-manga marked this pull request as ready for review July 4, 2022 09:02
@auto-assign auto-assign bot requested review from kodera123 and tknzk July 4, 2022 09:02
@osyo-manga osyo-manga removed request for tknzk and kodera123 July 7, 2022 02:57
@osyo-manga osyo-manga marked this pull request as draft April 19, 2023 01:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant