Class: DbAgile::Command::Schema::SqlScript

Inherits:
DbAgile::Command show all
Includes:
Commons
Defined in:
lib/dbagile/command/schema/sql_script.rb

Overview

Flush a SQL script for a creating/dropping/staging a schema

Usage: dba #DbAgile::Command#command_name drop, stage [SCHEMA_ARG, …]

dba #DbAgile::Command#command_name create [announced|effective|physical|FILE.yaml]

Flush a SQL script for creating a SQL database from a schema. Announced
schema of the database is the default. This command uses the fallback chain 
(announced -> effective -> physical) and has no side-effect on the database 
itself (read-only).

dba #DbAgile::Command#command_name drop [announced|effective|physical|FILE.yaml]

Flush a SQL script for dropping all objects of a SQL database. Announced
schema of the database is the default. This command uses the fallback chain 
(announced -> effective -> physical) and has no side-effect on the database 
itself (read-only).

dba #DbAgile::Command#command_name stage [schema1 schema2]

Flush a SQL script for staging a database from schema1 to schema2. Arguments 
can be installed schemas of the current database or schema files. Effective 
and announced schema of the current database are respectively used by default. 
The command uses the fallback chain (announced -> effective -> physical) and 
has no side-effect on the current database itself (read-only).

Schema-checking is on by default, which may lead to checking errors, typically when reverse engineering poorly designed databases. Doing so immediately informs you about a potential problem.

Use –no-check to bypass schema checking. See also schema:check.

Constant Summary collapse

SCRIPT_KIND =

Kinds of script

[:create, :drop, :stage]

Constants inherited from DbAgile::Command

CATEGORIES, CATEGORY_NAMES

Instance Attribute Summary collapse

Attributes included from Commons

#check_schemas, #on_stdin, #schema_arguments

Attributes inherited from DbAgile::Command

#environment

Attributes included from ClassMethods

#description, #summary, #usage

Instance Method Summary collapse

Methods included from Commons

#add_check_options, #add_stdin_options, #load_schema, #normalize_schema_argument, #normalize_schema_arguments, #with_schemas

Methods inherited from DbAgile::Command

#category, #check_command, #command_name, #description, #initialize, #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 Attribute Details

#script_kindObject

Kind of script [:create, :drop, :stage]



45
46
47
# File 'lib/dbagile/command/schema/sql_script.rb', line 45

def script_kind
  @script_kind
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



48
49
50
51
52
53
# File 'lib/dbagile/command/schema/sql_script.rb', line 48

def add_options(opt)
  opt.separator nil
  opt.separator "Options:"
  add_check_options(opt)
  add_stdin_options(opt)
end

#execute_commandObject

Executes the command



76
77
78
79
80
81
82
83
# File 'lib/dbagile/command/schema/sql_script.rb', line 76

def execute_command
  case kind_of_schema_arguments
    when :single
      execute_single_command
    when :double
      execute_double_command
  end
end

#execute_double_commandObject

Execution for :stage



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/dbagile/command/schema/sql_script.rb', line 103

def execute_double_command
  script = with_schemas{|left, right|
    case self.script_kind
      when :stage
        merged = left + right
        script = DbAgile::Core::Schema::stage_script(merged)
        if merged.status == DbAgile::Core::Schema::NO_CHANGE
          say("Nothing to stage", :magenta)
        end
        script
      else
        raise DbAgile::AssumptionFailedError, "Unknown script kind #{self.script_kind}"
    end
  }
  with_current_connection{|conn|
    conn.script2sql(script, environment.output_buffer)
  }
end

#execute_single_commandObject

Execution for :create and :drop



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/dbagile/command/schema/sql_script.rb', line 86

def execute_single_command
  script = with_schema{|schema|
    case self.script_kind
      when :create
        DbAgile::Core::Schema::create_script(schema)
      when :drop
        DbAgile::Core::Schema::drop_script(schema)
      else
        raise DbAgile::AssumptionFailedError, "Unknown script kind #{self.script_kind}"
    end
  }
  with_current_connection{|conn|
    conn.script2sql(script, environment.output_buffer)
  }
end

#kind_of_schema_argumentsObject

Returns :single



56
57
58
59
60
61
62
# File 'lib/dbagile/command/schema/sql_script.rb', line 56

def kind_of_schema_arguments
  if script_kind == :stage
    :double
  else
    :single
  end
end

#normalize_pending_arguments(arguments) ⇒ Object

Normalizes the pending arguments



65
66
67
68
69
70
71
72
73
# File 'lib/dbagile/command/schema/sql_script.rb', line 65

def normalize_pending_arguments(arguments)
  case arguments.size
    when 0
      bad_argument_list!(arguments, "script kind")
    else
      self.script_kind = is_in!("script kind", arguments.shift, SCRIPT_KIND)
      normalize_schema_arguments(arguments)
  end
end