You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

36 lines
1.1 KiB

  1. # Copyright 2018 Tecnativa - Pedro M. Baeza
  2. # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html).
  3. from psycopg2.extensions import AsIs
  4. from openupgradelib import openupgrade
  5. @openupgrade.migrate()
  6. def migrate(env, version):
  7. column_name = openupgrade.get_legacy_name('better_zip_id')
  8. openupgrade.logged_query(
  9. env.cr,
  10. "ALTER TABLE res_city_zip ADD %s INTEGER", (AsIs(column_name), ),
  11. )
  12. openupgrade.logged_query(
  13. env.cr, """
  14. INSERT INTO res_city_zip (
  15. %s, name, city_id
  16. )
  17. SELECT
  18. id, name, city_id
  19. FROM res_better_zip
  20. WHERE city_id IS NOT NULL""",
  21. (AsIs(column_name), ),
  22. )
  23. # Recompute display name for entries inserted by SQL
  24. env['res.city.zip'].search([])._compute_new_display_name()
  25. # Link res.partner with corresponding new entries
  26. openupgrade.logged_query(
  27. env.cr, """
  28. UPDATE res_partner rp
  29. SET zip_id = rcz.id
  30. FROM res_city_zip rcz
  31. WHERE rcz.%s = rp.%s""",
  32. (AsIs(column_name), AsIs(openupgrade.get_legacy_name('zip_id')), ),
  33. )