Sha256: dbcb4c3f33b84355fd68a8d27e48442e9b24f9e0177ea2254ed4d5b2da384951

Contents?: true

Size: 874 Bytes

Versions: 2

Compression:

Stored size: 874 Bytes

Contents

# frozen_string_literal: true

require_relative '../cop'

module ActiveRecord
  module DataIntegrity
    module Accordance
      # Checks the primary key integer has 8 bytes length
      class PrimaryKey < ActiveRecord::DataIntegrity::Cop
        def call
          log('has short integer primary key') unless valid?
          progress(valid?, 'P')
          valid?
        end

        private

        def valid?
          return true unless connection.table_exists?(model.table_name)

          @valid ||= !connection.primary_keys(model.table_name).map! do |pk|
            column_valid?(pk)
          end.include?(false)
        end

        def column_valid?(name)
          column = connection.columns(model.table_name).find { |c| c.name == name }
          column.type == :integer && column.limit.present? && column.limit >= 8
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
activerecord-data_integrity-0.2.1 lib/active_record/data_integrity/cop/accordance/primary_key.rb
activerecord-data_integrity-0.1.0 lib/active_record/data_integrity/cop/accordance/primary_key.rb