Changing a script into a database migration

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

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,

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

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.