Class: DbAgile::SequelAdapter::Schema::PhysicalDump

Inherits:
Object
  • Object
show all
Defined in:
lib/dbagile/adapter/sequel/schema/physical_dump.rb

Overview

Loads a schema from an existing database through Sequel

Instance Method Summary collapse

Instance Method Details

#dbtype_to_ruby_type(info) ⇒ Object

Returns the ruby type associated to a given column info



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 10

def dbtype_to_ruby_type(info)
  type = info[:type]
  capitalized = type.to_s.capitalize
  begin
    Kernel.eval(capitalized)
  rescue NameError
    case type
      when :datetime
        Time
      when :boolean
        SByC::TypeSystem::Ruby::Boolean
      else
        Object
    end
  end
end

#load_logical_schema(conn, builder) ⇒ Object



37
38
39
40
41
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 37

def load_logical_schema(conn, builder)
  conn.tables.each{|table|
    load_table_schema(conn, builder, table)
  }
end

#load_physical_schema(conn, builder) ⇒ Object

Loads the physical schema



92
93
94
95
96
97
98
99
100
101
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 92

def load_physical_schema(conn, builder)
  builder.indexes{
    conn.tables.each{|table|
      conn.indexes(table).each_pair{|name, defn|
        next if defn[:unique]
        builder.index(name, {:relvar => table, :attributes => defn[:columns]})
      }
    }
  }
end

#load_table_constraints(conn, builder, table, primary_key_columns = nil) ⇒ Object

Loads table constraint



79
80
81
82
83
84
85
86
87
88
89
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 79

def load_table_constraints(conn, builder, table, primary_key_columns = nil)
  builder.constraints{
    unless primary_key_columns.nil? or primary_key_columns.empty?
      builder.constraint(:pk, {:type => :primary_key, :attributes => primary_key_columns})
    end
    conn.indexes(table).each_pair{|name, defn|
      next unless defn[:unique]
      builder.constraint(name, {:type => :candidate_key, :attributes => defn[:columns]})
    }
  }
end

#load_table_heading(conn, builder, table) ⇒ Object

Loads a table heading. Returns primary key columns



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 52

def load_table_heading(conn, builder, table)
  primary_key_columns = []
  builder.heading{
    columns = conn.schema(table, {:reload => true})
    columns.each do |name, info|
      #puts info.inspect
      
      # find attribute definition
      defn = {:domain    => dbtype_to_ruby_type(info),
              :mandatory => !info[:allow_null] }
      unless info[:ruby_default].nil?
        defn[:default] = info[:ruby_default]
      end
      
      # mark primary key columns
      if primary_key_columns and info[:primary_key]
        primary_key_columns << name 
      end
      
      # build the attribute
      builder.attribute(name, defn)
    end
  }
  primary_key_columns
end

#load_table_schema(conn, builder, table) ⇒ Object

Loads a table schema



44
45
46
47
48
49
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 44

def load_table_schema(conn, builder, table)
  builder.relvar(table){
    primary_key_columns = load_table_heading(conn, builder, table)
    load_table_constraints(conn, builder, table, primary_key_columns)
  }
end

#run(conn, identifier) ⇒ Object

Runs the loading process and returns the schema instance



28
29
30
31
32
33
34
35
# File 'lib/dbagile/adapter/sequel/schema/physical_dump.rb', line 28

def run(conn, identifier)
  builder = DbAgile::Core::Schema::builder
  builder.schema(identifier){
    builder.logical{ load_logical_schema(conn, builder) }
    builder.physical{ load_physical_schema(conn, builder) }
  }
  builder._dump
end