| 1 |
# -*- coding: utf-8 -*- |
| 2 |
|
| 3 |
def convert_field(cr, model, field, target_model): |
| 4 |
table = model.replace('.', '_') |
| 5 |
|
| 6 |
cr.execute("""SELECT 1 |
| 7 |
FROM information_schema.columns |
| 8 |
WHERE table_name = %s |
| 9 |
AND column_name = %s |
| 10 |
AND table_schema = current_schema |
| 11 |
""", (table, field)) |
| 12 |
if not cr.fetchone(): |
| 13 |
return |
| 14 |
|
| 15 |
cr.execute("SELECT id FROM ir_model_fields WHERE model=%s AND name=%s", (model, field)) |
| 16 |
[fields_id] = cr.fetchone() |
| 17 |
|
| 18 |
cr.execute(""" |
| 19 |
INSERT INTO ir_property(name, type, fields_id, company_id, res_id, value_reference) |
| 20 |
SELECT %(field)s, 'many2one', %(fields_id)s, company_id, CONCAT('{model},', id), |
| 21 |
CONCAT('{target_model},', {field}) |
| 22 |
FROM {table} t |
| 23 |
WHERE {field} IS NOT NULL |
| 24 |
AND NOT EXISTS(SELECT 1 |
| 25 |
FROM ir_property |
| 26 |
WHERE fields_id=%(fields_id)s |
| 27 |
AND company_id=t.company_id |
| 28 |
AND res_id=CONCAT('{model},', t.id)) |
| 29 |
""".format(**locals()), locals()) |
| 30 |
|
| 31 |
cr.execute('ALTER TABLE "{0}" DROP COLUMN "{1}" CASCADE'.format(table, field)) |
| 32 |
|
| 33 |
def migrate(cr, version): |
| 34 |
convert_field(cr, 'res.partner', 'property_purchase_currency_id', 'res.currency') |
| 35 |
convert_field(cr, 'product.template', |
| 36 |
'property_account_creditor_price_difference', 'account.account') |