Class: RTM::OntopiaRdbms
- Defined in:
- lib/rtm/ontopia.rb
Class Method Summary collapse
-
.connect_with(config) ⇒ Object
Provides a given block with a (temporary) connection using the given configuration.
-
.migrate_database(config) ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema.
- .migrate_database_custom(name, params = {}) ⇒ Object
-
.migrate_database_down(params = {}) ⇒ Object
Drops the Ontopia schema from the database.
- .migrate_database_force_down(params = {}) ⇒ Object
Instance Method Summary collapse
- #check ⇒ Object
- #config(params = @params) ⇒ Object
-
#initialize(params = {}) ⇒ OntopiaRdbms
constructor
A new instance of OntopiaRdbms.
-
#migrate ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema.
- #migrate_custom ⇒ Object
-
#migrate_database ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema.
- #migrate_database_custom(name) ⇒ Object
-
#migrate_database_down ⇒ Object
Drops the Ontopia schema from the database.
- #migrate_database_force_down ⇒ Object
-
#migrate_down ⇒ Object
Drops the Ontopia schema from the database.
- #migrate_force_down ⇒ Object
Methods inherited from Ontopia
Constructor Details
#initialize(params = {}) ⇒ OntopiaRdbms
Returns a new instance of OntopiaRdbms.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/rtm/ontopia.rb', line 45 def initialize(params={}) require 'active_record' unless defined?(ActiveRecord) require 'jdbc_adapter' unless defined?(ActiveRecord::ConnectionAdapters::Jdbc) params2 = params || {} # XXX hack: maybe setting params in Engine#initialize is not the best idea # enhance all ontopia properties from rails-style config ontopia_properties = RTM::Ontopia::Rdbms::Properties.rails2ontopia(config(params2)) # FIXME: this works only if properties was not specified as file params2[:properties] ||= {} ontopia_properties.each do |k,v| params2[:properties][k] = v.to_s unless params2[:properties].key?(k) end super @params.merge(params2) end |
Class Method Details
.connect_with(config) ⇒ Object
Provides a given block with a (temporary) connection using the given configuration. Either an existing connection is used (and kept open) or a new connection is opened, yielded to the block and then closed again.
This is needed for migrations and for the connection check. Within the connection check it loads the needed JDBC connectors via ActiveRecord.
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/rtm/ontopia.rb', line 164 def self.connect_with(config) require 'active_record' unless defined?(ActiveRecord) require 'jdbc_adapter' unless defined?(ActiveRecord::ConnectionAdapters::Jdbc) # prepare variable for separate connection class (if needed) anonymous_active_record_base = nil # use a supplied connection or create one using the config connection = config[:connection] unless connection # create a new anonymous class extending ActiveRecord::Base for a separate connection anonymous_active_record_base = Class.new(ActiveRecord::Base) # connect to backend using the anonymous base class anonymous_active_record_base.establish_connection(config) connection = anonymous_active_record_base.connection end begin yield connection if block_given? ensure # close connection if we created a new one anonymous_active_record_base.remove_connection if anonymous_active_record_base end end |
.migrate_database(config) ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema. Uses a Rails-style database configuration hash to connect to the database.
Additional hash-keys to override the defaults:
:connection => an existing connection to be used instead of creating using given parameters
:schema_sql => directly supply sql to execute instead of :schema_file or autodetect
:schema_file => directly supply sql file to execute instead of autodetect
:adapter_schema_name => directly supply the schema name (generic, h2, mysql, oracle8, oracle9i, oracle10g, postresql, sqlserver)
:direction => :up|:down create or drop schema (defaults to :up)
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/rtm/ontopia.rb', line 99 def self.migrate_database(config) res = nil connect_with(config) do |connection| # see if we were provided with a schema directly or get it from file schema_sql = config[:schema_sql] unless schema_sql # should we migrate up or down? direction = config[:direction] || :up create_or_drop = case direction.to_s when "up" "create" when "down" "drop" when "force_down" "force_drop" else direction.to_s end # get the adapter name for ontopia from the config adapter_schema_name = config[:adapter_schema_name] || config[:adapter].sub(/^jdbc/, '') # check if it is one of the available adapters available_schemas = %w[generic h2 mysql oracle8 oracle9i oracle10g postresql sqlserver] adapter_schema_name = "generic" unless available_schemas.include?(adapter_schema_name) # find the corresponding schema file schema_file = config[:schema_file] || File.join(File.dirname(__FILE__), "/../../res/rdbms/setup/#{adapter_schema_name}.#{create_or_drop}.sql") raise "Could not find schema definition file #{File.(schema_file)}." unless File.exist?(schema_file) # read the schema schema_sql = File.read(schema_file) end # # execute the schema # res = connection.execute(schema_sql) # execute the schema, statement by statement statement_list = schema_sql.split(/;/).map(&:strip).reject{|s| s.blank?} statement_list.each do |statement| connection.execute(statement) end end res end |
.migrate_database_custom(name, params = {}) ⇒ Object
153 154 155 |
# File 'lib/rtm/ontopia.rb', line 153 def self.migrate_database_custom(name, params={}) migrate_database(params.merge(:direction => name)) end |
.migrate_database_down(params = {}) ⇒ Object
Drops the Ontopia schema from the database. This corresponds to migrating the database down.
145 146 147 |
# File 'lib/rtm/ontopia.rb', line 145 def self.migrate_database_down(params={}) migrate_database(params.merge(:direction => :down)) end |
.migrate_database_force_down(params = {}) ⇒ Object
149 150 151 |
# File 'lib/rtm/ontopia.rb', line 149 def self.migrate_database_force_down(params={}) migrate_database(params.merge(:direction => :force_down)) end |
Instance Method Details
#check ⇒ Object
190 191 192 193 194 195 196 |
# File 'lib/rtm/ontopia.rb', line 190 def check check_ok = false self.class.connect_with(config) do |connection| check_ok = true if connection.table_exists?('tm_topic_map') || connection.table_exists?('TM_TOPIC_MAP') # MySQL needs ALLCAPS, but e.g. H2 needs small letters. Dunno why. end check_ok end |
#config(params = @params) ⇒ Object
62 63 64 65 |
# File 'lib/rtm/ontopia.rb', line 62 def config(params = @params) # if no config-option is given, use params directly, but clean it up a bit params[:config] || params.reject{|k,v| k == :identifier || k == :backend || k == :implementation} end |
#migrate ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema. Uses the connection data provided with connect
71 72 73 |
# File 'lib/rtm/ontopia.rb', line 71 def migrate_database self.class.migrate_database(config) end |
#migrate_custom ⇒ Object
87 88 89 |
# File 'lib/rtm/ontopia.rb', line 87 def migrate_database_custom(name) self.class.migrate_database_custom(name, config) end |
#migrate_database ⇒ Object
Migrate the Database to contain the Ontopia RDBMS schema. Uses the connection data provided with connect
68 69 70 |
# File 'lib/rtm/ontopia.rb', line 68 def migrate_database self.class.migrate_database(config) end |
#migrate_database_custom(name) ⇒ Object
84 85 86 |
# File 'lib/rtm/ontopia.rb', line 84 def migrate_database_custom(name) self.class.migrate_database_custom(name, config) end |
#migrate_database_down ⇒ Object
Drops the Ontopia schema from the database. This corresponds to migrating the database down.
74 75 76 |
# File 'lib/rtm/ontopia.rb', line 74 def migrate_database_down self.class.migrate_database_down(config) end |
#migrate_database_force_down ⇒ Object
79 80 81 |
# File 'lib/rtm/ontopia.rb', line 79 def migrate_database_force_down self.class.migrate_database_force_down(config) end |
#migrate_down ⇒ Object
Drops the Ontopia schema from the database. This corresponds to migrating the database down.
77 78 79 |
# File 'lib/rtm/ontopia.rb', line 77 def migrate_database_down self.class.migrate_database_down(config) end |
#migrate_force_down ⇒ Object
82 83 84 |
# File 'lib/rtm/ontopia.rb', line 82 def migrate_database_force_down self.class.migrate_database_force_down(config) end |