ActiveRecordSqlExporter

This plugin takes a record in your database and converts it into SQL statements that can be used to insert it.

This was written after I had to restore some specific data for a user (a number of rows, across a number of tables.)

It might be useful as a simple backup utility - for example, store the restoration sql after something gets removed.

This moves down through relations along has_many, has_one relations, but will not move up along belongs_to relations.

However, this doesn’t make you any less likely to screw up and shoot yourself in the proverbial memory usage foot with a massive swath of data, so often you’ll want to use the second argument to to_sql, which is an array of classes to ignore.

Example

Obj.find( obj_id ).to_backup_sql

For a blog_post, that belongs to a user, that might have many posts: Post.find( id ).to_backup_sql( {}, [User] )

Export an object to a file: Post.find( id ).to_backup_sql( => a_file_object )

Copyright © 2010 Adam Palmblad ([email protected]), released under the MIT license

To Do

  • Watch dependent => nullify and write restoration query

  • The generation the SQL that checks for the presence of a value is broken!

  • Add ability to toggle sql options, such as the ON DUPLICATE KEY stuff

  • Support for non MySQL databases