Class: Overcommit::Hook::PreCommit::RailsSchemaUpToDate

Inherits:
Base
  • Object
show all
Defined in:
lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb

Overview

Check to see whether the schema file is in line with the migrations. When a schema file is present but a migration file is not, this is usually a failure. The exception is if the schema is at version 0 (i.e before any migrations have been run). In this case it is OK if there are no migrations.

Instance Attribute Summary

Attributes inherited from Base

#config

Instance Method Summary collapse

Methods inherited from Base

#applicable_files, #command, #description, #enabled?, #excluded?, #execute, #execute_in_background, #flags, #in_path?, #included_files, #initialize, #name, #parallelize?, #processors, #quiet?, #required?, #required_executable, #required_libraries, #run?, #run_and_transform, #skip?

Constructor Details

This class inherits a constructor from Overcommit::Hook::Base

Instance Method Details

#runObject

rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/overcommit/hook/pre_commit/rails_schema_up_to_date.rb', line 9

def run # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
  if migration_files.any? && schema_files.none?
    return :fail, "It looks like you're adding a migration, but did not update the schema file"
  elsif migration_files.none? && schema_files.any? && non_zero_schema_version?
    return :fail, "You're trying to change the schema without adding a migration file"
  elsif migration_files.any? && schema_files.any?
    # Get the latest version from the migration filename. Use
    # `File.basename` to prevent finding numbers that could appear in
    # directories, such as the home directory of a user with a number in
    # their username.
    latest_version = migration_files.map do |file|
      File.basename(file)[/\d+/]
    end.max

    up_to_date = schema.include?(latest_version)

    unless up_to_date
      return :fail, "The latest migration version you're committing is " \
                   "#{latest_version}, but your schema file " \
                   "#{schema_files.join(' or ')} is on a different version."
    end
  end

  :pass
end