Migrations: something like before_up/after_up hook?


#1

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


#2

Joshua M. 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?


#3

Jamal S. wrote:

Joshua M. 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…


#4

Joshua M. wrote:

Jamal S. wrote:

Joshua M. 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 ?


#5

Joshua M. 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 :slight_smile:

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 :slight_smile:
end
end

CreateUsers.up


#6

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.


#7

output:

-> Before
create users
-> After


#8

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…