Class: DbAgile::Command::Db::Stage

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

Overview

Stage the current database

Usage: dba #DbAgile::Command#command_name

Constant Summary

Constants inherited from DbAgile::Command

CATEGORIES, CATEGORY_NAMES

Instance Attribute Summary

Attributes inherited from DbAgile::Command

#environment

Attributes included from ClassMethods

#description, #summary, #usage

Instance Method Summary collapse

Methods inherited from DbAgile::Command

#add_options, #category, #check_command, #command_name, #description, #initialize, #normalize_pending_arguments, #options, #run, #set_default_options, #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 Method Details

#execute_commandDbAgile::Core::Database

Executes the command.

Returns:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dbagile/command/db/stage.rb', line 17

def execute_command
  with_current_database do |db|
    # left, right and merge
    left   = db.effective_schema(true)
    right  = db.announced_schema(true)
    merged = left + right
  
    # Bypass situations
    if merged.status == DbAgile::Core::Schema::NO_CHANGE
      say("Nothing to stage", :magenta)
      return
    elsif environment.interactive?
      # Show what will be performed
      DbAgile::dba(environment){|dba| dba.schema_diff %w{-u effective announced}}
      say("---")
      DbAgile::dba(environment){|dba| dba.schema_sql_script %w{stage effective announced}}
      say("\n")
      
      # Ask confirmation now
      answer = environment.ask("Are you sure?"){|q| q.validate = /^y(es)?|n(o)?|q(uit)?/i}
      return unless answer =~ /^y/i
    end
  
    # Everything seems ok now!
    begin
      stage(db, merged)
    rescue => ex
      say("Sorry, staging process failed for some reason.", :red)
      raise
    end
  end
end

#stage(db, merged) ⇒ Object

Makes staging itself



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

def stage(db, merged)
  script = DbAgile::Core::Schema::stage_script(merged)
  with_connection(db) do |conn|
    
    # stage the real database
    sql_script = conn.script2sql(script)
    conn.transaction do |t|
      t.direct_sql(sql_script)
      
      # split schemas now
      schemas = merged.split{|obj|
        case obj.status
          when DbAgile::Core::Schema::DROPPED
            :dropped
          else
            :effective
        end
      }
      
      # save schemas
      if s = schemas[:effective]
        db.set_effective_schema(s)
      end
    end
  end
end