rake db:schema:load loads schema.rb multiple times? - ruby

After upgrading from Rails 3 to Rails 4 the db:schema:load task is failing. I did some digging into it and found that after the upgrade when I run bundle exec rake db:schema:load the db/schema.rb file is being loaded twice. The first time it runs fine; then the second time through it fails due to a create_table force: true fails due to there being a dependency constraint on the table.
I've stripped out every additional rake task and enhance to try and rule out any of my code, but still this loads the schema.rb twice. It is always exactly twice as I am able to run it successfully on SQLite and see the same behavior there, but it runs to completion due to SQLite not enforcing the table constraints.

You are seeing it twice because in development Rails runs db tasks for test and development in the same run.
Please see the ActiveRecord::Tasks::Databasetasks file for details, especially methods #load_schema_current (this one because you were referring to it) and #each_current_configuration

You might check to be sure your Rakefile isn't loading tasks twice. When Rake registers a task with the same name as an existing task, it will run both in sequence rather than replace the old definition with the new one.

Related

ActiveJob instance freezes RSpec test

I have an after_create_commit callback on my model, Foo, which looks like this:
after_create_commit { LogBroadcastJob.perform_later self }
I've reduced my perform method to return nil to simplify things.
When I create a Foo instance in an RSpec test with factory_girl, the test suite freezes. This only happens when I test models with that callback.
FactoryGirl.create :foo
When I Ctl+C out of my test suite it fails to kill the process. I have to find the process, which is still using my database (Postgresql), and kill it, which means that I don't see any errors on the command line. If I run my test suite again, it creates another process that I have to find and kill.
Does this sound familiar to anyone? How would I find useful errors here?
Maybe relevant: I upgraded from Rails 4.2 to 5.0.0.1 a while back.
This was a concurrency issue. Thanks to the resource provided in #coreyward's comment, I was able to clear this up by setting config/environments/test.rb to
config.eager_load = true
This differs from my config in config/environments/development.rb (and everything works in development), so I can't say I understand yet why it works. But I can now run all my tests with bundle exec guard or bundle exec rake spec.

Run Rake task in parallel using different parameters

I've a scenario where data has to be loaded from different input files. So my current approach is to execute the loader script using selenium grid in 10 different systems. Each system will have their own input files and other information like PORT, IP_ADDRESS for grid will also be passed in rake task itself. Now, these information will be saved in an excel file and code has to be written to build n number of rake task with different environment variables and then execute them all together.
I'm unable to come up with a way where all the task will be created automatically and executed as well.
I know it has to be done using 'parallel_test' gem or rake multi-task feature but don't know how exactly this can be achieved. Any other approach is also welcomed.

Rollback all the migrations in ActiveRecord

How can I delete / rollback all the migrations that have been run without having to drop and setup the database
rake db:rollback
goes back just one version. How can I get it to go all the way ?
One way is :
rake db:rollback STEP=10000000
Which pretty much means hacking your way back so many steps that the migration always goes to step 0.
Another way is:
rake db:migrate VERSION=0
More references here.
You can run $ rake db:migrate:status to see all of your migrations and whether they're in the up or down state. Go to the very first migration in that list and run:
rake db:rollback VERSION=version_id_shown_by_migrate_status_list_you_just_did
Note that if you undo all migrations, all data will be lost when the columns or tables are dropped. You can then reload the migrations by doing a normal rake db:migrate
To rollback all the migrations use bin/rake:db reset.
Pay attention, if a migration can't be rolledback, rake db:reset may fails.
References here

flyway clean is not dropping scheduler jobs or programs

I recently added a scheduler job and program to my development schema. When I tried to refresh the schema, I did a flyway clean, and then a flyway migrate.
I got the following error:
ERROR: Found non-empty schema "TESTDATA" without metadata table! Use init() or set initOnMigrate to true to initialize the metadata table.
When I dropped the job and program by hand, I was then able to run migrate again.
I've been using flyway for a while, and it's always been very straightforward - but I'm not sure how to convince it to properly clean my schema, now that I have an overnight batch job.
Note: I see the option -initOnMigrate, but this causes me two problems:
I have a lot of batch files which would be sensitive to trying to add another runline option.
I use flyway both to update existing schemas and to refresh schemas from scratch. If I need to modify the job or program, I could only include initOnMigrate (and have it bomb on the update), or not include it, and have it bomb on refresh (my current problem).
Thank you
You can work around this by implementing FlywayCallback.afterClean() and do the cleanup yourself.
Also, please file an issue in the issue tracker so we can fix this in time for 3.1.

whenever gem minutely job. If minutely job takes more than one minute then what

I am plannning to use whenever gem which among other things will also run minutely rake task. If my rake task takes more than a minute then based on the output from whenever gem it seems like the second instance of the rake task will kick-in even though the first one is not quite finished.
Will whenever gem will wait for the miutely task to finish before starting the second one?
If not then what are the workarounds. I believe this question is better served in serverfault still I am putting it here.
whenever just writes cronjobs, and makes no effort to stop them overrunning themselves. This is the job of the task that is being run.
Use PID files, or file system locks to prevent the task running over the top of itself.
In my scheduled application, I scan the process list looking for other instances of my application running with the same configuration file on the command line - then exit with a logged note if a process is already running with the same configuration file.
That keeps the program from stepping on itself ...
PID files or some type of "locking" file are prone to problems when the process exits but the lock file still exists.

Resources