1
0
mirror of https://github.com/OpenNebula/one.git synced 2024-12-23 17:33:56 +03:00

Feature #4305: Add onedb sqlite2mysql command

(cherry picked from commit f9c950367b)
This commit is contained in:
Jaime Melis 2016-01-21 17:56:02 +01:00
parent d9acd68e09
commit 3ff13fbcdc
4 changed files with 94 additions and 1 deletions

View File

@ -356,4 +356,24 @@ cmd=CommandParser::CmdParser.new(ARGV) do
end end
end end
###########################################################################
# Migrate SQLite to MySQL
###########################################################################
sqlite2mysql_desc = <<-EOT.unindent
Migrates a SQLite OpenNebula Database to MySQL
EOT
command :sqlite2mysql , sqlite2mysql_desc, :options=>[BACKUP, FORCE] do
begin
options[:backend] = :sqlite
sqlite = OneDB.new(options)
options[:backend] = :mysql
mysql = OneDB.new(options)
mysql.sqlite2mysql(options, sqlite)
rescue Exception => e
[-1, e.message]
end
end
end end

View File

@ -20,6 +20,8 @@ require 'onedb_backend'
LOG_TIME = false LOG_TIME = false
class OneDB class OneDB
attr_accessor :backend
def initialize(ops) def initialize(ops)
if ops[:backend] == :sqlite if ops[:backend] == :sqlite
begin begin
@ -519,6 +521,40 @@ is preserved.
end end
end end
def sqlite2mysql(options, sqlite)
one_not_running()
sqlite_v = sqlite.backend.read_db_version
mysql_v = @backend.read_db_version
if !options[:force]
match = true
match = false if sqlite_v[:version] != mysql_v[:version]
match = false if sqlite_v[:local_version] != mysql_v[:local_version]
if !match
err_msg = "SQLite version: #{sqlite_v[:version]}\n"
err_msg << "SQLite local version: #{sqlite_v[:local_version]}\n"
err_msg << "MySQL version: #{mysql_v[:version]}\n"
err_msg << "MySQL local version: #{mysql_v[:local_version]}\n"
err_msg << "The MySQL and SQLite versions do not match. Please run "
err_msg << "'onedb -i' in order to bootstrap a blank OpenNebula DB."
raise err_msg
end
end
backup(options[:backup], options)
file = "#{RUBY_LIB_LOCATION}/onedb/sqlite2mysql.rb"
load(file)
@backend.extend Sqlite2MySQL
@backend.convert(sqlite.backend.db)
return 0
end
private private
def one_not_running() def one_not_running()

View File

@ -286,7 +286,11 @@ class BackEndSQLite < OneDBBacKEnd
require 'fileutils' require 'fileutils'
def initialize(file) def initialize(file)
@sqlite_file = file if !file.nil?
@sqlite_file = file
else
raise "SQLite database path not supplied."
end
end end
def bck_file def bck_file

33
src/onedb/sqlite2mysql.rb Normal file
View File

@ -0,0 +1,33 @@
module Sqlite2MySQL
PROGRESS = 1000
def convert(sqlite_db)
puts "Starting migration from SQLite to MySQL\n"
sqlite_db.tables.each do |table|
@db[table].delete
records = sqlite_db[table].count
puts "> #{table} (#{records} records)"
i=0
sqlite_db[table].each do |row|
@db[table].insert(row)
i+=1
if i % PROGRESS == 0
if i < PROGRESS+1
print " #{i}"
else
print "...#{i}"
end
end
end
puts if i > PROGRESS-1
end
puts "\nMigration successful."
end
end