Forum: Ruby on Rails rails migrations - enhanced usage

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 Surendra Singhi (Guest)
on 2006-04-03 10:42
(Received via mailing list)
Hello,
 When I am writing rails migrations, quite often I make mistake in them
which
causes the migration tasks to stop halfway with error.

This causes lots of problem, because it results in half of the things in
migrations changed and other half not. Also, as the schema number is
changed,
so things cannot be rolled back.

Is there any way to make the entire migration action atomic, i.e.,
everything
or none at all?

Also, while dropping tables how can I determine whether a particular
table
exists in the database?

Thanks.

--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
|     -- Orwell, 1984, 1948
`----
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-03 10:54
(Received via mailing list)
Surendra Singhi wrote:
>
> Also, while dropping tables how can I determine whether a particular table
> exists in the database?
>
> Thanks.
>
>
I've had this problem as well and like the idea of making it atomic --
or at least having an atomic option.
3dd4b52a0946bd698b1d1635a46ea3a3?d=identicon&s=25 François Beausoleil (fbeausoleil)
on 2006-04-03 16:08
(Received via mailing list)
Hi !

2006/4/3, Surendra Singhi <efuzzyone@netscape.net>:
> Hello,
>  When I am writing rails migrations, quite often I make mistake in them which
> causes the migration tasks to stop halfway with error.

If your DB supports it, do the migration in a transaction:

def self.up
  ActiveRecord::Base.connection.transaction do
    create_table ...
  end
end

On the other hand, make your migrations as small as possible.  That
way, there will be less things that you'll have to manually rollback
if something goes wrong.

Also, a trick I often use is I dump (with data) the production DB to
development, and do the migration on the dev database.  That way, I am
at least assured that the migration can run on existing data.

Last trick: dump your dev database before doing the migration.  If
something breaks, you just reload the schema.

Hope that helps !
59ea1b450935b9d70abfec4186b7a4d5?d=identicon&s=25 Jeff Coleman (progressions)
on 2006-04-03 19:10
Francois, thanks for that tip!  I'd encountered the same thing and
wasn't sure if transactions could be used in that way.

Jeff

François Beausoleil wrote:
> Hi !
>
> 2006/4/3, Surendra Singhi <efuzzyone@netscape.net>:
>> Hello,
>>  When I am writing rails migrations, quite often I make mistake in them which
>> causes the migration tasks to stop halfway with error.
>
> If your DB supports it, do the migration in a transaction:
>
> def self.up
>   ActiveRecord::Base.connection.transaction do
>     create_table ...
>   end
> end
>
> On the other hand, make your migrations as small as possible.  That
> way, there will be less things that you'll have to manually rollback
> if something goes wrong.
>
> Also, a trick I often use is I dump (with data) the production DB to
> development, and do the migration on the dev database.  That way, I am
> at least assured that the migration can run on existing data.
>
> Last trick: dump your dev database before doing the migration.  If
> something breaks, you just reload the schema.
>
> Hope that helps !
6edd67c92a1dab5eb23fed79f3c18564?d=identicon&s=25 David Heinemeier Hansson (Guest)
on 2006-04-03 20:10
(Received via mailing list)
> Francois, thanks for that tip!  I'd encountered the same thing and
> wasn't sure if transactions could be used in that way.

They only can for a few databases, like pgsql and mssql, that supports
transactions on schema changes. The rest of the databases will ignore
this.

So always test your migrations on a development database first and
make them small.
--
David Heinemeier Hansson
http://www.loudthinking.com -- Broadcasting Brain
http://www.basecamphq.com   -- Online project management
http://www.backpackit.com   -- Personal information manager
http://www.rubyonrails.com  -- Web-application framework
D449d54c3b0f8c9930c11c7d7d3e6cdd?d=identicon&s=25 Surendra Singhi (Guest)
on 2006-04-04 11:58
(Received via mailing list)
"Francois Beausoleil"
<francois.beausoleil@gmail.com> writes:

>   ActiveRecord::Base.connection.transaction do
> at least assured that the migration can run on existing data.
>
> Last trick: dump your dev database before doing the migration.  If
> something breaks, you just reload the schema.
>
> Hope that helps !

Sure it did.

Thanks Francois and David.

--
Surendra Singhi
http://ssinghi.kreeti.com, http://www.kreeti.com
Read my blog at: http://cuttingtheredtape.blogspot.com/
,----
| "War is Peace! Freedom is Slavery! Ignorance is Strength!"
|     -- Orwell, 1984, 1948
`----
This topic is locked and can not be replied to.