lib/sequel_rails/configuration.rb in sequel-rails-0.4.0 vs lib/sequel_rails/configuration.rb in sequel-rails-0.4.1
- old
+ new
@@ -3,11 +3,16 @@
module SequelRails
mattr_accessor :configuration
def self.setup(environment)
- ::Sequel.connect configuration.environment_for environment.to_s
+ config = configuration.environment_for(environment.to_s)
+ if config['url']
+ ::Sequel.connect config['url'], config
+ else
+ ::Sequel.connect config
+ end
end
class Configuration
def self.for(root, database_yml_hash)
@@ -21,11 +26,11 @@
def environment_for(name)
environments[name.to_s] || environments[name.to_sym]
end
def environments
- @environments ||= @raw.inject({}) do |normalized, environment|
+ @environments ||= raw.inject({}) do |normalized, environment|
name, config = environment.first, environment.last
normalized[name] = normalize_repository_config(config)
normalized
end
end
@@ -37,26 +42,54 @@
end
def normalize_repository_config(hash)
config = {}
hash.each do |key, value|
- config[key.to_s] =
+ config[key.to_s] =
if key.to_s == 'port'
value.to_i
elsif key.to_s == 'adapter' && value == 'sqlite3'
'sqlite'
- elsif key.to_s == 'database' && (hash['adapter'] == 'sqlite3' ||
+ elsif key.to_s == 'database' && (hash['adapter'] == 'sqlite3' ||
hash['adapter'] == 'sqlite' ||
hash[:adapter] == 'sqlite3' ||
hash[:adapter] == 'sqlite')
value == ':memory:' ? value : File.expand_path((hash['database'] || hash[:database]), root)
elsif key.to_s == 'adapter' && value == 'postgresql'
'postgres'
else
value
end
end
-
+
+ # always use jdbc when running jruby
+ if SequelRails.jruby?
+ if config['adapter']
+ case config['adapter'].to_sym
+ when :postgres
+ config['adapter'] = :postgresql
+ end
+ config['adapter'] = "jdbc:#{config['adapter']}"
+ end
+ end
+
+ # some adapters only support an url
+ if config['adapter'] && config['adapter'] =~ /^(jdbc|do):/
+ params = {}
+ config.each do |k, v|
+ next if ['adapter', 'host', 'port', 'database'].include?(k)
+ params[k] = v
+ end
+ params_str = params.map { |k, v| "#{k}=#{v}" }.join('&')
+ port = config['port'] ? ":#{config['port']}" : ''
+ config['url'] = case config['adapter']
+ when /sqlite/
+ "%s:%s" % [config['adapter'], config['database']]
+ else
+ "%s://%s%s/%s?%s" % [config['adapter'], config['host'], port, config['database'], params_str]
+ end
+ end
+
config
end
end