Class: Karafka::Web::Management::Migrations::Base

Inherits:
Object
  • Object
show all
Includes:
Core::Helpers::Time
Defined in:
lib/karafka/web/management/migrations/base.rb

Overview

Base for all our migrations

Each migration MUST have a #migrate method defined Migrations are expected to modify the provided state IN PLACE

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.typeObject

What resource does it relate it. One migration should modify only one resource type



19
20
21
# File 'lib/karafka/web/management/migrations/base.rb', line 19

def type
  @type
end

.versions_untilObject

First version that should NOT be affected by this migration



17
18
19
# File 'lib/karafka/web/management/migrations/base.rb', line 17

def versions_until
  @versions_until
end

Class Method Details

.applicable?(version) ⇒ Boolean

Returns is the given migration applicable.

Parameters:

  • version (String)

    sem-ver version

Returns:

  • (Boolean)

    is the given migration applicable



23
24
25
# File 'lib/karafka/web/management/migrations/base.rb', line 23

def applicable?(version)
  version < versions_until
end

.indexInteger

Returns index for sorting. Older migrations are always applied first.

Returns:

  • (Integer)

    index for sorting. Older migrations are always applied first



33
34
35
36
37
38
39
40
41
42
# File 'lib/karafka/web/management/migrations/base.rb', line 33

def index
  instance_method(:migrate)
    .source_location
    .first
    .split("/")
    .last
    .split("_")
    .first
    .to_i
end

.migrate(state) ⇒ Object

Parameters:

  • state (Hash)

    deserialized state to be modified

Raises:

  • (NotImplementedError)


28
29
30
# File 'lib/karafka/web/management/migrations/base.rb', line 28

def migrate(state)
  raise NotImplementedError, "Implement in a subclass"
end

.sorted_descendantsArray<Class>

Returns array with migrations sorted from oldest to latest. This is the order in which they need to be applied.

Returns:

  • (Array<Class>)

    array with migrations sorted from oldest to latest. This is the order in which they need to be applied



46
47
48
49
50
51
# File 'lib/karafka/web/management/migrations/base.rb', line 46

def sorted_descendants
  ObjectSpace
    .each_object(Class)
    .select { |klass| klass != self && klass.ancestors.include?(self) }
    .sort_by(&:index)
end