|
| 1 | +# Part of OpenSPP. See LICENSE file for full copyright and licensing details. |
| 2 | +"""Migrate legacy boolean disaggregation flags to dimension_ids. |
| 3 | +
|
| 4 | +Replaces the post_init_hook version, which only ran on fresh installs (where |
| 5 | +there is nothing to migrate) and hardcoded the m2m relation table name. As a |
| 6 | +post-migration script this runs on upgrade, and the relation/column names are |
| 7 | +derived from the field so they cannot drift from Odoo's canonical naming. |
| 8 | +""" |
| 9 | + |
| 10 | +import logging |
| 11 | + |
| 12 | +from psycopg2 import sql |
| 13 | + |
| 14 | +from odoo import SUPERUSER_ID, api |
| 15 | + |
| 16 | +_logger = logging.getLogger(__name__) |
| 17 | + |
| 18 | +# Mapping: old boolean field name -> dimension technical name |
| 19 | +_BOOL_TO_DIMENSION = { |
| 20 | + "disaggregate_by_gender": "gender", |
| 21 | + "disaggregate_by_age": "age_group", |
| 22 | + "disaggregate_by_disability": "disability_status", |
| 23 | +} |
| 24 | + |
| 25 | + |
| 26 | +def migrate(cr, version): |
| 27 | + """Link reports that had legacy boolean flags set to the matching dimensions. |
| 28 | +
|
| 29 | + ``version`` is the previously-installed module version, or a falsy value on |
| 30 | + a fresh install (nothing to migrate). Logs a warning (does not crash) if a |
| 31 | + dimension record is not found. |
| 32 | + """ |
| 33 | + if not version: |
| 34 | + # Fresh install: the legacy boolean columns never existed. |
| 35 | + return |
| 36 | + |
| 37 | + # Only columns that still physically exist in the table can be read. After |
| 38 | + # removing the boolean fields, Odoo leaves the obsolete columns in place, so |
| 39 | + # they are still readable here on the first upgrade. |
| 40 | + cr.execute( |
| 41 | + """ |
| 42 | + SELECT column_name FROM information_schema.columns |
| 43 | + WHERE table_name = 'spp_gis_report' |
| 44 | + AND column_name IN ('disaggregate_by_gender', 'disaggregate_by_age', 'disaggregate_by_disability') |
| 45 | + """ |
| 46 | + ) |
| 47 | + existing_columns = {row[0] for row in cr.fetchall()} |
| 48 | + if not existing_columns: |
| 49 | + _logger.info("No legacy boolean disaggregation columns found, skipping migration") |
| 50 | + return |
| 51 | + |
| 52 | + env = api.Environment(cr, SUPERUSER_ID, {}) |
| 53 | + Dimension = env["spp.demographic.dimension"] |
| 54 | + |
| 55 | + # Derive the m2m relation/column names from the field so this migration |
| 56 | + # cannot drift from Odoo's canonical (alphabetically sorted) table naming. |
| 57 | + m2m = env["spp.gis.report"]._fields["dimension_ids"] |
| 58 | + insert_stmt = sql.SQL("INSERT INTO {rel} ({col_report}, {col_dim}) VALUES (%s, %s) ON CONFLICT DO NOTHING").format( |
| 59 | + rel=sql.Identifier(m2m.relation), |
| 60 | + col_report=sql.Identifier(m2m.column1), |
| 61 | + col_dim=sql.Identifier(m2m.column2), |
| 62 | + ) |
| 63 | + |
| 64 | + for bool_field, dim_name in _BOOL_TO_DIMENSION.items(): |
| 65 | + if bool_field not in existing_columns: |
| 66 | + continue |
| 67 | + |
| 68 | + # bool_field is a trusted column name from _BOOL_TO_DIMENSION; quote it |
| 69 | + # safely via psycopg2.sql. |
| 70 | + query = sql.SQL("SELECT id FROM spp_gis_report WHERE {col} = true").format(col=sql.Identifier(bool_field)) |
| 71 | + cr.execute(query) |
| 72 | + report_ids = [row[0] for row in cr.fetchall()] |
| 73 | + if not report_ids: |
| 74 | + continue |
| 75 | + |
| 76 | + dimension = Dimension.search([("name", "=", dim_name)], limit=1) |
| 77 | + if not dimension: |
| 78 | + _logger.warning( |
| 79 | + "Dimension '%s' not found, cannot migrate %d reports with %s=True", |
| 80 | + dim_name, |
| 81 | + len(report_ids), |
| 82 | + bool_field, |
| 83 | + ) |
| 84 | + continue |
| 85 | + |
| 86 | + for report_id in report_ids: |
| 87 | + cr.execute(insert_stmt, (report_id, dimension.id)) |
| 88 | + |
| 89 | + _logger.info( |
| 90 | + "Migrated %d reports from %s=True to dimension '%s'", |
| 91 | + len(report_ids), |
| 92 | + bool_field, |
| 93 | + dim_name, |
| 94 | + ) |
0 commit comments