Class: Dynomite::Migration::Runner

Inherits:
Object
  • Object
show all
Includes:
Item::WaiterMethods
Defined in:
lib/dynomite/migration/runner.rb

Instance Method Summary collapse

Methods included from Item::WaiterMethods

#waiter

Constructor Details

#initialize(options = {}) ⇒ Runner

Returns a new instance of Runner.



9
10
11
12
# File 'lib/dynomite/migration/runner.rb', line 9

def initialize(options={})
  @options = options
  Dynomite.config.log_level = :info unless ENV['DYNOMITE_DEBUG']
end

Instance Method Details

#check_for_migration_errors!Object



147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/dynomite/migration/runner.rb', line 147

def check_for_migration_errors!
  errors = SchemaMigration.warn_on_scan(false).where(status: "error")
  errors_info = SchemaMigration.warn_on_scan(false).where(status: "error").map do |schema_migration|
    "    #{schema_migration.path} - #{schema_migration.error_message}"
  end.join("\n")
  if errors.count > 0
    puts <<~EOL
      Found error migrations. Please review the migration erors fix before continuing.
      You can clear them out manually by deleting them from the #{SchemaMigration.table_name} table.

      #{errors_info}

      You can also clear them with the following command:

          CLEAR_ERRORS=1 jets dynamodb:migrate

    EOL
    exit 1
  end
end

#clear_error_schema_migrations!Object



140
141
142
143
144
145
# File 'lib/dynomite/migration/runner.rb', line 140

def clear_error_schema_migrations!
  SchemaMigration.warn_on_scan(false).where(status: "error").each do |schema_migration|
    schema_migration.delete
    puts "Deleted error schema_migration: #{schema_migration.path}"
  end
end

#ensure_schema_migrations_exist!Object



133
134
135
136
137
138
# File 'lib/dynomite/migration/runner.rb', line 133

def ensure_schema_migrations_exist!
  migration = CreateSchemaMigrations.new
  return if migration.table_exist?(SchemaMigration.table_name)
  puts "Creating #{SchemaMigration.table_name} table for the first time"
  migration.up
end

#migrate(path) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/dynomite/migration/runner.rb', line 24

def migrate(path)
  # load migration class definition
  #   CreatePosts#up (up is called below)
  file_info = FileInfo.new(path)

  schema_migration = SchemaMigration.find_by(version: file_info.version)
  if schema_migration
    teriminal_statuses = %w[complete error]
    if teriminal_statuses.include?(schema_migration.status)
      return
    else
      action = with_timeout(message: "Timed out. You must respond within 60s.") do
        uncompleted_migration_prompt(file_info, schema_migration)
      end
    end
  end

  case action
  when :skip
    return
  when :delete
    schema_migration.delete
  when :completed
    schema_migration.status = "completed"
    schema_migration.save
    return
  when :exit
    puts "Exiting"
    exit
  end

  puts "Running migration: #{file_info.pretty_path}"
  load path

  # INSERT schema_migration table - in_progress
  unless schema_migration
    schema_migration = SchemaMigration.new(version: file_info.version, status: "in_progress", path: file_info.pretty_path)
    schema_migration.save
  end
  start_time = Time.now

  # Run actual migration
  migration_class = file_info.migration_class
  error_message = nil
  begin
    # Runs migration up command. Example:
    #   CreatePosts#up
    #   Migration#create_table
    #   Migration#execute (wait happens here)
    migration_class.new.up # wait happens within create_table or update_table
  rescue Aws::DynamoDB::Errors::ServiceError => error
    puts "Unable to #{@method_name.to_s.gsub('_',' ')}: #{error.message}".color(:red)
    error_message = error.message
  end

  # UPDATE schema_migrations table - complete status
  if error_message
    schema_migration.status = "error"
    schema_migration.error_message = error_message
  else
    schema_migration.status = "complete"
  end

  schema_migration.time_took = (Time.now - start_time).to_i
  schema_migration.save
  # schema_migration.delete # HACK
  puts "Time took: #{pretty_time_took(schema_migration.time_took)}"
  exit 1 if error_message # otherwise continue to next migration file
end

#pretty_time_took(total_seconds) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/dynomite/migration/runner.rb', line 169

def pretty_time_took(total_seconds)
  minutes = (total_seconds / 60) % 60
  seconds = total_seconds % 60
  if total_seconds < 60
    "#{seconds.to_i}s"
  else
    "#{minutes.to_i}m #{seconds.to_i}s"
  end
end

#runObject



14
15
16
17
18
19
20
21
22
# File 'lib/dynomite/migration/runner.rb', line 14

def run
  puts "Running Dynomite migrations"
  ensure_schema_migrations_exist!
  clear_error_schema_migrations! if ENV['CLEAR_ERRORS']
  check_for_migration_errors!
  Dynomite::Migration::FileInfo.all_files.each do |path|
    migrate(path)
  end
end

#uncompleted_migration_prompt(file_info, schema_migration) ⇒ Object



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dynomite/migration/runner.rb', line 104

def uncompleted_migration_prompt(file_info, schema_migration)
  choice = nil
  until %w[s d c e].include?(choice)
    puts(<<~EOL)
      The #{file_info.pretty_path} migration status is incomplete. Status: #{schema_migration.status}
      This can happen and if it was interrupted by a CTRL-C.
      Please check the migration to help determine what to do next.

      Options:

          s - skip and continue. leaves schema_migrations item as-is
          d - delete and continue. deletes the schema_migrations item
          c - mark as successful completed and continue. updates the schema_migrations item as completed.
          e - exit

      EOL
      print "Choose an option (s/d/c/e): "
    choice = $stdin.gets.strip
  end

  map = {
    "s" => :skip,
    "d" => :delete,
    "c" => :completed,
    "e" => :exit,
  }
  map[choice]
end

#with_timeout(options = {}, &block) ⇒ Object



94
95
96
97
98
99
100
101
102
# File 'lib/dynomite/migration/runner.rb', line 94

def with_timeout(options={}, &block)
  seconds = options[:seconds] || 60
  message = options[:message] || "Timed out after #{seconds}s."
  Timeout::timeout(seconds, &block)
rescue Timeout::Error => e
  puts "#{e.class}: #{e.message}"
  puts message
  exit 1
end