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

Add Rails/ReversibleMigration cop #3854

Merged
merged 2 commits into from
Jan 6, 2017

Conversation

sue445
Copy link
Contributor

@sue445 sue445 commented Jan 3, 2017

This cop checks whether the #change method of the migration file is reversible.

Example

# bad
def change
  change_table :users do |t|
    t.column :name, :string
  end
end

# good
def change
  create_table :users do |t|
    t.string :name
  end
end

# good
def change
  reversible do |dir|
    change_table :users do |t|
      dir.up do
        t.column :name, :string
      end

      dir.down do
        t.remove :name
      end
    end
  end
end

drop_table

# bad
def change
  drop_table :users
end

# good
def change
  drop_table :users do |t|
    t.string :name
  end
end

change_column_default

# bad
def change
  change_column_default(:suppliers, :qualification, 'new')
end

# good
def change
  change_column_default(:posts, :state, from: nil, to: "draft")
end

remove_column

# bad
def change
  remove_column(:suppliers, :qualification)
end

# good
def change
  remove_column(:suppliers, :qualification, :string)
end

remove_foreign_key

# bad
def change
  remove_foreign_key :accounts, column: :owner_id
end

# good
def change
  remove_foreign_key :accounts, :branches
end

Note

reversible migration commands are below.
http://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html


Before submitting the PR make sure the following are checked:

  • Wrote good commit messages.
  • Commit message starts with [Fix #issue-number] (if the related issue exists).
  • Used the same coding conventions as the rest of the project.
  • Feature branch is up-to-date with master (if not - rebase it).
  • Squashed related commits together.
  • Added tests.
  • Added an entry to the Changelog if the new code introduces user-observable changes. See changelog entry format.
  • All tests are passing.
  • The new code doesn't generate RuboCop offenses.
  • The PR relates to only one subject with a clear title
    and description in grammatically correct, complete sentences.
  • Updated cop documentation with rake generate_cops_documentation (required only when you've added a new cop or changed the configuration/documentation of an existing cop).

@sue445 sue445 force-pushed the rails-reversible-migration branch from ebab33e to 23e3290 Compare January 3, 2017 15:35
@bbatsov
Copy link
Collaborator

bbatsov commented Jan 3, 2017

That's one really handy cop! I'll review the code a bit later. It'd be nice if you wrote a matching rule for the Rails Style Guide.

@bbatsov
Copy link
Collaborator

bbatsov commented Jan 3, 2017

Seems you forgot to update the documentation.

@sue445 sue445 force-pushed the rails-reversible-migration branch 3 times, most recently from d023926 to 6bce3f7 Compare January 4, 2017 16:19
@sue445
Copy link
Contributor Author

sue445 commented Jan 4, 2017

@bbatsov I updated doc and CI is all green 💚

It'd be nice if you wrote a matching rule for the Rails Style Guide.

I see, I'll write a style guide later. I temporarily make this PR to WIP

@sue445 sue445 changed the title Add Rails/ReversibleMigration cop [WIP] Add Rails/ReversibleMigration cop Jan 4, 2017
@sue445 sue445 force-pushed the rails-reversible-migration branch from 6bce3f7 to 80753d0 Compare January 5, 2017 15:41
@sue445 sue445 changed the title [WIP] Add Rails/ReversibleMigration cop Add Rails/ReversibleMigration cop Jan 5, 2017
@sue445
Copy link
Contributor Author

sue445 commented Jan 5, 2017

@bbatsov I wrote style guide at rubocop/rails-style-guide#211 , and removed WIP. Please check these 🙇

@bbatsov bbatsov merged commit 9984480 into rubocop:master Jan 6, 2017
@sue445 sue445 deleted the rails-reversible-migration branch January 6, 2017 09:14
@sue445
Copy link
Contributor Author

sue445 commented Jan 6, 2017

🎉

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.

2 participants