Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] stock_picking_group_by_base: Avoid module load error due to DB lock timeout #1328

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 34 additions & 18 deletions stock_picking_group_by_base/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
# Copyright 2019-2020 Camptocamp
# Copyright 2020 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
import logging

from psycopg2.errors import LockNotAvailable
from psycopg2.extensions import AsIs

from odoo import api, models

_logger = logging.getLogger(__name__)


class StockPicking(models.Model):

Expand Down Expand Up @@ -36,25 +41,36 @@
# create index for the domain expressed into the
# stock_move._assign_picking_group_domain method
index_name = "stock_picking_groupby_key_index"
self.env.cr.execute(
"DROP INDEX IF EXISTS %(index_name)s", dict(index_name=AsIs(index_name))
)

self.env.cr.execute(
"""
CREATE INDEX %(index_name)s
ON %(table_name)s %(fields)s
%(where)s
""",
dict(
index_name=AsIs(index_name),
table_name=AsIs(self._table),
fields=tuple(
[AsIs(field) for field in self._get_index_for_grouping_fields()]

try:
self.env.cr.execute(
"DROP INDEX IF EXISTS %(index_name)s", dict(index_name=AsIs(index_name))
)

self.env.cr.execute(
"""
CREATE INDEX %(index_name)s
ON %(table_name)s %(fields)s
%(where)s
""",
dict(
index_name=AsIs(index_name),
table_name=AsIs(self._table),
fields=tuple(
[AsIs(field) for field in self._get_index_for_grouping_fields()]
),
where=AsIs(self._get_index_for_grouping_condition()),
),
where=AsIs(self._get_index_for_grouping_condition()),
),
)
)
except LockNotAvailable as e:
# Do nothing and let module load
_logger.warning(

Check warning on line 67 in stock_picking_group_by_base/models/stock_picking.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_group_by_base/models/stock_picking.py#L67

Added line #L67 was not covered by tests
"Impossible to create index in stock_picking_group_by_base module"
" due to DB Lock problem (%s)",
e,
)
except Exception:
raise

Check warning on line 73 in stock_picking_group_by_base/models/stock_picking.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_group_by_base/models/stock_picking.py#L72-L73

Added lines #L72 - L73 were not covered by tests

def init(self):
"""
Expand Down
Loading