Skip to content
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 8 commits into from
Jun 15, 2023
Merged

Prettify column defaults #44

merged 8 commits into from
Jun 15, 2023

Conversation

drwl
Copy link
Owner

@drwl drwl commented Jun 12, 2023

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 as Column#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 in Model#column_defaults, where as sqlite and postgres set it to be nil.

[1] https://github.com/drwl/annotaterb/pull/28/files#diff-92c15977800c36cb911c1d7971ede0da49dae93b0c9e555777a5ca67ee81d93dR12-R14

[2] https://gist.github.com/drwl/ef274a763b773d132abfdb95d32347d2

@drwl
Copy link
Owner Author

drwl commented Jun 12, 2023

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, ""]

@drwl
Copy link
Owner Author

drwl commented Jun 13, 2023

As a side note, I found out that column names should actually be strings. There are some tests that use mock_column(:some_symbol, ...) and this leads to incorrect test setup + expected output.

Will handle in another PR.

@drwl drwl merged commit 7e2e18e into main Jun 15, 2023
@drwl drwl deleted the drwl/prettify-col-defaults branch June 15, 2023 18:35
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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Column defaults change in migration
1 participant