mirror of
https://github.com/opf/openproject.git
synced 2026-06-13 19:20:00 +00:00
Fix error when users table does not exist (#21482)
* Fix error when users table does not exist
In multitenant context, when schema is switched to and the tables are
not created yet, getting `User.current` will fail with
ActiveRecord::StatementInvalid:"PG::UndefinedTable: ERROR: relation \"users\" does not exist" [...]
To prevent it, check if the table exists first and return `nil` if not.
* Avoid logging "slow sql" for database creation
* Better performance for handling unexisting users table
Checking multiple times the users table existence when something is
logged is probably a bad idea. Better rescue errors.
Also cache the return value of `#current_user` to call the method only
once.
This commit is contained in:
committed by
GitHub
parent
b679454fa3
commit
be23b6e89b
@@ -42,8 +42,8 @@ Rails.application.configure do
|
||||
# Skip transaction that may be blocked
|
||||
next if data[:sql].match?(/BEGIN|COMMIT/)
|
||||
|
||||
# Skip tenant creation (load dump)
|
||||
next if data[:sql][..120].include?("Dumped by pg_dump")
|
||||
# Skip tenant creation (load dump sql which is around 300kB)
|
||||
next if data[:sql][..120].include?("Dumped by pg_dump") || data[:sql].size > 200_000
|
||||
|
||||
# Skip smaller durations
|
||||
duration = ((finish - start) * 1000).round(4)
|
||||
|
||||
@@ -5,7 +5,7 @@ module OpenProject
|
||||
##
|
||||
# Consume a message and let it be handled
|
||||
# by all handlers
|
||||
def log(exception, context = {})
|
||||
def log(exception, context = {}) # rubocop:disable Metrics/AbcSize, Metrics/PerceivedComplexity
|
||||
# in case we're getting ActionController::Parameters
|
||||
context = if context.respond_to?(:to_unsafe_h)
|
||||
context.to_unsafe_h
|
||||
@@ -28,9 +28,11 @@ module OpenProject
|
||||
|
||||
# Set current contexts
|
||||
context[:level] ||= context[:exception] ? :error : :info
|
||||
context[:current_user] ||= User.current
|
||||
if the_current_user = current_user
|
||||
context[:current_user] ||= the_current_user
|
||||
end
|
||||
|
||||
registered_handlers.values.each do |handler|
|
||||
registered_handlers.each_value do |handler|
|
||||
handler.call message, context
|
||||
rescue StandardError => e
|
||||
Rails.logger.error "Failed to delegate log to #{handler.inspect}: #{e.inspect}\nMessage: #{message.inspect}"
|
||||
@@ -103,6 +105,14 @@ module OpenProject
|
||||
extended = OpenProject::Logging.extend_payload!(payload, context)
|
||||
OpenProject::Logging.formatter.call extended
|
||||
end
|
||||
|
||||
def current_user
|
||||
User.current
|
||||
# Probably more performant to rescue "ActiveRecord::StatementInvalid: PG::UndefinedTable"
|
||||
# than to check for table existence with `User.connection.data_source_exists?(User.table_name)`
|
||||
rescue StandardError
|
||||
nil
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user