Run Ruby on Rails migrations on specific Google App Engine service - ruby-on-rails

I've been working on a Ruby on Rails API and I'm using Google Cloud Platform, specifically Google App Engine, to deploy my app. I followed the instructions here. So far so good. My app was successfully deployed and I could successfully run the migrations. Let's call this environment production.
The thing is I need to deploy a sort of staging environment. For the moment I had to create another project to solve my problem and consider that as my staging environment. Now, I don't think this is necessary, actually seems to be redundant.
I've prepared another database my_app_db_staging. And I created a staging.yaml file stating to run as another instance with the service name staging. The service was successfully deployed. Where is the problem? Well, running the migration. I'm using the appengine gem. So, to run the migrations you're supposed to run:
bundle exec rake appengine:exec -- bundle exec rake db:migrate
But as the documentation states:
The appengine gem provides the Rake task appengine:exec to run a command against the most recent deployed version of your app in the production App Engine environment.
So, no clue on how to run the migrations against my recently created staging service.

I looked deeper into the appengine gem, and it has an option. It lacks more documentation so you don't have to waste time looking for an answer:
rake appengine:exec GAE_CONFIG=staging.yaml -- bundle exec rake db:migrate
The options can work together with GAE_SERVICE, but the yaml file has to have the same name, otherwise you'll get an error.

Related

How to run rake in ruby-on-rails application in production?

