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


46
47
48
# File 'lib/ekylibre/schema.rb', line 46

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

.model_namesObject


50
51
52
# File 'lib/ekylibre/schema.rb', line 50

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

.modelsObject


18
19
20
# File 'lib/ekylibre/schema.rb', line 18

def models
  @models ||= read_models.freeze
end

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


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

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

.reset!Object


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

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

.rootObject


14
15
16
# File 'lib/ekylibre/schema.rb', line 14

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

.setup_extensionsObject


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

def setup_extensions
  # TODO: rename postgis schema to 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;'
  ActiveRecord::Base.connection.execute 'CREATE EXTENSION IF NOT EXISTS "unaccent" SCHEMA postgis;'
end

.table_namesObject


42
43
44
# File 'lib/ekylibre/schema.rb', line 42

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

.tablesObject


38
39
40
# File 'lib/ekylibre/schema.rb', line 38

def tables
  @tables ||= read_tables.freeze
end