# Changing a script into a database migration

**URL:** <https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999>\
**Category:** Rails\
**Created:** [September 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999 "2009-09-05T14:59:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Fifigyuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@Fifigyuri](https://www.ruby-forum.com/u/Fifigyuri)\
**Post date:** [September 5, 2009, 2:59pm UTC](https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999/1 "2009-09-05T14:59:06Z")

</div>

Hi,

```
 I have a little dilemma where and how to place a script in my

```

rails application. I work on a reimplementation and the original  
database structure is changed. The old data needs to be migrated into  
a new schema. The migration is executed very few times, maybe just  
once. As the data set might be huge, I also do not wish to execute it  
often.  
Currently I placed the script in dir /script, but I think it does  
not fit the place well, it is meant for other things… isn’t it? I  
think it should among the migrations, but I don’t want it to be  
executed always as the others migration when the data model is  
created.  
Could you give me advice, how to tackle elegantly this thing? I  
want to keep my code in the rails project structure, although it will  
be executed few times, maybe only now. And I’d like to have it at a  
place which it fits well. Thanks for help!

Georg

---

<div class="post-metadata">

**Author:** ![Fifigyuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@Fifigyuri](https://www.ruby-forum.com/u/Fifigyuri)\
**Post date:** [September 5, 2009, 3:56pm UTC](https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999/2 "2009-09-05T15:56:55Z")

</div>

Fifigyuri wrote:

> Hi,
> 
> ```
> I have a little dilemma where and how to place a script in my
> 
> ```
> 
> rails application. I work on a reimplementation and the original  
> database structure is changed. The old data needs to be migrated into  
> a new schema. The migration is executed very few times, maybe just  
> once. As the data set might be huge, I also do not wish to execute it  
> often.  
> Currently I placed the script in dir /script, but I think it does  
> not fit the place well, it is meant for other things… isn’t it? I  
> think it should among the migrations, but I don’t want it to be  
> executed always as the others migration when the data model is  
> created.

The preferred way to create the DB is to use rake db:schema:load.  
Migrations are only for migration. So just write the script as a  
migration. That’s what migrations are for.

> ```
> Could you give me advice, how to tackle elegantly this thing? I
> 
> ```
> 
> want to keep my code in the rails project structure, although it will  
> be executed few times, maybe only now. And I’d like to have it at a  
> place which it fits well. Thanks for help!

You are describing a classic use case for a migration. There’s no  
reason to do it any other way. If you’re worried about it being run  
when it shouldn’t be, just throw in some if statements.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

> Georg

---

<div class="post-metadata">

**Author:** ![Fifigyuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@Fifigyuri](https://www.ruby-forum.com/u/Fifigyuri)\
**Post date:** [September 5, 2009, 7:36pm UTC](https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999/3 "2009-09-05T19:36:40Z")

</div>

I correct myself. The problem was in my code, and thought that not  
accessing the ActiveRecord::Base from Migrations is intended. No, it  
is possible, sorry for the false alarm.

---

<div class="post-metadata">

**Author:** ![Fifigyuri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/f17d59/32.png) [@Fifigyuri](https://www.ruby-forum.com/u/Fifigyuri)\
**Post date:** [September 5, 2009, 7:05pm UTC](https://www.ruby-forum.com/t/changing-a-script-into-a-database-migration/174999/4 "2009-09-05T19:05:38Z")

</div>

Thanks, I followed your advice. And its almost fine. The original  
script operated with the models, putting it to a migration causes take  
the migration misses the model. Claims “uninitialized constant” for  
the model name. I tried to require the model directly. On the other  
hand I am not sure whether using models in migrations is intended…  
currently it seems to be the most convenient way of changing the data.

bw

On Sep 5, 3:56 pm, Marnen Laibow-Koser \<rails-mailing-l…@andreas-
