Recent posts
-
How to downgrade the postgres addon version on Heroku.
We recently had to downgrade the version of the postgres addon on Heroku.
Our production was running on version
14
and our staging server was running on16
.We wanted to downgrade the staging server to version
14
to ensure that the versions are consistent across environments.This is how we did it.
First, we created a new postgres addon with the version we wanted to downgrade to.
heroku addons:create heroku-postgresql:essential-1 --version 14 --app my-heroku-app-name
We checked the status of the new postgres addon.
NOTE:
HEROKU_POSTGRESQL_NAVY
is the new postgres addon we created.heroku pg:info --app my-heroku-app-name === HEROKU_POSTGRESQL_NAVY_URL Plan: essential-1 Status: Available Connections: unknown/20 PG Version: 14.11 Created: 2024-09-20 11:24 Data Size: unknown usage / 10 GB (In compliance) Tables: 0/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-rugged-12345 === DATABASE_URL, HEROKU_POSTGRESQL_TEAL_URL Plan: essential-1 Status: Available Connections: 0/20 PG Version: 16.2 Created: 2024-08-20 08:17 Data Size: 3.99 GB / 10 GB (39.9%) (In compliance) Tables: 65/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-horizontal-12345
We turned the maintenance mode on to prevent any writes to the database.
heroku maintenance:on --app my-heroku-app-name
Then we captured latest backup of our current database.
heroku pg:backups:capture --app my-heroku-app-name # Ensure the backup is complete heroku pg:backups --app my-heroku-app-name === Backups Id Created at Status Size Database ──── ───────────────────────── ─────────────────────────────────── ──────── ──────── b003 2024-09-20 12:01:30 +0000 Running (processed 475.00MB) 475.00MB TEAL b002 2024-07-05 10:39:42 +0000 Completed 2024-07-05 10:40:13 +0000 642.51MB TEAL
We got the URL of the backup we just captured.
heroku pg:backups:url b003 --app my-heroku-app-name https://xfrtu.s3.amazonaws.com/73e...52
We restored the backup to the new postgres addon we created.
heroku pg:backups:restore https://xfrtu.s3.amazonaws.com/73e...52 HEROKU_POSTGRESQL_NAVY_URL --app my-heroku-app-name
Ensure the restore is complete before proceeding. Check for the number of tables, the data size etc.
heroku pg:info --app my-heroku-app-name === HEROKU_POSTGRESQL_NAVY_URL Plan: essential-1 Status: Available Connections: 0/20 PG Version: 14.11 Created: 2024-09-20 11:24 Data Size: 3.97 GB / 10 GB (39.69%) (In compliance) Tables: 65/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-rugged-12345 === DATABASE_URL, HEROKU_POSTGRESQL_TEAL_URL Plan: essential-1 Status: Available Connections: 0/20 PG Version: 16.2 Created: 2024-08-20 08:17 Data Size: 3.99 GB / 10 GB (39.9%) (In compliance) Tables: 65/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-horizontal-12345
Promote the new database to be the primary database.
heroku pg:promote HEROKU_POSTGRESQL_WHITE --app my-heroku-app-name Ensuring an alternate alias for existing DATABASE_URL... HEROKU_POSTGRESQL_TEAL_URL Promoting postgresql-rugged-12345 to DATABASE_URL on ⬢ my-heroku-app-name... done Checking release phase... pg:promote succeeded.
Ensure the new database is the primary database. You will see the
DATABASE_URL
is pointing to the new database now.heroku pg:info --app my-heroku-app-name === DATABASE_URL, HEROKU_POSTGRESQL_NAVY_URL Plan: essential-1 Status: Available Connections: 0/20 PG Version: 14.11 Created: 2024-09-20 11:24 Data Size: 3.97 GB / 10 GB (39.72%) (In compliance) Tables: 65/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-rugged-12345 === HEROKU_POSTGRESQL_TEAL_URL Plan: essential-1 Status: Available Connections: 0/20 PG Version: 16.2 Created: 2024-08-20 08:17 Data Size: 3.99 GB / 10 GB (39.9%) (In compliance) Tables: 65/4000 (In compliance) Fork/Follow: Unsupported Rollback: Unsupported Continuous Protection: Off Add-on: postgresql-horizontal-12345
Turn off the maintenance mode.
heroku maintenance:off --app my-heroku-app-name
Finally, we destroyed the old postgres addon.
heroku addons:destroy HEROKU_POSTGRESQL_TEAL --app my-heroku-app-name
That’s it. We successfully downgraded the postgres addon version on Heroku.
References
- Importing and Exporting Heroku Postgres Databases
- Upgrading the Version of a Heroku Postgres Database
-
How to start rails console on fly.io
Issue
read morefly ssh console Connecting to fdaa:0:xxxx:xxx:xxx:xxxx:xxxx:2... complete root@4xxxxxxxxxx8:/app# rails c -bash: rails: command not found
-
Error running '__rvm_make -j12'
Issue
read moreError running '__rvm_make -j12', please read /Users/deepak/.rvm/log/1704350883_ruby-3.3.0/make.log
-
What's new in Rails 7.1
Update:
Rails 7.1 Beta 1 is released on September 13, 2023.
Rails 7.1 is released on October 5, 2023.
Let’s see what is new in rails 7.1
Dockerfile
Rails will now auto generate docker related files in the application.
Dockerfile .dockerignore bin/docker-entrypoint
Build and run your application using docker
read moredocker build -t app . docker volume create app-storage docker run --rm -it -v app-storage:/rails/storage -p 3000:3000 --env RAILS_MASTER_KEY=<your-config-master-key> app
-
Error installing puma - Failed to build gem native extension
Issue
read moreBuilding native extensions. This could take a while... ERROR: Error installing puma: ERROR: Failed to build gem native extension.
-
How to get rid of bot accounts from Twitter timeline.
Tired of the bot accounts showing up in the twitter replies section?
There’s no direct way to block all the bot accounts as of 14th July 2023.
But we will show you a way to get rid of the bot accounts to some extent.
Let’s see how to do it:
read more -
undefined method `exists?' for File:Class (NoMethodError)
Recently we upgraded ruby to 3.2. We started getting the following error:
/Users/deepak/.rvm/gems/ruby-3.2.2@my-project/gems/pundit-2.3.0/lib/generators/rspec/templates/policy_spec.rb:1:in `template': undefined method `exists?' for File:Class (NoMethodError) Did you mean? exist?
This is how we traced it and fixed it:
read more -
How to clear Twitter interests in bulk.
Are you getting random tweets on your timeline?
Or you clicked on a tweet and now you’re getting tweets related to the same topic and can’t get rid of it.
Let’s see how to get rid of the interests:
read more -
Audio element autoplay issue on redirection with turbolinks.
Background of the issue
Recently, we were working on a feature where we wanted to autoplay the audio.
So like a normal developer we did the following:
<audio autoplay data-audio-target="audioElement"> <source src="https://www.xyz/..."> </audio>
Since this is a rails project and we have turbolinks on, we came across an interesting issue.
Whenever we were navigating from the page with the audio element to any other page the audio from the previous page used to play.
We tried to search the same and came across few issue in the turbolinks repository.
We found few solutions but since we were using stimulus js we solved it with the following approach
read more -
5 Must have gems in Ruby on Rails development environment.
I have been developing rails applications since 2014. I started my career with rails and since then I have worked on numerous rails projects.
These are a few gems that I like to add to almost all of my projects (including the personal projects). These gems definitely increase productivity and make the developer’s life easier.
These are my top 5 gems you should have in your development environment.
read more -
A step by step guide to upgrade rails 4 application to rails 5
Upgrading an application can be tedious if you don’t have good test coverage.
This article covers the common issues you might face while upgrading the from rails 4 to 5.
If you are upgrading to ruby 2.7 export
RUBYOPT="-W0"
to avoid getting your screen filled up with deprecations warningsread moreexport RUBYOPT="-W0" bundle exec rspec # OR use RUBYOPT="-W0" bundle exec rspec
-
Elasticsearch::Model - Partial update with custom callbacks
If you want to partially update the documents on Elasticsearch you can include
Elasticsearch::Model::Callbacks
module.You can also perform partial update with custom callbacks instead of indexing the whole document.
Elasticsearch Version - 5.3
Partially update a document
There is
read moreupdate_document_attributes
method inElasticsearch::Model
which is used for partial update but it’s not documented. You can still use the method to perform the partial update. -
Rails and TimeZone issue with query
You must have faced timezone issue if you work with the application in different timezone. ActiveRecord does take care of the timezone while creating, updating or querying the records. The DateTime is converted to UTC before the query is executed.
You will face issue if you are using raw queries or passing
read moreString
instead ofDateTime
. -
Elasticsearch::Model - Put Mapping
The PUT mapping API allows you to add fields to an existing index. This method is available with Elasticsearch::Model but it’s not documented.
Elasticsearch Version - 5.3
Create a User model
read more> rails g model user first_name:string last_name:string email:string phone_number:string invoke active_record create db/migrate/20180420162454_create_users.rb create app/models/user.rb > rake db:migrate
-
Generating migrations with proper commands
Creating a table
If the migration name is of the form “CreateXXX” and is followed by a list of column names and types then a migration creating the table XXX with the columns listed will be generated.
$ rails g migration CreateUsers email:string:index
db/migrate/20161201203703_create_users.rb
read moreclass CreateUsers < ActiveRecord::Migration def change create_table :users do |t| t.string :email end add_index :users, :email end end
-
Include scoped associations
Rails provide different ways to load associations from the database to avoid N + 1 query being fired. In this post, I will show you how to
include
associations with scope applied on them.I have two models
User
andPost
with following associations:read more# app/models/user.rb class User < ActiveRecord::Base has_many :posts, dependent: :destroy end # app/models/post.rb class Post < ActiveRecord::Base belongs_to :user scope :published, -> { where(published: true) } end