Rails - All test database opertaions hanging - ruby-on-rails

I was running a small migration to the test database (already run fine on local database), when it hung up. I terminated the process, and tried to re-run, I keep getting the error:
Cannot run migrations because another migration process is currently running.
So I try to reset the database, by running rails db:reset RAILS_ENV=test, but this again keeps running and giving no response. Desperate I thus try dropping and rebuilding the database, by first rails db:drop RAILS_ENV=test that again runs forever and gives no response.
How can I get the migration to run normally again?

You can try restarting the test database to clear any connections.
Depending on the platform you're using:
brew services restart mysql
or
docker-compose restart mysql

Related

"heroku run rails db:seed" stops without finishing ("State changed from up to complete")

I'm using Rails 6.0.2.2 with PostgreSQL 12.1. I've been trying to deploy my Rails app on Heroku, but when I run "heroku run rails db:seed", the seeding process stops without completing. If I check the logs, all it says is:
2020-04-19T04:47:10.789255+00:00 heroku[run.1139]: State changed from up to complete
But it never finished running the seed file. By adding print statements in the seed file, I can see that it always stops at the same place, where it's inserting (fairly large) amounts of data into the database. However, I don't see an out-of-memory or other error anywhere.
What might be the problem, or where can I find more information about what might be causing the problem?

removing postgres database on ubuntu from commandline

I am working on a rails app. Due to some messing up with the code I deleted the app from my local development machine and cloned the previous commit from the git repo.
Now i want to delete the db file(postgres) of the deleted one from the local machine as well. I had issues in the past that made me unable to access several features due to database conflicts(due to same db name). So i fixed that by removing the database and recreating with the cloned app. I had done it before from commandline but i forget now on how to do it. Could some one tell me where is the postgres database file located in ubuntu or how to remove from commandline? Thanks in advance.
There is a postgresql shell command dropdb which you can use.
After a bit of searching i found that doing this will remove the database. From the commandline just cd into your app directory and run this command
rake RAILS_ENV='development' db:drop
This will remove the database(although am still unable to find the exact location where its stored physically in hard drive). This is the solution if you cant access rails console, or some rake commands.
After that we can just recreate the database using
rake db:create

Do you need to restart Heroku after every migration?

Recently I had an issue where my db scheme change wasn't being reflected on Heroku PG. I double checked to see that both migration and seed succeeded. What was even weirder was that the db scheme change worked fine on a staging heroku deployment (after the exact same migration/seed). After some searching around, I learned that you are supposed to restart heroku after migrations via:
heroku restart --app=app_name
I've never had to do this (I'm not exactly a veteran, but I've run a good amount of migrations before and have never had to restart heroku for this particular reason).
Do I actually need to be restarting heroku after each migration? Or is this more of a case by case thing?
If you make changes to your DB via migrations then you will need to restart the application on Heroku. When Rails starts in production mode it caches the DB schema. Migrations run in one off processes which the running web process is not aware of. So for it to pick up the changes you need to restart at least your web processes. If your application was idling when you deployed and you ran the migrations it would pick up the new schema as the app started.
You need to get your app to pick up the new migration. Restarting the app works just fine. Or you can clear the schema cache:
heroku run rake db:schema:cache:clear --app=app_name
It depends i guess. Speaking generally, the answer is NO. If you restart your app locally after every migration, then it is needed else i don't think it is required .

Ruby on Rails config change while server is running?

Hi I'm new to Ruby on Rails. I have installed the Testia Tarantula application and am trying to read up on Ruby.
My question is how do I start/stop the server.
For example: I want to change the Admin email, so I execute the following command to change the configuration of the app:
RAILS_ENV=production rake db:config:app
But is this command ok to execute while the server is running, it has 'db' in the command which is what would warn me that I shouldn't run it while the server is up. Anyone have some helpful tips for learning Ruby on Rails server app management?
Welcome to Rails!
You can run rake db:xxxxx while the server is running and it won't hurt anything. However I usually stop my server, run my rake command and then start it back up to ensure that all changes will be picked up. If running in production, I would think you may want to restart the server just to make sure. I believe that the schema is generated/updated upon server startup, just fyi.
As far as starting and stopping the server, if you are attached to it you can just use ctrl + c. If it is detached, you can search for the pid and then kill -9 .
Running rake db:anything will load rails on its own. It doesn't matter if you have a server up or not. This will happen in the background. Think of it as the same as running a sql script while the server is running. It's a separate process.

Why do I get "We're sorry, but something went wrong." after restoring via pgbackups:restore with no migrations

This one took me a while to figure out. It's pretty tricky and seems like a bug in Heroku, so I will post my own answer in case it helps someone.
Scenario:
Rails application using postgresql adapter works fine on localhost.
All migrations are complete.
Deploying to Heroku via "git push heroku master" successfully updates Heroku instance.
Heroku PGBackups addon is installed and works fine.
pg_dump was used to generate a local db dump, and the file uploaded to an accessible internet location.
Problem:
After running
heroku pgbackups:restore DATABASE_URL 'http://mywebsite.com/pgbackup.dump'
I get the "something went wrong" message. Strange, considering all that happened was a database load, a database whose data works fine on the local machine. No migrations were performed, everything has been committed previously, no code updates... Only data in the database was changed, so why does the app no longer work if just data in the database changed?
Checking the heroku logs shows that it can't find tables that are clearly there. Lines like:
ActionView::Template::Error (PG::UndefinedTable: ERROR: relation "users" does not exist
Verifying with heroku pg:psql just makes the situation even stranger, because doing select * from users brings back the real results. Even performing a heroku restart at this stage won't get rid of the problem. I also tried combinations of that with heroku pg:reset and heroku run rake db:migrate.
Why does updating data in the database shut down the app?
Solution:
Run the pgbackups:restore as originally intended (and get error).
Run heroku restart.
Run heroku pg:psql (profit).
For some reason, it seems necessary to restart and then pg:psql before the database works after a pg:restore.
Even though pg:psql will gladly show that the tables are indeed there (contrary to the log messages), the app won't work until the dyno is restarted (and then it STILL won't work), not until you pg:psql.
As far as I know, pg:psql only establishes a connection to the remote database, and should not have any consequence on the state of the database server (though it seems it does).

Resources