Sha256: 38fa1d909ed6ac3604f61b315dbe50183bc0e95af31d15a1e5c789d95ec75436

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module PreCommit
  class MigrationCheck

    attr_accessor :error_message

    def call
      staged_files = load_staged_files
      run(staged_files)

      if !passed? && error_message
        $stderr.puts "pre-commit: #{error_message}"
      end

      passed?
    end

    def run(staged_files)
      migration_present = migration_file_present?(staged_files)
      schema_change = schema_file_present?(staged_files)

      if migration_present && !schema_change
        @error_message = "It looks like you're adding a migration, but did not update the schema file"
        @passed = false
      elsif schema_change && !migration_present
        @error_message = "You're trying to change the schema without adding a migration file"
        @passed = false
      else
        @passed = true
      end
    end

    def migration_file_present?(staged_files)
      staged_files.any? { |file| file =~ /db\/migrate\/.*\.rb/ }
    end

    def schema_file_present?(staged_files)
      staged_files.any? do |file|
        basename = File.basename(file)

        [/schema\.rb/i, /structure.*\.sql/].any? do |regex|
          basename =~ regex
        end
      end
    end

    def passed?
      @passed
    end

    def load_staged_files
      Utils.staged_files('.').split(" ")
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
pre-commit-0.8.1 lib/pre-commit/checks/migration_check.rb
pre-commit-0.8.0 lib/pre-commit/checks/migration_check.rb
pre-commit-0.7.0 lib/pre-commit/checks/migration_check.rb