Module: Ekylibre::Schema

Defined in:
lib/ekylibre/schema.rb,
lib/ekylibre/schema/column.rb

Defined Under Namespace

Classes: Column

Class Method Summary collapse

Class Method Details

.columns(table) ⇒ Object


44
45
46
# File 'lib/ekylibre/schema.rb', line 44

def columns(table)
  tables[table].values
end

.model_namesObject


48
49
50
# File 'lib/ekylibre/schema.rb', line 48

def model_names
  @model_names ||= models.collect { |m| m.to_s.camelcase.to_sym }.sort.freeze
end

.modelsObject


16
17
18
# File 'lib/ekylibre/schema.rb', line 16

def models
  @models ||= read_models.freeze
end

.references(table = nil, column = nil) ⇒ Object


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/ekylibre/schema.rb', line 20

def references(table = nil, column = nil)
  if table.present? && column.present?
    if t = tables[table]
      if c = t[column]
        return c.references
      end
    end
    return nil
  else
    return @references ||= tables.inject({}) do |h, table|
      h[table.first] = table.second
      h
    end.freeze
  end
end

.reset!Object


52
53
54
55
56
57
58
# File 'lib/ekylibre/schema.rb', line 52

def reset!
  @models = nil
  @model_names = nil
  @tables = nil
  @table_names = nil
  @references = nil
end

.rootObject


12
13
14
# File 'lib/ekylibre/schema.rb', line 12

def root
  Rails.root.join('db')
end

.setup_extensionsObject


6
7
8
9
10
# File 'lib/ekylibre/schema.rb', line 6

def setup_extensions
  ActiveRecord::Base.connection.execute 'CREATE SCHEMA IF NOT EXISTS postgis;'
  ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS postgis SCHEMA postgis;'
  ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "uuid-ossp" SCHEMA postgis;'
end

.table_namesObject


40
41
42
# File 'lib/ekylibre/schema.rb', line 40

def table_names
  @table_names ||= tables.keys.map(&:to_sym)
end

.tablesObject


36
37
38
# File 'lib/ekylibre/schema.rb', line 36

def tables
  @tables ||= read_tables.freeze
end