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.

35 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. "ALTER TABLE res_city_zip ADD %s INTEGER", (AsIs(column_name), ),
  10. )
  11. openupgrade.logged_query(
  12. env.cr, """
  13. INSERT INTO res_city_zip (
  14. %s, name, city_id
  15. )
  16. SELECT
  17. id, name, city_id
  18. FROM res_better_zip
  19. WHERE city_id IS NOT NULL""",
  20. (AsIs(column_name), ),
  21. )
  22. # Recompute display name for entries inserted by SQL
  23. env['res.city.zip'].search([])._compute_new_display_name()
  24. # Link res.partner with corresponding new entries
  25. openupgrade.logged_query(
  26. env.cr, """
  27. UPDATE res_partner rp
  28. SET zip_id = rcz.id
  29. FROM res_city_zip rcz
  30. WHERE rcz.%s = rp.%s""",
  31. (AsIs(column_name), AsIs(openupgrade.get_legacy_name('zip_id')), ),
  32. )