Rails now validates options used in migration functions
source link: https://blog.saeloun.com/2022/11/09/validate-options-in-migration-files
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
There are some aspects of Rails that seem like they shouldn’t have existed in the first place! One such neglected piece of code is the fact that Rails silently ignores options that are not valid for a given migration function.
Before
For example, the following migration would have worked fine in Rails 7.0 or lower:
# db/migrate/20221018030445_add_gender_to_patients.rb
class AddGenderToPatients < ActiveRecord::Migration[7.0]
def change
add_column :patients, :gender, :integer, default: 10, hola: :bola
end
end
Though the option “hola” is completely invalid for the “add_column” function, Rails would have happily accepted it and created the migration file. This is because the “add_column” function accepts a variable number of options, and Rails would have simply ignored the invalid option.
This is a problem because it can lead to a lot of confusion. For example, if one misspells an option, Rails would not raise an error, and the knowledge of the option not being applied would not have been communicated back to the user. This can lead to wasted debugging time or even potential security risks among other issues.
After
Thanks to this PR Rails now raises an error if an invalid option is passed to a migration function. This is a much better experience for the user, as they will be immediately notified of the error and can fix it.
It must be noted that validation of the options will only be applied for new migrations that are created after the upgrade to Rails 7.1. For existing migrations, the validation will not be applied, and the migration will continue to work as before.
Existing migrations are identified by the version of the migration class. For example, the migration class in the example above is “ActiveRecord::Migration[7.0]”, where the version is “7.0”. If the migration class is “ActiveRecord::Migration[7.1]”, where the version is “7.1”, then the validation will be applied.
Let’s change the migration class to “ActiveRecord::Migration[7.1]” and run the migration again:
class AddGenderToPatients < ActiveRecord::Migration[7.1]
def change
add_column :patients, :gender, :integer, default: 10, hola: :bola
end
end
To redo a migration simply run:
➜ rails db:migrate:redo VERSION=20221018030445
Now an error is raised indicating the use of an invalid migration option.
== 20221018030445 AddGenderToPatients: migrating ==============================
-- add_column(:patients, :gender, :integer, {:default=>10, :hola=>:bola})
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Unknown key: :hola. Valid keys are: :limit, :precision, :scale, :default, :null, :collation, :comment, :primary_key, :if_exists, :if_not_exists, :array, :using, :cast_as, :as, :type, :enum_type, :stored
/Users/swaathi/code/work/edge_rails/db/migrate/20221018030445_add_gender_to_patients.rb:3:in `change'
Caused by:
ArgumentError: Unknown key: :hola. Valid keys are: :limit, :precision, :scale, :default, :null, :collation, :comment, :primary_key, :if_exists, :if_not_exists, :array, :using, :cast_as, :as, :type, :enum_type, :stored
/Users/swaathi/code/work/edge_rails/db/migrate/20221018030445_add_gender_to_patients.rb:3:in `change'
Tasks: TOP => db:migrate:redo
(See full trace by running task with --trace)
The same validation is also applied to the “create_table” function. The valid migration options as of now are,
- :limit
- :precision
- :scale
- :default
- :null
- :collation
- :comment
- :primary_key
- :if_exists
- :if_not_exists
Right now the check is a complete manual effort and is not automated. This means that if a new option is added to a migration function, the validation will not be applied until the Rails team manually adds it to the list of valid options. This is a limitation of the current implementation, and we hope it improves in the future.
Recommend
-
69
Reads, parses and validates CSV files using streams - offdev/csv
-
232
README.md
-
27
README.md govalidate
-
10
Building your own ActiveRecord validation macros with validates_each A common task when writing your own Rails applications using ActiveRecord is creating your own validations for your models. While it’s...
-
4
validates_presence_of 不适用于 boolean 字段 validates_presence_of 广泛的应用于 Rails model 的验证,但它不能应用于 boolean 字段。因为 false 在 validates_presence_of 眼中,是 invalid,无法通过验证! 定义 confirmed 字...
-
5
Facebook validates armchair experts with a shiny badge Your online participation award ...
-
4
SpiceDB SpiceDB is a Zanzibar-inspired database that stores, computes, and validates application permissions. Developers create a schema that models their permissions re...
-
3
JSONverifyA web tool that validates and format JSON objects
-
6
Let's do launch — Rocket Report: NASA validates new engine design; Chinese firm tests mini Starship "It's time for us to start launching them right here at home."...
-
4
Pokémon GO maker says Apple Vision Pro validates their work as it closes studio and cancels games Zac Hall...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK