Skip to content

Commit

Permalink
[OU-FIX+IMP] website_slides: Complete migration scripts
Browse files Browse the repository at this point in the history
- Better explanations on work file
- Rework slide_type > slide_category conversion
- Shorten lines
- Fill source_type properly
- Delete record translations
- Handle nbr_article field

TT45249
  • Loading branch information
pedrobaeza committed Mar 11, 2024
1 parent a3d5acb commit 58038df
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,65 @@
# Copyright 2023 Viindoo - Nguyễn Việt Lâm
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade


def _fill_source_type_external(env):
"""Switch to external for those with `google_drive_id`, that are the only where
these applies:
https://github.com/odoo/odoo/blob/42bcc3ef284ca355e2323641176573b53e7d2e28/addons/
website_slides/controllers/main.py#L1197
"""
env["slide.slide"].search([]).filtered("google_drive_id").source_type = "external"


def _fill_nbr_article(env):
"""Fill the values recreating the compute method, but only for the field
nbr_article.
"""
for source in ["slide", "channel"]:
domain = [("is_published", "=", True), ("is_category", "=", False)]
if source == "slide":
records = env["slide.slide"].search([("is_category", "=", True)])
domain.append(("category_id", "in", records.ids))
else:
records = env["slide.channel"].search([])
domain.append(("channel_id", "in", records.ids))
res = env["slide.slide"]._read_group(
domain,
["category_id", "slide_category"],
["category_id", "slide_category"],
lazy=False,
)
category_stats = records._compute_slides_statistics_category(res)
for record in records:
record.nbr_article = category_stats.get(
record._origin.id, {"nb_article": 0}
)["nbr_article"]


@openupgrade.migrate()
def migrate(env, version):
openupgrade.load_data(env.cr, "website_slides", "16.0.2.6/noupdate_changes.xml")
openupgrade.delete_record_translations(
env.cr,
"website_slides",
[
"mail_template_channel_completed",
"mail_template_slide_channel_invite",
],
["name", "subject", "description"],
)
openupgrade.delete_record_translations(
env.cr,
"website_slides",
["mail_template_slide_channel_invite"],
["name", "description"],
)
openupgrade.delete_record_translations(
env.cr,
"website_slides",
["slide_template_published", "slide_template_shared"],
)
_fill_source_type_external(env)
_fill_nbr_article(env)
Original file line number Diff line number Diff line change
@@ -1,51 +1,39 @@
# Copyright 2023 Viindoo - Nguyễn Việt Lâm
# Copyright 2024 Tecnativa - Pedro M. Baeza
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).
from openupgradelib import openupgrade

renamed_fields = [
("slide.slide", "slide_slide", "datas", "binary_content"),
("slide.channel", "slide_channel", "share_template_id", "share_slide_template_id"),
]

xml_ids_to_rename = [
(
"website_slides.rule_slide_slide_resource_manager",
"website_slides.rule_slide_slide_resource_downloadable_manager",
),
]

# slide_slide


def create_and_fill_data_from_slide_type_to_slide_category(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE slide_slide
ADD COLUMN IF NOT EXISTS slide_category VARCHAR
""",
def _create_and_fill_data_from_slide_type_to_slide_category(env):
openupgrade.copy_columns(
env.cr, {"slide_slide": [("slide_type", "slide_category", None)]}
)

openupgrade.logged_query(
openupgrade.map_values(
env.cr,
"""
UPDATE slide_slide
SET slide_category = CASE
WHEN slide_type = 'webpage' THEN 'article'
WHEN slide_type = 'presentation' THEN 'document'
ELSE slide_type
END
""",
"slide_type",
"slide_category",
[("webpage", "article"), ("presentation", "document")],
table="slide_slide",
)
openupgrade.rename_columns(env.cr, {"slide_slide": [("slide_type", None)]})


def create_and_fill_data_for_source_type(env):
def _create_and_fill_data_for_source_type(env):
openupgrade.logged_query(
env.cr,
"""
ALTER TABLE slide_slide
ADD COLUMN IF NOT EXISTS source_type VARCHAR
""",
"ALTER TABLE slide_slide ADD COLUMN IF NOT EXISTS source_type VARCHAR",
)

openupgrade.logged_query(
env.cr,
"""
Expand All @@ -58,10 +46,7 @@ def create_and_fill_data_for_source_type(env):
)


# slide_slide_resource


