-
-
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
Rails/BulkChangeTable and remove_column #161
Comments
Maybe you can avoid this offense by writing as follows: class ChangeACoupleOfColumnsInThing < ActiveRecord::Migration[5.2]
def change
reversible do |dir|
change_table :things, bulk: true do |t|
dir.up do
t.remove :column_one
t.column :column_two, :string, null: false, default: ''
end
dir.down do
t.boolean :column_one # , null: ..., default: ...
t.remove :column_two
end
end
end
end
end But if the time to execute the original two ALTER queries is small enough, this change will be nonsense... Unfortunately, Depending on the actual environment, I recommend that you select how to write. |
I don't understand how this Cop can improve my code, though. |
んー So this cop can potentially make some migrations faster for large databases, while introducing some awkwardness in other cases. I don't know if, with this current behavior, this cop should be activated by default. But it is obviously not to me to choose. |
We're in the process of moving all Rails-related functionality to a standalone library (gem) named |
@koic, would you mind moving this issue to the rubocop/rails tracker? I stumbled on this while updating and I think it's an issue that needs to be addressed. Also see: #48 (comment) I can also open a new issue there, but I would prefer the original issue with the context be moved if you don't mind. |
@louim Thanks for the mention. I transferred this issue to rubocop-hq/rubocop-rails repo. |
One way to handle the case could be like it was done in #48 and remove the |
Same goes for |
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
Add type
|
This code would trigger
Rails/BulkChangeTable
The problem is that this:
Will raise
RailsReversibleMigration
.Version:
The text was updated successfully, but these errors were encountered: