From 557cdfd910841482207af7feec3b844f8e192719 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Thu, 15 Jul 2021 13:35:38 +0200 Subject: [PATCH] [FIX] project: Complete migration scripts - Set default if no value in rating_status_period - Change noupdate records deletion order - Compute allowed users properly and in an optimal way --- .../project/14.0.1.1/post-migration.py | 98 ++++++------------- .../scripts/project/14.0.1.1/pre-migration.py | 22 +++++ .../14.0.1.1/upgrade_analysis_work.txt | 16 +-- 3 files changed, 63 insertions(+), 73 deletions(-) diff --git a/openupgrade_scripts/scripts/project/14.0.1.1/post-migration.py b/openupgrade_scripts/scripts/project/14.0.1.1/post-migration.py index bc5cc995387d..178fdd47d3ff 100644 --- a/openupgrade_scripts/scripts/project/14.0.1.1/post-migration.py +++ b/openupgrade_scripts/scripts/project/14.0.1.1/post-migration.py @@ -1,4 +1,5 @@ # Copyright 2021 ForgeFlow S.L. +# Copyright 2021 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade @@ -13,98 +14,59 @@ def map_project_project_rating_status(env): ) -def correct_fill_project_res_users_m2m_tables(env): - # delete wrong defaults +def _fill_res_users_m2m_tables(env): + # TODO: Take into account channels and task followers part of the old rule openupgrade.logged_query( env.cr, """ - DELETE FROM project_allowed_internal_users_rel; - DELETE FROM project_allowed_portal_users_rel""", - ) - openupgrade.logged_query( - env.cr, - """ - INSERT INTO project_allowed_internal_users_rel ( - project_project_id, res_users_id) - SELECT pp.id, pp.create_uid - FROM project_project pp - JOIN res_users ru ON pp.create_uid = ru.id - WHERE NOT ru.share""", - ) - openupgrade.logged_query( - env.cr, - """ - INSERT INTO project_allowed_internal_users_rel ( - project_project_id, res_users_id) + INSERT INTO project_allowed_internal_users_rel + (project_project_id, res_users_id) SELECT pp.id, ru.id FROM project_project pp - JOIN res_users ru ON pp.partner_id = ru.partner_id - WHERE pp.privacy_visibility = 'portal' AND NOT ru.share - ON CONFLICT DO NOTHING""", + JOIN mail_followers mf ON mf.res_model = 'project.project' AND mf.res_id = pp.id + JOIN res_users ru ON ru.partner_id = mf.partner_id AND NOT ru.share + WHERE pp.privacy_visibility = 'followers' + """, ) openupgrade.logged_query( env.cr, """ - INSERT INTO project_allowed_portal_users_rel ( - project_project_id, res_users_id) + INSERT INTO project_allowed_portal_users_rel + (project_project_id, res_users_id) SELECT pp.id, ru.id FROM project_project pp - JOIN res_users ru ON pp.partner_id = ru.partner_id - WHERE pp.privacy_visibility = 'portal' AND ru.share""", - ) - projects = env["project.project"].search([]) - for project in projects: - project.allowed_user_ids |= project.message_follower_ids.partner_id.user_ids - - -def correct_fill_task_res_users_m2m_tables(env): - openupgrade.logged_query( - env.cr, - """DELETE FROM project_task_res_users_rel""", + JOIN mail_followers mf ON mf.res_model = 'project.project' AND mf.res_id = pp.id + JOIN res_partner rp ON mf.partner_id = rp.id + OR mf.partner_id = rp.commercial_partner_id + JOIN res_users ru ON ru.partner_id = rp.id AND ru.share + WHERE pp.privacy_visibility = 'portal' + """, ) openupgrade.logged_query( env.cr, """ - INSERT INTO project_task_res_users_rel ( - project_task_id, res_users_id) - SELECT pt.id, ru.id - FROM project_task pt - JOIN project_project pp ON pt.project_id = pp.id - JOIN project_allowed_internal_users_rel rel - ON rel.project_project_id = pp.id - JOIN res_users ru ON rel.res_users_id = ru.id - WHERE pp.privacy_visibility = 'followers' AND NOT ru.share""", + INSERT INTO project_task_res_users_rel + (project_task_id, res_users_id) + SELECT pt.id, rel.res_users_id + FROM project_allowed_internal_users_rel rel + JOIN project_task pt ON pt.project_id = rel.project_project_id + UNION + SELECT pt.id, rel.res_users_id + FROM project_allowed_portal_users_rel rel + JOIN project_task pt ON pt.project_id = rel.project_project_id + """, ) - openupgrade.logged_query( - env.cr, - """ - INSERT INTO project_task_res_users_rel ( - project_task_id, res_users_id) - SELECT pt.id, ru.id - FROM project_task pt - JOIN project_project pp ON pt.project_id = pp.id - JOIN project_allowed_portal_users_rel rel - ON rel.project_project_id = pp.id - JOIN res_users ru ON rel.res_users_id = ru.id - WHERE pp.privacy_visibility = 'portal' AND ru.share""", - ) - tasks = env["project.task"].search([]) - for task in tasks: - task.allowed_user_ids |= task.message_follower_ids.partner_id.user_ids @openupgrade.migrate() def migrate(env, version): map_project_project_rating_status(env) - correct_fill_project_res_users_m2m_tables(env) - correct_fill_task_res_users_m2m_tables(env) + _fill_res_users_m2m_tables(env) openupgrade.load_data(env.cr, "project", "14.0.1.1/noupdate_changes.xml") openupgrade.delete_records_safely_by_xml_id( env, [ "project.msg_task_4", - "project.project_project_data", - "project.project_tag_data", "project.project_task_data_0", "project.project_task_data_1", "project.project_task_data_11", @@ -120,6 +82,10 @@ def migrate(env, version): "project.project_stage_data_0", "project.project_stage_data_1", "project.project_stage_data_2", + # We do this at the end for assuring not having this records assigned on the + # rest of the demo data + "project.project_project_data", + "project.project_tag_data", ], ) openupgrade.delete_record_translations( diff --git a/openupgrade_scripts/scripts/project/14.0.1.1/pre-migration.py b/openupgrade_scripts/scripts/project/14.0.1.1/pre-migration.py index d015d35eaba2..f822ca50ad32 100644 --- a/openupgrade_scripts/scripts/project/14.0.1.1/pre-migration.py +++ b/openupgrade_scripts/scripts/project/14.0.1.1/pre-migration.py @@ -1,8 +1,17 @@ # Copyright 2021 ForgeFlow S.L. +# Copyright 2021 Tecnativa - Pedro M. Baeza # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade +def _set_default_rating_status_period(env): + openupgrade.logged_query( + env.cr, + "UPDATE project_project SET rating_status_period = 'monthly' WHERE " + "rating_status_period IS NULL", + ) + + @openupgrade.migrate() def migrate(env, version): openupgrade.copy_columns( @@ -30,3 +39,16 @@ def migrate(env, version): ("project.access_partner_task user", "project.access_partner_task_user"), ], ) + _set_default_rating_status_period(env) + # Manually create tables for avoiding the automatic launch of the compute or default + # FK constraints and indexes will be added by ORM + openupgrade.logged_query( + env.cr, + """CREATE TABLE project_allowed_internal_users_rel + (project_project_id INTEGER, res_users_id INTEGER)""", + ) + openupgrade.logged_query( + env.cr, + """CREATE TABLE project_task_res_users_rel + (project_task_id INTEGER, res_users_id INTEGER)""", + ) diff --git a/openupgrade_scripts/scripts/project/14.0.1.1/upgrade_analysis_work.txt b/openupgrade_scripts/scripts/project/14.0.1.1/upgrade_analysis_work.txt index aef470b8c72c..c25e7ce42857 100644 --- a/openupgrade_scripts/scripts/project/14.0.1.1/upgrade_analysis_work.txt +++ b/openupgrade_scripts/scripts/project/14.0.1.1/upgrade_analysis_work.txt @@ -10,30 +10,32 @@ project / project.project / allow_subtasks (boolean) : NEW ha # NOTHING TO DO: new features project / project.project / description (html) : NEW +# NOTHING TO DO: New feature + project / project.project / partner_email (char) : NEW isfunction: function, stored project / project.project / partner_phone (char) : NEW isfunction: function, stored -# NOTHING TO DO +# NOTHING TO DO: Stored by ORM project / project.project / portal_show_rating (boolean) : DEL project / project.project / rating_active (boolean) : NEW hasdefault # DONE: pre-migration: renamed field project / project.project / rating_status (selection) : selection_keys is now '['periodic', 'stage']' ('['no', 'periodic', 'stage']') +# DONE: pre-migration: copy column for preserving data # DONE: post-migration: map 'no' to 'stage' project / project.project / rating_status_period (selection): now required, req_default: function -# NOTHING TO DO +# DONE: pre-migration: Apply default if no value project / project.project / resource_calendar_id (many2one): not stored anymore project / project.project / resource_calendar_id (many2one): now related -# NOTHING TO DO +# NOTHING TO DO: There's the possibility of having another calendar in v13, but as this was only used for computing number of working days and hours till closed, it's OK to ignore it project / project.project / allowed_internal_user_ids (many2many): NEW relation: res.users, hasdefault project / project.project / allowed_portal_user_ids (many2many): NEW relation: res.users -# DONE: post-migration: computed correctly - project / project.task / allowed_user_ids (many2many) : NEW relation: res.users, isfunction: function, stored -# DONE: post-migration: computed correctly +# DONE: pre-migration: Pre-create tables for not launching the default + computed +# DONE: post-migration: fill these tables from followers project / project.task / company_id (many2one) : now a function project / project.task / email_from (char) : now a function @@ -70,7 +72,7 @@ project / project.task.recurrence / task_ids (one2many) : NEW re project / project.task.recurrence / thu (boolean) : NEW project / project.task.recurrence / tue (boolean) : NEW project / project.task.recurrence / wed (boolean) : NEW -# NOTHING TO DO: new method, empty when migrating +# NOTHING TO DO: new model, empty when migrating project / project.task.type / active (boolean) : NEW hasdefault # NOTHING TO DO: has default true