def create_column_and_migrate_data_from_slide_link_to_slide_resource(env):
def _create_column_and_migrate_data_from_slide_link_to_slide_resource(env):
openupgrade.logged_query(
env.cr,
"""
Expand All @@ -70,38 +55,44 @@ def create_column_and_migrate_data_from_slide_link_to_slide_resource(env):
ADD COLUMN IF NOT EXISTS resource_type VARCHAR
""",
)

openupgrade.logged_query(
env.cr,
"""
UPDATE slide_slide_resource
SET resource_type = 'file'
""",
"UPDATE slide_slide_resource SET resource_type = 'file'",
)

openupgrade.logged_query(
env.cr,
"""
INSERT INTO slide_slide_resource (
name,
link,
slide_id,
resource_type
)
SELECT
name,
link,
slide_id,
'url'
INSERT INTO slide_slide_resource
(name, link, slide_id, resource_type)
SELECT name, link, slide_id, 'url'
FROM slide_slide_link
""",
)


def _create_nbr_article(env):
"""Pre-create and fill these fields for avoiding KeyError crashes as the compute
method uses read_group.
"""
openupgrade.logged_query(
env.cr,
"ALTER TABLE slide_channel ADD COLUMN IF NOT EXISTS nbr_article INT4",
)
openupgrade.logged_query(
env.cr,
"ALTER TABLE slide_slide ADD COLUMN IF NOT EXISTS nbr_article INT4 DEFAULT 0",
)
openupgrade.logged_query(
env.cr,
"ALTER TABLE slide_slide ALTER COLUMN nbr_article DROP DEFAULT",
)


@openupgrade.migrate()
def migrate(env, version):
openupgrade.rename_fields(env, renamed_fields)
openupgrade.rename_xmlids(env.cr, xml_ids_to_rename)
create_and_fill_data_from_slide_type_to_slide_category(env)
create_column_and_migrate_data_from_slide_link_to_slide_resource(env)
create_and_fill_data_for_source_type(env)
_create_and_fill_data_from_slide_type_to_slide_category(env)
_create_column_and_migrate_data_from_slide_link_to_slide_resource(env)
_create_and_fill_data_for_source_type(env)
_create_nbr_article(env)
Original file line number Diff line number Diff line change
@@ -1,65 +1,61 @@
---Models in module 'website_slides'---
obsolete model slide.slide.link

website_slides / slide.slide.link / link (char) : DEL required
website_slides / slide.slide.link / name (char) : DEL required
website_slides / slide.slide.link / slide_id (many2one) : DEL relation: slide.slide, required
# DONE: move data from slide.slide.link to slide.slide.resource in pre-migration
# REASON: previously website_slides seperate slide.slide.link and slide.slide.resource one is store url another store file. Now combined into slide.slide.resource

---Fields in module 'website_slides'---
website_slides / slide.channel / cover_properties (text) : NEW hasdefault: default
# NOTHING TO DO: New feature to customize the cover of the course.

website_slides / slide.channel / nbr_article (integer) : NEW isfunction: function, stored
website_slides / slide.slide / nbr_article (integer) : NEW isfunction: function, stored
# DONE: pre-migration: Pre-create and fill the field for avoiding KeyError in the compute as it uses `read_group` method.
# DONE: post-migration: Fill the field using ORM recreating the compute method, as the computation by SQL gets hard and it doesn't worth due to the scope.

website_slides / slide.channel / nbr_presentation (integer) : DEL
website_slides / slide.channel / nbr_webpage (integer) : DEL
website_slides / slide.channel / share_channel_template_id (many2one): NEW relation: mail.template, hasdefault: default

# NOTHING TO DO: executed by ORM

website_slides / slide.channel / share_channel_template_id (many2one): NEW relation: mail.template, hasdefault: default
# NOTHING TO DO: This value is needed for sharing the channel, so better to have one filled, and the default one given by ORM is OK.

website_slides / slide.channel / share_slide_template_id (many2one): NEW relation: mail.template, hasdefault: default
website_slides / slide.channel / share_template_id (many2one) : DEL relation: mail.template
website_slides / slide.slide / binary_content (binary) : NEW attachment: True
website_slides / slide.slide / datas (binary) : DEL attachment: True

# DONE: renamed fields in pre-migration
# DONE: pre-migration: renamed fields

website_slides / slide.slide / document_id (char) : DEL

# NOTHING TO DO
# NOTHING TO DO: Following the logic where this field was involved, now it is computed on the fly (stored=False) from URL.

