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

[14.0][IMP]init hooks for avoiding data loss for state field. #2

Open
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Open
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion partner_stage/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
from . import models
from .init_hook import post_init_hook
from .init_hook import post_init_hook, pre_init_hook
1 change: 1 addition & 0 deletions partner_stage/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"views/res_partner_views.xml",
],
"post_init_hook": "post_init_hook",
"pre_init_hook": "pre_init_hook",
"installable": True,
"maintainers": ["dreispt"],
}
18 changes: 18 additions & 0 deletions partner_stage/init_hook.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,30 @@
_logger = logging.getLogger(__name__)


def pre_init_hook(cr):
api.Environment(cr, SUPERUSER_ID, {})
query = """
ALTER TABLE res_partner
ADD COLUMN state_old VARCHAR
"""
cr.execute(query)
cr.execute("UPDATE res_partner SET state_old = state")


def post_init_hook(cr, registry):
"""Set default Stage on partners"""
env = api.Environment(cr, SUPERUSER_ID, {})
Partner = env["res.partner"]
default_stage = Partner._get_default_stage_id()
missing_stages = Partner.search([("stage_id", "=", False)])

query = """
UPDATE res_partner as prt SET stage_id = (
SELECT id FROM res_partner_stage as ps WHERE ps.state = prt.state_old
)
"""
cr.execute(query)
cr.execute("ALTER TABLE res_partner DROP COLUMN state_old")
if default_stage and missing_stages:
_logger.info("Init stage_id for %d partner records...", len(missing_stages))
cr.execute(
Expand Down