Class: PhobosDBCheckpoint::CLI::Commands

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/phobos_db_checkpoint/cli.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_rootObject



87
88
89
# File 'lib/phobos_db_checkpoint/cli.rb', line 87

def self.source_root
  File.expand_path(File.join(File.dirname(__FILE__), '../..'))
end

Instance Method Details

#copy_migrationsObject



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
# File 'lib/phobos_db_checkpoint/cli.rb', line 41

def copy_migrations
  if options[:config]
    ENV['DB_CONFIG'] = options[:config]
  end

  unless active_connection?
    PhobosDBCheckpoint.configure
  end

  destination_fullpath = File.join(destination_root, options[:destination])
  generated_migrations = list_migrations(destination_fullpath)
  FileUtils.mkdir_p(destination_fullpath)
  file_path = nil
  .each do ||
    if migration_exists?(generated_migrations, [:name])
      say_status('exists', [:name])
    else
      file_path = File.join(options[:destination], "#{[:number]}_#{[:name]}")
      template_path = File.join('templates/migrate', [:path])
      template(template_path, file_path)
    end
  end
rescue
  FileUtils.rm_f(file_path.to_s)
  raise
end

#initObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/phobos_db_checkpoint/cli.rb', line 19

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_apiObject



82
83
84
85
# File 'lib/phobos_db_checkpoint/cli.rb', line 82

def init_events_api
  copy_file 'templates/config.ru', 'config.ru'
  say '   Start the API with: `rackup config.ru`'
end

#migration(name) ⇒ Object



73
74
75
76
77
78
79
# File 'lib/phobos_db_checkpoint/cli.rb', line 73

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

#versionObject



14
15
16
# File 'lib/phobos_db_checkpoint/cli.rb', line 14

def version
  puts PhobosDBCheckpoint::VERSION
end