From 50fab833721a5820cf3c7ab1bb9c9c2813581066 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 28 Aug 2013 10:55:31 +0200 Subject: [PATCH] Bug #2276: onedb restore does not require the sqlite file to exist --- src/onedb/onedb_backend.rb | 76 +++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 37 deletions(-) diff --git a/src/onedb/onedb_backend.rb b/src/onedb/onedb_backend.rb index 6c37e7b865..e24bf97396 100644 --- a/src/onedb/onedb_backend.rb +++ b/src/onedb/onedb_backend.rb @@ -28,40 +28,42 @@ class OneDBBacKEnd def read_db_version connect_db - version = "2.0" - timestamp = 0 - comment = "" - - @db.fetch("SELECT version, timestamp, comment FROM db_versioning " + - "WHERE oid=(SELECT MAX(oid) FROM db_versioning)") do |row| - version = row[:version] - timestamp = row[:timestamp] - comment = row[:comment] - end - - return [version, timestamp, comment] - - rescue Exception => e - if e.class == Sequel::DatabaseConnectionError - raise e - elsif !db_exists? - # If the DB doesn't have db_version table, it means it is empty or a 2.x - raise "Database schema does not look to be created by " << - "OpenNebula: table user_pool is missing or empty." - end - begin - # Table image_pool is present only in 2.X DBs - @db.fetch("SELECT * FROM image_pool") { |row| } - rescue - raise "Database schema looks to be created by OpenNebula 1.X." << - "This tool only works with databases created by 2.X versions." + version = "2.0" + timestamp = 0 + comment = "" + + @db.fetch("SELECT version, timestamp, comment FROM db_versioning " + + "WHERE oid=(SELECT MAX(oid) FROM db_versioning)") do |row| + version = row[:version] + timestamp = row[:timestamp] + comment = row[:comment] + end + + return [version, timestamp, comment] + + rescue Exception => e + if e.class == Sequel::DatabaseConnectionError + raise e + elsif !db_exists? + # If the DB doesn't have db_version table, it means it is empty or a 2.x + raise "Database schema does not look to be created by " << + "OpenNebula: table user_pool is missing or empty." + end + + begin + # Table image_pool is present only in 2.X DBs + @db.fetch("SELECT * FROM image_pool") { |row| } + rescue + raise "Database schema looks to be created by OpenNebula 1.X." << + "This tool only works with databases created by 2.X versions." + end + + comment = "Could not read any previous db_versioning data, " << + "assuming it is an OpenNebula 2.0 or 2.2 DB." + + return [version, timestamp, comment] end - - comment = "Could not read any previous db_versioning data, " << - "assuming it is an OpenNebula 2.0 or 2.2 DB." - - return [version, timestamp, comment] end def history @@ -222,10 +224,6 @@ class BackEndSQLite < OneDBBacKEnd def initialize(file) @sqlite_file = file - - if !File.exists?(@sqlite_file) - raise "File #{@sqlite_file} doesn't exist" - end end def bck_file @@ -239,7 +237,7 @@ class BackEndSQLite < OneDBBacKEnd end def restore(bck_file, force=nil) - if !force + if File.exists?(@sqlite_file) && !force raise "File #{@sqlite_file} exists, use -f to overwrite." end @@ -250,6 +248,10 @@ class BackEndSQLite < OneDBBacKEnd private def connect_db + if !File.exists?(@sqlite_file) + raise "File #{@sqlite_file} doesn't exist" + end + begin @db = Sequel.sqlite(@sqlite_file) rescue Exception => e