deltacloud-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mfoj...@redhat.com
Subject [PATCH core 1/8] CIMI: Replaced DataMapper with Sequel ORM
Date Wed, 16 Jan 2013 11:58:26 GMT
From: Michal Fojtik <mfojtik@redhat.com>


Signed-off-by: Michal fojtik <mfojtik@redhat.com>
---
 server/deltacloud-core.gemspec             |  5 +-
 server/lib/cimi/helpers/database_helper.rb | 19 +++++--
 server/lib/db.rb                           | 81 +++++++++++++++++++++++-------
 3 files changed, 80 insertions(+), 25 deletions(-)

diff --git a/server/deltacloud-core.gemspec b/server/deltacloud-core.gemspec
index 7fad5be..977cc26 100644
--- a/server/deltacloud-core.gemspec
+++ b/server/deltacloud-core.gemspec
@@ -78,9 +78,8 @@ Gem::Specification.new do |s|
   s.add_dependency('nokogiri', '>= 1.4.3')
   s.add_dependency('require_relative') if RUBY_VERSION < '1.9'
 
-  s.add_dependency('data_mapper')
-  s.add_dependency('dm-sqlite-adapter')
-  s.add_dependency('dm-timestamps')
+  s.add_dependency('sequel', '<= 3.42.0')
+  s.add_dependency('sqlite3')
 
   # dependencies for various cloud providers:
 
diff --git a/server/lib/cimi/helpers/database_helper.rb b/server/lib/cimi/helpers/database_helper.rb
index ac6ae4f..d432f69 100644
--- a/server/lib/cimi/helpers/database_helper.rb
+++ b/server/lib/cimi/helpers/database_helper.rb
@@ -27,11 +27,14 @@ module Deltacloud
       def delete_attributes_for(model)
         return if test_environment?
         entity = get_entity(model)
-        !entity.nil? && entity.destroy!
+        !entity.nil? && entity.destroy
       end
 
       def get_entity(model)
-        Deltacloud::Database::Entity.first(:be_kind => model.to_entity, :be_id => model.id,
'provider.driver' => driver_symbol.to_s, 'provider.url' => current_provider)
+        current_db.entities_dataset.first(
+          :be_kind => model.to_entity,
+          :be_id => model.id,
+        )
       end
 
       def current_provider
@@ -43,23 +46,29 @@ module Deltacloud
       #
 
       def current_db
-        Deltacloud::Database::Provider.first_or_create(:driver => driver_symbol.to_s,
:url => current_provider)
+        Deltacloud::Database::Provider.find_or_create(:driver => driver_symbol.to_s, :url
=> current_provider)
       end
 
       def store_attributes_for(model, attrs={})
         return if test_environment? or model.nil? or attrs.empty?
         return if model.id.nil?
-        entity = get_entity(model) || current_db.entities.new(:be_kind => model.to_entity,
:be_id => model.id)
+
+        unless entity = get_entity(model)
+          entity = Deltacloud::Database::Entity.new(:provider_id => current_db.id)
+        end
 
         entity.description = extract_attribute_value('description', attrs) if attrs.has_key?
'description'
         entity.name = extract_attribute_value('name', attrs) if attrs.has_key? 'name'
+
         if attrs.has_key? 'properties'
           entity.ent_properties = extract_attribute_value('properties', attrs).to_json
         elsif attrs.has_key? 'property'
           entity.ent_properties = extract_attribute_value('property', attrs).to_json
         end
 
-        entity.save && entity
+        entity.exists? ? entity.save_changes : entity.save
+
+        entity
       end
 
       # In XML serialization the values stored in attrs are arrays, dues to
diff --git a/server/lib/db.rb b/server/lib/db.rb
index 567b356..64cc283 100644
--- a/server/lib/db.rb
+++ b/server/lib/db.rb
@@ -4,27 +4,74 @@ module Deltacloud
     ENV['RACK_ENV'] == 'test' || ENV['DELTACLOUD_NO_DATABASE']
   end
 
-  unless test_environment?
-    require 'data_mapper'
-    require_relative './db/provider'
-    require_relative './db/entity'
-    require_relative './db/machine_template'
-    require_relative './db/address_template'
-    require_relative './db/volume_configuration'
-    require_relative './db/volume_template'
-  end
+  require 'sequel' unless test_environment?
+  require 'logger'
+
+  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] ||
+    'sqlite://'+File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}", 'db.sqlite')
 
-  DATABASE_LOCATION = ENV['DATABASE_LOCATION'] || File.join('/', 'var', 'tmp', "deltacloud-mock-#{ENV['USER']}",
'db.sqlite')
+  def self.database(opts={})
+    opts[:logger] = ::Logger.new($stdout) if ENV['API_VERBOSE']
+    @db ||=  Sequel.connect(DATABASE_LOCATION, opts)
+  end
 
   def self.initialize_database
-    DataMapper::Logger.new($stdout, :debug) if ENV['API_VERBOSE']
-    dbdir = File::dirname(DATABASE_LOCATION)
-    FileUtils::mkdir(dbdir) unless File::directory?(dbdir)
-    DataMapper::setup(:default, "sqlite://#{DATABASE_LOCATION}")
-    DataMapper::finalize
-    DataMapper::auto_upgrade!
+
+    database.create_table?(:providers) {
+      primary_key :id
+
+      column :driver, :string, { :null => false }
+      column :url, :string
+      index [ :url, :driver ]
+    }
+
+    database.create_table?(:entities) {
+      primary_key :id
+      foreign_key :provider_id, :providers, { :index => true, :null => false }
+      column :created_at, :timestamp
+
+      # Base
+      column :model, :string, { :index => true, :null => false, :default => 'entity'
}
+
+      # Map Entity to Deltacloud model
+      # (like: Machine => Instance)
+      column :be_kind, :string
+      column :be_id, :string
+
+      # Entity
+      column :name, :string
+      column :description, :string
+      column :ent_properties, :string, { :text => true }
+
+
+      column :machine_config, :string
+      column :machine_image, :string
+
+      column :network, :string
+      column :ip, :string
+      column :hostname, :string
+      column :allocation, :string
+      column :default_gateway, :string
+      column :dns, :string
+      column :protocol, :string
+      column :mask, :string
+
+      column :format, :string
+      column :capacity, :string
+
+      column :volume_config, :string
+      column :volume_image, :string
+    }
   end
 
 end
 
-Deltacloud::initialize_database unless Deltacloud.test_environment?
+unless Deltacloud.test_environment?
+  Deltacloud::initialize_database
+  require_relative './db/provider'
+  require_relative './db/entity'
+  require_relative './db/machine_template'
+  require_relative './db/address_template'
+  require_relative './db/volume_configuration'
+  require_relative './db/volume_template'
+end
-- 
1.8.0.2


Mime
View raw message