Run rake task from rails migration file - ruby-on-rails

I have a rake task that I run from the terminal using the command below.
bundle exec rake migrations:seed_us_users
I have created this migration file but I don’t know how the code should be to run this rake task.
class AddNewUsers < ActiveRecord::Migration[5.1]
def change
end
end

You can execute a rake task from within a loaded Rails environment with either
Rake::Task['migrations:seed_us_users'].invoke or Rake::Task['migrations:seed_us_users'].execute
You can pass data to the task inside of the invoke or execute method. Example:
Rake::Task['migrations:seed_us_users'].invoke(params)
For more details please check
https://sampatbadhe.medium.com/rake-task-invoke-or-execute-419cd689c3bd

Related

Rake task dependent if database already exists

Just to preface that I've read through this question and others but no answer really mentions a way to do it in a rake task.
I want to do a setup script that somewhat looks like this:
if database_exists?
sh 'rake db:migrate'
else
sh 'rake db:setup'
end
I haven't written database_exists? but how can I tell if the users system has this database already created?
I don't want to run rake db:setup every time which will drop the users database.
I suppose I could run rake db:create && rake db:migrate each time but then the user would need to run rake db:seed in addition to the rake task which right now is part of the db:setup process.
My advice is to write a ruby script to run with rails runner or a rake task, where you can define your database_exists? method like this:
def database_exists?
ActiveRecord::Base.connection
rescue ActiveRecord::NoDatabaseError
false
else
true
end
Documentation
ActiveRecord::Base.connection
ActiveRecord::NoDatabaseError

How to run rake task during rails initiation?

I have my own secret key generating script that I've placed in a rake command. I can execute this on its own without starting the rails server, but I want to incorporate this rake task into the rails server initiation so I don't need to call the rake task by itself anymore.
Where/How do I run this rake command such that it's part of the initiation process?
If the secret key rake task doesn't have the environment dependency, you can just create a new initializer in config/initializers and name it secret_generate.rb for example, and in it write:
`rake your_task_name`
This will run your rake task on rails server initiation.

How to run a rake task by command line in rails

I have defined a rake task as follows in a file called file_locker_task.rake
namespace :myspace do
task :process => :environment do
FileLocker.lock_files
end
end
How do I execute this rake task from the command line?
I tried:
rake myspace:process and rake process but both are throwing an error like this:
rake aborted!
Don't know how to build task 'process'
Run rake -T -A from your Rails home directory to see all the tasks that rake knows about. Yours must be in that list for rake to run it.
By default, in a Rails app, rake looks in the lib/tasks directory and its subdirectories for your .rake files. Check that. (I suspect this is the problem.)
According to docs
Any ruby file (including other rakefiles) can be included with a standard Ruby require command.
-
Additional rake files (with the file extension “.rake”) may be placed in rakelib directory located at the top level of a project (i.e. the same directory that contains the main Rakefile). Also, rails projects may include additional rake files in the lib/tasks directory.

Figuring out how rake works and how command line arguments can be handled in a rake tasks in rails

How can I pass arguments to a rake task so that it executes the rake task on a different schema? For example I have rake code such as the one below:
namespace :update_persons_table do
task :import => :environment do
config = Rails.configuration.database_configuration
ActiveRecord::Base.connection.schema_search_path = "my, public, data_master_reports"
# do stuff make updates to table....
end
end
I call this rake task from the command line like this:
RAILS_ENV='production' rake update_persons_table:import
BTW, does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line? Because in my database.yml file i do have a production: database entry. Im trying to figure out how the whole plumbing for this works. This rake task updates a table in a database. But I want to be able to call it on another clone table in a different database. How can I do that with passing parameters in the command line?
How can I pass arguments to a rake task so that it executes the rake task on a different schema?
I think what you are asking is
How can I pass arguments to a rake task so that it executes the rake task on a different environment?
The schema is the same for all environments of your app
does the above RAILS_ENV call I am using have to do with the :environment do statement I am using in the second line?
Yes :environment do will take whatever environment(:development, :test, :production) you are specifying. In your example, its RAILS_ENV='production'
Now to run the rake task in a different environment, like say the development environment
`RAILS_ENV='development' rake update_persons_table:import'
Now the same code that was executed on the production environment db when you ran `RAILS_ENV='production' rake update_persons_table:import' has been run in the development environment db
Hope this is clear enough to get you started

How do I run a ruby script, that I put in my /lib/tasks/ directory in my Rails app, once?

Eventually I would like to get to setting it up as a Rake task and do a cron job, but for right now...all I want to do is take my ruby script that used to work as a standalone script and have it work within my Rails app.
I renamed the file to be .rake instead of .rb and tried doing rake my_script at the command-line, but that gave me this error message:
rake aborted!
Don't know how to build task 'my_script'
(See full trace by running task with --trace)
How do I run this script within my Rails environment?
This is the first time I am doing something like this, so any assistance would be greatly appreciated.
Thanks.
I think what you're looking for is rails runner. I know in Rails 2.3.x you'd do
ruby script/runner <your file>
In Rails 3 it might be slightly different.
http://guides.rubyonrails.org/command_line.html#rails-runner
The primary difference between a runner and a rake task is : runner would boot rails while rake task doesn't (you can tell it to do so).
Since rake can do both (boot/no boot), there's no concept of runner in rails-3 anymore.
So, create a rake task: whatever_name.rake
Example:
desc "This task does awesome stuff"
task :do_awesome_stuff do
awesome_method
end
def awesome_method
#put your ruby code here
end
Now from your command prompt, type rake do_awesome_stuff to execute this rake task.
To make it boot Rails, change task definition to this:
task :do_awesome_stuff => :environment do

Resources