Skip to content

Instantly share code, notes, and snippets.

@alikhajeh1
Created March 12, 2013 22:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alikhajeh1/5147679 to your computer and use it in GitHub Desktop.
Save alikhajeh1/5147679 to your computer and use it in GitHub Desktop.
Rake task to convert a Postgres DB to a MySQL DB using the taps gem (https://github.com/ricardochimal/taps)
# bundle exec rake dev:create_mysql_snapshot
namespace :dev do
desc "Converts a local Postgres DB into a MySQL DB"
task :create_mysql_snapshot, [:target_env] => :environment do |t, args|
pg_user = 'postgres'
pg_password = 'password'
pg_creds = pg_password.empty? ? "#{pg_user}" : "#{pg_user}:#{pg_password}"
mysql_user = 'root'
mysql_password = 'password'
mysql_creds = mysql_password.empty? ? "#{mysql_user}" : "#{mysql_user}:#{mysql_password}"
puts 'WARNING: this rake task will wipe your local mysql_db_snapshot MySQL DB, Ctrl+C if you want to cancel or press enter to continue'
STDIN.gets
%x{mysql -u #{mysql_user} -e 'DROP DATABASE IF EXISTS mysql_db_snapshot'}
%x{mysql -u #{mysql_user} -e 'CREATE DATABASE mysql_db_snapshot'}
server_pid = Process.spawn("bundle exec taps server postgres://#{pg_creds}@localhost/my_postgres_db user password",
:out => "/dev/null", :err => "/dev/null")
sleep 5 # Wait for server to start up
# List of tables that should be converted
tables = ['users', 'schema_migrations', '...']
tables.each do |table|
%x{bundle exec taps pull mysql2://#{mysql_creds}@localhost/mysql_db_snapshot http://user:password@localhost:5000 --tables #{table}}
end
# Kill taps server process
Process.kill "TERM", server_pid
Process.wait server_pid
%x{mysqldump -u #{mysql_user} my_db > mysql_db_snapshot.dump}
puts "MySQL DB snapshot created in mysql_db_snapshot.dump"
end
end
# Gems needed to convert a Postgres DB to MySQL
gem 'pg', '~> 0.14.0'
gem 'mysql2', '~> 0.3.11'
gem 'taps', '~> 0.3.24'
gem 'sqlite3', '~> 1.2'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment