From 0a5577e4cfd880721e716481feb7a402ac09f695 Mon Sep 17 00:00:00 2001 From: "Pedro M. Baeza" Date: Tue, 27 Aug 2019 18:49:52 +0200 Subject: [PATCH] [MIG] base_location: Don't need cities for migrating Previous script required to have cities populated for working, but that's not usual thing, as people in v11 may choose to not import them, and for older databases they even weren't that option. With this improve script, now we populate city table for ZIP entries without city, so the rest of the queries are properly executed in any case. --- .../migrations/12.0.1.0.0/post-migration.py | 30 ++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/base_location/migrations/12.0.1.0.0/post-migration.py b/base_location/migrations/12.0.1.0.0/post-migration.py index aee54faf0..093fb0564 100644 --- a/base_location/migrations/12.0.1.0.0/post-migration.py +++ b/base_location/migrations/12.0.1.0.0/post-migration.py @@ -12,6 +12,33 @@ def migrate(env, version): env.cr, "ALTER TABLE res_city_zip ADD %s INTEGER", (AsIs(column_name), ), ) + # Create a city for ZIPs without it + openupgrade.logged_query( + env.cr, """ + INSERT INTO res_city ( + name, state_id, country_id, + create_uid, create_date, write_uid, write_date + ) + SELECT + city, state_id, country_id, + MIN(create_uid), MIN(create_date), MIN(write_uid), MIN(write_date) + FROM res_better_zip + WHERE city_id IS NULL + GROUP BY city, state_id, country_id + ON CONFLICT DO NOTHING""", + ) + # Update city_id in res_better_zip + openupgrade.logged_query( + env.cr, """ + UPDATE res_better_zip rbz + SET city_id = rc.id + FROM res_city rc + WHERE rc.name = rbz.city + AND rc.state_id = rbz.state_id + AND rc.country_id = rbz.country_id + AND rbz.city_id IS NULL""", + ) + # Create records for new model openupgrade.logged_query( env.cr, """ INSERT INTO res_city_zip ( @@ -20,7 +47,8 @@ def migrate(env, version): SELECT id, name, city_id FROM res_better_zip - WHERE city_id IS NOT NULL""", + WHERE city_id IS NOT NULL + ON CONFLICT DO NOTHING""", (AsIs(column_name), ), ) # Recompute display name for entries inserted by SQL