Class: DbAgile::Command::Schema::Dump

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

Overview

Dump a schema of the current database

Usage: dba #DbAgile::Command#command_name [options] [announced|effective|physical]

This command dumps the schema of the current database on the output console. Without argument, the announced schema is implicit. This command uses the fallback chain (announced -> effective -> physical) and has no side-effect on the database itself (read-only).

Schema filtering is possible via the –include and –exclude options, which allow including/excluding specific schema object kinds. When used conjointly, the semantics is ‘–include AND NOT(–exclude)’. The different object kinds are:

* logical            (all logical objects, see indented list below)
  * table            (base tables)
  * view             (derived tables, aka views)
  * constraint       (all constraints, see indented list below)
    * candidate_key  (all candidate keys, primary or not)
    * primary_key    (all primary keys)
    * foreign_key    (all foreign keys)
* physical           (all physical objects, see indented list below)
  * index            (all physical indexes)

A typical usage of schema filtering is to generate a script that drops all constraints at once by chaining this command with sql-script:

dba schema:dump --include=constraint | dba schema:sql-script --stdin drop

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 collapse

QUERIES =
{
  :logical       => [:logical?],
  :table         => [:attribute?, :constraint?],
  :view          => [:relview?],
  :constraint    => [:constraint?],
  :candidate_key => [:candidate_key], 
  :primary_key   => [:primary_key],
  :foreign_key   => [:foreign_key],
  :physical      => [:physical?],
  :index         => [:index?]
}

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, #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

#exclude_kindObject

Include and exclude options



61
62
63
# File 'lib/dbagile/command/schema/dump.rb', line 61

def exclude_kind
  @exclude_kind
end

#include_kindObject

Include and exclude options



61
62
63
# File 'lib/dbagile/command/schema/dump.rb', line 61

def include_kind
  @include_kind
end

Instance Method Details

#add_options(opt) ⇒ Object

Contribute to options



84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/dbagile/command/schema/dump.rb', line 84

def add_options(opt)
  opt.separator nil
  opt.separator "Options:"
  add_check_options(opt)
  add_stdin_options(opt)
  opt.on('--include=x,y,z', Array, 
         "Include object kinds (logical, physical, candidate_key, ...)") do |values|
    values.each{|value| include_kind!(value.to_sym)}
  end
  opt.on('--exclude=x,y,z', Array, 
         "Exclude object kinds (logical, physical, candidate_key, ...)") do |values|
    values.each{|value| exclude_kind!(value.to_sym)}
  end
end

#exclude_kind!(kind) ⇒ Object

Marks a kind a begin excluded



74
75
76
77
78
79
80
81
# File 'lib/dbagile/command/schema/dump.rb', line 74

def exclude_kind!(kind)
  if queries = QUERIES[kind]
    self.exclude_kind ||= []
    self.exclude_kind += queries
  else
    raise DbAgile::Error, "Unrecognized object kind #{kind}"
  end
end

#execute_commandObject

Executes the command



117
118
119
120
121
122
123
124
# File 'lib/dbagile/command/schema/dump.rb', line 117

def execute_command
  with_schema do |schema|
    say("# Schema of #{schema.schema_identifier.inspect}", :magenta)
    schema = filter_shema(schema)
    flush(schema.to_yaml)
    schema
  end
end

#filter_shema(schema) ⇒ Object

Filters the schema according to –select and –reject options.



112
113
114
# File 'lib/dbagile/command/schema/dump.rb', line 112

def filter_shema(schema)
  schema.filter{|obj| include?(obj)}
end

#include?(obj) ⇒ Boolean

Returns the kind of an object

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
108
# File 'lib/dbagile/command/schema/dump.rb', line 100

def include?(obj)
  if include_kind && !include_kind.any?{|kind| obj.send(kind)}
    return false
  end
  if exclude_kind && exclude_kind.any?{|kind| obj.send(kind)}
    return false
  end
  true
end

#include_kind!(kind) ⇒ Object

Marks a kind a begin included



64
65
66
67
68
69
70
71
# File 'lib/dbagile/command/schema/dump.rb', line 64

def include_kind!(kind)
  if queries = QUERIES[kind]
    self.include_kind ||= []
    self.include_kind += queries
  else
    raise DbAgile::Error, "Unrecognized object kind #{kind}"
  end
end

#kind_of_schema_argumentsObject

Returns :single



56
57
58
# File 'lib/dbagile/command/schema/dump.rb', line 56

def kind_of_schema_arguments
  :single
end