Project

General

Profile

Actions

Defect #33121

closed

IssueQuery not usable from plugins

Added by Stefan Rinkes over 4 years ago. Updated over 3 years ago.

Status:
Closed
Priority:
Normal
Assignee:
-
Category:
Plugin API
Target version:
-
Start date:
Due date:
% Done:

0%

Estimated time:
Resolution:
Duplicate
Affected version:

Description

I'm currently working on updating some plugins for the usage with Redmine 4.1.0.

And noticed I cannot init Redmine while my plugins are already located in plugins-folder.

I stripped it down to an patch for IssueQuery included in the plugin.

Here's a minimal plugin to trigger the issue:

require 'redmine'

module RedmineA
  module IssueQueryPatch
  end

  module ProjectPatch
  end
end

Rails.configuration.to_prepare do
  unless Project.included_modules.include? RedmineA::ProjectPatch
    Project.send(:include, RedmineA::ProjectPatch)
  end

  unless IssueQuery.included_modules.include? RedmineA::IssueQueryPatch
    IssueQuery.send(:include, RedmineA::IssueQueryPatch)
  end
end

Redmine::Plugin.register :redmine_a do
  name 'Redmine Test Plugin'
  author 'Stefan Rinkes'
  description 'To show IssueQuery-Patch failure'
  version '0.0.0'
end

Place this as /usr/src/redmine/plugins/redmine_a/init.rb and try to create a brand new database.

