Sha256: 64c1b8745592f9ec1d6eae7453edfa750a79b784d6113ef8b51dde338fc5fa53

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

# frozen_string_literal: true

require "active_record_doctor/detectors/base"

module ActiveRecordDoctor
  module Detectors
    class ShortPrimaryKeyType < Base # :nodoc:
      @description = "detect primary keys with short integer types"
      @config = {
        ignore_tables: {
          description: "tables whose primary keys should not be checked",
          global: true
        }
      }

      private

      def message(table:, column:)
        "change the type of #{table}.#{column} to bigint"
      end

      def detect
        each_table(except: config(:ignore_tables)) do |table|
          column = primary_key(table)
          next if column.nil?
          next if bigint?(column) || uuid?(column)

          problem!(table: table, column: column.name)
        end
      end

      def bigint?(column)
        if column.respond_to?(:bigint?)
          column.bigint?
        else
          /\Abigint\b/.match?(column.sql_type)
        end
      end

      def uuid?(column)
        column.sql_type == "uuid"
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
active_record_doctor-1.11.0 lib/active_record_doctor/detectors/short_primary_key_type.rb