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


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

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

[View source]

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

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

[View source]

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

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)
[View source]

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

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

[View source]

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

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