Sha256: ced55fa0208d5496fe06be9641ec538871d720320302107a07068c972373cf52

Contents?: true

Size: 1.94 KB

Versions: 19

Compression:

Stored size: 1.94 KB

Contents

require 'pre-commit/checks/plugin'

module PreCommit
  module Checks
    class Migration < Plugin
      VERSION_PATTERN = /(\d{14})/

      self::VersionedFile = Struct.new(:file, :version) do
        def to_s
          "<#{self.class.name} file=\"#{file}\" version=\"#{version}\">"
        end
      end

      def self.aliases
        [:migrations]
      end

      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

      private

      def versioned_migration_files(staged_files)
        files = staged_files.grep(/db\/migrate\/.*\.rb/)

        files.each_with_object([]) do |f, result|
          if f =~ VERSION_PATTERN
            result << VersionedFile.new(f, $1)
          end
        end
      end

      def versioned_schema_files(staged_files)
        files = staged_files.select do |f|
          File.basename(f) =~ /schema\.rb|structure.*\.sql/
        end

        files.each_with_object([]) do |f, result|
          File.read(f).scan(VERSION_PATTERN) do |i|
            result << VersionedFile.new(f, i.first)
          end
        end
      end

      def self.description
        "Detects rails database migrations and schema incompatibilities."
      end

    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
pre-commit-0.38.1 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.38.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.37.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.36.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.35.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.34.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.33.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.32.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.31.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.30.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.29.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.28.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.27.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.26.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.25.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.24.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.23.0 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.22.1 lib/plugins/pre_commit/checks/migration.rb
pre-commit-0.22.0 lib/plugins/pre_commit/checks/migration.rb