Created
March 12, 2013 22:29
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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