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

Reduce update queries if pk is uuid #147

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 19 additions & 4 deletions lib/activerecord-bitemporal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ def bitemporal_id_key
'bitemporal_id'
end

def bitemporal_id_key_uuid?
column_for_attribute(bitemporal_id_key).type == :uuid
end

# Override ActiveRecord::Core::ClassMethods#cached_find_by_statement
# `.find_by` not use caching
def cached_find_by_statement(key, &block)
Expand Down Expand Up @@ -141,10 +145,21 @@ def bitemporalize(
}
end

after_create do
# MEMO: #update_columns is not call #_update_row (and validations, callbacks)
update_columns(bitemporal_id_key => swapped_id) unless send(bitemporal_id_key)
swap_id!(without_clear_changes_information: true)
around_create do |_, block|
if self.class.bitemporal_id_key_uuid? && public_send(bitemporal_id_key).nil?
# When bitamporal_id is uuid, skip update query after create for optimization.
uuid = id || SecureRandom.uuid
assign_attributes(id: uuid, bitemporal_id_key => uuid)

block.call
else
block.call

# MEMO: #update_columns is not call #_update_row (and validations, callbacks)
update_columns(bitemporal_id_key => swapped_id) unless send(bitemporal_id_key)
swap_id!(without_clear_changes_information: true)
end

@previously_force_updated = false
end

Expand Down
39 changes: 39 additions & 0 deletions spec/activerecord-bitemporal/bitemporal_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,45 @@
it { is_expected.to have_attributes(created_at: created_at, transaction_from: created_at) }
end
end

context 'bitemporal_id_key is uuid' do
context 'on create' do
context "with `bitemporal_id`" do
it "swaps id with bitemporal_id" do
previous_record = Plan.create!(name: "Jane", valid_from: "2019/01/01", valid_to: "2019/04/01")
new_record = Plan.create!(bitemporal_id: previous_record.id, name: 'Tom', valid_from: "2019/04/01", valid_to: "2019/10/01" )

expect(new_record).to have_attributes(
bitemporal_id: previous_record.id,
previous_changes: include(
"id" => [nil, new_record.swapped_id],
"valid_from" => [nil, be_present],
"valid_to" => [nil, "2019/10/01".in_time_zone],
"name" => [nil, "Tom"]
)
)
end
end

context "without `bitemporal_id`" do
it "updates bitemporal_id by id" do
new_record = Plan.create!(name: 'Tom')

expect(new_record).to have_attributes(
bitemporal_id: new_record.id,
swapped_id: new_record.id,
previous_changes: include(
"id" => [nil, new_record.id],
"valid_from" => [nil, be_present],
"valid_to" => [nil, ActiveRecord::Bitemporal::DEFAULT_VALID_TO],
"name" => [nil, "Tom"]
),
previously_force_updated?: false
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we test here that the swapped_id is equal to the bitemporal_id?

end
end
end
end
end

describe ".find" do
Expand Down
18 changes: 18 additions & 0 deletions spec/schema.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

ActiveRecord::Schema.define(version: 1) do
enable_extension "pgcrypto"

create_table :companies, force: true do |t|
t.string :name

Expand Down Expand Up @@ -82,6 +84,19 @@

t.timestamps
end

create_table :plans, force: true, id: :uuid do |t|
t.string :name
t.uuid :bitemporal_id

t.datetime :valid_from, precision: 6
t.datetime :valid_to, precision: 6
t.datetime :deleted_at, precision: 6
t.datetime :transaction_from, precision: 6
t.datetime :transaction_to, precision: 6

t.timestamps
end
end

class Company < ActiveRecord::Base
Expand Down Expand Up @@ -151,3 +166,6 @@ class AddressWithoutBitemporal < ActiveRecord::Base
belongs_to :employee, foreign_key: :employee_id
end

class Plan < ActiveRecord::Base
include ActiveRecord::Bitemporal
end