Module: DbAgile::SequelAdapter::Schema::ConcreteScript

Extended by:
ConcreteScript
Includes:
Schema2SequelArgs
Included in:
ConcreteScript
Defined in:
lib/dbagile/adapter/sequel/schema/concrete_script.rb

Instance Method Summary collapse

Methods included from Schema2SequelArgs

#attribute2column_args, #candidate_key2primary_key_args, #candidate_key2unique_args, #foreign_key2foreign_key_args, #index2index_args, #ruby_type2sequel_type

Instance Method Details

#build_sequel_collapse_generator(conn, op, generator, prefix) ⇒ Object

Builds a sequel collapse generator



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 114

def build_sequel_collapse_generator(conn, op, generator, prefix)
  op.each_sub_operation{|kind, operand|
    case kind
      when :attribute
        args = [ operand.name ]
        generator.send(:"#{prefix}column", *args)
      when :candidate_key, :foreign_key
        args = [ operand.name ]
        generator.send(:"#{prefix}constraint", *args) 
      when :index
        column_names = operand.indexed_attributes.collect{|a| a.name}
        args = [ column_names, {:name => operand.name}]
        generator.send(:"#{prefix}index", *args)
      else
        raise DbAgile::AssumptionFailedError, "Unknown script operation kind #{kind}"
    end
  }
end

#build_sequel_expand_generator(conn, op, generator, prefix) ⇒ Object

Builds a sequel expand generator



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/adapter/sequel/schema/concrete_script.rb', line 87

def build_sequel_expand_generator(conn, op, generator, prefix)
  op.each_sub_operation{|kind, operand|
    case kind
      when :attribute
        args = attribute2column_args(operand)
        generator.send(:"#{prefix}column", *args)
      when :candidate_key
        if operand.primary?
          args = candidate_key2primary_key_args(operand)
          generator.send(:"#{prefix}primary_key", *args) 
        else
          args = candidate_key2unique_args(operand)
          generator.send(:"#{prefix}unique", *args)
        end
      when :foreign_key
        args = foreign_key2foreign_key_args(operand)
        generator.send(:"#{prefix}foreign_key", *args)
      when :index
        args = index2index_args(operand)
        generator.send(:"#{prefix}index", *args)
      else
        raise DbAgile::AssumptionFailedError, "Unknown script operation kind #{kind}"
    end
  }
end

#collapse_table(conn, op, buffer) ⇒ Object



51
52
53
54
55
56
57
58
59
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 51

def collapse_table(conn, op, buffer)
  gen = Sequel::Schema::AlterTableGenerator.new(conn)
  build_sequel_collapse_generator(conn, op, gen, "drop_")
  buffer << to_sql(conn, op, gen)
  staged!(op)
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#create_table(conn, op, buffer) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 31

def create_table(conn, op, buffer)
  gen = Sequel::Schema::Generator.new(conn)
  build_sequel_expand_generator(conn, op, gen, "")
  buffer << to_sql(conn, op, gen)
  staged!(op)
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#create_view(conn, op, buffer) ⇒ Object



13
14
15
16
17
18
19
20
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 13

def create_view(conn, op, buffer)
  tname = conn.send(:quote_schema_table, op.table_name)
  buffer <<  "CREATE VIEW #{tname} AS #{op.relview.definition};" << "\n"
  staged!(op)
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#drop_table(conn, op, buffer) ⇒ Object

Drops table



62
63
64
65
66
67
68
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 62

def drop_table(conn, op, buffer)
  buffer << conn.send(:drop_table_sql, op.table_name) << ";\n"
  op.relvar.visit{|obj,parent| op.staged!(obj)}
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#drop_view(conn, op, buffer) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 22

def drop_view(conn, op, buffer)
  tname = conn.send(:quote_schema_table, op.table_name)
  buffer <<  "DROP VIEW #{tname};" << "\n"
  staged!(op)
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#expand_table(conn, op, buffer) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 41

def expand_table(conn, op, buffer)
  gen = Sequel::Schema::AlterTableGenerator.new(conn)
  build_sequel_expand_generator(conn, op, gen, "add_")
  buffer << to_sql(conn, op, gen)
  staged!(op)
rescue Sequel::Error => ex
  buffer << "-- UNSUPPORTED: #{op.to_sql92}" << "\n"
  unsupported!(op)
end

#script2sql(conn, script, buffer) ⇒ Object

Converts an abstract script to a concrete one



8
9
10
11
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 8

def script2sql(conn, script, buffer)
  script.each{|op| self.send(op.kind, conn, op, buffer)}
  buffer
end

#staged!(op) ⇒ Object

Mark objects as staged for an operation



71
72
73
74
75
76
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 71

def staged!(op)
  if op.supports_sub_operation?(nil)
    op.each_sub_operation{|kind, operand| op.staged!(operand)}
  end
  op.staged!(op.relvar)
end

#to_sql(conn, op, generator) ⇒ Object

Converts a sequel generator to SQL for a given operation



134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 134

def to_sql(conn, op, generator)
  case generator
    when Sequel::Schema::Generator
      sql = conn.send(:create_table_sql, op.table_name, generator, {})
      sql = sql + ";\n"
      sql
    when Sequel::Schema::AlterTableGenerator
      sql = conn.send(:alter_table_sql_list, op.table_name, generator.operations)
      sql = sql.flatten.join(";\n")
      sql + ";\n"
    else
      raise DbAgile::AssumptionFailedError, "Unknown generator #{gen.class}"
  end
end

#unsupported!(op) ⇒ Object

Mark objects as not staged for an operation



79
80
81
82
83
84
# File 'lib/dbagile/adapter/sequel/schema/concrete_script.rb', line 79

def unsupported!(op)
  if op.supports_sub_operation?(nil)
    op.each_sub_operation{|kind, operand| op.not_staged!(operand)}
  end
  op.not_staged!(op.relvar)
end