class PhobosDBCheckpoint::CLI::Commands

Public Class Methods

source_root() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 84
def self.source_root
  File.expand_path(File.join(File.dirname(__FILE__), '../..'))
end

Public Instance Methods

copy_migrations() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 42
def copy_migrations
  ENV['DB_CONFIG'] = options[:config] if options[:config]

  PhobosDBCheckpoint.configure unless active_connection?

  destination_fullpath = File.join(destination_root, options[:destination])
  generated_migrations = list_migrations(destination_fullpath)
  FileUtils.mkdir_p(destination_fullpath)
  file_path = nil
  template_migrations_metadata.each do |metadata|
    if migration_exists?(generated_migrations, metadata[:name])
      say_status('exists', metadata[:name])
    else
      file_path = File.join(options[:destination], "#{metadata[:number]}_#{metadata[:name]}")
      template_path = File.join('templates/migrate', metadata[:path])
      template(template_path, file_path)
    end
  end
rescue StandardError
  FileUtils.rm_f(file_path.to_s)
  raise
end
init() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 20
def init
  create_file('Rakefile') unless File.exist?(File.join(destination_root, 'Rakefile'))
  prepend_to_file 'Rakefile', "require 'phobos_db_checkpoint'\nPhobosDBCheckpoint.load_tasks\n"
  copy_file 'templates/database.yml.example', 'config/database.yml'

  cmd = self.class.new
  cmd.destination_root = destination_root
  cmd.invoke(:copy_migrations)

  create_file('phobos_boot.rb') unless File.exist?(File.join(destination_root, 'phobos_boot.rb'))
  append_to_file 'phobos_boot.rb', File.read(phobos_boot_template)
end
init_events_api() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 79
def init_events_api
  copy_file 'templates/config.ru', 'config.ru'
  say '   Start the API with: `rackup config.ru`'
end
migration(name) click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 70
def migration(name)
  migration_name = name.gsub(/[^\w]*/, '')
  @new_migration_class_name = migration_name.split('_').map(&:capitalize).join('')
  file_name = "#{migration_number}_#{migration_name}.rb"
  destination_fullpath = File.join(destination_root, options[:destination], file_name)
  template(new_migration_template, destination_fullpath)
end
version() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 15
def version
  puts PhobosDBCheckpoint::VERSION
end

Private Instance Methods

active_connection?() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 129
def active_connection?
  ActiveRecord::Base
    .connection_pool
    .with_connection(&:active?)
rescue StandardError
  false
end
list_migrations(dir) click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 112
def list_migrations(dir)
  return [] unless Dir.exist?(dir)
  Dir.entries(dir).select { |f| f =~ /\.rb(\.erb)?$/ }.sort
end
migration_exists?(list, name) click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 90
def migration_exists?(list, name)
  list.find { |filename| filename =~ /#{name}/ }
end
migration_number(index = 0) click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 94
def migration_number(index = 0)
  [Time.now.utc.strftime('%Y%m%d%H%M%S%6N'), format('%.21d', index)].max
end
migrations_template_dir() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 117
def migrations_template_dir
  File.join(self.class.source_root, 'templates/migrate')
end
new_migration_template() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 125
def new_migration_template
  File.join(self.class.source_root, 'templates/new_migration.rb.erb')
end
phobos_boot_template() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 121
def phobos_boot_template
  File.join(self.class.source_root, 'templates/phobos_boot.rb')
end
template_migrations() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 108
def template_migrations
  @template_migrations ||= list_migrations(migrations_template_dir)
end
template_migrations_metadata() click to toggle source
# File lib/phobos_db_checkpoint/cli.rb, line 98
def template_migrations_metadata
  @template_migrations_metadata ||= begin
    index = 0
    template_migrations.map do |path|
      index += 1
      { path: path, name: path.gsub(/\.erb$/, ''), number: migration_number(index) }
    end
  end
end