Class: DbAgile::Command::Db::Add

Inherits:
DbAgile::Command show all
Defined in:
lib/dbagile/command/db/add.rb

Overview

Add a new database configuration

Usage: dba #DbAgile::Command#command_name NAME URI

Constant Summary

Constants inherited from DbAgile::Command

CATEGORIES, CATEGORY_NAMES

Instance Attribute Summary collapse

Attributes inherited from DbAgile::Command

#environment

Attributes included from ClassMethods

#description, #summary, #usage

Instance Method Summary collapse

Methods inherited from DbAgile::Command

#category, #command_name, #description, #initialize, #options, #run, #show_help, #summary, #unsecure_run, #usage

Methods included from ClassMethods

#build_command_options, #build_me, #category, #command_for, #command_name, #command_name_of, #each_subclass, #inherited, #ruby_method_for, #subclasses

Methods included from Robust

#ambigous_argument_list!, #assumption_error!, #bad_argument_list!, #has_command!, #is_in!, #valid_argument_list!, #valid_read_file!

Methods included from DbAgile::Core::IO::Robustness

#has_database!, #valid_database_name!, #valid_database_uri!, #valid_schema_files!

Constructor Details

This class inherits a constructor from DbAgile::Command

Instance Attribute Details

#currentObject

Makes it the current database?



19
20
21
# File 'lib/dbagile/command/db/add.rb', line 19

def current
  @current
end

#db_nameObject

Database name



13
14
15
# File 'lib/dbagile/command/db/add.rb', line 13

def db_name
  @db_name
end

#uriObject

Database URI



16
17
18
# File 'lib/dbagile/command/db/add.rb', line 16

def uri
  @uri
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



27
28
29
30
31
32
33
# File 'lib/dbagile/command/db/add.rb', line 27

def add_options(opt)
  opt.separator nil
  opt.separator "Options:"
  opt.on("--[no-]current", "Set as current database when created (see use)") do |value|
    self.current = false
  end
end

#check_commandObject

Checks command



41
42
43
44
# File 'lib/dbagile/command/db/add.rb', line 41

def check_command
  valid_database_name!(self.db_name)
  valid_database_uri!(self.uri)
end

#execute_commandDbAgile::Core::Database

Executes the command.

Returns:



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
# File 'lib/dbagile/command/db/add.rb', line 51

def execute_command
  db = nil
  with_repository do |repository|

    if repository.has_database?(self.db_name)
      raise DatabaseNameConflictError, "Database #{self.db_name} already exists"
    else
      # Create the database and adds it
      name, uri = self.db_name, self.uri
      db = DbAgile::Core::Database.new(name, uri)
      repository << db
      infer_schemas(db) if db.ping?

      # Makes it the current one if requested
      if self.current
        repository.current_db_name = db.name 
      end

      # Flush the repository file
      repository.save!
    end
  
  end

  # List available databases now
  DbAgile::dba(environment){|dba| dba.db_list %w{}}

  # Returns created database
  db
end

#infer_schemas(db) ⇒ Object

Infers database schemas



83
84
85
86
87
88
89
# File 'lib/dbagile/command/db/add.rb', line 83

def infer_schemas(db)
  schema = db.physical_schema
  db.set_announced_schema(schema)
  db.set_effective_schema(schema)
rescue => ex
  say("An error occured when infering schema: #{ex.message}\nYou'll have to install them manually. Sorry", :magenta)
end

#normalize_pending_arguments(arguments) ⇒ Object

Normalizes the pending arguments



36
37
38
# File 'lib/dbagile/command/db/add.rb', line 36

def normalize_pending_arguments(arguments)
  self.db_name, self.uri = valid_argument_list!(arguments, Symbol, String)
end

#set_default_optionsObject

Sets the default options



22
23
24
# File 'lib/dbagile/command/db/add.rb', line 22

def set_default_options
  @current = true
end