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 new Rails/CreateTableWithTimestamps cop #5077

Merged

Conversation

wata727
Copy link
Contributor

@wata727 wata727 commented Nov 20, 2017

Hi there,

The other day I made a mistake in forgetting to add timestamps when creating a new table :(
So, I want to prevent such simple mistakes by using RuboCop.

This cop checks the migration for which timestamps are not included when creating a new table. In many cases, timestamps are useful information and should be added.

WDYT?


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(rake spec) are passing.
  • The new code doesn't generate RuboCop offenses that are checked by rake internal_investigation.
  • 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).

@wata727 wata727 force-pushed the add_rails_create_table_with_timestamps branch 2 times, most recently from 544d810 to 848d34c Compare November 20, 2017 14:01
@mikegee
Copy link
Contributor

mikegee commented Nov 20, 2017

This seems like a good idea. Here are some questions to consider:

  • Should this cop be restricted to db/migrations to avoid false positives?
  • Should this cop allow manually declaring updated_at and created_at (because timestamps does not configure a default value, for instance)?
  • Should manually declaring only one of created_at or updated_at satisfy this cop?

I'm ok with answering "no" to all of those, just asking ...


def on_send(node)
return unless create_table_call?(node)
ancestor = node.ancestors.first
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we can use node.parent instead of ancestors.first.

(block
(send nil? :create_table ...)
(args (arg _var))
begin)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The last begin matcher is not correct. This pattern does not match empty blocks or blocks that have one statement only.
For example, this cop should add offenses for the following code, but it does not add:

create_table :users do |t|
end


create_table :users do |t|
  t.string :name
end

Because the ASTs for this cases don't have begin node.

$ ruby-parse test.rb
(begin
  (block
    (send nil :create_table
      (sym :users))
    (args
      (procarg0 :t)) nil)
  (block
    (send nil :create_table
      (sym :users))
    (args
      (procarg0 :t))
    (send
      (lvar :t) :string
      (sym :name))))

ancestor = node.ancestors.first

if create_table_with_block?(ancestor)
unless ancestor.body.child_nodes.any? { |child| timestamps?(child) }
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

begin)
PATTERN

def_node_matcher :create_table_call?, <<-PATTERN
Copy link
Collaborator

@Drenmi Drenmi Nov 20, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This use of a node matcher is a bit gratuitous. You can just use:

node.command?(:create)

on send nodes anywhere. 🙂

@wata727
Copy link
Contributor Author

wata727 commented Nov 21, 2017

@mikegee Thanks for your questions.

Should this cop be restricted to db/migrations to avoid false positives?

Yes. That is a good idea. I will fix config/default.yml for restricting to db/migrates/*.rb

Should this cop allow manually declaring updated_at and created_at (because timestamps does not configure a default value, for instance)?

Yes. If they are declared manually, this cop should not add offenses. I will fix it.

Should manually declaring only one of created_at or updated_at satisfy this cop?

Yes. If a user explicitly declared only one of them, I think we can judge that a user has not forgotten to add timestamps. I will also fix this.

This cop checks the migration for which timestamps are not included
when creating a new table.
In many cases, timestamps are useful information and should be added.
@wata727 wata727 force-pushed the add_rails_create_table_with_timestamps branch from 2335163 to f6e0936 Compare November 23, 2017 06:43
@bbatsov bbatsov merged commit 7769d50 into rubocop:master Nov 25, 2017
@wata727 wata727 deleted the add_rails_create_table_with_timestamps branch December 2, 2017 12:20
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.

5 participants