Class: Charming::Database::Commands

Inherits:
Object
  • Object
show all
Defined in:
lib/charming/database/commands.rb

Overview

Commands implements the runtime side of ‘charming db:COMMAND` (other than `db:install`, which lives in Generators::DatabaseInstaller). It loads the app’s ‘config/database.rb`, delegates the actual work to ActiveRecord, and prints a short status line on success.

Supported commands: db:create, db:migrate, db:rollback [STEP=n], db:drop, db:seed, db:setup, db:reset, db:prepare, db:status, db:version, db:schema:dump, db:schema:load. The target database file is selected by CHARMING_ENV (development, test, production).

Instance Method Summary collapse

Constructor Details

#initialize(command, out:, destination:, args: []) ⇒ Commands

command is the subcommand string (e.g., “db:create”). args holds extra CLI arguments such as “STEP=2”. out is the status-output stream. destination is the app root for resolving ‘config/database.rb` and `db/`.



19
20
21
22
23
24
# File 'lib/charming/database/commands.rb', line 19

def initialize(command, out:, destination:, args: [])
  @command = command
  @args = args
  @out = out
  @destination = destination
end

Instance Method Details

#runObject

Dispatches the configured command. Raises Generators::Error for unknown commands.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/charming/database/commands.rb', line 27

def run
  case command
  when "db:create" then create
  when "db:migrate" then migrate
  when "db:rollback" then rollback
  when "db:drop" then drop
  when "db:seed" then seed
  when "db:setup" then setup
  when "db:reset" then reset
  when "db:prepare" then prepare
  when "db:status" then status
  when "db:version" then version
  when "db:schema:dump" then schema_dump
  when "db:schema:load" then schema_load
  else raise Generators::Error, "Unknown database command: #{command}"
  end
end