Skip to content

Commit

Permalink
[MIG] stock_picking_group_by_base: Migration to 18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
natuan9 committed Jan 19, 2025
1 parent 661e678 commit 38c95ad
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 19 deletions.
1 change: 1 addition & 0 deletions stock_picking_group_by_base/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ Contributors
------------

- Denis Roussel <[email protected]>
- Tuan Nguyen <[email protected]>

Maintainers
-----------
Expand Down
1 change: 1 addition & 0 deletions stock_picking_group_by_base/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from . import models
from .hooks import uninstall_hook
2 changes: 1 addition & 1 deletion stock_picking_group_by_base/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"name": "Stock Picking Group By Base",
"summary": """
Allows to define a way to create index on extensible domain""",
"version": "16.0.1.0.1",
"version": "18.0.1.0.0",
"license": "AGPL-3",
"author": "ACSONE SA/NV,Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-workflow",
Expand Down
6 changes: 4 additions & 2 deletions stock_picking_group_by_base/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from psycopg2.extensions import AsIs


def uninstall_hook(cr, registry):
def uninstall_hook(env):
"""
This method will remove created index
"""
index_name = "stock_picking_groupby_key_index"
cr.execute("DROP INDEX IF EXISTS %(index_name)s", dict(index_name=AsIs(index_name)))
env.cr.execute(

Check warning on line 11 in stock_picking_group_by_base/hooks.py

View check run for this annotation

Codecov / codecov/patch

stock_picking_group_by_base/hooks.py#L10-L11

Added lines #L10 - L11 were not covered by tests
"DROP INDEX IF EXISTS %(index_name)s", dict(index_name=AsIs(index_name))
)
41 changes: 26 additions & 15 deletions stock_picking_group_by_base/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import logging

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

from odoo import api, models
from odoo.tools import SQL

_logger = logging.getLogger(__name__)

Expand All @@ -32,7 +32,10 @@ def _get_index_for_grouping_fields(self):
def _get_index_for_grouping_condition(self):
return """
WHERE printed is False
AND state in ('draft', 'confirmed', 'waiting', 'partially_available', 'assigned')
AND state in (
'draft', 'confirmed', 'waiting',
'partially_available', 'assigned'
)
"""

@api.model
Expand All @@ -43,30 +46,38 @@ def _create_index_for_grouping(self):

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

self.env.cr.execute(
"""
SQL(
"""
CREATE INDEX %(index_name)s
ON %(table_name)s %(fields)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()]
index_name=SQL.identifier(index_name),
table_name=SQL.identifier(self._table),
fields=SQL(", ").join(
[
SQL.identifier(field)
for field in self._get_index_for_grouping_fields()
]
),
where=AsIs(self._get_index_for_grouping_condition()),
),
where=SQL(self._get_index_for_grouping_condition()),
)
)
except LockNotAvailable as e:

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#L73

Added line #L73 was not covered by tests
# Do nothing and let module load
_logger.warning(

Check warning on line 75 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#L75

Added line #L75 was not covered by tests
"Impossible to create index in stock_picking_group_by_base module"
" due to DB Lock problem (%s)",
e,
self.env._(
"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 83 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#L82-L83

Added lines #L82 - L83 were not covered by tests
Expand Down
1 change: 1 addition & 0 deletions stock_picking_group_by_base/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Denis Roussel \<<[email protected]>\>
- Tuan Nguyen \<<[email protected]>\>
1 change: 1 addition & 0 deletions stock_picking_group_by_base/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,7 @@ <h2><a class="toc-backref" href="#toc-entry-4">Authors</a></h2>
<h2><a class="toc-backref" href="#toc-entry-5">Contributors</a></h2>
<ul class="simple">
<li>Denis Roussel &lt;<a class="reference external" href="mailto:denis.roussel&#64;acsone.eu">denis.roussel&#64;acsone.eu</a>&gt;</li>
<li>Tuan Nguyen &lt;<a class="reference external" href="mailto:tuanna&#64;trobz.com">tuanna&#64;trobz.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
Expand Down
3 changes: 2 additions & 1 deletion stock_picking_group_by_base/tests/test_groupby_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo.tests.common import TransactionCase
from odoo.tools import SQL


class TestPickingGroupBy(TransactionCase):
def test_index(self):
index_name = "stock_picking_groupby_key_index"
self.env.cr.execute(
"SELECT indexname FROM pg_indexes WHERE indexname = %s", (index_name,)
SQL("SELECT indexname FROM pg_indexes WHERE indexname = %s", (index_name,))
)
self.assertTrue(self.env.cr.fetchone())

0 comments on commit 38c95ad

Please sign in to comment.