Class: WraithDB::Schema

Inherits:
ActiveRecord::Schema
  • Object
show all
Defined in:
lib/wraithdb/schema.rb

Constant Summary collapse

SCHEMA_REGEX =
/^ActiveRecord::Schema.define\(:version => \d+\) do/
END_REGEX =
/^end\s*\z/

Class Method Summary collapse

Class Method Details

.adapter_path(type) ⇒ Object



26
27
28
# File 'lib/wraithdb/schema.rb', line 26

def adapter_path(type)
  "#{File.expand_path('..', __FILE__)}/adapters/#{type}_adapter.rb"
end

.build_connectionObject



17
18
19
20
21
22
23
24
# File 'lib/wraithdb/schema.rb', line 17

def build_connection
  type = ActiveRecord::Base.configurations[Rails.env]['adapter']
  type = 'default' unless File.exist?(adapter_path(type))

  require adapter_path(type)

  WraithDB::Adapters.const_get("#{type.capitalize}Adapter").new
end

.connectionObject



11
12
13
14
15
# File 'lib/wraithdb/schema.rb', line 11

def connection
  @connection ||= build_connection
  load
  @connection
end

.loadObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/wraithdb/schema.rb', line 34

def load
  return if @loaded
  @loaded = true
  schema_locations = [
    ENV["SCHEMA"],
    "#{Rails.root}/db/#{Rails.env}_schema.rb",
    "#{Rails.root}/db/schema.rb"
  ]
  file = schema_locations.find { |f| f.present? && File.exist?(f) }
  source = File.read(file)
  if (source =~ SCHEMA_REGEX && source =~ END_REGEX)
    source.sub!(SCHEMA_REGEX, "")
    source.sub!(END_REGEX, "")
  else
    raise StandardError.new("Invalid format for #{file}.")
  end
  instance_eval(source)
end

.tablesObject



30
31
32
# File 'lib/wraithdb/schema.rb', line 30

def tables
  connection.tables
end

.write(*args) ⇒ Object



7
8
9
# File 'lib/wraithdb/schema.rb', line 7

def write(*args)
  # normally this would be noisy like a migration, this makes it quiet
end