Class: DbAgile::Command::Schema::Diff

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

Overview

Show differences between database schemas

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

This command is a diff for database schemas, helping database staging and migrations by showing objects to create/drop/alter.

dba #DbAgile::Command#command_name

Shortcut for 'dba schema:diff announced effective'

dba #DbAgile::Command#command_name schema1 schema2

Show differences between two particular schemas, being either installed
schemas [announced|effective|physical] or schema files.  This command 
uses a fallback chain (announced -> effective -> physical) and has no 
side-effect on the database itself (read-only).

NOTE: 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

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_pending_arguments, #normalize_schema_argument, #normalize_schema_arguments, #with_schemas

Methods inherited from DbAgile::Command

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

#output_optionsObject (readonly)

Output options



34
35
36
# File 'lib/dbagile/command/schema/diff.rb', line 34

def output_options
  @output_options
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



42
43
44
45
46
47
48
49
50
# File 'lib/dbagile/command/schema/diff.rb', line 42

def add_options(opt)
  opt.separator nil
  opt.separator "Options:"
  add_check_options(opt)
  opt.on("--[no-]skip-unchanged", "-u",
         "Don't show objects that did'nt change") do |value|
    self.output_options[:skip_unchanged] = value
  end
end

#execute_commandObject

Executes the command



58
59
60
61
62
63
64
# File 'lib/dbagile/command/schema/diff.rb', line 58

def execute_command
  with_schemas do |left, right|
    merged = DbAgile::Core::Schema::merge(left, right)
    show_diff(left, right, merged, environment, output_options)
    merged
  end
end

#kind_of_schema_argumentsObject

Returns :single



53
54
55
# File 'lib/dbagile/command/schema/diff.rb', line 53

def kind_of_schema_arguments
  :double
end

#set_default_optionsObject

Sets the default options



37
38
39
# File 'lib/dbagile/command/schema/diff.rb', line 37

def set_default_options
  @output_options = {:skip_unchanged => false}
end

#show_diff(left, right, merged, environment, output_options) ⇒ Object

Shows difference between left and right



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dbagile/command/schema/diff.rb', line 67

def show_diff(left, right, merged, environment, output_options)
  # Validity
  if left.looks_valid?
    lv = environment.color("(valid!)", :green)
  else
    lv = environment.color("(INVALID, you'd better run 'dba schema:check --effective')", HighLine::RED + HighLine::BOLD)
  end
  if right.looks_valid?
    rv = environment.color("(valid!)", :green)
  else
    rv = environment.color("(INVALID, you'd better run 'dba schema:check')", HighLine::RED + HighLine::BOLD)
  end
  
  # Debug
  ld, rd = left.schema_identifier.inspect, right.schema_identifier.inspect
  ld, rd = "#{ld} #{lv}", "#{rd} #{rv}"
  
  # Say
  environment.say '###'
  environment.say "### LEFT: #{ld}"
  environment.say "### RIGHT: #{rd}"
  environment.say '###'
  environment.say "### " + environment.color('Objects to add on LEFT', :green)
  environment.say '### ' + environment.color('Objects to remove on LEFT', :red)
  environment.say '### ' + environment.color('Objects to alter on LEFT', :cyan)
  environment.say '###'
  environment.say "\n"
  merged.yaml_display(environment, output_options)
  environment.say "\n"
end