def migrate(env, version): openupgrade.rename_columns( env.cr, { "account_invoice_global_discount": [("invoice_id", None)], "account_invoice_global_discount_rel": [("invoice_id", None)], }, ) openupgrade.logged_query( env.cr, "ALTER TABLE account_invoice_global_discount ADD invoice_id INT4") openupgrade.logged_query( env.cr, "ALTER TABLE account_invoice_global_discount_rel ADD invoice_id INT4") openupgrade.logged_query( env.cr, sql.SQL("""UPDATE account_invoice_global_discount aigd SET invoice_id = ai.move_id FROM account_invoice ai WHERE ai.id = aigd.{}""").format( sql.Identifier(openupgrade.get_legacy_name("invoice_id"))), ) openupgrade.logged_query( env.cr, sql.SQL("""UPDATE account_invoice_global_discount_rel aigdr SET invoice_id = ai.move_id FROM account_invoice ai WHERE ai.id = aigdr.{}""").format( sql.Identifier(openupgrade.get_legacy_name("invoice_id"))), )
def update_price_history(cr): # Create price history for all existing variants openupgrade.logged_query( cr, """ INSERT INTO product_price_history (company_id, product_id, datetime, cost, write_date, write_uid, create_date, create_uid) SELECT pph.company_id, pr.id, pph.datetime, pph.cost, pph.write_date, pph.write_uid, pph.create_date, pph.create_uid FROM product_price_history as pph INNER JOIN product_template as pt ON pt.id = pph.%(product_tmpl_id)s LEFT JOIN product_product pr ON pr.product_tmpl_id = pt.id """ % { 'product_tmpl_id': openupgrade.get_legacy_name('product_template_id') }) # Delete the records that refer to the product template openupgrade.logged_query( cr, """ DELETE FROM product_price_history WHERE %(product_tmpl_id)s IS NOT NULL """ % { 'product_tmpl_id': openupgrade.get_legacy_name('product_template_id') })
def map_expense_state(env): # Mapping values of state field for hr_expense. openupgrade.map_values( env.cr, openupgrade.get_legacy_name('state'), 'state', [('confirm', 'submit'), ('accepted', 'approve'), ('done', 'post'), ('paid', 'done'), ('cancelled', 'cancel')], table=openupgrade.get_legacy_name('hr_expense_expense'))
def migrate_from_v8(env): """Reconvert delivery carriers that were the grids from the same v8 carrier again on children with this new structure. """ cr = env.cr old_column = openupgrade.get_legacy_name('carrier_id') old_table = openupgrade.get_legacy_name('delivery_carrier') cr.execute( "SELECT COUNT({0}), {0} FROM delivery_carrier GROUP BY {0}".format( old_column, )) rows = cr.fetchall() Carrier = env['delivery.carrier'] for count, old_carrier_id in rows: if count <= 1: continue # Get children ids cr.execute( "SELECT id FROM delivery_carrier WHERE {} = %s".format(old_column), (old_carrier_id, )) child_ids = [x[0] for x in cr.fetchall()] # Get old carrier data cr.execute( "SELECT name, partner_id FROM {} WHERE id = %s".format(old_table), (old_carrier_id, )) old_carrier_vals = cr.fetchone() # Create new carrier and put the rest of the carriers as children carrier = Carrier.create({ 'name': old_carrier_vals[0], 'partner_id': old_carrier_vals[1], 'destination_type': 'multi', }) cr.execute("UPDATE delivery_carrier SET parent_id = %s WHERE id IN %s", (carrier.id, tuple(child_ids)))
def set_analytic_account_visibility(cr): """Hide view analytic accounts with previous state considered as closed. It also hides analytic accounts of type=view. If we want to restore the visibility of these accounts, we have to perform: UPDATE account_analytic_account SET account_type='normal' WHERE %s = 'view' AND %s NOT IN ('cancelled', 'close') % ( openupgrade.get_legacy_name('type'), openupgrade.get_legacy_name('state'), ) """ openupgrade.map_values( cr, openupgrade.get_legacy_name('state'), 'account_type', [ ('cancelled', 'closed'), ('close', 'closed'), ], table='account_analytic_account', ) openupgrade.logged_query( cr, """UPDATE account_analytic_account SET account_type='closed' WHERE %s = 'view'""" % openupgrade.get_legacy_name('type'))
def migrate(env, version): cr = env.cr cr.execute("select id, %s, %s FROM sale_order_type" % ( openupgrade.get_legacy_name('validate_automatically_picking'), openupgrade.get_legacy_name('validate_automatically_invoice'), # no existia en v8 # openupgrade.get_legacy_name('validate_automatically_payment'), )) for rec in cr.fetchall(): (id, validate_automatically_picking, validate_automatically_invoice # validate_automatically_payment ) = rec so_type = env['sale.order.type'].browse(id) if validate_automatically_picking: so_type.picking_atomation = 'validate' # before, if we have a journal configured, we automatically create # invoice if so_type.journal_id: so_type.invoicing_atomation = 'create_invoice' if validate_automatically_invoice: so_type.invoicing_atomation = 'validate_invoice' if (so_type.payment_journal_id # and validate_automatically_payment ): so_type.payment_atomation = 'validate_payment'
def migrate(env, version): column_name = openupgrade.get_legacy_name('better_zip_id') openupgrade.logged_query( env.cr, "ALTER TABLE res_city_zip ADD %s INTEGER", (AsIs(column_name), ), ) openupgrade.logged_query( env.cr, """ INSERT INTO res_city_zip ( %s, name, city_id ) SELECT id, name, city_id FROM res_better_zip WHERE city_id IS NOT NULL""", (AsIs(column_name), ), ) # Recompute display name for entries inserted by SQL env['res.city.zip'].search([])._compute_new_display_name() # Link res.partner with corresponding new entries openupgrade.logged_query( env.cr, """ UPDATE res_partner rp SET zip_id = rcz.id FROM res_city_zip rcz WHERE rcz.%s = rp.%s""", ( AsIs(column_name), AsIs(openupgrade.get_legacy_name('zip_id')), ), )
def convert_many2one_stock_inventory_product_and_location(env): openupgrade.m2o_to_x2m(env.cr, env['stock.inventory'], 'stock_inventory', 'location_ids', openupgrade.get_legacy_name('location_id')) openupgrade.m2o_to_x2m(env.cr, env['stock.inventory'], 'stock_inventory', 'product_ids', openupgrade.get_legacy_name('product_id'))
def migrate(cr, version): if not version: return legacy_reviewer_id =\ openupgrade.get_legacy_name("reviewer_id") legacy_reviewer_group_id =\ openupgrade.get_legacy_name("reviewer_group_id") with api.Environment.manage(): env = api.Environment(cr, SUPERUSER_ID, {}) openupgrade.m2o_to_x2m( cr=cr, model=env["tier.definition.review"], table="tier_definition_review", field="reviewer_ids", source_field=legacy_reviewer_id, ) openupgrade.m2o_to_x2m( cr=cr, model=env["tier.definition.review"], table="tier_definition_review", field="reviewer_group_ids", source_field=legacy_reviewer_group_id, ) openupgrade.drop_columns( cr=cr, column_spec=[ ("tier_definition_review", legacy_reviewer_id), ("tier_definition_review", legacy_reviewer_group_id), ], )
def convert_m2o_to_x2m_fields(env): openupgrade.m2o_to_x2m( env.cr, env["stock.inventory"], "stock_inventory", "location_ids", openupgrade.get_legacy_name("location_id"), ) openupgrade.m2o_to_x2m( env.cr, env["stock.inventory"], "stock_inventory", "product_ids", openupgrade.get_legacy_name("product_id"), ) openupgrade.m2o_to_x2m( env.cr, env["stock.inventory"], "stock_inventory", "categ_ids", openupgrade.get_legacy_name("category_id"), ) openupgrade.m2o_to_x2m( env.cr, env["stock.inventory"], "stock_inventory", "lot_ids", openupgrade.get_legacy_name("lot_id"), )
def migrate(env, version): openupgrade.load_data(env.cr, "sale_commission", "migrations/13.0.1.0.0/noupdate_changes.xml") openupgrade.logged_query( env.cr, sql.SQL(""" UPDATE account_invoice_line_agent aila SET object_id = aml.id FROM account_move_line aml WHERE aml.old_invoice_line_id = aila.{} """).format(sql.Identifier(openupgrade.get_legacy_name("object_id"))), ) openupgrade.logged_query( env.cr, sql.SQL(""" UPDATE account_invoice_line_agent aila SET invoice_id = am.id FROM account_move am WHERE am.old_invoice_id = aila.{} """).format(sql.Identifier(openupgrade.get_legacy_name("invoice_id"))), ) openupgrade.logged_query( env.cr, """ UPDATE sale_commission_settlement scs SET invoice_id = am.id FROM account_move am WHERE am.old_invoice_id = scs.invoice """, ) openupgrade.logged_query( env.cr, sql.SQL(""" UPDATE sale_commission_settlement_line scsl SET invoice_line_id = aml.id FROM account_move_line aml WHERE aml.old_invoice_line_id = scsl.{} """).format(sql.Identifier( openupgrade.get_legacy_name("invoice_line_id"))), ) openupgrade.logged_query( env.cr, """ UPDATE account_move am SET commission_total = ai.commission_total FROM account_invoice ai WHERE ai.id = am.old_invoice_id AND ai.commission_total IS NOT NULL AND ai.commission_total != 0.0 """, ) openupgrade.logged_query( env.cr, """ UPDATE account_move_line aml SET commission_free = ail.commission_free FROM account_invoice_line ail WHERE aml.old_invoice_line_id = ail.id AND ail.commission_free """, )
def convert_blog_post_cover(env): """Put default value for posts without cover image and put URL of the cover in field cover_properties for using it for the rest.""" post_obj = env['blog.post'] # Without cover image env.cr.execute( "SELECT id FROM blog_post WHERE {} IS NULL".format( openupgrade.get_legacy_name('background_image') ) ) posts = post_obj.browse([x[0] for x in env.cr.fetchall()]) posts.write({'cover_properties': post_obj._defaults['cover_properties']}) # With cover image env.cr.execute( "SELECT id, {0} FROM blog_post WHERE {0} IS NOT NULL".format( openupgrade.get_legacy_name('background_image') ) ) for row in env.cr.fetchall(): post = post_obj.browse(row[0]) post.cover_properties = ( '{{"background-image": "url({})",' ' "opacity": "1",' ' "background-color": "oe_none",' ' "resize_class": "cover cover_full"}}' ).format(row[1])
def fill_res_partner_industry(cr): cr.execute( """ INSERT INTO res_partner_industry (name, active, create_uid, create_date, write_uid, write_date, %s) SELECT name, TRUE as active, create_uid, create_date, write_uid, write_date, id FROM res_partner_sector """, (AsIs(openupgrade.get_legacy_name('old_sector_id')), ), ) # corrected parent_id here: cr.execute( """ UPDATE res_partner_industry rpi SET parent_id = rpi2.id FROM res_partner_sector rps INNER JOIN res_partner_industry rpi2 ON rps.parent_id = rpi2.%s WHERE rpi.%s = rps.id """, ( AsIs(openupgrade.get_legacy_name('old_sector_id')), AsIs(openupgrade.get_legacy_name('old_sector_id')), ), )
def create_withholding_data_lines(env): """ Create ftpa_withholding_ids from ftpa_withholding_type and ftpa_withholding_amount """ column_wht_amount = openupgrade.get_legacy_name('withholding_tax_amount') column_wht_type = openupgrade.get_legacy_name('ftpa_withholding_type') exists = openupgrade.column_exists(env.cr, 'account_invoice', column_wht_amount) mapping = { 'name': 'ai.{ftpa_withholding_type}'.format( ftpa_withholding_type=column_wht_type), 'invoice_id': 'ai.id', 'create_uid': 'ai.create_uid', 'create_date': 'ai.create_date', 'write_date': 'ai.write_date', 'write_uid': 'ai.write_uid', } if exists: mapping.update( {'amount': 'ai.{ftpa_withholding_amount}'.format( ftpa_withholding_amount=column_wht_amount)}) query = """ INSERT INTO withholding_data_line ({columns}) SELECT {values} FROM account_invoice AS ai WHERE ai.{ftpa_withholding_type} IS NOT NULL;""".format( columns=','.join(mapping.keys()), values=','.join(mapping.values()), ftpa_withholding_type=column_wht_type) openupgrade.logged_query(env.cr, sql.SQL(query))
def fill_stock_inventory_fields(env): # categ_id openupgrade.logged_query( env.cr, """ UPDATE stock_inventory_line sil SET categ_id = si.{} FROM stock_inventory si WHERE sil.inventory_id = si.id AND sil.categ_id IS NULL """.format(openupgrade.get_legacy_name('category_id'))) # prod_lot_id openupgrade.logged_query( env.cr, """ UPDATE stock_inventory_line sil SET prod_lot_id = si.{} FROM stock_inventory si WHERE sil.inventory_id = si.id AND sil.prod_lot_id IS NULL """.format(openupgrade.get_legacy_name('lot_id'))) # package_id openupgrade.logged_query( env.cr, """ UPDATE stock_inventory_line sil SET package_id = si.{} FROM stock_inventory si WHERE sil.inventory_id = si.id AND sil.package_id IS NULL """.format(openupgrade.get_legacy_name('package_id'))) # partner_id openupgrade.logged_query( env.cr, """ UPDATE stock_inventory_line sil SET partner_id = si.{} FROM stock_inventory si WHERE sil.inventory_id = si.id AND sil.partner_id IS NULL """.format(openupgrade.get_legacy_name('partner_id')))
def update_asset_group_links(cr): parent_column = sql.Identifier(openupgrade.get_legacy_name('parent_id')) origin_column = sql.Identifier( openupgrade.get_legacy_name('view_asset_id')) openupgrade.logged_query( cr, sql.SQL(""" INSERT INTO account_asset_profile_group_rel (profile_id, group_id) SELECT aap.id, aag.id FROM account_asset_profile aap JOIN account_asset_group aag ON aag.{origin_column} = aap.{parent_column}""").format( parent_column=parent_column, origin_column=origin_column, ), ) openupgrade.logged_query( cr, sql.SQL(""" INSERT INTO account_asset_group_rel (asset_id, group_id) SELECT aa.id, aag.id FROM account_asset aa JOIN account_asset_group aag ON aag.{origin_column} = aa.{parent_column}""").format( parent_column=parent_column, origin_column=origin_column, ), )
def map_res_partner_bank_aba_routing(env): openupgrade.logged_query( env.cr, """ UPDATE res_partner_bank SET aba_routing = '' || %s WHERE %s IS NOT NULL """, (openupgrade.get_legacy_name('aba_routing'), openupgrade.get_legacy_name('aba_routing')))
def migrate(env, version): column_name = openupgrade.get_legacy_name('better_zip_id') openupgrade.logged_query( 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 rbz WHERE city_id IS NULL AND rbz.country_id IS NOT NULL AND rbz.name IS NOT 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 IS NOT DISTINCT FROM 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 ( %s, name, city_id ) SELECT id, name, city_id FROM res_better_zip WHERE city_id IS NOT NULL ON CONFLICT DO NOTHING""", (AsIs(column_name), ), ) # Recompute display name for entries inserted by SQL env['res.city.zip'].search([])._compute_new_display_name() # Link res.partner with corresponding new entries openupgrade.logged_query( env.cr, """ UPDATE res_partner rp SET zip_id = rcz.id FROM res_city_zip rcz WHERE rcz.%s = rp.%s""", (AsIs(column_name), AsIs(openupgrade.get_legacy_name('zip_id')), ), )
def map_account_payment_check_number(env): openupgrade.logged_query( env.cr, """ UPDATE account_payment SET check_number = '' || %s WHERE %s IS NOT NULL """, (openupgrade.get_legacy_name('check_number'), openupgrade.get_legacy_name('check_number')) )
def migrate(cr, version): pool = pooler.get_pool(cr.dbname) # Migrate m2o categ_id to m2m categ_ids openupgrade.m2o_to_m2m( cr, pool.get('purchase.order'), 'purchase_order', 'categ_ids', openupgrade.get_legacy_name('categ_id')) openupgrade.m2o_to_m2m( cr, pool.get('account.invoice'), 'account_invoice', 'categ_ids', openupgrade.get_legacy_name('categ_id'))
def type_change_payment_transaction_and_sale_order(env): if openupgrade.column_exists(env.cr, 'sale.order', openupgrade.get_legacy_name('payment_tx_id')): openupgrade.m2o_to_x2m(env.cr, env['sale.order'], 'sale_order', 'transaction_ids', openupgrade.get_legacy_name('payment_tx_id')) openupgrade.m2o_to_x2m(env.cr, env['payment.transaction'], 'payment_transaction', 'sale_order_ids', openupgrade.get_legacy_name('sale_order_id'))
def map_track_service(env): """"Map values for old `track_service` field (copied in sale pre-migration) according this mapping: track_service service_tracking ------------- ---------------- 'manual' 'no' 'task' if not project_id: 'task_new_project' if project_id: 'task_global_project' 'timesheet' 'project_only' Project field depends on company, so this is applicable as soon as one company has any project set. """ openupgrade.map_values( env.cr, openupgrade.get_legacy_name('track_service'), 'service_tracking', [ ('manual', 'no'), ('timesheet', 'project_only'), ], table='product_template', ) # Need to be done through subquery as unique option for proper joining openupgrade.logged_query( env.cr, """ UPDATE product_template pt SET service_tracking = 'task_new_project' FROM ( SELECT pt.id FROM product_template pt JOIN ir_model_fields imf ON imf.name = 'project_id' AND imf.model = 'product.template' LEFT JOIN ir_property ip ON ip.fields_id = imf.id AND ip.res_id = 'product.template,' || pt.id::text WHERE pt.%s = 'task' AND ip.value_reference IS NULL ) sub WHERE sub.id = pt.id""", (AsIs(openupgrade.get_legacy_name('track_service')), ), ) openupgrade.logged_query( env.cr, """ UPDATE product_template pt SET service_tracking = 'task_global_project' FROM ( SELECT pt.id FROM product_template pt JOIN ir_model_fields imf ON imf.name = 'project_id' AND imf.model = 'product.template' LEFT JOIN ir_property ip ON ip.fields_id = imf.id AND ip.res_id = 'product.template,' || pt.id::text WHERE pt.%s = 'task' AND ip.value_reference IS NOT NULL ) sub WHERE sub.id = pt.id""", (AsIs(openupgrade.get_legacy_name('track_service')), ), )
def fill_account_chart_template_account_code_prefix(cr): # if company_id was filled: openupgrade.logged_query( cr, """ UPDATE account_chart_template act SET bank_account_code_prefix = rc.bank_account_code_prefix FROM res_company rc WHERE act.%s = rc.id AND act.bank_account_code_prefix IS NULL """, (AsIs(openupgrade.get_legacy_name('company_id')), ), ) openupgrade.logged_query( cr, """ UPDATE account_chart_template act SET cash_account_code_prefix = rc.cash_account_code_prefix FROM res_company rc WHERE act.%s = rc.id AND act.cash_account_code_prefix IS NULL """, (AsIs(openupgrade.get_legacy_name('company_id')), ), ) # if company_id was not filled: openupgrade.logged_query( cr, """ UPDATE account_chart_template act SET bank_account_code_prefix = 'OUB' WHERE act.bank_account_code_prefix IS NULL """) openupgrade.logged_query( cr, """ UPDATE account_chart_template act SET cash_account_code_prefix = 'OUB' WHERE act.cash_account_code_prefix IS NULL """) # transfer_account_code_prefix: openupgrade.logged_query( cr, """ UPDATE account_chart_template act SET transfer_account_code_prefix = trim(trailing '0' from aat.code) FROM account_account_template aat WHERE act.%s = aat.id """, (AsIs(openupgrade.get_legacy_name('transfer_account_id')), ), ) openupgrade.logged_query( cr, """ UPDATE res_company rc SET transfer_account_code_prefix = act.transfer_account_code_prefix FROM account_chart_template act WHERE act.%s = rc.id """, (AsIs(openupgrade.get_legacy_name('company_id')), ), )
def migrate(cr, version): """Restores visibility of accounts of type='view'. They are not exactly the same as parent accounts, but both values are very tighted""" openupgrade.logged_query( cr, """UPDATE account_analytic_account SET account_type='normal' WHERE %s = 'view' AND %s NOT IN ('cancelled', 'close') """ % ( openupgrade.get_legacy_name('type'), openupgrade.get_legacy_name('state'), ))
def migrate(env, version): # Link crm.lead with corresponding new entries openupgrade.logged_query( env.cr, sql.SQL("""UPDATE crm_lead cl SET location_id = rcz.id FROM res_city_zip rcz WHERE rcz.{} = cl.{}""").format( sql.Identifier(openupgrade.get_legacy_name('better_zip_id')), sql.Identifier(openupgrade.get_legacy_name('location_id')), ))
def get_sale_order_opportunity_id(env): """ Fetch sale order references from the obsolete reference fields from the crm module """ env.cr.execute( """UPDATE sale_order so SET opportunity_id = cl.id FROM crm_lead cl WHERE cl.%s = 'sale.order,'||so.id OR cl.%s = 'sale.order,'||so.id""", (AsIs(openupgrade.get_legacy_name('ref')), AsIs(openupgrade.get_legacy_name('ref2'))))
def migrate(cr, version): if not version: return with api.Environment.manage(): env = api.Environment(cr, SUPERUSER_ID, {}) openupgrade.m2o_to_x2m( cr, env['stock.move'], 'stock_move', 'invoice_line_ids', openupgrade.get_legacy_name('invoice_line_id')) openupgrade.m2o_to_x2m( cr, env['stock.picking'], 'stock_picking', 'invoice_ids', openupgrade.get_legacy_name('invoice_id'))
def map_account_tax_template_type(cr): """ See comments in method map_account_tax_type in the pre-migration script.""" if not openupgrade.logged_query(cr, """ select id FROM account_tax where {name_v8} = 'code' """.format(name_v8=openupgrade.get_legacy_name('type'))): return openupgrade.map_values( cr, openupgrade.get_legacy_name('type'), 'amount_type', [('code', 'code')], table='account_tax_template', write='sql')
def fill_slide_likes_dislikes_views(env): """The new structure needs an extra slide.slide.partner record for registering views and possible likes/dislikes. We add fake records here for getting the same numbers that were cumulated in previous versions. """ partner_root = env.ref("base.partner_root") openupgrade.logged_query( env.cr, sql.SQL(""" INSERT INTO slide_slide_partner (create_uid, create_date, write_uid, write_date, slide_id, channel_id, partner_id, vote, completed, quiz_attempts_count) SELECT ss.create_uid, ss.create_date, ss.write_uid, ss.write_date, ss.id, ss.channel_id, %s, 1, False, 0 FROM slide_slide ss CROSS JOIN generate_series(1, ss.{likes}) AS sub WHERE ss.{likes} > 0 """).format( likes=sql.Identifier(openupgrade.get_legacy_name("likes"))), (partner_root.id, )) openupgrade.logged_query( env.cr, sql.SQL(""" INSERT INTO slide_slide_partner (create_uid, create_date, write_uid, write_date, slide_id, channel_id, partner_id, vote, completed, quiz_attempts_count) SELECT ss.create_uid, ss.create_date, ss.write_uid, ss.write_date, ss.id, ss.channel_id, %s, -1, False, 0 FROM slide_slide ss CROSS JOIN generate_series(1, ss.{dislikes}) AS sub WHERE ss.{dislikes} > 0 """).format( dislikes=sql.Identifier(openupgrade.get_legacy_name("dislikes"))), (partner_root.id, )) openupgrade.logged_query( env.cr, sql.SQL(""" INSERT INTO slide_slide_partner (create_uid, create_date, write_uid, write_date, slide_id, channel_id, partner_id, vote, completed, quiz_attempts_count) SELECT ss.create_uid, ss.create_date, ss.write_uid, ss.write_date, ss.id, ss.channel_id, %s, 0, False, 0 FROM slide_slide ss CROSS JOIN generate_series(1, ss.{slide_views} - COALESCE(ss.{likes}, 0) - COALESCE(ss.{dislikes}, 0)) AS sub WHERE ss.{slide_views} > 0 """).format( likes=sql.Identifier(openupgrade.get_legacy_name("likes")), dislikes=sql.Identifier(openupgrade.get_legacy_name("dislikes")), slide_views=sql.Identifier( openupgrade.get_legacy_name("slide_views")), ), (partner_root.id, ))
def fill_mass_mailing_list_contact_rel_data(cr): openupgrade.logged_query( cr, """ UPDATE mail_mass_mailing_contact_list_rel mmmclr SET unsubscription_date = mmmc.%s, opt_out = mmmc.%s FROM mail_mass_mailing_contact mmmc WHERE mmmclr.contact_id = mmmc.id """, ( AsIs(openupgrade.get_legacy_name('unsubscription_date')), AsIs(openupgrade.get_legacy_name('opt_out')), ), )
# -*- coding: utf-8 -*- # Copyright 2016 Tecnativa - Pedro M. Baeza <*****@*****.**> # License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). from openupgradelib import openupgrade column_copys = { 'account_analytic_account': [ (openupgrade.get_legacy_name('date_start'), 'date_start', None), ], } @openupgrade.migrate() def migrate(cr, version): openupgrade.copy_columns(cr, column_copys) openupgrade.update_module_names( cr, [ ('hr_timesheet_invoice', 'contract'), ('contract_journal', 'contract'), ('contract_discount', 'contract'), ('contract_recurring_invoicing_marker', 'contract'), ('contract_recurring_invoicing_monthly_last_day', 'contract'), ('contract_show_recurring_invoice', 'contract'), ], merge_modules=True, )
def convert_binary_field_to_attachment(env, field_spec): """This method converts the 8.0 binary fields to attachments like Odoo 9.0 makes with the new attachment=True attribute. It has to be called on post-migration script, as there's a call to get the res_name of the target model, which is not yet loaded on pre-migration. You need to rename the involved column in pre-migration script if you don't want to lose your data in the process. This method also removes after the conversion the source column for avoiding data duplication. This is done through Odoo ORM, because there's a lot of logic associated with guessing MIME type, format and length, file saving in store... that is doesn't worth to recreate it via SQL as there's not too much performance problem. :param env: Odoo environment :param field_spec: A dictionary with the ORM model name as key, and as dictionary values a tuple with: * field name to be converted as attachment as first element. * SQL column name that contains actual data as second element. If the second element is None, then the column name is taken calling `get_legacy_name` method, which is the typical technique. """ logger = logging.getLogger('OpenUpgrade') attachment_model = env['ir.attachment'] for model_name in field_spec: model = env[model_name] for field, column in field_spec[model_name]: if column is None: column = openupgrade.get_legacy_name(field) logger.info( "Converting to attachment field {} from model {} stored in " "column {}".format(field, model_name, column) ) last_id = 0 while True: env.cr.execute( """SELECT id, {0} FROM {1} WHERE {0} IS NOT NULL AND id > {2} ORDER BY id LIMIT 500; """.format(column, model._table, last_id) ) rows = env.cr.fetchall() if not rows: break logger.info( " converting {0} items starting after {1}..." "".format(len(rows), last_id)) for row in rows: last_id = row[0] data = bytes(row[1]) if data and data != 'None': attachment_model.create({ 'name': field, 'res_model': model_name, 'res_field': field, 'res_id': last_id, 'type': 'binary', 'datas': data, }) # Remove source column for cleaning the room env.cr.execute("ALTER TABLE {} DROP COLUMN {}".format( model._table, column, ))