-
Notifications
You must be signed in to change notification settings - Fork 35
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
Prettify column defaults #44
Merged
Merged
Conversation
This file contains 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
Some notes from debugging this. The default value as a String gets passed from ActiveRecord internally itself. # The column object we get from AR has a default value that's String
# [4] pry(#<AnnotateRb::ModelAnnotator::ColumnAnnotation::AttributesBuilder>)> rcol = @column.instance_variable_get("@column")
# => #<ActiveRecord::ConnectionAdapters::PostgreSQL::Column:0x000000010e19ea60
# @collation=nil,
# @comment=nil,
# @default="0",
# @default_function=nil,
# @generated="",
# @name="duration_minutes",
# @null=true,
# @serial=nil,
# @sql_type_metadata=
# #<ActiveRecord::ConnectionAdapters::SqlTypeMetadata:0x000000010e19f140
# @limit=4,
# @precision=nil,
# @scale=nil,
# @sql_type="integer",
# @type=:integer>>
# Internally, it also is a String
# [4] pry(#<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter>)> field
# => ["duration_minutes", "integer", "0", false, 23, -1, nil, nil, ""] |
As a side note, I found out that column names should actually be strings. There are some tests that use Will handle in another PR. |
drwl
added a commit
that referenced
this pull request
Jun 17, 2023
Columns that ActiveRecord gives us has String `#names`. Some tests pass in a Symbol name into `mock_column` which is not accurate, so this PR corrects that. Follows up on: #44 (comment)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Fixes #45.
When I extracted out column annotation building [1], I changed the way the default value was read.
Model#column_defaults
has the column default values that are casted appropriately, where asColumn#default
is uncasted and straight from the database, most of the time as a String. It's important to note that the various adapters (sqlite, mysql, postgres) implement the columns differently [2], especially as it relates to Column#default and Model#column_default values.This PR restores parity with the original gem on how defaults are handled. It also adds tests for the various adapters, because the implementations vary under the hood.
As an example, the mysql adapter sets the value for an integer field without a default to be
0
inModel#column_defaults
, where as sqlite and postgres set it to benil
.[1] https://github.com/drwl/annotaterb/pull/28/files#diff-92c15977800c36cb911c1d7971ede0da49dae93b0c9e555777a5ca67ee81d93dR12-R14
[2] https://gist.github.com/drwl/ef274a763b773d132abfdb95d32347d2