Rails Postgresql Query timeout set and skip during migration

Hi, I am Shubham Mishra from India,  this is the part of Ruby on Rails exploration journey.  In this post we will discuss about the rails DB (Postgresql) query time out setting and how can we avoid this timeout setting during migration and I am sure you will get excited to make your hands dirty with those code, so why to wait let's get started...


Question:

Rails and postgresql I want to set query timeout to 10 sec but skip to check this timeout setting during rails migration how can i achieve?

Answer:

To set a query timeout of 10 seconds for PostgreSQL in a Rails application, you can use the statement_timeout configuration option in your database.yml file. Here is an example configuration that sets the query timeout to 10 seconds:


This will set the query timeout to 10 seconds for all queries executed by the PostgreSQL adapter in the production environment.

To skip checking the query timeout setting during Rails migrations, you can use a custom connection adapter that sets the statement_timeout to nil during migrations. Here is an example implementation of a custom adapter that does this:


This custom adapter overrides the exec_query method of the PostgreSQLAdapter class to set the statement_timeout to 10 seconds for all queries executed outside of migrations, and to skip the timeout check during migrations. The with_statement_timeout method is used to set the timeout temporarily and ensure that it is reset to the previous value after the query is executed.

To use this custom adapter during migrations, you can set the __migration_context__ thread-local variable to true before running the migration. For example:


This will ensure that the custom adapter skips the query timeout check during the migration, and sets the timeout to 10 seconds for all other queries executed by the PostgreSQL adapter.

Another Approach: Now in case you dont want to go for these many changes, u can use a database connection that has a different configuration specifically for migrations. This way, you can set the query timeout to a higher value or disable it altogether for migrations, without affecting the configuration for the rest of the application. Here's an example configuration that sets a query timeout of 10 seconds for the production environment, but disables the query timeout for migrations:


In this configuration, the production_for_migrations connection uses the same PostgreSQL database as production, but does not set the statement_timeout variable, effectively disabling the query timeout.

To use the production_for_migrations connection for migrations, you can set the DATABASE_URL environment variable to the URL of this connection when running migrations. For example:

$ DATABASE_URL=postgres://my_app:secret@localhost/my_app_production_for_migrations rails db:migrate

OR

In the deployment script you can mention Rails Env with the db migrate cmd which will make sure to run the migration with mentioned confg connection,

RAILS_ENV=production_for_migrations rake db:migrate

This will ensure that the query timeout is disabled for migrations, while still using the normal configuration with the query timeout set to 10 seconds for the rest of the application.

But wait, I have one more solution, is you can have seprate config for normal DB queries and migrations timeout in rakefile, rakefile it always be there on the root path of your rails application. And the final code would look like this:


You can change the value 10000 to any other value and 0 timeout for migration block is saying no timeout.

One more solution:

In this solution what you need to do is to create a initializer file and register callbacks for migrations, before_migration and after_migration,


I think this is best solution rather than touching rakefile

Actually in my case i had Rails 7.0.4.3 so above solution doesn't worked as those callbacks were not available for my version so u can try below solutions:


You can keep on trying by changing the migration timeout to '1' to get time out for rake db:migrate

You can explore my previous blog: DB query optimization in Ruby on Rails

Or you can explore my other blogs too here,
Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.


Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.
Liked my blogs, wanna to connect:

LinkedIn   GitHub  |  HackerEarth   ResearchGate  |  Twitter  |  Facebook  |  StackOverflow

Thanks for reading this post, Have a good day :)



Comments