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

[17.0][OU-ADD] base_automation: migration to 17.0 #4459

Open
wants to merge 1 commit into
base: 17.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 docsource/modules160-170.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Module coverage 16.0 -> 17.0
+---------------------------------------------------+----------------------+-------------------------------------------------+
| base_address_extended | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| base_automation | | |
| base_automation | Done | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
| base_geolocalize | | |
+---------------------------------------------------+----------------------+-------------------------------------------------+
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


def _ir_act_server_update_name_if_base_automation(env):
act_servers = (
env["ir.actions.server"]
.with_context(active_test=False)
.search([("base_automation_id", "!=", False)])
)
if act_servers:
act_servers._compute_name()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is possible that certain models are not yet loaded in the migration and that an error occurs when executing this point.

2024-10-08 08:35:05,839 1 ERROR database OpenUpgrade: base_automation: error in migration script /opt/odoo/auto/addons/openupgrade_scripts/scripts/base_automation/17.0.1.0/post-migration.py: 'project.task' 
2024-10-08 08:35:05,844 1 ERROR database OpenUpgrade: 'project.task' 
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/openupgradelib/openupgrade.py", line 2298, in wrapped_function
    func(
  File "/opt/odoo/auto/addons/openupgrade_scripts/scripts/base_automation/17.0.1.0/post-migration.py", line 75, in migrate
    _ir_act_server_update_name_if_base_automation(env)
  File "/opt/odoo/auto/addons/openupgrade_scripts/scripts/base_automation/17.0.1.0/post-migration.py", line 13, in _ir_act_server_update_name_if_base_automation
    act_servers._compute_name()
  File "/opt/odoo/auto/addons/base_automation/models/ir_actions_server.py", line 47, in _compute_name
    action.name = f"{action_type} {action._stringify_path()}"
  File "/opt/odoo/custom/src/odoo/odoo/addons/base/models/ir_actions.py", line 679, in _stringify_path
    model = self.env[self.model_id.model]
  File "/opt/odoo/auto/addons/openupgrade_framework/odoo_patch/odoo/api.py", line 39, in __getitem__
    return Environment.__getitem__._original_method(self, model_name)
  File "/opt/odoo/custom/src/odoo/odoo/api.py", line 534, in __getitem__
    return self.registry[model_name](self, (), ())
  File "/opt/odoo/custom/src/odoo/odoo/modules/registry.py", line 213, in __getitem__
    return self.models[model_name]
KeyError: 'project.task'

Maybe move this part to an end-migrate.py?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it can be move to end-migrate.
Unfortunately i'm no longer doing migration anymore, at least until next year. Feel free to cherry-pick my commit and create you own if you have to



def _base_automation_update_trigger_fields_ids_if_null(env):
"""
Need to update the trigger_fields_ids if Null for 'on_create_or_write'
or else we will get weird error
Also this is base on hint from
https://github.com/odoo/odoo/pull/114352#issuecomment-1836948745
"""
base_automations = (
env["base.automation"]
.with_context(active_test=False)
.search(
[
("trigger", "=", "on_create"),
("trigger_field_ids", "=", False),
]
)
)
if base_automations:
create_date_fields = env["ir.model.fields"].search(
[
("model_id", "in", tuple(base_automations.model_id.ids)),
("name", "=", "create_date"),
]
)
for automation in base_automations:
create_date_field = create_date_fields.filtered(
lambda field, automation=automation: field.model_id
== automation.model_id
)[:1]
if create_date_field:
automation.trigger_field_ids = [(4, create_date_field.id)]


def _ir_ui_view_remove_inherit_id_from_automation_form(env):
"""
Somehow this inherit_id from ir.actions.server of 'view_base_automation_form'
won't disappear so we need to remove it from here
"""
view = env.ref(
"base_automation.view_base_automation_form", raise_if_not_found=False
)
if view and view.inherit_id:
view.inherit_id = False


def _base_automation_update_deprecate_trigger(env):
openupgrade.logged_query(
env.cr,
"""
UPDATE base_automation
SET trigger = 'on_create_or_write'
WHERE trigger IN ('on_create', 'on_write')
""",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env, "base_automation", "17.0.1.0/noupdate_changes.xml")
_ir_act_server_update_name_if_base_automation(env)
_base_automation_update_trigger_fields_ids_if_null(env)
# Need to update deprecated trigger after add trigger field for 'on_create' trigger
_base_automation_update_deprecate_trigger(env)
_ir_ui_view_remove_inherit_id_from_automation_form(env)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Copyright 2024 Viindoo Technology Joint Stock Company (Viindoo)
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
from openupgradelib import openupgrade


def _ir_act_server_update_base_automation_id(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE ir_act_server
ADD COLUMN IF NOT EXISTS base_automation_id INTEGER;
""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE ir_act_server ias
SET base_automation_id = ba.id
FROM base_automation ba
WHERE ba.action_server_id = ias.id
""",
)


def _base_automation_sync_from_ir_act_server(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE base_automation
ADD COLUMN IF NOT EXISTS model_id INTEGER,
ADD COLUMN IF NOT EXISTS name JSONB;
""",
)
openupgrade.logged_query(
env.cr,
"""
UPDATE base_automation ba
SET model_id = ias.model_id,
name = ias.name
FROM ir_act_server ias
WHERE ba.action_server_id = ias.id
""",
)


@openupgrade.migrate()
def migrate(env, version):
_ir_act_server_update_base_automation_id(env)
_base_automation_sync_from_ir_act_server(env)
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---Models in module 'base_automation'---
---Fields in module 'base_automation'---
base_automation / base.automation / _order : _order is now 'id' ('sequence')
base_automation / base.automation / description (html) : NEW
base_automation / base.automation / log_webhook_calls (boolean) : NEW hasdefault: default
base_automation / base.automation / record_getter (char) : NEW hasdefault: default
base_automation / base.automation / webhook_uuid (char) : NEW
# NOTHING TO DO

base_automation / base.automation / _inherits : DEL _inherits: {'ir.actions.server': 'action_server_id'}
base_automation / base.automation / action_server_id (many2one) : DEL relation: ir.actions.server, required
base_automation / base.automation / action_server_ids (one2many) : NEW relation: ir.actions.server, hasdefault: compute
base_automation / ir.actions.server / base_automation_id (many2one) : NEW relation: base.automation
# DONE pre-migration: convert m2o to o2m by filling base_automation_id for ir.action.servers


base_automation / base.automation / model_id (many2one) : is now stored
base_automation / base.automation / model_id (many2one) : not related anymore
base_automation / base.automation / name (char) : is now stored
base_automation / base.automation / name (char) : not related anymore
# DONE pre-migration: create column and fill value

base_automation / base.automation / trg_field_ref (many2one_reference): NEW relation: trg_field_ref_model_name, hasdefault: compute
base_automation / base.automation / trg_selection_field_id (many2one): NEW relation: ir.model.fields.selection, hasdefault: compute
# NOTHING TO DO: new feature

base_automation / base.automation / trigger (selection) : selection_keys is now '['on_archive', 'on_change', 'on_create', 'on_create_or_write', 'on_message_received', 'on_message_sent', 'on_priority_set', 'on_stage_set', 'on_state_set', 'on_tag_set', 'on_time', 'on_time_created', 'on_time_updated', 'on_unarchive', 'on_unlink', 'on_user_set', 'on_webhook', 'on_write']' ('['on_change', 'on_create', 'on_create_or_write', 'on_time', 'on_unlink', 'on_write']')
# DONE post-migration: set 'on_create' and 'on_write' by 'on_create_or_write'

base_automation / ir.actions.server / name (False) : NEW mode: modify, hasdefault: compute
# DONE post-migration: update name for action that is related to automation using ORM to support translation

---XML records in module 'base_automation'---
NEW ir.ui.view: base_automation.ir_actions_server_view_form_automation
NEW ir.ui.view: base_automation.view_base_automation_kanban
# NOTHING TO DO
Loading