def migrate(cr, version): # Rename lngth into packaging_length if column_exists(cr, "product_packaging", "packaging_length"): cr.execute("UPDATE product_packaging SET packaging_length = lngth") elif column_exists(cr, "product_packaging", "lngth"): rename_column(cr, "product_packaging", "lngth", "packaging_length")
def pre_init_hook(cr): """ account.invoice and account.invoice.line inherits from l10n_br_account.fiscal_document and l10n_br_account.fiscal_document.line respectively. But the problem is that you may have existing invoice and lines (like demo data or because you were using Odoo before installing this module or because you use your Odoo instance for other countries than Brazil) so we should make the Odoo ORM happy for these records and we do that with dummy records that we use to fill these new foreign keys. """ env = api.Environment(cr, SUPERUSER_ID, {}) if not column_exists(cr, "account_invoice", "fiscal_document_id"): create_column(cr, "account_invoice", "fiscal_document_id", "INTEGER") fiscal_doc_id = env.ref("l10n_br_fiscal.fiscal_document_dummy").id cr.execute( """update account_invoice set fiscal_document_id=%s where fiscal_document_id IS NULL;""", (fiscal_doc_id, ), ) fiscal_doc_line_id = env.ref( "l10n_br_fiscal.fiscal_document_line_dummy").id if not column_exists(cr, "account_invoice_line", "fiscal_document_line_id"): create_column(cr, "account_invoice_line", "fiscal_document_line_id", "INTEGER") cr.execute( """update account_invoice_line set fiscal_document_line_id=%s where fiscal_document_line_id IS NULL;""", (fiscal_doc_line_id, ), )
def store_exception_fields(cr): if not column_exists(cr, "account_move", "main_exception_id"): logger.info("Creating field main_exception_id on account_move") cr.execute(""" ALTER TABLE account_move ADD COLUMN main_exception_id int; COMMENT ON COLUMN account_move.main_exception_id IS 'Main Exception'; """) if not column_exists(cr, "account_move", "ignore_exception"): logger.info("Creating field ignore_exception on account_move") cr.execute(""" ALTER TABLE account_move ADD COLUMN ignore_exception boolean; COMMENT ON COLUMN account_move.ignore_exception IS 'Ignore Exceptions'; """)
def migrate(cr, version): if not column_exists(cr, "queue_job", "exec_time"): # Disable trigger otherwise the update takes ages. cr.execute( """ ALTER TABLE queue_job DISABLE TRIGGER queue_job_notify; """ ) cr.execute( """ ALTER TABLE queue_job ADD COLUMN exec_time double precision DEFAULT 0; """ ) cr.execute( """ UPDATE queue_job SET exec_time = EXTRACT(EPOCH FROM (date_done - date_started)); """ ) cr.execute( """ ALTER TABLE queue_job ENABLE TRIGGER queue_job_notify; """ )
def update_db_column(self, model, column): """ Create/update the column corresponding to ``self``. For creation of geo column :param model: an instance of the field's model :param column: the column's configuration (dict) if it exists, or ``None`` """ # the column does not exist, create it if not column: create_geo_column(model._cr, model._table, self.name, self.geo_type, self.srid, self.dim, self.string) return if column['udt_name'] == self.column_type[0]: return self.update_geo_db_column(model) if column['udt_name'] in self.column_cast_from: sql.convert_column(model._cr, model._table, self.name, self.column_type[1]) else: newname = (self.name + '_moved{}').format i = 0 while sql.column_exists(model._cr, model._table, newname(i)): i += 1 if column['is_nullable'] == 'NO': sql.drop_not_null(model._cr, model._table, self.name) sql.rename_column(model._cr, model._table, self.name, newname(i)) sql.create_column(model._cr, model._table, self.name, self.column_type[1], self.string)
def pre_init_hook(cr): table = "hr_employee" column = "partner_id" field_type = "int4" if not sql.column_exists(cr, table, column): sql.create_column(cr, table, columnname=column, columntype=field_type) cr.execute("SELECT id FROM %s WHERE %s is null", (AsIs(table), AsIs(column))) employee_ids = [] for row in cr.fetchall(): employee_ids.append(row[0]) env = api.Environment(cr, SUPERUSER_ID, {}) for employee in env["hr.employee"].browse(employee_ids): partner = env["res.partner"].create({ "name": employee.name, "is_practitioner": True, "active": employee.active, }) partner.flush() cr.execute( "UPDATE %s SET %s = %s WHERE id = %s", (AsIs(table), AsIs(column), partner.id, employee.id), ) cr.execute("SELECT id FROM %s WHERE %s is null", (AsIs(table), AsIs(column)))
def _auto_init(self): # Skip the computation of the field `l10n_latam_document_type_id` at the module installation # Without this, at the module installation, # it would call `_compute_l10n_latam_document_type` on all existing records # which can take quite a while if you already have a lot of moves. It can even fail with a MemoryError. # In addition, it sets `_compute_l10n_latam_document_type = False` on all records # because this field depends on the many2many `l10n_latam_available_document_type_ids`, # which relies on having records for the model `l10n_latam.document.type` # which only happens once the according localization module is loaded. # The localization module is loaded afterwards, because the localization module depends on this module, # (e.g. `l10n_cl` depends on `l10n_latam_invoice_document`, and therefore `l10n_cl` is loaded after) # and therefore there are no records for the model `l10n_latam.document.type` at the time this fields # gets computed on installation. Hence, all records' `_compute_l10n_latam_document_type` are set to `False`. # In addition, multiple localization module depends on this module (e.g. `l10n_cl`, `l10n_ar`) # So, imagine `l10n_cl` gets installed first, and then `l10n_ar` is installed next, # if `l10n_latam_document_type_id` needed to be computed on install, # the install of `l10n_cl` would call the compute method, # because `l10n_latam_invoice_document` would be installed at the same time, # but then `l10n_ar` would miss it, because `l10n_latam_invoice_document` would already be installed. # Besides, this field is computed only for drafts invoices, as stated in the compute method: # `for rec in self.filtered(lambda x: x.state == 'draft'):` # So, if we want this field to be computed on install, it must be done only on draft invoices, and only once # the localization modules are loaded. # It should be done in a dedicated post init hook, # filtering correctly the invoices for which it must be computed. # Though I don't think this is needed. # In practical, it's very rare to already have invoices (draft, in addition) # for a Chilian or Argentian company (`res.company`) before installing `l10n_cl` or `l10n_ar`. if not column_exists(self.env.cr, "account_move", "l10n_latam_document_type_id"): create_column(self.env.cr, "account_move", "l10n_latam_document_type_id", "int4") return super()._auto_init()
def _auto_init(self): """ Create column for `preferred_payment_method_id` to avoid having it computed by the ORM on installation. Since `property_payment_method_id` is introduced in this module, there is no need for UPDATE """ if not column_exists(self.env.cr, "account_move", "preferred_payment_method_id"): create_column(self.env.cr, "account_move", "preferred_payment_method_id", "int4") return super()._auto_init()
def migrate(cr, version): if not column_exists(cr, "l10n_es_aeat_mod347_move_record", "move_type"): return # migration directly from v13 cr.execute( """UPDATE l10n_es_aeat_mod347_move_record SET amount = -amount WHERE move_type in ('receivable_refund', 'payable_refund')""" )
def _auto_init(self): # Skip the computation of the field `l10n_latam_document_type_id` at the module installation # See `_auto_init` in `l10n_latam_invoice_document/models/account_move.py` for more information if not column_exists(self.env.cr, "account_move_line", "l10n_latam_document_type_id"): create_column(self.env.cr, "account_move_line", "l10n_latam_document_type_id", "int4") return super()._auto_init()
def migrate(cr, version): # Rename estimated_pack_weight into estimated_pack_weight_kg if column_exists(cr, "stock_quant_package", "estimated_pack_weight"): rename_column( cr, "stock_quant_package", "estimated_pack_weight", "estimated_pack_weight_kg", )
def pre_init_hook(cr): """ account.invoice and account.invoice.line inherits from l10n_br_account.fiscal_document and l10n_br_account.fiscal_document.line respectively. But the problem is that you may have existing invoice and lines (like demo data or because you were using Odoo before installing this module or because you use your Odoo instance for other countries than Brazil) so we should make the Odoo ORM happy for these records and we do that with dummy records that we use to fill these new foreign keys. """ env = api.Environment(cr, SUPERUSER_ID, {}) # Create fiscal_document_id fields if not column_exists(cr, "account_invoice", "fiscal_document_id"): create_column(cr, "account_invoice", "fiscal_document_id", "INTEGER") # Create fiscal_document_line_id fields if not column_exists(cr, "account_invoice_line", "fiscal_document_line_id"): create_column(cr, "account_invoice_line", "fiscal_document_line_id", "INTEGER") companies = env["res.company"].search([]) for company in companies: cr.execute( """ UPDATE account_invoice SET fiscal_document_id=%s WHERE fiscal_document_id IS NULL;""", (company.fiscal_dummy_id.id, ), ) cr.execute( """ UPDATE account_invoice_line SET fiscal_document_line_id=%s WHERE fiscal_document_line_id IS NULL;""", (company.fiscal_dummy_id.line_ids[0].id, ), )
def pre_init_hook(cr): if not column_exists(cr, "res_partner", "is_customer"): cr.execute( """ ALTER TABLE res_partner ADD COLUMN is_customer boolean""", ) cr.execute(""" UPDATE res_partner SET is_customer = customer_rank::boolean """) if not column_exists(cr, "res_partner", "is_supplier"): cr.execute( """ ALTER TABLE res_partner ADD COLUMN is_supplier boolean""", ) cr.execute(""" UPDATE res_partner SET is_supplier = supplier_rank::boolean """)
def _auto_init(self): """ Create related field here, too slow when computing it afterwards through _compute_related. Since group_id.sale_id is created in this module, no need for an UPDATE statement. """ if not column_exists(self.env.cr, 'stock_picking', 'sale_id'): create_column(self.env.cr, 'stock_picking', 'sale_id', 'int4') return super()._auto_init()
def migrate(cr, version): """Migrate only_quotation field - Copy product.template values to product.product - Rename to shop_only_quotation """ if ( not version or column_exists(cr, "product_product", "shop_only_quotation") or not column_exists(cr, "product_template", "only_quotation") ): return create_column(cr, "product_product", "shop_only_quotation", "BOOLEAN") cr.execute( """ UPDATE product_product pp SET shop_only_quotation = pt.only_quotation FROM product_template pt WHERE pp.product_tmpl_id = pt.id """ ) cr.execute("ALTER TABLE product_template DROP COLUMN only_quotation")
def create_risk_partner_id_column(cr): if not sql.column_exists(cr, "sale_order_line", "risk_partner_id"): sql.create_column(cr, "sale_order_line", "risk_partner_id", "int4") logger.info("Computing field risk_partner_id on sale.order.line") cr.execute(""" UPDATE sale_order_line sol SET risk_partner_id = p.commercial_partner_id FROM sale_order so LEFT JOIN res_partner p ON p.id = so.partner_invoice_id WHERE so.id = sol.order_id and sol.risk_partner_id IS DISTINCT FROM p.commercial_partner_id; """)
def migrate(cr, version): if not version: return if not column_exists(cr, 'jira_backend_timestamp', 'last_timestamp'): cr.execute(""" ALTER TABLE jira_backend_timestamp ADD COLUMN last_timestamp timestamp; """) cr.execute(""" UPDATE jira_backend_timestamp SET last_timestamp = import_start_time WHERE last_timestamp IS NULL; """) cr.execute(""" ALTER TABLE jira_backend_timestamp ALTER COLUMN last_timestamp SET NOT NULL; """) if not column_exists(cr, 'jira_account_analytic_line', 'jira_updated_at'): cr.execute(""" ALTER TABLE jira_account_analytic_line ADD COLUMN jira_updated_at timestamp; """) cr.execute(""" UPDATE jira_account_analytic_line SET jira_updated_at = sync_date WHERE jira_updated_at IS NULL; """) if not column_exists(cr, 'jira_project_task', 'jira_updated_at'): cr.execute(""" ALTER TABLE jira_project_task ADD COLUMN jira_updated_at timestamp; """) cr.execute(""" UPDATE jira_project_task SET jira_updated_at = sync_date WHERE jira_updated_at IS NULL; """)
def migrate(cr, version): if not column_exists(cr, "delay_export", "__temp_user_id"): return with api.Environment.manage(): env = api.Environment(cr, SUPERUSER_ID, {}) field = env["delay.export"]._fields["user_ids"] rel, id1, id2 = field.relation, field.column1, field.column2 env.cr.execute(""" INSERT INTO %s (%s, %s) SELECT id, __temp_user_id FROM delay_export """ % (rel, id1, id2)) env.cr.execute("ALTER TABLE delay_export DROP COLUMN __temp_user_id;")
def create_commercial_partner_id_column(cr): if not sql.column_exists(cr, 'sale_order_line', 'commercial_partner_id'): sql.create_column(cr, 'sale_order_line', 'commercial_partner_id', 'int4') logger.info('Computing field commercial_partner_id on sale.order.line') cr.execute(""" UPDATE sale_order_line sol SET commercial_partner_id = p.commercial_partner_id FROM res_partner p WHERE p.id = sol.order_partner_id and sol.commercial_partner_id IS DISTINCT FROM p.commercial_partner_id; """)
def _auto_init(self): """ Create column to stop ORM from computing it himself (too slow) """ if not column_exists(self.env.cr, 'sale_order_line', 'is_service'): create_column(self.env.cr, 'sale_order_line', 'is_service', 'bool') self.env.cr.execute(""" UPDATE sale_order_line line SET is_service = (pt.type = 'service') FROM product_product pp LEFT JOIN product_template pt ON pt.id = pp.product_tmpl_id WHERE pp.id = line.product_id """) return super()._auto_init()
def migrate(cr, version): logger = logging.getLogger( "odoo.addons.account_banking_mandate.migrations.14.0.1.0.0") if not column_exists(cr, "account_move_line", "mandate_id"): logger.warning("Column account_move_line.mandate_id not found when " "populating account_move.mandate_id") return logger.info("Populating account_move.mandate_id from obsolete " "account_move_line.mandate_id") cr.execute(""" UPDATE account_move am SET mandate_id = aml.mandate_id FROM account_move_line aml WHERE aml.mandate_id IS NOT NULL AND am.mandate_id IS NULL """)
def migrate(cr, version): # Rename lnght into packaging_length if column_exists(cr, "product_packaging", "lnght"): rename_column(cr, "product_packaging", "lnght", "packaging_length") # Convert old hard-coded uom values (mm) # into new default uom values (m) cr.execute( """ UPDATE product_packaging SET packaging_length = packaging_length/1000, height = height/1000, width = width/1000, """ )
def migrate(cr, version): if not table_exists(cr, "queue_job"): return if not column_exists(cr, "queue_job", "records"): cr.execute(""" ALTER TABLE queue_job ADD COLUMN records text; """) cr.execute(""" UPDATE queue_job SET records = '{"_type": "odoo_recordset"' || ', "model": "' || model_name || '"' || ', "uid": ' || user_id || ', "ids": ' || record_ids || '}' WHERE records IS NULL; """)
def migrate(cr, installed_version): if not column_exists(cr, 'request_request', 'author_id'): cr.execute(""" ALTER TABLE request_request ADD COLUMN author_id INTEGER; UPDATE request_request r SET author_id=( SELECT partner_id FROM res_users u WHERE u.id=r.created_by_id); UPDATE request_request r SET partner_id=(SELECT parent_id FROM res_partner p WHERE p.id=r.author_id) WHERE r.partner_id IS NULL; """)
def migrate(cr, version): # Disable trigger otherwise the update takes ages. cr.execute(""" ALTER TABLE queue_job DISABLE TRIGGER queue_job_notify; """) if not column_exists(cr, "queue_job", "records"): cr.execute(""" ALTER TABLE queue_job ADD COLUMN records text; """) cr.execute(""" UPDATE queue_job SET records = '{"_type": "odoo_recordset"' || ', "model": "' || model_name || '"' || ', "uid": ' || user_id || ', "ids": ' || record_ids || '}' WHERE records IS NULL; """) cr.execute(""" ALTER TABLE queue_job ENABLE TRIGGER queue_job_notify; """)
def migrate(cr, version): # Get all related tables using thumb mixin _logger.info("Pre-computing new thumbs relations") cr.execute("SELECT DISTINCT res_model FROM storage_thumbnail") res = [x[0] for x in cr.fetchall()] for model in res: # assume table has std name table = model.replace(".", "_") for scale in ("medium", "small"): if not column_exists(cr, table, "thumb_%s_id" % scale): cr.execute( """ ALTER TABLE %s ADD COLUMN thumb_%s_id int4 REFERENCES storage_thumbnail(id) """, (AsIs(table), AsIs(scale)), ) _logger.info("Added thumb %s column to %s", scale, table) _store_relation(cr, table, model, scale) _logger.info("Computed thumb %s relation for %s", scale, table)
def migrate(env, version): if column_exists(env.cr, "account_move", "sii_thirdparty_invoice"): openupgrade.rename_columns(env.cr, _column_renames)
def migrate(cr, version): if column_exists(cr, "res_partner", "state"): if column_exists(cr, "res_partner", "old_state"): cr.execute("ALTER TABLE res_partner DROP COLUMN old_state") rename_column(cr, "res_partner", "state", "old_state")
def migrate(cr, version): if column_exists(cr, "delay_export", "user_id"): rename_column(cr, "delay_export", "user_id", "__temp_user_id")
def pre_init_hook(cr): for table, column in COLUMNS: if not column_exists(cr, table, column): _logger.info("Create discount column %s in database", column) create_column(cr, table, column, "numeric")