Sha256: 4adfdfe18117b0b5b3df9c7db4e15f5c4b6888d13e60a90bca019f871a5a4906

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module PgHaMigrations
  class LockMode
    include Comparable

    MODE_CONFLICTS = ActiveSupport::OrderedHash.new

    MODE_CONFLICTS[:access_share] = %i[
      access_exclusive
    ]

    MODE_CONFLICTS[:row_share] = %i[
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:row_exclusive] = %i[
      share
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:share_update_exclusive] = %i[
      share_update_exclusive
      share
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:share] = %i[
      row_exclusive
      share_update_exclusive
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:share_row_exclusive] = %i[
      row_exclusive
      share_update_exclusive
      share
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:exclusive] = %i[
      row_share
      row_exclusive
      share_update_exclusive
      share
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    MODE_CONFLICTS[:access_exclusive] = %i[
      access_share
      row_share
      row_exclusive
      share_update_exclusive
      share
      share_row_exclusive
      exclusive
      access_exclusive
    ]

    attr_reader :mode

    delegate :to_s, to: :mode

    def initialize(mode)
      @mode = mode
        .to_s
        .underscore
        .delete_suffix("_lock")
        .to_sym

      if !MODE_CONFLICTS.keys.include?(@mode)
        raise ArgumentError, "Unrecognized lock mode #{@mode.inspect}. Valid modes: #{MODE_CONFLICTS.keys}"
      end
    end

    def to_sql
      mode
        .to_s
        .upcase
        .gsub("_", " ")
    end

    def <=>(other)
      MODE_CONFLICTS.keys.index(mode) <=> MODE_CONFLICTS.keys.index(other.mode)
    end

    def conflicts_with?(other)
      MODE_CONFLICTS[mode].include?(other.mode)
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pg_ha_migrations-1.8.0 lib/pg_ha_migrations/lock_mode.rb