Return-Path: X-Original-To: apmail-deltacloud-dev-archive@www.apache.org Delivered-To: apmail-deltacloud-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 639C3E7C1 for ; Thu, 7 Mar 2013 02:19:29 +0000 (UTC) Received: (qmail 74428 invoked by uid 500); 7 Mar 2013 02:19:29 -0000 Delivered-To: apmail-deltacloud-dev-archive@deltacloud.apache.org Received: (qmail 74413 invoked by uid 500); 7 Mar 2013 02:19:29 -0000 Mailing-List: contact dev-help@deltacloud.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@deltacloud.apache.org Delivered-To: mailing list dev@deltacloud.apache.org Received: (qmail 74404 invoked by uid 99); 7 Mar 2013 02:19:29 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Mar 2013 02:19:29 +0000 X-ASF-Spam-Status: No, hits=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of lutter@redhat.com designates 209.132.183.28 as permitted sender) Received: from [209.132.183.28] (HELO mx1.redhat.com) (209.132.183.28) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Mar 2013 02:19:25 +0000 Received: from int-mx01.intmail.prod.int.phx2.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id r272J4dH028436 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Wed, 6 Mar 2013 21:19:04 -0500 Received: from avon.watzmann.net (ovpn-112-44.phx2.redhat.com [10.3.112.44]) by int-mx01.intmail.prod.int.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id r272IuLv019980 for ; Wed, 6 Mar 2013 21:19:03 -0500 From: lutter@redhat.com To: dev@deltacloud.apache.org Subject: [PATCH 8/8] Remove global constants DATABASE and DATABASE_LOCATION Date: Wed, 6 Mar 2013 18:18:53 -0800 Message-Id: <1362622733-15361-9-git-send-email-lutter@redhat.com> In-Reply-To: <1362622733-15361-1-git-send-email-lutter@redhat.com> References: <1362622733-15361-1-git-send-email-lutter@redhat.com> X-Scanned-By: MIMEDefang 2.67 on 10.5.11.11 X-Virus-Checked: Checked by ClamAV on apache.org From: David Lutterkort 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