website_slides / slide.slide / embed_ids (one2many) : NEW relation: slide.embed
website_slides / slide.slide / embedcount_ids (one2many) : DEL relation: slide.embed

# NOTHING TO DO: rename from embedcount_ids to embed_ids still one2many type

website_slides / slide.slide / link_ids (one2many) : DEL relation: slide.slide.link
website_slides / slide.slide / mime_type (char) : DEL
website_slides / slide.slide / nbr_article (integer) : NEW isfunction: function, stored
website_slides / slide.slide / nbr_presentation (integer) : DEL
website_slides / slide.slide / nbr_webpage (integer) : DEL

# NOTHING TO DO

website_slides / slide.slide / slide_category (selection) : NEW required, selection_keys: ['article', 'document', 'infographic', 'quiz', 'video'], hasdefault: default

# DONE: move value from old slide_type to slide_category in pre-migration
# DONE: pre-migration: copy column slide_type + map values that changes

website_slides / slide.slide / slide_type (selection) : selection_keys is now '['article', 'doc', 'google_drive_video', 'image', 'pdf', 'quiz', 'sheet', 'slides', 'vimeo_video', 'youtube_video']' ('['document', 'infographic', 'presentation', 'quiz', 'video', 'webpage']')

# NOTHING TO DO: let ORM handle this section because this logic based on field video_source_type and this field computed by external library
# NOTHING TO DO: Let ORM handle recomputes this field

website_slides / slide.slide / source_type (selection) : NEW required, selection_keys: ['external', 'local_file'], hasdefault: default
# DONE: post-migration: Switch to external for those with `google_drive_id`, that are the only where these applies: https://github.com/odoo/odoo/blob/42bcc3ef284ca355e2323641176573b53e7d2e28/addons/website_slides/controllers/main.py#L1197

# DONE: create column and fill data in pre-migration

website_slides / slide.slide.link / link (char) : DEL required
website_slides / slide.slide.link / name (char) : DEL required
website_slides / slide.slide.link / slide_id (many2one) : DEL relation: slide.slide, required
website_slides / slide.slide.resource / file_name (char) : NEW

# NOTHING TO DO

website_slides / slide.slide.resource / link (char) : NEW hasdefault: compute
website_slides / slide.slide.resource / resource_type (selection) : NEW required, selection_keys: ['file', 'url']

# DONE: create column and fill data in pre-migration
# DONE: pre-migration: create column and fill data

---XML records in module 'website_slides'---
NEW ir.actions.act_window: website_slides.action_slide_channel_pages_list
Expand All @@ -83,12 +79,10 @@ NEW ir.actions.act_window.view: website_slides.slide_slide_action_report_view_tr
DEL ir.actions.act_window.view: website_slides.rating_rating_action_slide_channel_report_view_graph
DEL ir.actions.act_window.view: website_slides.rating_rating_action_slide_channel_report_view_pivot
DEL ir.actions.act_window.view: website_slides.rating_rating_action_slide_channel_report_view_tree

# NOTHING TO DO

DEL ir.model.access: website_slides.access_slide_slide_link_all
DEL ir.model.access: website_slides.access_slide_slide_link_officer

# NOTHING TO DO

NEW ir.model.constraint: website_slides.constraint_slide_channel_partner_channel_partner_uniq
Expand All @@ -97,13 +91,11 @@ NEW ir.model.constraint: website_slides.constraint_slide_slide_partner_check_vot
NEW ir.model.constraint: website_slides.constraint_slide_slide_partner_slide_partner_uniq
NEW ir.model.constraint: website_slides.constraint_slide_slide_resource_check_file_type
NEW ir.model.constraint: website_slides.constraint_slide_slide_resource_check_url

# NOTHING TO DO


NEW ir.rule: website_slides.rule_slide_slide_resource_downloadable_manager (noupdate)
DEL ir.rule: website_slides.rule_slide_slide_resource_manager (noupdate)

# DONE: rename xmlid in pre-migration

NEW ir.ui.menu: website_slides.menu_slide_channel_pages
Expand All @@ -127,5 +119,4 @@ DEL ir.ui.view: website_slides.rating_rating_view_kanban_slide_channel
DEL ir.ui.view: website_slides.slide_edit_options
DEL ir.ui.view: website_slides.user_navbar_inherit_website_slides
NEW mail.template: website_slides.mail_template_channel_shared (noupdate)

# NOTHING TO DO

0 comments on commit 58038df

Please sign in to comment.