From: David Lutterkort <[email protected]>
Also streamlines how we create/retrieve the current DB
---
server/lib/db.rb | 16 ++++++++++------
server/lib/initializers/database_initialize.rb | 17 +++++++----------
2 files changed, 17 insertions(+), 16 deletions(-)
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 0cd18e4..7371438 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -15,19 +15,23 @@
module Deltacloud
- def self.database(opts={})
+ def self.connect(location)
if ENV['API_VERBOSE']
if Deltacloud.respond_to? :config
- opts[:logger] = Deltacloud.config[:cimi].logger
+ logger = Deltacloud.config[:cimi].logger
else
- opts[:logger] = ::Logger.new($stdout)
+ logger = ::Logger.new($stdout)
end
end
- @db ||= Sequel.connect(DATABASE_LOCATION, opts)
+ @db = Sequel.connect(location, :logger => logger)
end
- def self.initialize_database
- db = database
+ def self.database
+ @db
+ end
+
+ def self.initialize_database(location)
+ db = connect(location)
db.create_table?(:providers) {
primary_key :id
diff --git a/server/lib/initializers/database_initialize.rb
b/server/lib/initializers/database_initialize.rb
index 7ec68aa..13f2904 100644
--- a/server/lib/initializers/database_initialize.rb
+++ b/server/lib/initializers/database_initialize.rb
@@ -36,19 +36,16 @@ Sequel.extension :migration
# For more details about possible values see:
# http://sequel.rubyforge.org/rdoc/files/doc/opening_databases_rdoc.html
#
-if ENV['DATABASE_LOCATION']
- DATABASE_LOCATION = ENV['DATABASE_LOCATION']
-else
+unless location = ENV['DATABASE_LOCATION']
if ENV['RACK_ENV'] == 'test'
if RUBY_PLATFORM=='java'
- DATABASE_LOCATION = 'jdbc:sqlite::memory'
+ location = 'jdbc:sqlite::memory'
else
- DATABASE_LOCATION = 'sqlite:/'
+ location = 'sqlite:/'
end
else
sequel_driver = (RUBY_PLATFORM=='java') ? 'jdbc:sqlite:' : 'sqlite://'
- DATABASE_LOCATION =
- "#{sequel_driver}#{File.join(BASE_STORAGE_DIR, 'db.sqlite')}"
+ location = "#{sequel_driver}#{File.join(BASE_STORAGE_DIR, 'db.sqlite')}"
end
end
@@ -57,7 +54,7 @@ if RUBY_PLATFORM == 'java'
Jdbc::SQLite3.load_driver
end
-DATABASE = Deltacloud::initialize_database
+database = Deltacloud::initialize_database(location)
# Detect if there are some pending migrations to run.
# We don't actually run migrations during server startup, just print
@@ -66,12 +63,12 @@ DATABASE = Deltacloud::initialize_database
DATABASE_MIGRATIONS_DIR = File.join(File.dirname(__FILE__), '..', '..', 'db',
'migrations')
-unless Sequel::Migrator.is_current?(DATABASE, DATABASE_MIGRATIONS_DIR)
+unless Sequel::Migrator.is_current?(database, DATABASE_MIGRATIONS_DIR)
# Do not exit when this intitializer is included from deltacloud-db-upgrade
# script
#
if ENV['RACK_ENV'] == 'test' || ENV['DB_UPGRADE']
- Sequel::Migrator.apply(DATABASE, DATABASE_MIGRATIONS_DIR)
+ Sequel::Migrator.apply(database, DATABASE_MIGRATIONS_DIR)
else
warn "WARNING: The database needs to be upgraded. Run:
'deltacloud-db-upgrade' command."
exit(1)
--
1.8.1.4