Class: Blackbird::Schema::Loader

Inherits:
Object
  • Object
show all
Defined in:
lib/blackbird/schema/loader.rb

Class Method Summary (collapse)

Instance Method Summary (collapse)

Class Method Details

+ (Object) load



3
4
5
# File 'lib/blackbird/schema/loader.rb', line 3

def self.load
  new.load
end

Instance Method Details

- (Object) load



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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
68
69
70
71
# File 'lib/blackbird/schema/loader.rb', line 7

def load
  schema = Blackbird::Schema.new
  builder = Blackbird::Schema::Builder.new(schema)

  schema.patches = []

  connection.tables.each do |table|

    if table == 'blackbird_patches'
      schema.patches = connection.select_values(
        %{ SELECT name FROM blackbird_patches })
    end

    pk_name = connection.primary_key(table)
    has_pk  = !!pk_name

    builder.table(nil, table, :id => has_pk, :primary_key => pk_name) do |t|

      connection.columns(table).each do |column|
        next if column.name == pk_name

        options = {}

        if !column.null.nil?
          options[:null] = column.null
        else
          options[:null] = true
        end

        if column.limit and column.limit != 255
          options[:limit] = column.limit
        end

        if column.has_default?
          options[:default] = column.default
        end

        if column.type == :decimal
          options[:scale]     = column.scale
          options[:precision] = column.precision
        end

        t.column(column.name, column.type, options)

      end

      connection.indexes(table).each do |index|

        options = {
          :name => index.name
        }

        if index.unique
          options[:unique] = index.unique
        end

        t.index(index.columns, options)

      end

    end
  end

  schema
end