def check_indexes(self, cr, model_names): """ Create or drop column indexes for the given models. """ expected = [("%s_%s_index" % (Model._table, field.name), Model._table, field.name, field.index) for model_name in model_names for Model in [self.models[model_name]] if Model._auto and not Model._abstract for field in Model._fields.values() if field.column_type and field.store] if not expected: return cr.execute("SELECT indexname FROM pg_indexes WHERE indexname IN %s", [tuple(row[0] for row in expected)]) existing = {row[0] for row in cr.fetchall()} for indexname, tablename, columnname, index in expected: if index and indexname not in existing: try: with cr.savepoint(flush=False): sql.create_index(cr, indexname, tablename, ['"%s"' % columnname]) except psycopg2.OperationalError: _schema.error("Unable to add index for %s", self) elif not index and indexname in existing: _schema.info("Keep unexpected index %s on table %s", indexname, tablename)
def add_helper_invoice_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move ADD COLUMN old_invoice_id integer""", ) openupgrade.logged_query( env.cr, """ UPDATE account_move am SET old_invoice_id = ai.id FROM account_invoice ai WHERE ai.move_id = am.id """, ) openupgrade.logged_query( env.cr, """ UPDATE account_move am SET old_invoice_id = aml.invoice_id FROM account_move_line aml WHERE aml.move_id = am.id AND am.old_invoice_id IS NULL """, ) openupgrade.logged_query( env.cr, """ UPDATE account_invoice ai SET move_id = am.old_invoice_id FROM account_move am WHERE am.old_invoice_id = ai.id AND ai.move_id IS NULL """, ) openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line ADD COLUMN old_invoice_line_id integer""", ) openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line ADD COLUMN old_invoice_tax_id integer""", ) # Create index for these columns, as they are going to be accessed frequently for table, field in [ ("account_move", "old_invoice_id"), ("account_move_line", "old_invoice_line_id"), ("account_move_line", "old_invoice_tax_id"), ]: index_name = '%s_%s_index' % (table, field) sql.create_index(env.cr, index_name, table, ['"%s"' % field])
def migrate(env, version): cr = env.cr openupgrade.rename_xmlids(cr, _xmlid_renames) fill_product_pricelist_item_prices(env) fill_product_pricelist_item_active_default(env) sql.create_index( env.cr, 'product_template_attribute_value_ou_migration_idx', "product_template_attribute_value", ['product_attribute_value_id', 'product_tmpl_id']) insert_missing_product_template_attribute_line(env) fill_product_template_attribute_value_attribute_line_id(env) insert_missing_product_template_attribute_value(env) fill_product_template_attribute_value__attribute_id_related(env) calculate_product_product_combination_indices(env)
def add_helper_invoice_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move ADD COLUMN old_invoice_id integer""", ) openupgrade.logged_query( env.cr, """ UPDATE account_move am SET old_invoice_id = ai.id FROM account_invoice ai WHERE ai.move_id = am.id """, ) index_name = '%s_%s_index' % ("account_move", "old_invoice_id") sql.create_index(env.cr, index_name, "account_move", ['"%s"' % "old_invoice_id"]) openupgrade.logged_query( env.cr, """ UPDATE account_move am SET old_invoice_id = aml.invoice_id FROM account_move_line aml WHERE aml.move_id = am.id AND am.old_invoice_id IS NULL """, ) openupgrade.logged_query( env.cr, """ UPDATE account_invoice ai SET move_id = am.old_invoice_id FROM account_move am WHERE am.old_invoice_id = ai.id AND ai.move_id IS NULL """, ) openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line ADD COLUMN old_invoice_line_id integer""", ) openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line ADD COLUMN old_invoice_tax_id integer""", )
def check_indexes(self, cr, model_names): """ Create or drop column indexes for the given models. """ expected = [("%s_%s_index" % (Model._table, field.name), Model, field.name, field) for model_name in model_names for Model in [self.models[model_name]] if Model._auto and not Model._abstract for field in Model._fields.values() if field.column_type and field.store] if not expected: return trgm = sql.has_pg_trgm(cr) cr.execute("SELECT indexname FROM pg_indexes WHERE indexname IN %s", [tuple(row[0] for row in expected)]) existing = {row[0] for row in cr.fetchall()} for indexname, model, columnname, field in expected: tablename = model._table index = field.index assert index in ('btree', 'gin', 'not null', True, False) if field.index and indexname not in existing: where = '' method = 'btree' operator = '' if index == 'not null': where = ' WHERE "%s" IS NOT NULL' % columnname if index == 'gin': if trgm: operator = 'gin_trgm_ops' method = 'gin' else: method = "btree" try: with cr.savepoint(flush=False): sql.create_index(cr, indexname, tablename, ['"%s" %s' % (columnname, operator)], method, where) except psycopg2.OperationalError: _schema.error("Unable to add index for %s", self) elif not index and indexname in existing: _schema.info("Keep unexpected index %s on table %s", indexname, tablename)
def check_indexes(self, cr, model_names): """ Create or drop column indexes for the given models. """ expected = [(f"{Model._table}_{field.name}_index", Model._table, field.name, field.index) for model_name in model_names for Model in [self.models[model_name]] if Model._auto and not Model._abstract for field in Model._fields.values() if field.column_type and field.store] if not expected: return cr.execute("SELECT indexname FROM pg_indexes WHERE indexname IN %s", [tuple(row[0] for row in expected)]) existing = {row[0] for row in cr.fetchall()} if not self.has_trigram and any(row[3] == 'trigram' for row in expected): self.has_trigram = sql.install_pg_trgm(cr) for indexname, tablename, columnname, index in expected: assert index in ('btree', 'btree_not_null', 'trigram', True, False, None) if index and indexname not in existing: method = 'btree' operator = '' where = '' if index == 'btree_not_null': where = f'"{columnname}" IS NOT NULL' elif index == 'trigram' and self.has_trigram: method = 'gin' operator = 'gin_trgm_ops' try: with cr.savepoint(flush=False): expression = f'"{columnname}" {operator}' sql.create_index(cr, indexname, tablename, [expression], method, where) except psycopg2.OperationalError: _schema.error("Unable to add index for %s", self) elif not index and indexname in existing: _schema.info("Keep unexpected index %s on table %s", indexname, tablename)