Class: DbAgile::Core::Schema::Migrate::Operation

Inherits:
Object
  • Object
show all
Defined in:
lib/dbagile/core/schema/migrate/operation.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rel_object) ⇒ Operation

Returns a new instance of Operation.



20
21
22
23
24
25
26
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 20

def initialize(rel_object)
  unless rel_object.relvar? or rel_object.relview?
    raise ArgumentError, "Relvar expected for rel_object, got #{rel_object.class}"
  end
  @rel_object = rel_object
  @operations = []
end

Instance Attribute Details

#operationsObject (readonly)

The sub operations



8
9
10
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 8

def operations
  @operations
end

#rel_objectObject (readonly) Also known as: relvar, relview

Targetted relation variable/view



11
12
13
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 11

def rel_object
  @rel_object
end

Instance Method Details

#attribute(attribute) ⇒ Object

Create/alter an attribute



97
98
99
100
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 97

def attribute(attribute)
  supports_sub_operation!(:attribute)
  operations << [:attribute, attribute]
end

#candidate_key(ckey) ⇒ Object

Create/alter a candidate key



103
104
105
106
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 103

def candidate_key(ckey)
  supports_sub_operation!(:candidate_key)
  operations << [:candidate_key, ckey]
end

#each_sub_operation(&block) ⇒ Object

Yields the block with each (subop_kind, operand) pair



91
92
93
94
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 91

def each_sub_operation(&block)
  supports_sub_operation!(nil)
  operations.each{|op| block.call(op[0], op[1])}
end

#foreign_key(fkey) ⇒ Object

Create/alter a foreign key



109
110
111
112
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 109

def foreign_key(fkey)
  supports_sub_operation!(:foreign_key)
  operations << [:foreign_key, fkey]
end

#index(index) ⇒ Object

Create/alter an index



115
116
117
118
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 115

def index(index)
  supports_sub_operation!(:index)
  operations << [:index, index]
end

#kindObject

Returns kind of this operation



29
30
31
32
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 29

def kind
  unqualified = DbAgile::RubyTools::class_unqualified_name(self.class).to_s
  unqualified.gsub(/[A-Z]/){|x| "_#{x.downcase}"}[1..-1].to_sym
end

#not_staged!(obj = relvar) ⇒ Object

Mark an object as not being staged



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 56

def not_staged!(obj = relvar)
  obj.status = case obj.status
    when Schema::TO_CREATE
      Schema::DEFERED
    when Schema::TO_ALTER
      Schema::DEFERED
    when Schema::TO_DROP
      Schema::DEFERED
    when Schema::DEFERED
      Schema::DEFERED
    when Schema::NO_CHANGE
      Schema::NO_CHANGE
    else
      status_str = obj.status.to_s.upcase
      raise DbAgile::AssumptionFailedError, "Unexpected staged! source status #{status_str} on #{obj}"
  end
end

#ops_to_sql92(ops) ⇒ Object

Converts operations to simili SQL



121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 121

def ops_to_sql92(ops)
  ops.collect{|op|
    kind, operand = op
    case kind
      when :attribute
        "COLUMN #{operand.name} #{operand.domain}"
      when :candidate_key
        "CANDIDATE KEY #{operand.name})"
      when :foreign_key
        "FOREIGN KEY #{operand.name}"
      when :index
        "INDEX #{operand.name}"
      else
        raise DbAgile::AssumptionFailedError, "Unexpected operation kin #{kind}"
    end
  }.join(';')
end

#staged!(obj = relvar) ⇒ Object

Mark an object as upgraded



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 39

def staged!(obj = relvar)
  obj.status = case obj.status
    when Schema::TO_CREATE, Schema::CREATED
      Schema::CREATED
    when Schema::TO_ALTER, Schema::ALTERED
      Schema::ALTERED
    when Schema::TO_DROP, Schema::DROPPED
      Schema::DROPPED
    when Schema::NO_CHANGE
      Schema::NO_CHANGE
    else
      status_str = obj.status.to_s.upcase
      raise DbAgile::AssumptionFailedError, "Unexpected staged! source status #{status_str} on #{obj}"
  end
end

#supports_sub_operation!(name) ⇒ Object

Asserts that this operation supports sub operations



84
85
86
87
88
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 84

def supports_sub_operation!(name)
  unless supports_sub_operation?(name)
    raise DbAgile::AssumptionFailedError, "#{self.class} does not support sub operation #{name}"
  end
end

#supports_sub_operation?(name = nil) ⇒ Boolean

Asserts that this operation supports sub operations

Returns:

  • (Boolean)


79
80
81
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 79

def supports_sub_operation?(name = nil)
  !self.kind_of?(Migrate::DropTable)
end

#table_nameObject

Returns table name



16
17
18
# File 'lib/dbagile/core/schema/migrate/operation.rb', line 16

def table_name
  relvar.name
end