Jason Guiditta
2008-Nov-05 14:03 UTC
[Ovirt-devel] [PATCH server] Simplified active_record_env to make less error-prone.
Too many requires to keep track of, this should make it easier until we rewrite it again. Signed-off-by: Jason Guiditta <jguiditt at redhat.com> --- src/dutils/active_record_env.rb | 80 ++++--------------------------- src/test/unit/active_record_env_test.rb | 31 ++++++++++++ 2 files changed, 41 insertions(+), 70 deletions(-) create mode 100644 src/test/unit/active_record_env_test.rb diff --git a/src/dutils/active_record_env.rb b/src/dutils/active_record_env.rb index afbf8f2..ad5e762 100644 --- a/src/dutils/active_record_env.rb +++ b/src/dutils/active_record_env.rb @@ -19,81 +19,21 @@ # also available at http://www.gnu.org/copyleft/gpl.html. $: << File.join(File.dirname(__FILE__), "../app") -$: << File.join(File.dirname(__FILE__), "../vendor/plugins/betternestedset/lib") +#$: << File.join(File.dirname(__FILE__), "../vendor/plugins/betternestedset/lib") require 'rubygems' -gem 'activeldap' +$LOAD_PATH << File.expand_path(File.dirname(__FILE__)) -require 'active_ldap' -require 'active_support' -require 'active_record' -require 'action_pack' -require 'action_controller' -require 'action_view' -require 'erb' +ENV['RAILS_ENV'] = 'production' || ENV['RAILS_ENV'] -OVIRT_DIR = "/usr/share/ovirt-server" +require File.dirname(__FILE__) + '/../config/boot' +require "#{RAILS_ROOT}/config/environment" -require "#{OVIRT_DIR}/vendor/plugins/betternestedset/init.rb" -require "#{OVIRT_DIR}/vendor/plugins/acts_as_xapian/lib/acts_as_xapian" - -def database_connect - yml = YAML::load(ERB.new(IO.read("#{OVIRT_DIR}/config/database.yml")).result) - rails_env = ENV['RAILS_ENV'] - rails_env = 'production' unless rails_env - dbconfig = yml[rails_env] - ActiveRecord::Base.establish_connection( - :adapter => dbconfig['adapter'], - :host => dbconfig['host'], - :username => dbconfig['username'], - :password => dbconfig['password'], - :database => dbconfig['database'] - ) +def database_connect(environment) + conf = YAML::load(File.open(File.dirname(__FILE__) + '/../config/database.yml')) + ActiveRecord::Base.establish_connection(conf[environment]) end -database_connect - -require 'models/account.rb' -require 'models/pool.rb' -require 'models/permission.rb' -require 'models/quota.rb' - -require 'models/hardware_pool.rb' -require 'models/directory_pool.rb' -require 'models/smart_pool.rb' -require 'models/host.rb' -require 'models/cpu.rb' -require 'models/boot_type.rb' -require 'models/bonding.rb' -require 'models/bonding_type.rb' -require 'models/nic.rb' - -require 'models/vm_resource_pool.rb' -require 'models/vm.rb' - -require 'models/task' -require 'models/host_task.rb' -require 'models/storage_task.rb' -require 'models/vm_task.rb' - -require 'models/storage_pool.rb' -require 'models/iscsi_storage_pool.rb' -require 'models/nfs_storage_pool.rb' -require 'models/lvm_storage_pool.rb' - -require 'models/storage_volume.rb' -require 'models/iscsi_storage_volume.rb' -require 'models/nfs_storage_volume.rb' -require 'models/lvm_storage_volume.rb' -require 'models/smart_pool.rb' -require 'models/smart_pool_tag.rb' - -require 'models/ip_address.rb' -require 'models/ip_v4_address.rb' -require 'models/ip_v6_address.rb' -require 'models/network.rb' -require 'models/physical_network.rb' -require 'models/usage.rb' -require 'models/vlan.rb' - +# Open ActiveRecord connection +database_connect(ENV['RAILS_ENV']) \ No newline at end of file diff --git a/src/test/unit/active_record_env_test.rb b/src/test/unit/active_record_env_test.rb new file mode 100644 index 0000000..e5fe064 --- /dev/null +++ b/src/test/unit/active_record_env_test.rb @@ -0,0 +1,31 @@ +# +# Copyright (C) 2008 Red Hat, Inc. +# Written by Jason Guiditta <jguiditt at redhat.com> +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; version 2 of the License. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, +# MA 02110-1301, USA. A copy of the GNU General Public License is +# also available at http://www.gnu.org/copyleft/gpl.html. + +require File.dirname(__FILE__) + '/../test_helper' +require File.dirname(__FILE__) + '/../../dutils/active_record_env' + +class ActiveRecordEnvTest < Test::Unit::TestCase + fixtures :pools, :storage_pools, :hosts, :cpus, :vms, :tasks + + def test_can_find_hosts + database_connect(ENV["RAILS_ENV"]) + hosts = Host.find(:all, :limit => 2) + assert_not_nil hosts, 'you have no hosts list!' + end +end -- 1.5.6.5
Chris Lalancette
2008-Nov-05 15:03 UTC
[Ovirt-devel] [PATCH server] Simplified active_record_env to make less error-prone.
Jason Guiditta wrote:> Too many requires to keep track of, this should make it easier until we rewrite it again. > > Signed-off-by: Jason Guiditta <jguiditt at redhat.com> > --- > src/dutils/active_record_env.rb | 80 ++++--------------------------- > src/test/unit/active_record_env_test.rb | 31 ++++++++++++ > 2 files changed, 41 insertions(+), 70 deletions(-) > create mode 100644 src/test/unit/active_record_env_test.rbOK, I've done some basic testing of this, and it seems to work. As Jason explained to me, basically all of the things we were doing we get for free by loading up the Rails environment. Taskomatic seems happy with it too. ACK Chris Lalancette
Chris Lalancette
2008-Nov-05 15:17 UTC
[Ovirt-devel] [PATCH server] Simplified active_record_env to make less error-prone.
Jason Guiditta wrote:> Too many requires to keep track of, this should make it easier until we rewrite it again. > > Signed-off-by: Jason Guiditta <jguiditt at redhat.com> > --- > src/dutils/active_record_env.rb | 80 ++++--------------------------- > src/test/unit/active_record_env_test.rb | 31 ++++++++++++ > 2 files changed, 41 insertions(+), 70 deletions(-) > create mode 100644 src/test/unit/active_record_env_test.rbCommitted. -- Chris Lalancette