Skip to content

Instantly share code, notes, and snippets.

View pbonnell's full-sized avatar

Peter B pbonnell

  • CIRCUIT LLC
View GitHub Profile
@pbonnell
pbonnell / mongiod_mysql_migrator.rb
Last active August 29, 2015 14:27 — forked from tomriley/mongiod_mysql_migrator.rb
Migrate a Mongoid / MongoDB database to an ActiveRecord based SQL one. One method to convert the schema, another to migrate data. Copes with most basic data types. Some hacks to infer TEXT columns. Converts embeds_one relationships to AR aggregations. I wrote this for a one-time migration. It could be a good starting point for someone doing the …
# Migrate schema and data from Mongoid to MySQL ActiveRecord
class MongoidMysqlMigrator
def randomize_auto_increment_values(source_models, from=5500, to=10500)
source_models.each do |model|
value = rand(from-to)+from
sql = %(ALTER TABLE #{model.name.tableize} AUTO_INCREMENT=#{value})
puts sql
ActiveRecord::Base.connection.execute(sql)
end