diff --git a/queue_job/migrations/19.0.2.5.0/pre-migration.py b/queue_job/migrations/19.0.2.5.0/pre-migration.py new file mode 100644 index 0000000000..ac05290069 --- /dev/null +++ b/queue_job/migrations/19.0.2.5.0/pre-migration.py @@ -0,0 +1,25 @@ +# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl.html) +from openupgradelib import openupgrade + +# The indexes were created in init() with a free text comment. models.Index compares +# the comment with its own definition and rebuilds the index when they differ, so they +# get the comment the ORM expects and are not rebuilt on the first update. +INDEXES = { + "queue_job_identity_key_state_partial_index": ( + "INDEX (identity_key) " + "WHERE state in ('pending','enqueued','wait_dependencies') " + "AND identity_key IS NOT NULL" + ), + "queue_job_channel_date_done_date_created_index": ( + "INDEX (channel, date_done, date_created)" + ), +} + + +@openupgrade.migrate() +def migrate(env, version): + cr = env.cr + for name, definition in INDEXES.items(): + cr.execute("SELECT to_regclass(%s)", [name]) + if cr.fetchone()[0]: + cr.execute(f'COMMENT ON INDEX "{name}" IS %s', [definition]) diff --git a/queue_job/models/queue_job.py b/queue_job/models/queue_job.py index a2481adb75..d20001e019 100644 --- a/queue_job/models/queue_job.py +++ b/queue_job/models/queue_job.py @@ -8,7 +8,6 @@ from odoo import api, exceptions, fields, models from odoo.fields import Domain from odoo.tools import SQL, html_escape -from odoo.tools.sql import create_index from odoo.addons.base_sparse_field.models.fields import Serialized @@ -139,33 +138,20 @@ class QueueJob(models.Model): readonly=True, ) - channel = fields.Char(index=True) + channel = fields.Char() identity_key = fields.Char(readonly=True) worker_pid = fields.Integer(readonly=True) - def init(self): - cr = self.env.cr - # Used by Job.job_record_with_same_identity_key - create_index( - cr, - "queue_job_identity_key_state_partial_index", - "queue_job", - ["identity_key"], - where=( - "state in ('pending','enqueued','wait_dependencies') " - "AND identity_key IS NOT NULL" - ), - comment=("Queue Job: partial index for identity_key on active states"), - ) - # Used by ._gc_old_jobs - create_index( - cr, - "queue_job_channel_date_done_date_created_index", - "queue_job", - ["channel", "date_done", "date_created"], - comment="Queue Job: index to accelerate autovacuum", - ) + # Used by Job.job_record_with_same_identity_key + _identity_key_state_partial_index = models.Index( + "(identity_key) WHERE state in ('pending','enqueued','wait_dependencies') " + "AND identity_key IS NOT NULL" + ) + # Used by ._gc_old_jobs + _channel_date_done_date_created_index = models.Index( + "(channel, date_done, date_created)" + ) @api.depends("dependencies") def _compute_dependency_ids(self):