Sha256: 412395301f6a604a39f0d52a8e70f6e38bdae684a3eb1ac38b59477dbdff35b7

Contents?: true

Size: 1.58 KB

Versions: 2

Compression:

Stored size: 1.58 KB

Contents

# Copyright: Copyright 2009 Topic Maps Lab, University of Leipzig.
# License:   Apache License, Version 2.0

module RTM::AR
  module RdbmsConfig

    # This function migrates the database schema using the default
    # ActiveRecord connection.
    def migrate_database
      TMDM::InitialSchema.migrate(:up)
    end

    # 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.
    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

    # Connects Active Record to a database or in Memory database if no parameters given.
    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
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
rtm-activerecord-0.3.1 lib/rtm/activerecord/rdbms_config.rb
rtm-activerecord-0.3.0 lib/rtm/activerecord/rdbms_config.rb