From a65d315cda204de6a1d619e1c45b6c5544d1299c Mon Sep 17 00:00:00 2001 From: git_admin Date: Mon, 27 Apr 2026 08:14:12 +0000 Subject: [PATCH] Tower: upload cetmix_tower_git 16.0.2.0.4 (via marketplace) --- .../migrations/16.0.2.0.0/post-migration.py | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 addons/cetmix_tower_git/migrations/16.0.2.0.0/post-migration.py diff --git a/addons/cetmix_tower_git/migrations/16.0.2.0.0/post-migration.py b/addons/cetmix_tower_git/migrations/16.0.2.0.0/post-migration.py new file mode 100644 index 0000000..d5ead46 --- /dev/null +++ b/addons/cetmix_tower_git/migrations/16.0.2.0.0/post-migration.py @@ -0,0 +1,82 @@ +import logging + +from odoo import SUPERUSER_ID, api + +_logger = logging.getLogger(__name__) + + +def migrate(cr, version): + """ + Convert URLs in remotes to repositories. + Add repo_id to remotes. + """ + + _logger.info( + "Converting URLs in remotes to repositories and adding repo_id to remotes." + ) + env = api.Environment(cr, SUPERUSER_ID, {}) + + # Fetch all remotes using SQL query Group them {"url": [remote_id, remote_id, ...]} + cr.execute( + """ + SELECT url, array_agg(id) as remote_ids + FROM cx_tower_git_remote + GROUP BY url + """ + ) + remote_urls = cr.fetchall() + remote_urls_dict = {url: remote_ids for url, remote_ids in remote_urls} + + # Create repo for each url and add this repo to all remotes + url_count = 0 + remote_obj = env["cx.tower.git.remote"] + repo_obj = env["cx.tower.git.repo"] + for url, remote_ids in remote_urls_dict.items(): + repo_id = repo_obj.name_create(url)[0] + # Check if any of the remotes is private + remotes = remote_obj.browse(remote_ids) + is_private = bool(remotes.filtered(lambda r: r.is_private)) + + # Add repo to remotes + # We are using SQL to avoid post-write triggers + cr.execute( + """ + UPDATE cx_tower_git_remote + SET repo_id = %s + WHERE id = ANY(%s) + """, + (repo_id, remote_ids), + ) + + # Update repo.is_private + # We are using SQL to avoid post-write triggers + if is_private: + cr.execute( + """ + UPDATE cx_tower_git_repo + SET is_private = true + WHERE id = %s + """, + (repo_id,), + ) + + url_count += 1 + + # Compute project_ids for repositories + _logger.info("Computing project_ids for repositories.") + remote_obj.invalidate_model() + repo_obj.invalidate_model() + repo_obj.search([])._compute_git_project_ids() + + # Sanitize all remote heads that contain a slash + # Use the SQL query to avoid post-write triggers + _logger.info("Sanitizing remote heads that contain a slash.") + cr.execute( + """ + UPDATE cx_tower_git_remote + SET head = (regexp_match(head, '[^/]+$'))[1] + WHERE head LIKE '%/%' + """ + ) + + _logger.info("Migration completed. %s unique urls processed", url_count)