# rake db:create
W, [2020-03-10T14:00:19.875929 #12605]  WARN -- : Creating scope :system. Overwriting existing method Enumeration.system.
W, [2020-03-10T14:00:19.905024 #12605]  WARN -- : Creating scope :sorted. Overwriting existing method Group.sorted.
W, [2020-03-10T14:00:19.949338 #12605]  WARN -- : Creating scope :sorted. Overwriting existing method User.sorted.
rake aborted!
ActiveRecord::NoDatabaseError: Unknown database 'redmine_test'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/mysql2_adapter.rb:26:in `rescue in mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/mysql2_adapter.rb:12:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:466:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/attributes.rb:234:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/attribute_decorators.rb:51:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:459:in `block in load_schema'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:456:in `load_schema'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:336:in `columns_hash'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/inheritance.rb:78:in `descends_from_active_record?'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/inheritance.rb:84:in `finder_needs_type_condition?'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/core.rb:287:in `relation'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/scoping/default.rb:34:in `unscoped'
/usr/src/redmine/app/models/user.rb:822:in `anonymous'
/usr/src/redmine/app/models/user.rb:816:in `current'
/usr/src/redmine/app/models/query.rb:91:in `groupable'
/usr/src/redmine/app/models/query.rb:31:in `initialize'
/usr/src/redmine/app/models/issue_query.rb:36:in `new'
/usr/src/redmine/app/models/issue_query.rb:36:in `<class:IssueQuery>'
/usr/src/redmine/app/models/issue_query.rb:20:in `<top (required)>'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:378:in `block in require_or_load'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:37:in `block in load_interlock'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:37:in `load_interlock'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:356:in `require_or_load'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:510:in `load_missing_constant'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:195:in `const_missing'
/usr/src/redmine/plugins/redmine_a/init.rb:16:in `block in <top (required)>'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:426:in `instance_exec'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:426:in `block in make_lambda'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:606:in `block (2 levels) in default_terminator'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:605:in `catch'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:605:in `block in default_terminator'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:199:in `block in halting'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `block in invoke_before'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `each'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `invoke_before'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:131:in `run_callbacks'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/reloader.rb:89:in `prepare!'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/finisher.rb:63:in `block in <module:Finisher>'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `instance_exec'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `run'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:61:in `block in run_initializers'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:60:in `run_initializers'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:361:in `initialize!'
/usr/src/redmine/config/environment.rb:16:in `<top (required)>'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:337:in `require'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:337:in `require_environment!'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/usr/local/bundle/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'

Caused by:
Mysql2::Error: Unknown database 'redmine_test'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `connect'
/usr/local/bundle/gems/mysql2-0.5.3/lib/mysql2/client.rb:90:in `initialize'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `new'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/mysql2_adapter.rb:22:in `mysql2_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:830:in `new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:874:in `checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:853:in `try_to_checkout_new_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:814:in `acquire_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:538:in `checkout'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:382:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:1033:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:118:in `retrieve_connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/connection_handling.rb:90:in `connection'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:466:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/attributes.rb:234:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/attribute_decorators.rb:51:in `load_schema!'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:459:in `block in load_schema'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:456:in `load_schema'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/model_schema.rb:336:in `columns_hash'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/inheritance.rb:78:in `descends_from_active_record?'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/inheritance.rb:84:in `finder_needs_type_condition?'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/core.rb:287:in `relation'
/usr/local/bundle/gems/activerecord-5.2.4.1/lib/active_record/scoping/default.rb:34:in `unscoped'
/usr/src/redmine/app/models/user.rb:822:in `anonymous'
/usr/src/redmine/app/models/user.rb:816:in `current'
/usr/src/redmine/app/models/query.rb:91:in `groupable'
/usr/src/redmine/app/models/query.rb:31:in `initialize'
/usr/src/redmine/app/models/issue_query.rb:36:in `new'
/usr/src/redmine/app/models/issue_query.rb:36:in `<class:IssueQuery>'
/usr/src/redmine/app/models/issue_query.rb:20:in `<top (required)>'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `block in require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:257:in `load_dependency'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:291:in `require'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:378:in `block in require_or_load'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:37:in `block in load_interlock'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:14:in `block in loading'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/concurrency/share_lock.rb:151:in `exclusive'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies/interlock.rb:13:in `loading'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:37:in `load_interlock'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:356:in `require_or_load'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:510:in `load_missing_constant'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/dependencies.rb:195:in `const_missing'
/usr/src/redmine/plugins/redmine_a/init.rb:16:in `block in <top (required)>'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:426:in `instance_exec'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:426:in `block in make_lambda'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:198:in `block (2 levels) in halting'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:606:in `block (2 levels) in default_terminator'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:605:in `catch'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:605:in `block in default_terminator'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:199:in `block in halting'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `block in invoke_before'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `each'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:513:in `invoke_before'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/callbacks.rb:131:in `run_callbacks'
/usr/local/bundle/gems/activesupport-5.2.4.1/lib/active_support/reloader.rb:89:in `prepare!'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application/finisher.rb:63:in `block in <module:Finisher>'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `instance_exec'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:32:in `run'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:61:in `block in run_initializers'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/initializable.rb:60:in `run_initializers'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:361:in `initialize!'
/usr/src/redmine/config/environment.rb:16:in `<top (required)>'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:337:in `require'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:337:in `require_environment!'
/usr/local/bundle/gems/railties-5.2.4.1/lib/rails/application.rb:520:in `block in run_tasks_blocks'
/usr/local/bundle/gems/rake-13.0.1/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:create => db:load_config => environment
(See full trace by running task with --trace)

Removing the plugin will let you create the database and works flawlessy once the database is there.


Related issues

Is duplicate of Redmine - Defect #33290: Unnecessary database access when IssueQuery class is definedClosedGo MAEDA

Actions
Actions #1

Updated by Ko Nagase almost 4 years ago

Well, I encountered almost the same issue in our plugin.
(Note that Redmine 4.0-stable branch is no problem.)

Actions #2

Updated by Jens Krämer almost 4 years ago

As a workaround catching ActiveRecord::NoDatabaseError in the to_prepare block should do the trick.

Actions #3

Updated by Ko Nagase almost 4 years ago

Well, catching ActiveRecord::NoDatabaseError solved the rake db:create issue,
but when executing rake db:drop (without rake db:migrate), I encountered the following error on PostgreSQL database.


rake aborted!
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR:  relation "users" does not exist
LINE 8:                WHERE a.attrelid = '"users"'::regclass
                                          ^
:               SELECT a.attname, format_type(a.atttypid, a.atttypmod),
                     pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod,
                     c.collname, col_description(a.attrelid, a.attnum) AS comment
                FROM pg_attribute a
                LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum
                LEFT JOIN pg_type t ON a.atttypid = t.oid
                LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation
               WHERE a.attrelid = '"users"'::regclass
                 AND a.attnum > 0 AND NOT a.attisdropped
               ORDER BY a.attnum
:

I confirmed that applying the following redmine core patch solved the both rake db:create and rake db:drop case, but is this acceptable way ?

--- a/app/models/query.rb
+++ b/app/models/query.rb
@@ -87,7 +87,9 @@ end
 class TimestampQueryColumn < QueryColumn
   def groupable
     if @groupable
-      Redmine::Database.timestamp_to_date(sortable, User.current.time_zone)
+      if ActiveRecord::Base.connection_pool.connected? and ActiveRecord::Base.connection.table_exists?(:users)
+        Redmine::Database.timestamp_to_date(sortable, User.current.time_zone)
+      end
     end
   end

Actions #4

Updated by Kevin Fischer almost 4 years ago

#33290 is related to this and the patch attached there should solve this problem, too

Actions #5

Updated by Marius BĂLTEANU almost 4 years ago

  • Related to Defect #33290: Unnecessary database access when IssueQuery class is defined added
Actions #6

Updated by Marius BĂLTEANU almost 4 years ago

  • Target version set to 4.1.2

Assign this to 4.1.2 in order to be review together with #33290. It seems to be the same problem.

Actions #7

Updated by Marius BĂLTEANU almost 4 years ago

  • Assignee set to Jean-Philippe Lang
Actions #8

Updated by Marius BĂLTEANU over 3 years ago

  • Target version changed from 4.1.2 to 4.2.0
Actions #9

Updated by Marius BĂLTEANU over 3 years ago

  • Related to deleted (Defect #33290: Unnecessary database access when IssueQuery class is defined)
Actions #10

Updated by Marius BĂLTEANU over 3 years ago

  • Is duplicate of Defect #33290: Unnecessary database access when IssueQuery class is defined added
Actions #11

Updated by Marius BĂLTEANU over 3 years ago

  • Assignee deleted (Jean-Philippe Lang)
  • Target version deleted (4.2.0)
  • Resolution set to Duplicate

Closing this in favour of #33290.

Actions #12

Updated by Marius BĂLTEANU over 3 years ago

Please watch the related issue for updates.

Actions #13

Updated by Go MAEDA over 3 years ago

  • Status changed from New to Closed

Committed the fix. Thank you for your contribution.

Actions

Also available in: Atom PDF