过度使用RailsS​​chemaUpToDate在--run时传递但在提交时失败

时间:2017-09-22 12:38:15

标签: ruby-on-rails ruby git ruby-overcommit

我遇到overcommitRails 5.0.2应用ruby 2.4.0的奇怪问题:

$ overcommit --run
Running pre-commit hooks
Check if database schema is up to date..........[RailsSchemaUpToDate] OK
Check YAML syntax........................................[YamlSyntax] OK
Check Gemfile dependencies..............................[BundleCheck] OK
Check for vulnerable versions of gems...................[BundleAudit] OK
Analyze with RuboCop........................................[RuboCop] OK
Analyze with haml-lint.....................................[HamlLint] OK

✓ All pre-commit hooks passed
$ git ci -m "Overcommit and Linters added"
Running pre-commit hooks
Check if database schema is up to date..........[RailsSchemaUpToDate] FAILED
It looks like you're adding a migration, but did not update the schema file
Check YAML syntax........................................[YamlSyntax] OK
Check Gemfile dependencies..............................[BundleCheck] OK
Check for vulnerable versions of gems...................[BundleAudit] OK
Analyze with RuboCop........................................[RuboCop] OK
Analyze with haml-lint.....................................[HamlLint] OK

✗ One or more pre-commit hooks failed

这是我Gemfile的相关部分:

group :lint do
  gem 'overcommit', '~> 0.41', require: false  # hook event pre-commit, pre-push
  gem 'brakeman', '~> 3.7.2', require: false  # A static analysis security vulnerability scanner
  gem 'haml-lint', '~> 0.26', require: false  # Syntax checker for HAML
  gem 'ruby_css_lint', '~> 0.1', require: false  # Syntax checker for CSS
  gem 'rubocop', '~> 0.50', require: false  # A Ruby static code analyzer
  gem 'bundler-audit', '~> 0.6'  # Check dependencies
end

.overcommit.yml我有:

PreCommit:
  ALL:
    exclude:
      - 'bin/**'
      - 'public/**'
      - 'vendor/**'
  # ...

  # Migration Check
  RailsSchemaUpToDate:
    enabled: true
    on_warn: fail

我的db文件夹结构是:

+-- db
| +-- seeds.rb
| +-- migrate
  | +-- 20170922082256_devise_create_users.rb
  | +-- 20170922084050_create_fans.rb
| +-- schema.rb

这是我db/schema.rb的相关部分:

ActiveRecord::Schema.define(version: 20170922084050) do
# ...
end

我尝试降级为ruby 2.3.1,但我仍然遇到同样的问题。

有人遇到同样的问题吗?

2 个答案:

答案 0 :(得分:3)

我实际想通了。 似乎overcommit --run针对整个项目运行(除了未提交的更改的更改除外),而预提交挂钩仅在要提交的更改时触发。

我添加了该提交中的所有短信,我在bundle exec rubocop --auto-correct中为所有警察运行了db/schema.rb,不包括.rubocop.yml

这导致migration_files(已编辑的先前迁移文件)没有schema_filesschema.rb未进行任何更改),因此:

if migration_files.any? && schema_files.none?
发现

根据source linked by Brian提出错误。

出于这个原因,我刚刚提交了禁用overcommit,并且从下一次提交开始,此错误不再重现

$ OVERCOMMIT_DISABLED=1 git ci -m "Overcommit and Linters added"

答案 1 :(得分:1)

我认为不同之处在于overcommit --run正在查看已经提交给主的内容,而预提交钩子正在查看您尝试提交的内容。

查看the sourceif migration_files.any? && schema_files.none?为真时会显示您收到的错误消息。也就是说,db/migrate/下有文件,但没有db/schema.rb文件。

你能展示git status的输出吗?也许您的db/schema.rb文件以某种方式被删除了?