-
Notifications
You must be signed in to change notification settings - Fork 3
Get attribute values before type casting to fix enum attributes insert #3
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require "spec_helper" | ||
|
|
||
| class EnumTestRecord < ActiveRecord::Base | ||
| self.table_name = "enum_test_records" | ||
|
|
||
| if ActiveRecord.gem_version >= Gem::Version.new("7.0") | ||
| enum :foo, {bar: 2, baz: 3} | ||
| else | ||
| enum foo: {bar: 2, baz: 3} | ||
| end | ||
| end | ||
|
|
||
| # Regression coverage for the bug where `Session#normalize_records` calls | ||
| # `attributes_before_type_cast` on AR objects, returning the *enum label* | ||
| # (e.g. "bar") instead of the underlying database integer (e.g. 2). | ||
| RSpec.describe "StagingTable with enum attributes" do | ||
| shared_examples "preserves enum integer values" do | ||
| let(:enum_values) do | ||
| EnumTestRecord.defined_enums.fetch("foo").transform_values(&:to_i) | ||
| end | ||
|
|
||
| let(:session) do | ||
| StagingTable::Session.new(EnumTestRecord) | ||
| end | ||
|
|
||
| before do | ||
| ActiveRecord::Base.connection.create_table(EnumTestRecord.table_name, force: true) do |t| | ||
| t.string :name | ||
| t.string :email | ||
| t.integer :foo | ||
| t.timestamps null: true | ||
| end | ||
| EnumTestRecord.reset_column_information | ||
| end | ||
|
|
||
| after do | ||
| session.drop_table | ||
| ActiveRecord::Base.connection.drop_table(EnumTestRecord.table_name, if_exists: true) | ||
| end | ||
|
|
||
| it "stages the enum's database integer, not the label, when given AR objects" do | ||
| record = EnumTestRecord.new( | ||
| id: 1, | ||
| name: "Alice", | ||
| email: "alice@example.com", | ||
| foo: :bar | ||
| ) | ||
|
|
||
| session.create_table | ||
|
|
||
| expect { session.insert([record]) }.not_to raise_error | ||
|
|
||
| raw_value = session.staging_model.connection.select_value( | ||
| "SELECT foo FROM #{session.staging_model.table_name}" | ||
| ) | ||
|
|
||
| expect(Integer(raw_value)).to eq(enum_values.fetch("bar")) | ||
| end | ||
|
|
||
| it "round-trips an enum value from AR object through staging into the source table" do | ||
| original = EnumTestRecord.new( | ||
| id: 2, | ||
| name: "Bob", | ||
| email: "bob@example.com", | ||
| foo: :bar | ||
| ) | ||
|
|
||
| session.create_table | ||
| session.insert([original]) | ||
| result = session.transfer | ||
|
|
||
| expect(result.inserted).to eq(1) | ||
| expect(EnumTestRecord.count).to eq(1) | ||
| expect(EnumTestRecord.first.foo).to eq("bar") | ||
| end | ||
|
|
||
| it "stages the enum integer when given an ActiveRecord::Relation" do | ||
| EnumTestRecord.create!(name: "Carol", email: "carol@example.com", foo: :bar) | ||
| EnumTestRecord.create!(name: "Dave", email: "dave@example.com", foo: :baz) | ||
|
|
||
| session.create_table | ||
|
|
||
| expect { session.insert(EnumTestRecord.all) }.not_to raise_error | ||
|
|
||
| staged_values = session.staging_model.connection.select_values( | ||
| "SELECT foo FROM #{session.staging_model.table_name} ORDER BY foo" | ||
| ).map { |value| Integer(value) } | ||
|
|
||
| expect(staged_values).to eq([ | ||
| enum_values.fetch("bar"), | ||
| enum_values.fetch("baz") | ||
| ]) | ||
| end | ||
|
|
||
| it "still accepts plain hashes with the integer value" do | ||
| session.create_table | ||
|
|
||
| expect { | ||
| session.insert([{ | ||
| name: "Eve", | ||
| email: "eve@example.com", | ||
| foo: enum_values.fetch("bar") | ||
| }]) | ||
| }.not_to raise_error | ||
|
|
||
| raw_value = session.staging_model.connection.select_value( | ||
| "SELECT foo FROM #{session.staging_model.table_name}" | ||
| ) | ||
|
|
||
| expect(Integer(raw_value)).to eq(enum_values.fetch("bar")) | ||
| end | ||
| end | ||
|
|
||
| context "with PostgreSQL", :postgresql do | ||
| include_examples "preserves enum integer values" | ||
| end | ||
|
|
||
| context "with MySQL", :mysql do | ||
| include_examples "preserves enum integer values" | ||
| end | ||
|
|
||
| context "with SQLite", :sqlite do | ||
| include_examples "preserves enum integer values" | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.