Initializer script is interfering with rake db:migrate

Hi all,

I’m using an initializer script in config/intializers/my_script.rb to
initialize the database through an xml configuration file. I’m using
JAXB to create the xml bindings and unmarshal the xml information.
Everything was working fine until I decided that I wanted to drop my
table and re-run my migrations. It looks like Rails tries to load the
intializer scripts before any migrations are run… And its awfully hard
to populate a non-existent database. Some might even say, its “nigh
impossible”… :smiley:

So, my question is, does anyone know of a better way to initialize a
database using an xml file and Active Record on startup, without
conflicting with rails db migrations?..

Currently, I have a simple “if” expression in my initializer script now,
that I can toggle on and off… for db migrations and rollbacks, but
that just seems cheezy. Curious to hear your thoughts and
recommendations.

Cheers,
Eric

Could you just use the database connection to check if the table you
need exists, and print a message reminding you to run migrations and
skip loading from XML if it’s not there yet?

/Nick

On Sun, Aug 22, 2010 at 10:19 PM, Eric S. [email protected]
wrote:

Eric

Posted via http://www.ruby-forum.com/.


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email

Nick S. wrote:

Could you just use the database connection to check if the table you
need exists, and print a message reminding you to run migrations and
skip loading from XML if it’s not there yet?

/Nick

Nick,

I don’t think so, because its when I actually am trying to run the
migration that I’m having the problem… So, when type into a terminal:

jruby -S rake db:migrate

It bombs because my initializer script beats the migration to the punch
and tries to populate the database with data from the xml file. It seems
that when I’m trying to run the migration, the initializer script gets
in the way, and causes the whole process to error out.

However, if you think that there might be away to check in my script if
a migration is underway, and then pause until the migration is
completed, that would be pretty slick. :smiley: I have no idea how to go
about doing that though. The Rails Guides seemed a little thin on how to
write advanced Initializer scripts.

-Eric

Eric S. wrote:

Nick S. wrote:

Could you just use the database connection to check if the table you
need exists, and print a message reminding you to run migrations and
skip loading from XML if it’s not there yet?

/Nick

Nick,

I don’t think so, because its when I actually am trying to run the
migration that I’m having the problem… So, when type into a terminal:

jruby -S rake db:migrate

It bombs because my initializer script beats the migration to the punch
and tries to populate the database with data from the xml file. It seems
that when I’m trying to run the migration, the initializer script gets
in the way, and causes the whole process to error out.

However, if you think that there might be away to check in my script if
a migration is underway, and then pause until the migration is
completed, that would be pretty slick. :smiley: I have no idea how to go
about doing that though. The Rails Guides seemed a little thin on how to
write advanced Initializer scripts.

-Eric

Also,

That would probably mean that the initializer script would have to be in
a separate thread, in order for the migration to be allowed continue,
correct?

Thanks for the quick reply, by the way. I really appreciate the help!

-Eric