I am trying to figure out how to deploy my ruby-on-rails application in production.
The operating system is ubuntu and the application uses Postgres. I have managed to deploy the application and am able to login etc... but there are certain functionality that is not working (I am new to the application and rails).
The application uses elastic search, which I have installed and the service is running (I can access the page via http://localhost:9200). But when the application tries to access components of elastic search i get an error.
There is a rake file in the directory /app/lib/tasks, which has several files and one of the them is elasticsearch.rake
namespace :app do
desc "Bootstraping Elasticsearch index"
task(:elasticsearch => :environment) do
Image.__elasticsearch__.create_index! force:true
Image.import
CaseStudy.__elasticsearch__.create_index! force:true
CaseStudy.import
end
end
As the description in the file says this bootstraps the elastic search index. What I don't know is how do I run all this in a production environment? There are other rake files as well and I want to know how to run them.
You should go to the root path of your project directory in production and type the following command:
RAILS_ENV=production bundle exec rake app:elasticsearch
Note:
To check the available list of rake tasks, you can type rake -T
rake task-name RAILS_ENV=production

I cannot deploy my newly created app on heroku because of postgresql

I'm learning Ruby on Rails, and having lots of difficulty setting up.
So I have created this new app, using 'rails new' command.
Modified a bit, and now on way to deploy it on heroku.
First try, it failed because heroku required the app to use postgresql rather than sqlite3, which was set as a default database.
So I downloaded and installed postgresql, and here is the where the trouble began.
After installing the postgres, I set up the path.
And then I was unable to run the app locally anymore.
In command, "rails server" command is not working anymore, showing all kinds of error not being able to load stuff.
It looks like this:
D:\ruby\appname>rails server
c:\RailsInstaller/Ruby2.1.0/lib/ruby/gems/2.1.0/gems/pg-0.17.1-x86-mingw32/lib/pg.rb:10:in 'require': cannot load such file --2.1/pg_ext <LoadError>
and another bunch of load error.
No need to mention deploying on heroku, it fails everytime.
After installing postgresql, I can't use any database related commands as it says "loaderror" everywhere.
I can't run the app locally, nor deploy on heroku.
How should I fix this database problem?
In development you can still use SQLite, and in production use Postgres.
It's not advisable forever to have that disparity, but it does mean you an make progress.
Remove any production settings in database.yml, but still include 'pg' in your Gemfile.
Heroku will automatically insert database settings for Postgres in production. Deploy and then run heroku run rake db:migrate to set up your database.

Running a heroku database command from pgAdmin III? (Harry's Prelaunchr)

so i've successfully installed Harrys Prelauncher on Heroku (https://github.com/harrystech/prelaunchr)
and to export my collected emails into a csv i need to run this command (bundle exec rake prelaunchr:create_winner_csvs)
is there any way to run that command through pgadmin or some other program?
or is the only way for me to download my heroku database and run the command locally? also how and what would i need to do this?
i'm pretty new to rails and postgresql and would really appreciate if someone could help me out!
Because the rake task creates files locally you can't just run it on heroku via heroku run rake. You can however set up your local database.yml to connect to your heroku postgresql instance and run the rake task locally.
Run heroku pg:credentials to get the required database values.
Fill in the production environment of config/database.yml with the values you obtained from step 1 (for the value of 'database' in the yml file, use dbnmae from step 1)
Test your connection with RAILS_ENV=production rails db. This should drop you into a psql console.
Run the rake task. RAILS_ENV=production bundle exec rake prelaunchr:create_winner_csvs
The files will save locally in lib/assets as indicated by the documentation.
From within the directory of the project you can use
heroku run rake prelaunchr:create_winner_csvs
You should probably create a UI form in to your application.
On click on export CSV, it should run background job on heroku (Using delayed jobs).
heroku run rake prelaunchr:create_winner_csvs
Use send_data ruby method. To send your generated and dumped data file on to your browser.
Download the file on to your local system from running heroku instance.
Hope this will resolve your problem.
Cheers!!!
I ran into this problem recently while developing the Prelaunchr campaign for a client. Assuming you have a local version of your app and are using Postgres Copper in Heroku, you can "pull" your Heroku database down to your local machine, set that as your development database in database.yml, and run the rake task from your local app, which should now have the same database as your heroku version. Here is the command to pull the db (subbing out name_for_database & heroku_app_name with your own):
heroku pg:pull HEROKU_POSTGRESQL_COPPER_URL name_for_database --app heroku_app_name
Make sure to restart your local server to see the new database info populated.

Rails - Undefined Method in Staging, but fine in local development

I'm working on a project and I added a migration to add a project_page_description field to a Company Model. When I've ran the migrations,
#company = Company.first
#company.project_page_description
works locally, but any time I attempt to access project_page_description after deploying, I get an undefined method error. Why does it work locally but not remotely?
I've deployed all code so the codebases are identical, I've ran all migrations, I'm using Bundler and Capistrano, and I'm deploying to a CentOS server.
Also, besides stop programming, what can I do to stop this from happening again?
Looks like the migrations have not been run. Maybe you ran them using the development environment? Try running them like so: RAILS_ENV=staging bundle exec rake db:migrate.
Check your bundler groups, application config, and environment initializer files, maybe you have something defined as being development-only and not staging? Also try opening a console up on staging and trying to manually load and invoke the module that's not found. Remember that require returns true if the module was not already loaded.

running Rails console in production

I have just gone live with my first Rails site, but now I have a problem. When I run the project in development mode on my IDE I can run the console to something like:
User.first.name='whatever' to change a users name.
How do I accomplish the same task on a live site in production mode?
if you're running rails 3.0 or greater, you can also use
rails console production
production can of course be substituted with development or test (value is development by default)
Adding the option --sandbox makes it so that any changes you make to your database in the console will be rolled back after you exit
If this isn't working for you, you may need to try
bundle exec rails console production
If you are actually trying to run the rails console on your production server, try googling "run rails console [your cloud hosting provider]" e.g. "run rails console heroku"
As of Rails 6 you need to use
RAILS_ENV=production bundle exec rails c
or
RAILS_ENV=production rails c
depending on your setup
Pretty easy:
RAILS_ENV=production rails console
If you have already deployed your site to the server, you can also use:
bundle exec rails console production
...in the webroot of your rails app. That is if you haven't installed the rails package directly on the server yet or if you want to run console within the context of your web app.
Try below command.
rails c -e production
Note: This answer assumes you are using Heroku as your hosting service.
It depends on what hosting service you are using. For Heroku, you can go to your terminal and type in
heroku run rails console
This will load up the rails console for your production site and will allow you to create records for your live site.
You can also look into seeding a database but that is generally meant for testing. RailsCasts has some videos on the topic but they are a bit outdated.
With Rails 6.1.6 on AlmaLinux8, the below command worked for me.
bundle exec rails console -e production
today with rails 6 run in console RAILS_ENV=production rails console

Resources