Updating table data

I have an application in rails in which i am adding a new column in one
of the tables. The value for the new column is a bit.ly link and I need
to populate this for each record in the table approx. 250 rows will be
affected.

Q1: Can this be done outside the application? i.e. write a script that
can interact with the db and then populate the appropriate column? And
is this advisable?

or

Q2: if the aforementioned idea is not a good way then what will be the
best way to update the existing records as any code i will write will
only be used to update the table once.

One suggestion that has been made is to use migrations but I am not 100%
on how i can do that...

Plz help

Quee WM wrote:

I have an application in rails in which i am adding a new column in one
of the tables. The value for the new column is a bit.ly link and I need
to populate this for each record in the table approx. 250 rows will be
affected.

Q1: Can this be done outside the application? i.e. write a script that
can interact with the db and then populate the appropriate column?

Yes.

And
is this advisable?

Absolutely not.

or

Q2: if the aforementioned idea is not a good way then what will be the
best way to update the existing records as any code i will write will
only be used to update the table once.

Use a migration. That's what they're for.

One suggestion that has been made is to use migrations but I am not 100%
on how i can do that...

Well, what have you figured out so far? We can help you better if you
tell us what you don't understand.

In general, though, *all* DB schema changes should be made through
migrations. This makes maintenance easier.

Plz help

Best,

Quee WM wrote:

I have an application in rails in which i am adding a new column in one
of the tables. The value for the new column is a bit.ly link and I need
to populate this for each record in the table approx. 250 rows will be
affected.

One suggestion that has been made is to use migrations but I am not 100%
on how i can do that...

A migration is a Ruby script.
The Rails sugar helps you tell the application (for instance):
"Hey there! I need you to add a column to this table. And while you're
at it, check every row, and do this on every row, so we can populate
that new column."

Start by taking a look at this:
http://railsbrain.com/api/rails-2.3.2/doc/index.html?a=M001956&name=add_column

If the value of the column is the same for every row, can't you just
use default value when you create the column?

Curtis Cooley wrote:

Aldric Giacomoni wrote:

That is true - but since he mentioned a bit.ly link, I thought it would
be different for every row.

You are 100% right that it will be different for each row. With the help
of others on this forum I have written a callback function which
provides me with a bitly link at creation time of a new record.

I believe that I can use the same function and provide it the required
information but the biggest question I have right now is how to make
that call? I could not see a decent example on google where the database
was being populated using a function at creation time. So any help in
this regards will keep me going.

Also I posted the same question on the ruby forum and one suggestion so
far is that

For one-shot scripts that are not exactly migrations, it should be an
acceptable way to use an external script. I find some weight in this as
well as what if I need to redeploy the same app on a new server, with
fresh data, if the code to add the bitly is in the migration, it will be
useless and just adding weight to the app.

Thoughts?

Quee WM wrote:

Aldric Giacomoni wrote:

That is true - but since he mentioned a bit.ly link, I thought it would
be different for every row.

You are 100% right that it will be different for each row. With the help
of others on this forum I have written a callback function which
provides me with a bitly link at creation time of a new record.

I believe that I can use the same function and provide it the required
information but the biggest question I have right now is how to make
that call? I could not see a decent example on google where the database
was being populated using a function at creation time. So any help in
this regards will keep me going.

Your Rails app classes are accessible in migrations, so you can call the
bit.ly link generator just like you would from the application itself.

At this point, stop asking questions and go write some migration code.
If you run into trouble, then come back, ask, and post the code you
have. We could talk about this all year, but now is the time for code.

Best,

Quee WM wrote:

Also I posted the same question on the ruby forum and one suggestion so
far is that

For one-shot scripts that are not exactly migrations,

But this is exactly a migration. It gets the database from one
consistent state to the next. That's the definition of a migration.

it should be an
acceptable way to use an external script.

You were told it would be possible, not advisable.

I find some weight in this as
well as what if I need to redeploy the same app on a new server, with
fresh data, if the code to add the bitly is in the migration, it will be
useless and just adding weight to the app.

No. If you redeploy the app to a new server, you shouldn't be running
the old migrations. The preferred way to do initial DB setup is with
rake db:schema:load. Migrations are only for DB changes.

Thoughts?

I don't know why you're being so resistant to migrations when you're
doing the exact task they were meant for.

Best,

Your Rails app classes are accessible in migrations, so you can call the
bit.ly link generator just like you would from the application itself.

Thanks for the info, did not know this before.

At this point, stop asking questions and go write some migration code.
If you run into trouble, then come back, ask, and post the code you
have. We could talk about this all year, but now is the time for code.

I would love to start coding, but want to have all my ducks in a row
that i do it right. :slight_smile:

But this is exactly a migration. It gets the database from one
consistent state to the next. That's the definition of a migration.

You were told it would be possible, not advisable.

Thanks for the clarification and making a very strong point. :slight_smile:

No. If you redeploy the app to a new server, you shouldn't be running
the old migrations. The preferred way to do initial DB setup is with
rake db:schema:load. Migrations are only for DB changes.

Did not know this as have not reached that point yet and have not come
across the issue. I learn and appreciate ruby more and more every day.

I don't know why you're being so resistant to migrations when you're
doing the exact task they were meant for.

Not being resistant at all, just learning. The role of migrations is
much more clear to me now so will be fairly confident to play with them.

Just as you said time to code, and then come back with questions.

Thanks for all the help guys. :slight_smile: