Sha256: dd6db96173e112e53b5c0c844edba45708c6c49a382e9cf820cf182c2ea28d6b

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

class 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 migraiton 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

7 entries across 7 versions & 1 rubygems

Version Path
pre-commit-0.1.16 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.15 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.14 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.13 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.12 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.11 lib/pre-commit/checks/migration_check.rb
pre-commit-0.1.10 lib/pre-commit/checks/migration_check.rb