Hi,

in case it's useful to someone else, here's a Rake task I wrote to
simulate a 'database rename' in MySQL (database rename is available in
MySQL 5.1, so the Rake task is relevant only for 5.0 or before, where
it's not available). I use it to publish my warehouse once everything
is loaded and all the screens have passed - so the idea is a bit
similar to using temp tables, except that the temp tables will perform
better than dumping everything if you have a lot of data, not to
forget that not everybody is granted the right to drop and create
database (eg: issues on shared hosting for instance).

Anyway, here's the snippet:

  def run_query(command,query)
    credentials = "[EMAIL PROTECTED] [EMAIL PROTECTED]"
    puts "Launching #{command} with '#{query}'"
    throw "Error while running #{command} with '#{query}'" unless
system("#{command} #{credentials} #{query}")
  end

  desc "Publish the warehouse (MySQL only)"
  task :publish_warehouse => :environment do
    config = ActiveRecord::Base.configurations[RAILS_ENV]
    database_name,@username,@password = %w(database username
password).map { |e| config[e] }
    published_database_name = "#{database_name}_published"
    backup_file = "#{database_name}.bak"

    run_query 'mysqldump',"#{database_name} > #{backup_file}"
    run_query 'mysql',    "-e \"drop database if exists
#{published_database_name}\""
    run_query 'mysql',    "-e \"create database #{published_database_name}\""
    run_query 'mysql',    "#{published_database_name} < #{backup_file} "
  end

cheers

Thibaut
_______________________________________________
Activewarehouse-discuss mailing list
Activewarehouse-discuss@rubyforge.org
http://rubyforge.org/mailman/listinfo/activewarehouse-discuss

Reply via email to