Module: RTM::AR::RdbmsConfig

Included in:
TopicMapSystem
Defined in:
lib/rtm/activerecord/rdbms_config.rb

Instance Method Summary collapse

Instance Method Details

#connect(*args) ⇒ Object

Connects Active Record to a database or in Memory database if no parameters given.



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rtm/activerecord/rdbms_config.rb', line 26

def connect(*args)
  base_class = ::ActiveRecord::Base
  puts("WARNING: ActiveRecord::Base is already connected. Your're now disconnected and reconnected using the RTM connection you specified.") if connected?(base_class)

  if args.size == 0
    base_class.establish_connection(
      :adapter => "#{"jdbc" if PLATFORM && PLATFORM =~ /java/}sqlite3",
      :database  => ":memory:"
    )
    no_output do
      migrate_database
    end
  else
    base_class.establish_connection(*args)
  end
end

#connected?(base_class = ::ActiveRecord::Base) ⇒ Boolean

Checks if there is already an ActiveRecord connection of if one was configured. To do so, it’s not enough to call connected? but we also need to try to access something, as Rails will use the preconfigured database then.

Returns:

  • (Boolean)


16
17
18
19
20
21
22
23
# File 'lib/rtm/activerecord/rdbms_config.rb', line 16

def connected?(base_class=::ActiveRecord::Base)
  begin
    RTM::AR::TMDM::TopicMap.first # this is needed because Rails establishes the connection upon first usage
  rescue
    # puts "GOT AN EXCEPTION. I.e. we were not already connected and there was no connection configured -- or we were connected, but the topic_maps table was missing."
  end
  base_class.connected?
end

#migrate_databaseObject

This function migrates the database schema using the default ActiveRecord connection.



9
10
11
# File 'lib/rtm/activerecord/rdbms_config.rb', line 9

def migrate_database
  TMDM::InitialSchema.migrate(:up)
end