-
Notifications
You must be signed in to change notification settings - Fork 1
fix: normalize default values in schema for ruby 4.0 and rails 8 compatibility #252
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
Open
matthv
wants to merge
1
commit into
main
Choose a base branch
from
fix/normalize-default-value
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,4 @@ | ||
| class User < ApplicationRecord | ||
| enum :status, { inactive: 0, active: 1, archived: 2 } | ||
| enum :role, { user: 0, admin: 1, moderator: 2 } | ||
| end |
8 changes: 8 additions & 0 deletions
8
packages/_examples/demo/db/migrate/20260114100000_add_defaults_to_users.rb
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,8 @@ | ||
| class AddDefaultsToUsers < ActiveRecord::Migration[8.0] | ||
| def change | ||
| add_column :users, :active, :boolean, default: false | ||
| add_column :users, :verified, :boolean, default: true | ||
| add_column :users, :status, :integer, default: 0 | ||
| add_column :users, :role, :integer, default: 1 | ||
| end | ||
| end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 |
|---|---|---|
|
|
@@ -37,3 +37,5 @@ | |
| /db/*.sqlite3 | ||
| /db/*.sqlite3-journal | ||
| /db/*.sqlite3-[0-9]* | ||
|
|
||
| .forestadmin-rpc-schema.json | ||
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
4 changes: 4 additions & 0 deletions
4
packages/forest_admin_datasource_active_record/spec/dummy/app/models/test_default.rb
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,4 @@ | ||
| class TestDefault < ApplicationRecord | ||
| enum :status, { inactive: 0, active: 1, archived: 2 } | ||
| enum :priority, { low: 0, medium: 1, high: 2 } | ||
| end |
13 changes: 13 additions & 0 deletions
13
...min_datasource_active_record/spec/dummy/db/migrate/20260113130000_create_test_defaults.rb
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,13 @@ | ||
| class CreateTestDefaults < ActiveRecord::Migration[7.0] | ||
| def change | ||
| create_table :test_defaults do |t| | ||
| t.boolean :active, default: false | ||
| t.boolean :verified, default: true | ||
| t.integer :status, default: 0 | ||
| t.integer :priority, default: 1 | ||
| t.string :name | ||
|
|
||
| t.timestamps | ||
| end | ||
| end | ||
| end |
87 changes: 87 additions & 0 deletions
87
...ecord/spec/lib/forest_admin_datasource_active_record/parser/column_default_values_spec.rb
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,87 @@ | ||
| require 'spec_helper' | ||
|
|
||
| module ForestAdminDatasourceActiveRecord | ||
| module Parser | ||
| describe Column do | ||
| include described_class | ||
|
|
||
| let(:datasource) { Datasource.new({ adapter: 'sqlite3', database: 'db/database.db' }) } | ||
| let(:collection) { ForestAdminDatasourceActiveRecord::Collection.new(datasource, TestDefault) } | ||
|
|
||
| before do | ||
| # Run the migration to create the test_defaults table | ||
| unless ActiveRecord::Base.connection.table_exists?(:test_defaults) | ||
| ActiveRecord::Migration.suppress_messages do | ||
| CreateTestDefaults.migrate(:up) | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'normalize_default_value' do | ||
| context 'with boolean fields' do | ||
| it 'normalizes false default value' do | ||
| column = TestDefault.columns.find { |c| c.name == 'active' } | ||
| result = normalize_default_value(column) | ||
|
|
||
| expect(result).to be(false) | ||
| expect(result).to be_a(FalseClass) | ||
| end | ||
|
|
||
| it 'normalizes true default value' do | ||
| column = TestDefault.columns.find { |c| c.name == 'verified' } | ||
| result = normalize_default_value(column) | ||
|
|
||
| expect(result).to be(true) | ||
| expect(result).to be_a(TrueClass) | ||
| end | ||
| end | ||
|
|
||
| context 'with integer/enum fields' do | ||
| it 'normalizes integer default value to 0' do | ||
| column = TestDefault.columns.find { |c| c.name == 'status' } | ||
| result = normalize_default_value(column) | ||
|
|
||
| expect(result).to eq(0) | ||
| expect(result).to be_a(Integer) | ||
| end | ||
|
|
||
| it 'normalizes integer default value to 1' do | ||
| column = TestDefault.columns.find { |c| c.name == 'priority' } | ||
| result = normalize_default_value(column) | ||
|
|
||
| expect(result).to eq(1) | ||
| expect(result).to be_a(Integer) | ||
| end | ||
| end | ||
|
|
||
| context 'with nil default' do | ||
| it 'returns the original value for string with no default' do | ||
| column = TestDefault.columns.find { |c| c.name == 'name' } | ||
|
|
||
| # name has no default, so column.default is nil | ||
| # In the collection, we check nil before calling normalize | ||
| expect(column.default).to be_nil | ||
| end | ||
| end | ||
| end | ||
|
|
||
| describe 'integration with real columns' do | ||
| it 'handles all default value types correctly' do | ||
| # Test that actual column defaults are normalized properly | ||
| columns = TestDefault.columns | ||
|
|
||
| active = columns.find { |c| c.name == 'active' } | ||
| verified = columns.find { |c| c.name == 'verified' } | ||
| status = columns.find { |c| c.name == 'status' } | ||
| priority = columns.find { |c| c.name == 'priority' } | ||
|
|
||
| # All these should be properly normalized | ||
| expect(normalize_default_value(active)).to be(false) | ||
| expect(normalize_default_value(verified)).to be(true) | ||
| expect(normalize_default_value(status)).to eq(0) | ||
| expect(normalize_default_value(priority)).to eq(1) | ||
| end | ||
| end | ||
| end | ||
| 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.
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.
Function with high complexity (count = 5): normalize_default_value [qlty:function-complexity]