Forum: Ruby on Rails Migrations: something like before_up/after_up hook?

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.
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-02 19:47
Hi all

I have to execute some code always after the up methods of my
migrations. I wondered if there is a before_up/after_up hook that I can
override? Or how could I achieve that?

Thanks a lot
Josh
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-02 21:39
Joshua Muheim wrote:
> Hi all
>
> I have to execute some code always after the up methods of my
> migrations. I wondered if there is a before_up/after_up hook that I can
> override? Or how could I achieve that?
>
> Thanks a lot
> Josh

Use method call in the end of up method?
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-02 21:44
Jamal Soueidan wrote:
> Joshua Muheim wrote:
>> Hi all
>>
>> I have to execute some code always after the up methods of my
>> migrations. I wondered if there is a before_up/after_up hook that I can
>> override? Or how could I achieve that?
>>
>> Thanks a lot
>> Josh
>
> Use method call in the end of up method?

No, I want it to execute automatically, because it's always the same
action and I don't want it in every migration...
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-02 21:47
Joshua Muheim wrote:
> Jamal Soueidan wrote:
>> Joshua Muheim wrote:
>>> Hi all
>>>
>>> I have to execute some code always after the up methods of my
>>> migrations. I wondered if there is a before_up/after_up hook that I can
>>> override? Or how could I achieve that?
>>>
>>> Thanks a lot
>>> Josh
>>
>> Use method call in the end of up method?
>
> No, I want it to execute automatically, because it's always the same
> action and I don't want it in every migration...

What do you exactly need to do before or after every migration?

create_table, maybe have something before and after ?
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-02 21:51
> What do you exactly need to do before or after every migration?
>
> create_table, maybe have something before and after ?

I'm working on a plugin that automatically loads some fixtures according
to every migration, something like

db/migrate/fixtures/001/countries.yml
db/migrate/fixtures/002/users.yml
db/migrate/fixtures/003/bla.yml
db/migrate/fixtures/003/users.yml

So I just want the up method of a migration to check if there's a
directory with the corresponding migration number and any fixtures in
it, and if so, to load them.
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-02 22:09
Joshua Muheim wrote:
>> What do you exactly need to do before or after every migration?
>>
>> create_table, maybe have something before and after ?
>
> I'm working on a plugin that automatically loads some fixtures according
> to every migration, something like
>
> db/migrate/fixtures/001/countries.yml
> db/migrate/fixtures/002/users.yml
> db/migrate/fixtures/003/bla.yml
> db/migrate/fixtures/003/users.yml
>
> So I just want the up method of a migration to check if there's a
> directory with the corresponding migration number and any fixtures in
> it, and if so, to load them.

This might give you a idea how to do it :)

class A # ActiveRecord::Migration
  def self.create_table
    puts "create users"
  end
end

class Invoke < A  # Your own class which inherent
ActiveRecord::Migration
  def self.create_table
    puts "-> Before"
    super
    self.after_table
  end
  def self.after_table
    puts "-> After"
  end
end


class CreateUsers < Invoke # This is your new class you would use to
create migration, which will inherent Invoke
  def self.up
      create_table #create your stuff normal :)
  end
end

CreateUsers.up
E3513c4edd6810bb4b9914b58da2a2c3?d=identicon&s=25 Jamal Soueidan (jamal)
on 2007-04-02 22:10
output:

-> Before
create users
-> After
Fc582698581884352e745d1d4c64699d?d=identicon&s=25 Joshua Muheim (josh)
on 2007-04-09 12:15
Thank you for your answer!

I remarked that your code only works when calling create_table in the
self.up method. But I need it to work in any case, not depending on what
I call in the self.up method.
As soon as self.up is called, it should execute before_up...
This topic is locked and can not be replied to.