Module: Gitlab::Database::BackgroundMigration::RakeTask
- Defined in:
- lib/gitlab/database/background_migration/rake_task.rb
Instance Method Summary collapse
- #connection_and_id_from_params(migration_id) ⇒ Object
-
#print_error(msg, force_exit: true) ⇒ Object
rubocop:disable Rails/Output – We do want to write to stdout.
- #print_message(msg, force_exit: false) ⇒ Object
- #print_row(row, column_widths) ⇒ Object
- #print_table(data, headers: true) ⇒ Object
Instance Method Details
#connection_and_id_from_params(migration_id) ⇒ Object
9 10 11 12 13 14 |
# File 'lib/gitlab/database/background_migration/rake_task.rb', line 9 def connection_and_id_from_params(migration_id) database_name, id = migration_id.split('_') model = Gitlab::Database.database_base_models[database_name] [model.connection, id.to_i] end |
#print_error(msg, force_exit: true) ⇒ Object
rubocop:disable Rails/Output – We do want to write to stdout
17 18 19 20 21 |
# File 'lib/gitlab/database/background_migration/rake_task.rb', line 17 def print_error(msg, force_exit: true) puts Rainbow(msg).red exit 1 if force_exit # rubocop:disable Rails/Exit -- used only in rake tasks end |
#print_message(msg, force_exit: false) ⇒ Object
23 24 25 26 27 |
# File 'lib/gitlab/database/background_migration/rake_task.rb', line 23 def (msg, force_exit: false) puts Rainbow(msg).green exit if force_exit # rubocop:disable Rails/Exit -- used only in rake tasks end |
#print_row(row, column_widths) ⇒ Object
64 65 66 67 68 69 |
# File 'lib/gitlab/database/background_migration/rake_task.rb', line 64 def print_row(row, column_widths) formatted_cells = row.each_with_index.map do |cell, index| cell.ljust(column_widths[index]) end puts formatted_cells.join(' | ') end |
#print_table(data, headers: true) ⇒ Object
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 |
# File 'lib/gitlab/database/background_migration/rake_task.rb', line 29 def print_table(data, headers: true) return if data.nil? || data.empty? puts # Convert all elements to strings and handle nil values string_data = data.map { |row| row.map(&:to_s) } # Calculate the maximum width for each column column_widths = [] string_data.each do |row| row.each_with_index do |cell, index| column_widths[index] = [column_widths[index] || 0, cell.length].max end end if headers # Print header row header = string_data.first print_row(header, column_widths) # Print separator line separator = column_widths.map { |width| '-' * width }.join('-|-') puts separator end # Print data rows start = headers ? 1 : 0 string_data[start..].each do |row| print_row(row, column_widths) end puts end |