Module: Earth::Model::Schema

Defined in:
lib/earth/model.rb

Constant Summary collapse

COMMENT =
%r{/\*(?:.|[\r\n])*?\*/}
WHITESPACE =
/\s+/
SEMICOLON =
/ ?; ?/

Instance Method Summary collapse

Instance Method Details

#create_table!(force = true) ⇒ Object



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
# File 'lib/earth/model.rb', line 37

def create_table!(force = true)
  c = ActiveRecord::Base.connection_pool.checkout

  if c.table_exists?(table_name)
    return unless force
    c.drop_table table_name
  end

  statements = const_get(:TABLE_STRUCTURE).gsub(COMMENT, '').gsub(WHITESPACE, ' ').split(SEMICOLON).select(&:present?)

  statements.each do |sql|
    c.execute sql
  end

  # safely reset column information
  if c.respond_to?(:schema_cache)
    c.schema_cache.clear!
  end
  reset_column_information
  descendants.each do |descendant|
    descendant.reset_column_information
  end

  nil
ensure
  ActiveRecord::Base.connection_pool.checkin c
end