Module: DbAgile::Command::Schema::Commons

Included in:
Check, Diff, Dump, Merge, SqlScript
Defined in:
lib/dbagile/command/schema/commons.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#check_schemasObject

Checks schema(s) first?



7
8
9
# File 'lib/dbagile/command/schema/commons.rb', line 7

def check_schemas
  @check_schemas
end

#on_stdinObject

Take schema on standard input?



10
11
12
# File 'lib/dbagile/command/schema/commons.rb', line 10

def on_stdin
  @on_stdin
end

#schema_argumentsObject

Schema arguments



13
14
15
# File 'lib/dbagile/command/schema/commons.rb', line 13

def schema_arguments
  @schema_arguments
end

Instance Method Details

#add_check_options(opt) ⇒ Object

Adds –[no-]check option



16
17
18
19
20
21
# File 'lib/dbagile/command/schema/commons.rb', line 16

def add_check_options(opt)
  self.check_schemas = true
  opt.on('--[no-]check', "Perform/Bypass schema checking") do |value|
    self.check_schemas = value
  end
end

#add_stdin_options(opt) ⇒ Object

Adds –stdin option



24
25
26
27
28
# File 'lib/dbagile/command/schema/commons.rb', line 24

def add_stdin_options(opt)
  opt.on('--stdin', "Take schema on the standard command input") do
    self.on_stdin = true
  end
end

#load_schema(schema_argument, check = self.check_schemas) ⇒ Object

Loads a given schema from a schema argument



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/dbagile/command/schema/commons.rb', line 75

def load_schema(schema_argument, check = self.check_schemas)
  schema = case schema_argument
    when :stdin
      r = environment.input_buffer.read
      s = DbAgile::Core::Schema::yaml_load(r)
      s.schema_identifier = "--stdin"
      s
    when Symbol
      with_current_database do |database|
        case schema_argument
          when :announced
            database.announced_schema(true)
          when :effective
            database.effective_schema(true)
          when :physical
            database.physical_schema
        end
      end
    when String
      s = DbAgile::Core::Schema::yaml_file_load(schema_argument)
      s.schema_identifier = schema_argument
      s
  end
  
  # A small check, to be sure
  if schema.nil?
    assumption_error!("Unexpected schema argument kind #{schema_argument}")
  end
      
  # Check schema if requested
  if check
    schema.check!
  end
  
  # Return schema now
  schema
end

#normalize_pending_arguments(arguments) ⇒ Object

Normalizes the pending arguments



70
71
72
# File 'lib/dbagile/command/schema/commons.rb', line 70

def normalize_pending_arguments(arguments)
  normalize_schema_arguments(arguments)
end

#normalize_schema_argument(argument) ⇒ Object

Normalizes a schema argument



31
32
33
34
35
36
37
# File 'lib/dbagile/command/schema/commons.rb', line 31

def normalize_schema_argument(argument)
  if File.exists?(argument) and File.file?(argument)
    argument
  else
    is_in!("schema", argument, [:announced, :effective, :physical])
  end
end

#normalize_schema_arguments(arguments) ⇒ Object

Normalizes the pending arguments



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/dbagile/command/schema/commons.rb', line 40

def normalize_schema_arguments(arguments)
  @schema_arguments = case kind_of_schema_arguments
    when :single
      if arguments.empty?
        on_stdin ? [ :stdin ] : [ :announced ]
      elsif arguments.size == 1
        arguments.collect{|arg| normalize_schema_argument(arg) }
      else
        bad_argument_list!(arguments)
      end
    when :double
      if arguments.empty?
        [ :effective, :announced ]
      elsif arguments.size == 2
        arguments.collect{|arg| normalize_schema_argument(arg) }
      else
        bad_argument_list!(arguments)
      end
    when :multiple
      if arguments.size >= 2
        arguments.collect{|arg| normalize_schema_argument(arg) }
      else
        bad_argument_list!(arguments)
      end
    else
      assumption_error!("Unexpected kind of schema argument #{kind_of_schema_arguments}")
  end
end

#with_schemas(check = self.check_schemas) ⇒ Object Also known as: with_schema

Yields the block, passing schemas as an array, according to kind_of_schema_arguments



115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/dbagile/command/schema/commons.rb', line 115

def with_schemas(check = self.check_schemas)
  schemas = schema_arguments.collect{|arg| load_schema(arg, check)}
  case kind_of_schema_arguments
    when :single
      yield(schemas.first)
    when :double
      yield(schemas[0], schemas[1])
    when :multiple
      yield(schemas)
    else
      assumption_error!("Unexpected kind of schema argument #{kind_of_schema_arguments}")
  end
end