Class: PreCommit::Checks::Migration

Inherits:
Plugin
  • Object
show all
Defined in:
lib/plugins/pre_commit/checks/migration.rb

Constant Summary collapse

VERSION_PATTERN =
/(\d{14})/

Instance Attribute Summary

Attributes inherited from Plugin

#config, #pluginator

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Plugin

#initialize, #name

Constructor Details

This class inherits a constructor from PreCommit::Checks::Plugin

Class Method Details

.aliasesObject



14
15
16
# File 'lib/plugins/pre_commit/checks/migration.rb', line 14

def self.aliases
  [:migrations]
end

Instance Method Details

#call(staged_files) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/plugins/pre_commit/checks/migration.rb', line 18

def call(staged_files)
  migration_files = versioned_migration_files(staged_files)
  schema_files = versioned_schema_files(staged_files)

  if migration_files.any? && schema_files.none?
    "It looks like you're adding a migration, but did not update the schema file"
  elsif migration_files.none? && schema_files.any?
    "You're trying to change the schema without adding a migration file"
  elsif migration_files.any? && schema_files.any?
    migration_versions = migration_files.map(&:version)
    missing_versions = migration_versions - schema_files.map(&:version)
    if missing_versions.any?
      "You did not add the schema versions for "\
      "#{migration_versions.join(', ')} to #{schema_files.map(&:file).join(' or ')}"
    end
  end
end