-
-
Notifications
You must be signed in to change notification settings - Fork 263
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
remove_column should not be included in BulkChangeTable cop #110
Comments
Hi @connorshea, I'm an author of this cop. If you focus only on suppressing warnings, there is a way to define class AddAndRemoveColumnsToTable < ActiveRecord::Migration[6.0]
def up
change_table :table_name, bulk: true do |t|
t.remove :before_value
t.remove :after_value
t.jsonb :differences
end
end
def down
change_table :table_name, bulk: true do |t|
t.text :before_value
t.text :after_value
t.remove :differences
end
end
end But if you feel this way is redundant, I think you should not follow the Initially, I added this cop for telling us about how to use grouping alter statements, but I understand the opinion that it's a bit too assertive. |
Fair enough, thanks for clarifying 👍 |
Note that |
Hi @wata727 ! Now that Rails 6.1 released, I feel like this issue could be re-evaluated. Should a line like |
Follow up to rubocop#110 (comment). This PR fixes a false positive for `Rails/ReversibleMigration` when using `t.remove` with `type` option in Rails 6.1.
The [original link fields]( https://github.com/alphagov/govuk-coronavirus-content/blob/12bacb98cdfa9a96a8f6f344143be0be1361babd/content/coronavirus_landing_page.yml#L14-L15) are used to determine where the action link text in the header should wrap when the landing page is being viewed in mobile view. For example, at the moment the fields contain "Find out how to stay safe and help" and "prevent the spread". On a desktop this is displayed as one line "Find out how to stay safe and help prevent the spread", but on a mobile, it wraps in the expected pla ce. i.e. the use sees: "Find out how to stay safe and help" "prevent the spread" This commit provides two header text fields: `header_link_pre_wrap_text` and `header_link_post_wrap_text`. In the migration, I initially wanted to use (header_link_post_wrap_text omitted from the following examples to save space): ``` def change rename_column :coronavirus_pages, :header_link_text, :header_link_pre_wrap_text change_column :coronavirus_pages, :header_link_pre_wrap_text, :string end ``` However the linter threw a ‘Rails/BulkChangeTable: You can use change_table :coronavirus_pages, bulk: true to combine alter que ries.’ error… hence using `bulk: true`. ``` def change change_table :coronavirus_pages, bulk: true do |t| t.rename :header_link_text, :header_link_pre_wrap_text t.string :header_link_pre_wrap_text end end ``` This did not remove the header_link_text and added the header_link_pre_wrap_text. I decided to remove the colu mn instead: ``` def change change_table :coronavirus_pages, bulk: true do |t| t.remove :header_link_text t.string :header_link_pre_wrap_text end end ``` However the linter threw a `Rails/ReversibleMigration` error. I therefore used `up` and `down` methods to make the migration reversable, as [suggested by the author of the cop](https://gith ub.com/rubocop/rubocop-rails/issues/110#issuecomment-522461464). Annoyingly `remove_column` is usually reversible with a `type` but the bulk [change_table syntax](http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table) does not al low a type to be specified. Its a bit convoluted to appease the linter... it seems to have been raised as an issue to [Rubocop](https://github.com/rubocop/ rubocop-rails/issues/161) but oh well.
The [original link fields] are used to determine where the action link text in the header should wrap when the landing page is being viewed in mobile view. For example, at the moment the fields contain "Find out how to stay safe and help" and "prevent the spread". On a desktop this is displayed as one line "Find out how to stay safe and help prevent the spread", but on a mobile, it wraps in the expected place. i.e. the use sees: "Find out how to stay safe and help" "prevent the spread" This commit provides two header text fields: `header_link_pre_wrap_text` and `header_link_post_wrap_text`. In the migration, I initially wanted to use (header_link_post_wrap_text omitted from the following examples to save space): ``` def change rename_column :coronavirus_pages, :header_link_text, :header_link_pre_wrap_text change_column :coronavirus_pages, :header_link_pre_wrap_text, :string end ``` However the linter threw a: ``` Rails/BulkChangeTable: You can use change_table :coronavirus_pages, bulk: true to combine alter queries. ``` error… hence using `bulk: true`. ``` def change change_table :coronavirus_pages, bulk: true do |t| t.rename :header_link_text, :header_link_pre_wrap_text t.string :header_link_pre_wrap_text end end ``` This did not remove the header_link_text and added the header_link_pre_wrap_text. I decided to remove the column instead: ``` def change change_table :coronavirus_pages, bulk: true do |t| t.remove :header_link_text t.string :header_link_pre_wrap_text end end ``` However the linter threw a `Rails/ReversibleMigration` error. I therefore used `up` and `down` methods to make the migration reversable, as [suggested by the author of the cop]. Annoyingly `remove_column` is usually reversible with a `type` but the bulk [change_table syntax] does not allow a type to be specified. Its a bit convoluted to appease the linter... it seems to have been raised as an issue to [Rubocop] but oh well. [original link fields]: https://github.com/alphagov/govuk-coronavirus-content/blob/12bacb98cdfa9a96a8f6f344143be0be1361babd/content/coronavirus_landing_page.yml#L14-L15 [suggested by the author of the cop]: rubocop/rubocop-rails#110 (comment) [change_table syntax]: http://apidock.com/rails/ActiveRecord/ConnectionAdapters/SchemaStatements/change_table [Rubocop]: rubocop/rubocop-rails#161
Disables Cop BulkChangeTable rubocop/rubocop-rails#110
Disables Cop BulkChangeTable rubocop/rubocop-rails#110
@wata727 Can you sort out the current situation and work on this? |
I believe this issue is already ready to be closed. The |
By the way, this list of combinable transformations seems a bit outdated. Perhaps we can add the list below:
References
|
@wata727 Superb! I'll close this issue. OTOH, If you'd like, please open a PR as the results of your investigation if necessary. Thank you! |
Rubocop warns about multiple uses of
remove_column
in the same migration, saying it should be changed to use achange_table, bulk: true
. However,change_table
has no equivalent means of removing a column in a way which can be reversed. So when you try to fix the warning, a new warning for a non-reversible migration shows up.Expected behavior
I would expect that multiple uses of
remove_column
would not cause a warning fromRails/BulkChangeTable
, because remove_column can't be converted tochange_table
in a way that's reversible.Actual behavior
Or, if you change it to use
change_table, bulk: true
:Steps to reproduce the problem
Rails/BulkChangeTable
andRails/ReversibleMigration
copsbundle exec rubocop
, see that there are violations forRails/BulkChangeTable
.Rails/ReversibleMigration
(see the docs),t.remove
is an alias ofremove_columns
and it doesn't seem like there's any way to specify a column type. So it's not possible (as far as I can tell) to reversibly usechange_table, bulk: true
when removing columns.RuboCop version
The text was updated successfully, but these errors were encountered: