diff --git a/lib/mongo_to_mysql.rb b/lib/mongo_to_mysql.rb new file mode 100644 index 000000000..2cc69d56c --- /dev/null +++ b/lib/mongo_to_mysql.rb @@ -0,0 +1,15 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class MongoToMysql + def id_sed + @id_sed = sed_replace('{\ \"$oid\"\ :\ \(\"[^"]*\"\)\ }') + end + def date_sed + @date_sed = sed_replace('{\ \"$date\"\ :\ \([0-9]*\)\ }') + end + def sed_replace(regex) + "sed 's/#{regex}/\\1/g'" + end +end diff --git a/lib/tasks/migrations.rake b/lib/tasks/migrations.rake new file mode 100644 index 000000000..df11d87f3 --- /dev/null +++ b/lib/tasks/migrations.rake @@ -0,0 +1,33 @@ +# Copyright (c) 2010, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +require File.join(Rails.root, 'lib/rake_helpers') +include RakeHelpers + +namespace :migrations do + desc 'export data for mysql import' + task :export_for_mysql do + require 'lib/mongo_to_mysql' + migrator = MongoToMysql.new + db_name = "diaspora-development" + models = [ + :aspects, + :comments, + :contacts, + :invitations, + :notifications, + :people, + :posts, + :requests, + :users, + ] + `mkdir -p #{Rails.root}/tmp/export-for-mysql` + models.each do |model| + filename = "#{Rails.root}/tmp/export-for-mysql/#{model}.json" + `mongoexport -d #{db_name} -c #{model} | #{migrator.id_sed} | #{migrator.date_sed} > #{filename}` + puts "#{model} exported to #{filename}" + #`mongoexport -d #{db_name} -c #{model} -jsonArray | sed 's/\"[^"]*\"/"IAMID"/g' > #{filename}` + end + end +end