Class: Gitlab::DowntimeCheck

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/downtime_check.rb,
lib/gitlab/downtime_check/message.rb

Overview

Checks if a set of migrations requires downtime or not.

Defined Under Namespace

Classes: Message

Constant Summary collapse

DOWNTIME_CONST =

The constant containing the boolean that indicates if downtime is needed or not.

:DOWNTIME
DOWNTIME_REASON_CONST =

The constant that specifies the reason for the migration requiring downtime.

:DOWNTIME_REASON

Instance Method Summary collapse

Instance Method Details

#check(migrations) ⇒ Object

Checks the given migration paths and returns an Array of `Gitlab::DowntimeCheck::Message` instances.

migrations - The migration file paths to check.


18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/gitlab/downtime_check.rb', line 18

def check(migrations)
  migrations.map do |path|
    require(path)

    migration_class = class_for_migration_file(path)

    unless migration_class.const_defined?(DOWNTIME_CONST)
      raise "The migration in #{path} does not specify if it requires " \
        "downtime or not"
    end

    if online?(migration_class)
      Message.new(path)
    else
      reason = downtime_reason(migration_class)

      unless reason
        raise "The migration in #{path} requires downtime but no reason " \
          "was given"
      end

      Message.new(path, true, reason)
    end
  end
end

#check_and_print(migrations) ⇒ Object

Checks the given migrations and prints the results to STDOUT/STDERR.

migrations - The migration file paths to check.


47
48
49
50
51
# File 'lib/gitlab/downtime_check.rb', line 47

def check_and_print(migrations)
  check(migrations).each do |message|
    puts message.to_s # rubocop: disable Rails/Output
  end
end

#class_for_migration_file(path) ⇒ Object

Returns the class for the given migration file path.


54
55
56
57
# File 'lib/gitlab/downtime_check.rb', line 54

def class_for_migration_file(path)
  File.basename(path, File.extname(path)).split('_', 2).last.camelize
    .constantize
end

#downtime_reason(migration) ⇒ Object

Returns the downtime reason, or nil if none was defined.


65
66
67
68
69
70
71
# File 'lib/gitlab/downtime_check.rb', line 65

def downtime_reason(migration)
  if migration.const_defined?(DOWNTIME_REASON_CONST)
    migration.const_get(DOWNTIME_REASON_CONST, false)
  else
    nil
  end
end

#online?(migration) ⇒ Boolean

Returns true if the given migration can be performed without downtime.

Returns:

  • (Boolean)

60
61
62
# File 'lib/gitlab/downtime_check.rb', line 60

def online?(migration)
  migration.const_get(DOWNTIME_CONST, false) == false
end