Skip to content

Commit

Permalink
Merge pull request #13555 from opf/bugfix/49771-extend-postgres-state…
Browse files Browse the repository at this point in the history
…ment-timeout-for-journals-migration

[49771] Set statement timeout to 15 minutes for the validity period migration
  • Loading branch information
aaron-contreras committed Aug 24, 2023
2 parents 7ef9fa5 + b4b8ebe commit 53859f1
Show file tree
Hide file tree
Showing 4 changed files with 173 additions and 0 deletions.
35 changes: 35 additions & 0 deletions config/initializers/migration_statement_timeout.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

require_relative '../../lib_static/open_project/migration_statement_timeout/manager'
require_relative '../../lib_static/open_project/migration_statement_timeout/migration_extensions'

ActiveRecord::Migration.prepend(MigrationStatementTimeout::Manager)
ActiveRecord::Migration.extend(MigrationStatementTimeout::MigrationExtensions)
2 changes: 2 additions & 0 deletions db/migrate/20230608151123_add_validity_period_to_journals.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class AddValidityPeriodToJournals < ActiveRecord::Migration[7.0]
set_minimum_statement_timeout('15min')

def change
add_column :journals, :validity_period, :tstzrange

Expand Down
83 changes: 83 additions & 0 deletions lib_static/open_project/migration_statement_timeout/manager.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module MigrationStatementTimeout
module Manager
def exec_migration(conn, direction)
min_timeout = self.class.minimum_statement_timeout
return super unless min_timeout
return super unless direction == :up

if disable_ddl_transaction
raise 'Cannot set local statement_timeout outside of a transaction. ' \
'Try removing disable_ddl_transaction! from your migration.'
end

set_statement_timeout_at_least_at(conn, min_timeout)

super
end

def set_statement_timeout_at_least_at(conn, min_timeout)
current_timeout = get_timeout(conn)
if in_ms(min_timeout) > in_ms(current_timeout)
set_timeout(conn, min_timeout)
say "set statement_timeout to #{min_timeout} (was #{current_timeout} before)"
else
say "ignore set statement_timeout to #{min_timeout}: it would be lower than current value #{current_timeout}"
end
end

def set_timeout(conn, timeout)
conn.execute("SET LOCAL statement_timeout = '#{timeout}'")
end

def get_timeout(conn)
conn.execute('SHOW statement_timeout').first['statement_timeout']
end

def in_ms(timeout)
case timeout
when Integer
timeout
when /\A\d+(ms)?\z/
timeout.to_i
when /\A\d+s\z/
timeout.to_i * 1000
when /\A\d+min\z/
timeout.to_i * 1000 * 60
when /\A\d+h\z/
timeout.to_i * 1000 * 60 * 60
else
raise "Unrecognized statement timeout duration #{timeout.inspect}"
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) 2012-2023 the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module MigrationStatementTimeout
module MigrationExtensions
attr_accessor :minimum_statement_timeout

# Sets the minimum statement timeout for this migration.
#
# If the current statement timeout is lower than the given value, it will be
# set to this value. It does nothing if the statement timeout is already set
# to a higher value.
#
# When the given value is an integer or a string without units, it is
# interpreted as milliseconds.
#
# When the given value is a string with units, it is interpreted
# accordingly. Valid units for this parameter are "ms", "s", "min", and "h".
# Examples: "15min", "90s", "2h".
#
# @param [Integer|String] timeout duration
def set_minimum_statement_timeout(timeout)
self.minimum_statement_timeout = timeout
end
end
end

0 comments on commit 53859f1

Please sign in to comment.