From ba12d0f6664e5fb17510e10cdf055bda57600d76 Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Tue, 13 Feb 2024 18:00:42 -0500 Subject: [PATCH 1/6] Remove association overrides --- lib/temporal_tables/temporal_class.rb | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/temporal_tables/temporal_class.rb b/lib/temporal_tables/temporal_class.rb index 0c7522e..cc7f49a 100644 --- a/lib/temporal_tables/temporal_class.rb +++ b/lib/temporal_tables/temporal_class.rb @@ -42,8 +42,7 @@ def self.temporalize_associations! # rubocop:disable Metrics/MethodLength, Metri association.macro, association.name, **association.options.merge( class_name: clazz.name, - foreign_key: association.foreign_key, - primary_key: clazz.orig_class.primary_key + foreign_key: association.foreign_key ) ) end From 976a042f770f2c44fbb2265954ee32916a3c578b Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Wed, 14 Feb 2024 15:48:07 -0500 Subject: [PATCH 2/6] Add tests --- spec/basic_history_spec.rb | 16 ++++++++++++++++ spec/internal/app/models/hamster.rb | 7 +++++++ spec/internal/app/models/hamster_wheel.rb | 5 +++++ spec/internal/db/schema.rb | 18 ++++++++++++++++++ 4 files changed, 46 insertions(+) create mode 100644 spec/internal/app/models/hamster.rb create mode 100644 spec/internal/app/models/hamster_wheel.rb diff --git a/spec/basic_history_spec.rb b/spec/basic_history_spec.rb index b323c0c..fdb3e4c 100644 --- a/spec/basic_history_spec.rb +++ b/spec/basic_history_spec.rb @@ -264,3 +264,19 @@ end end end + +describe Hamster do + context 'when a hamster and wheel exist' do + let(:hamster) { Hamster.create name: 'Fluffy' } + let(:wheel) { HamsterWheel.create hamster: hamster } + + it 'can create instance of class with nested class name with history entries' do + expect(hamster).not_to be_nil + expect(wheel).not_to be_nil + expect(hamster.hamster_wheel).not_to be_nil + hamster_history = Hamster.history.at(Time.now.utc).first + expect(hamster_history).not_to be_nil + expect(hamster_history.hamster_wheel).not_to be_nil + end + end +end diff --git a/spec/internal/app/models/hamster.rb b/spec/internal/app/models/hamster.rb new file mode 100644 index 0000000..932ce8d --- /dev/null +++ b/spec/internal/app/models/hamster.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +class Hamster < ActiveRecord::Base + self.primary_key = :uuid + + has_one :hamster_wheel, foreign_key: :hamster_uuid, inverse_of: :hamster +end diff --git a/spec/internal/app/models/hamster_wheel.rb b/spec/internal/app/models/hamster_wheel.rb new file mode 100644 index 0000000..5f98e44 --- /dev/null +++ b/spec/internal/app/models/hamster_wheel.rb @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class HamsterWheel < ActiveRecord::Base + belongs_to :hamster, foreign_key: :hamster_uuid, primary_key: :uuid, inverse_of: :hamster_wheel +end diff --git a/spec/internal/db/schema.rb b/spec/internal/db/schema.rb index 0e577ab..5e4baff 100644 --- a/spec/internal/db/schema.rb +++ b/spec/internal/db/schema.rb @@ -69,4 +69,22 @@ t.belongs_to :bird, type: (postgres ? :uuid : :integer) t.integer :height end + + if postgres + create_table :hamsters, id: false do |t| + t.column :uuid, :uuid, default: 'gen_random_uuid()' + t.string :name + end + execute 'ALTER TABLE hamsters ADD PRIMARY KEY (uuid);' + add_temporal_table :hamsters + else + create_table :hamsters, primary_key: :uuid, temporal: true do |t| + t.string :name + end + end + + create_table :hamster_wheels, id: (postgres ? :uuid : :integer), temporal: true do |t| + t.column :hamster_uuid, (postgres ? :uuid : :integer), null: false + t.foreign_key :hamsters, column: :hamster_uuid, primary_key: :uuid + end end From 3d66abf0af6874246f08954c3c41422704b5567d Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Wed, 14 Feb 2024 15:52:06 -0500 Subject: [PATCH 3/6] Fix column type --- spec/internal/db/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/internal/db/schema.rb b/spec/internal/db/schema.rb index 5e4baff..b77991f 100644 --- a/spec/internal/db/schema.rb +++ b/spec/internal/db/schema.rb @@ -84,7 +84,7 @@ end create_table :hamster_wheels, id: (postgres ? :uuid : :integer), temporal: true do |t| - t.column :hamster_uuid, (postgres ? :uuid : :integer), null: false + t.column :hamster_uuid, (postgres ? :uuid : :bigint), null: false t.foreign_key :hamsters, column: :hamster_uuid, primary_key: :uuid end end From 6b69b26f7cb9d1afdbdb237e482e01dc1fc49d49 Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Wed, 14 Feb 2024 15:58:54 -0500 Subject: [PATCH 4/6] Bump version --- lib/temporal_tables/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/temporal_tables/version.rb b/lib/temporal_tables/version.rb index 4c8a7a6..cdee7f5 100644 --- a/lib/temporal_tables/version.rb +++ b/lib/temporal_tables/version.rb @@ -1,5 +1,5 @@ # frozen_string_literal: true module TemporalTables - VERSION = '3.0.0' + VERSION = '3.0.1' end From 74e0a584733b1dcc6eb6471dab82e00ff8040148 Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Tue, 20 Feb 2024 11:10:44 -0500 Subject: [PATCH 5/6] Update test context string Co-authored-by: Brent Kroeker --- spec/basic_history_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/basic_history_spec.rb b/spec/basic_history_spec.rb index fdb3e4c..dcf4465 100644 --- a/spec/basic_history_spec.rb +++ b/spec/basic_history_spec.rb @@ -266,7 +266,7 @@ end describe Hamster do - context 'when a hamster and wheel exist' do + context 'with tables that have non-default primary key names' do let(:hamster) { Hamster.create name: 'Fluffy' } let(:wheel) { HamsterWheel.create hamster: hamster } From d9d580dddd0aa2c4d2e92b9b32ec3a98fe2b5422 Mon Sep 17 00:00:00 2001 From: Evan Raffel Date: Tue, 20 Feb 2024 11:19:48 -0500 Subject: [PATCH 6/6] update lockfiles --- gemfiles/Gemfile.6.1.mysql.lock | 4 ++-- gemfiles/Gemfile.6.1.pg.lock | 4 ++-- gemfiles/Gemfile.7.0.mysql.lock | 4 ++-- gemfiles/Gemfile.7.0.pg.lock | 4 ++-- gemfiles/Gemfile.7.1.mysql.lock | 4 ++-- gemfiles/Gemfile.7.1.pg.lock | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/gemfiles/Gemfile.6.1.mysql.lock b/gemfiles/Gemfile.6.1.mysql.lock index 0b6106a..8f28c11 100644 --- a/gemfiles/Gemfile.6.1.mysql.lock +++ b/gemfiles/Gemfile.6.1.mysql.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/ diff --git a/gemfiles/Gemfile.6.1.pg.lock b/gemfiles/Gemfile.6.1.pg.lock index 366786a..4a4593a 100644 --- a/gemfiles/Gemfile.6.1.pg.lock +++ b/gemfiles/Gemfile.6.1.pg.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/ diff --git a/gemfiles/Gemfile.7.0.mysql.lock b/gemfiles/Gemfile.7.0.mysql.lock index bddde12..06b63c1 100644 --- a/gemfiles/Gemfile.7.0.mysql.lock +++ b/gemfiles/Gemfile.7.0.mysql.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/ diff --git a/gemfiles/Gemfile.7.0.pg.lock b/gemfiles/Gemfile.7.0.pg.lock index 9652c3a..a028873 100644 --- a/gemfiles/Gemfile.7.0.pg.lock +++ b/gemfiles/Gemfile.7.0.pg.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/ diff --git a/gemfiles/Gemfile.7.1.mysql.lock b/gemfiles/Gemfile.7.1.mysql.lock index 7740ca1..f8c76f1 100644 --- a/gemfiles/Gemfile.7.1.mysql.lock +++ b/gemfiles/Gemfile.7.1.mysql.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/ diff --git a/gemfiles/Gemfile.7.1.pg.lock b/gemfiles/Gemfile.7.1.pg.lock index 8b8cb55..0b2506e 100644 --- a/gemfiles/Gemfile.7.1.pg.lock +++ b/gemfiles/Gemfile.7.1.pg.lock @@ -1,8 +1,8 @@ PATH remote: .. specs: - temporal_tables (3.0.0) - rails (>= 6.0, < 7.2) + temporal_tables (3.0.1) + rails (>= 6.1, < 7.2) GEM remote: https://rubygems.org/