Skip to content

Allow bigint annotations in rails 4. #569

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 3 commits into from
Aug 28, 2018

Conversation

siegfault
Copy link
Contributor

@siegfault siegfault commented Jul 30, 2018

In rails 4, columns do not respond to bigint?. However, in both rails
4 and rails 5, columns do respond to sql_type. This way, annotations
should work in both versions.

Very open to suggestions in specs! Should I change mock_column's signature to mock_column(name, type, sql_type, options ={})?

If the backstory is helpful, bigint annotating was originally added in #515.

In rails 4, columns do not respond to `bigint?`. However, in both rails
4 and rails 5, columns do respond to `sql_type`. This way, annotations
should work in both versions.
`bigint(8)` is a bit redundant. Can just use bigint.
@siegfault
Copy link
Contributor Author

@ctran I would appreciate any suggestions on this one. Thanks!

@@ -362,7 +362,7 @@ def get_index_info(klass, options = {})
end

def get_col_type(col)
if col.respond_to?(:bigint?) && col.bigint?
if col.sql_type == 'bigint'
Copy link
Owner

Choose a reason for hiding this comment

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

There seems to be some logic behind bigint?.

I'd change it to:

if (col.respond_to?(:bigint?) && col.bigint?) || /\Abigint\b/.match?(col.sql_type)

This way we'll pick up any logic from activerecord if it's implemented.

Thanks for the PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Great call! Relying on the existing logic is much better than writing our own match.

@@ -183,7 +184,7 @@ def mock_column(name, type, options = {})
[
mock_column(:id, :integer),
mock_column(:integer, :integer, unsigned?: true),
mock_column(:bigint, :integer, unsigned?: true, bigint?: true),
mock_column(:bigint, :integer, unsigned?: true, sql_type: 'bigint'),
mock_column(:float, :float, unsigned?: true),
Copy link
Owner

Choose a reason for hiding this comment

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

Can we test both of these?

  • mock_column(:bigint, :integer, unsigned?: true, bigint?: true) # when bigint? is implemented
  • mock_column(:bigint, :bigint, unsigned?: true) # when it's not

When using rails 5, it should use the logic already defined by
ActiveRecord. When using rails <5, we should use very similar logic.
Add spec to test for both cases.
@siegfault
Copy link
Contributor Author

Thanks for the feedback @ctran! Updated with both changes

@ctran ctran merged commit 365b012 into ctran:develop Aug 28, 2018
@ctran
Copy link
Owner

ctran commented Aug 28, 2018

Thanks!!!

@siegfault siegfault deleted the bigint_support_for_rails4 branch August 28, 2018 16:23
mgpnd pushed a commit to mgpnd/annotate_models that referenced this pull request Feb 15, 2019
In rails 4, columns do not respond to `bigint?`.  However, in both rails
4 and rails 5, columns do respond to `sql_type`. This way, annotations
should work in both versions.
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.

2 participants