Class: PgHaMigrations::Relation

Inherits:
Struct
  • Object
show all
Defined in:
lib/pg_ha_migrations/relation.rb

Overview

This object represents a pointer to an actual relation in Postgres. The mode attribute is optional metadata which can represent a lock that has already been acquired or a potential lock that we are looking to acquire.

Direct Known Subclasses

Index, Table

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, schema, mode = nil) ⇒ Relation

Returns a new instance of Relation.



14
15
16
17
18
# File 'lib/pg_ha_migrations/relation.rb', line 14

def initialize(name, schema, mode=nil)
  super(name, schema)

  self.mode = LockMode.new(mode) if mode.present?
end

Instance Attribute Details

#modeObject

Returns the value of attribute mode

Returns:

  • (Object)

    the current value of mode



6
7
8
# File 'lib/pg_ha_migrations/relation.rb', line 6

def mode
  @mode
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



6
7
8
# File 'lib/pg_ha_migrations/relation.rb', line 6

def name
  @name
end

#schemaObject

Returns the value of attribute schema

Returns:

  • (Object)

    the current value of schema



6
7
8
# File 'lib/pg_ha_migrations/relation.rb', line 6

def schema
  @schema
end

Class Method Details

.connectionObject



7
8
9
# File 'lib/pg_ha_migrations/relation.rb', line 7

def self.connection
  ActiveRecord::Base.connection
end

Instance Method Details

#==(other) ⇒ Object



45
46
47
# File 'lib/pg_ha_migrations/relation.rb', line 45

def ==(other)
  eql?(other)
end

#conflicts_with?(other) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
# File 'lib/pg_ha_migrations/relation.rb', line 31

def conflicts_with?(other)
  eql?(other) && (
    mode.nil? || other.mode.nil? || mode.conflicts_with?(other.mode)
  )
end

#eql?(other) ⇒ Boolean

Some code paths need to compare lock modes, while others just need to determine if the relation is the same object in Postgres, so equality here is simply looking at the relation name / schema. To also compare lock modes, #conflicts_with? is used.

Returns:

  • (Boolean)


41
42
43
# File 'lib/pg_ha_migrations/relation.rb', line 41

def eql?(other)
  other.is_a?(Relation) && hash == other.hash
end

#fully_qualified_nameObject



20
21
22
23
24
25
# File 'lib/pg_ha_migrations/relation.rb', line 20

def fully_qualified_name
  @fully_qualified_name ||= [
    PG::Connection.quote_ident(schema),
    PG::Connection.quote_ident(name),
  ].join(".")
end

#hashObject



49
50
51
# File 'lib/pg_ha_migrations/relation.rb', line 49

def hash
  [name, schema].hash
end

#present?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/pg_ha_migrations/relation.rb', line 27

def present?
  name.present? && schema.present?
end