Last active
April 21, 2023 07:49
-
-
Save codertcet111/3bea6a680c89893513f877a2c743f200 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # config/initializers/active_record_query_timeout.rb | |
| # Set statement timeout for each query | |
| if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") # 10 seconds | |
| ActiveRecord::MigrationContext.prepend(Module.new do | |
| def initialize(*args) | |
| super | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '0';") | |
| end | |
| def iterate_with_statement_timeout(*args, &block) | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") | |
| super | |
| end | |
| end) | |
| ActiveRecord::MigrationProxy.prepend(Module.new do | |
| def run_migration(*args) | |
| migration = migration | |
| ActiveRecord::MigrationContext.new(migration.version, migration.name, nil).iterate_with_statement_timeout do | |
| super | |
| end | |
| end | |
| end) | |
| end | |
| #One more soulution you can try if above doesn't support for your verion: | |
| # config/initializers/active_record_query_timeout.rb | |
| if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") # 10 seconds | |
| # Register a before migration callback to reset the timeout to 0 which means no timeout | |
| ActiveRecord::MigrationContext.before_each_migration do |migration| | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '0';") | |
| end | |
| # Register an after migration callback to reset the timeout | |
| ActiveRecord::MigrationContext.after_each_migration do |migration| | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") | |
| end | |
| end | |
| #OR | |
| # config/initializers/active_record_query_timeout.rb | |
| # Set statement timeout for each query | |
| if ActiveRecord::Base.connection_config[:adapter] == 'postgresql' | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") # 10 seconds | |
| # Define a method to reset the timeout to 10 seconds | |
| def reset_query_timeout | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '10000';") | |
| end | |
| # Add a before_action to set the timeout to 0 before the migration | |
| ActiveSupport.on_load(:active_record) do | |
| ActiveRecord::Migration.before_each_migration do |migration| | |
| ActiveRecord::Base.connection.execute("SET statement_timeout = '0';") | |
| end | |
| ActiveRecord::Migration.after_each_migration do |migration| | |
| reset_query_timeout | |
| end | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment