# Extracting SQL and Rebuilding from SQL?

**URL:** <https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320>\
**Category:** Rails\
**Created:** [December 29, 2005, 5:37pm UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320 "2005-12-29T17:37:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 29, 2005, 5:37pm UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/1 "2005-12-29T17:37:33Z")

</div>

Hello-

With a database filled with several customers’ datasets, I thought it  
would be a nice feature of my app’s backend interface to be able to  
extract (and optionally delete) the data from a single customer. It  
would then also be nice to rebuild that data from the stored info.

What’s a good way to do this efficiently?

If all the “:dependent =\> true” are setup properly on associations  
within models, a single “.destroy()” called on the customer will yank  
all the data. Is there a way to do something else with those rows?  
i.e. sqldump of some sort?

I thought of adding a “before\_destroy” hook, checking a global  
“archive\_on\_destroy” or something and dumping those rows out. Is this a  
good way?

Anyway, I’m just shooting in the dark now to brainstorm ways of  
performing this archival and retrieval. Any ideas would be greatly  
appreciated!

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 30, 2005, 12:12am UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/2 "2005-12-30T00:12:29Z")

</div>

Jake J. wrote:

> Hello-
> 
> With a database filled with several customers’ datasets, I thought it  
> would be a nice feature of my app’s backend interface to be able to  
> extract (and optionally delete) the data from a single customer. It  
> would then also be nice to rebuild that data from the stored info.
> 
> What’s a good way to do this efficiently?
> 
> If all the “:dependent =\> true” are setup properly on associations  
> within models, a single “.destroy()” called on the customer will yank  
> all the data. Is there a way to do something else with those rows?  
> i.e. sqldump of some sort?
> 
> I thought of adding a “before\_destroy” hook, checking a global  
> “archive\_on\_destroy” or something and dumping those rows out. Is this a  
> good way?
> 
> Anyway, I’m just shooting in the dark now to brainstorm ways of  
> performing this archival and retrieval. Any ideas would be greatly  
> appreciated!

Is this possibly supported using some obscure application of schemas or  
migrations?

Jake

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 30, 2005, 12:25am UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/3 "2005-12-30T00:25:17Z")

</div>

Jake J. wrote:

> Hello-
> 
> With a database filled with several customers’ datasets, I thought it  
> would be a nice feature of my app’s backend interface to be able to  
> extract (and optionally delete) the data from a single customer. It  
> would then also be nice to rebuild that data from the stored info.
> 
> What’s a good way to do this efficiently?

Not sure if this is exactly what you need, but take a look at topfunky’s  
plugin at  
[http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data](http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data).

You’ll want the dump\_to\_file and load\_from\_file helpers.

Hope this helps,  
Jeff

> **[Hey Daddio!](http://www.softiesonrails.com)**
>
> Home of Hey Daddio Software

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 30, 2005, 3:23am UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/4 "2005-12-30T03:23:14Z")

</div>

> Not sure if this is exactly what you need, but take a look at topfunky’s  
> plugin at  
> [http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data](http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data).
> 
> You’ll want the dump\_to\_file and load\_from\_file helpers.
> 
> Hope this helps,  
> Jeff  
> [www.softiesonrails.com](http://www.softiesonrails.com)

Thanks. It’s close. In fact, it asks my question differently. I  
originally thought that an SQL dump would be better. This seems to  
indicate that perhaps a YAML dump would be more appropriate. Afterall,  
the tables already exist, so I’m not trying to reconstruct the form of  
the db, just the content of some of it.

This solution would likely be more immune to version differences, too, I  
suspect.

I’ll need to write something more appropriate to my usage, but it’s a  
good reference. Thanks!

Jake

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 30, 2005, 1:14am UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/5 "2005-12-30T01:14:18Z")

</div>

On Dec 29, 2005, at 3:12 PM, Jake J. wrote:

> > If all the “:dependent =\> true” are setup properly on associations  
> > performing this archival and retrieval. Any ideas would be greatly  
> > appreciated!
> 
> Is this possibly supported using some obscure application of  
> schemas or  
> migrations?
> 
> Jake

Jake-

```
You might want to look at acts_as_versioned or acts_as_paranoid to

```

get some ideas about how to do what you want. Google for both of  
these terms to get more info,

Cheers-  
-Ezra Z.  
Yakima Herald-Republic  
WebMaster  
[http://yakimaherald.com](http://yakimaherald.com)  
509-577-7732  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![janovetz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/53a042/32.png) [@janovetz](https://www.ruby-forum.com/u/janovetz)\
**Post date:** [December 30, 2005, 3:41am UTC](https://www.ruby-forum.com/t/extracting-sql-and-rebuilding-from-sql/51320/6 "2005-12-30T03:41:08Z")

</div>

Jake J. wrote:

> > Not sure if this is exactly what you need, but take a look at topfunky’s  
> > plugin at  
> > [http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data](http://nubyonrails.com/articles/2005/12/27/dump-or-slurp-yaml-reference-data).

One question that doesn’t seem to be addressed in this case is how to  
rebuild an associated database where the new records may have different  
rows.

The new database will need to be reconstructed in a hierarchical manner  
so that the association IDs can be replaced as the new items are added  
(with presumably new IDs!).

Being somewhat new to Rails, I’m wondering if there are any shortcuts  
available to me to do this.

Jake
