diff --git a/addons/account/migrations/13.0.1.1/post-migration.py b/addons/account/migrations/13.0.1.1/post-migration.py index 3f2f2e4abb80..186a2597fe92 100644 --- a/addons/account/migrations/13.0.1.1/post-migration.py +++ b/addons/account/migrations/13.0.1.1/post-migration.py @@ -467,7 +467,7 @@ def compute_balance_for_draft_invoice_lines(env): def migration_voucher_moves(env): openupgrade.logged_query( env.cr, - "ALTER TABLE account_voucher ADD COLUMN " + "ALTER TABLE account_voucher ADD COLUMN IF NOT EXISTS " "IF NOT EXISTS message_main_attachment_id int4" ) openupgrade.logged_query( diff --git a/addons/account/migrations/13.0.1.1/pre-migration.py b/addons/account/migrations/13.0.1.1/pre-migration.py index e57abf7cdd66..0069460e4123 100644 --- a/addons/account/migrations/13.0.1.1/pre-migration.py +++ b/addons/account/migrations/13.0.1.1/pre-migration.py @@ -92,7 +92,7 @@ def create_account_invoice_amount_tax_company_signed(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_invoice - ADD COLUMN amount_tax_company_signed numeric""" + ADD COLUMN IF NOT EXISTS amount_tax_company_signed numeric""" ) openupgrade.logged_query( env.cr, """ @@ -150,7 +150,7 @@ def create_account_move_new_columns(env): openupgrade.logged_query( env.cr, """ ALTER TABLE {table} - ADD COLUMN {column} {column_type}""".format( + ADD COLUMN IF NOT EXISTS {column} {column_type}""".format( table=table, column=column, column_type=column_type ), ) @@ -161,8 +161,8 @@ def fill_account_move_line(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line - ADD COLUMN parent_state varchar, - ADD COLUMN account_internal_type varchar""", + ADD COLUMN IF NOT EXISTS parent_state varchar, + ADD COLUMN IF NOT EXISTS account_internal_type varchar""", ) openupgrade.logged_query( env.cr, """ @@ -183,7 +183,7 @@ def create_res_partner_ranks(env): openupgrade.logged_query( env.cr, """ ALTER TABLE res_partner - ADD COLUMN customer_rank integer + ADD COLUMN IF NOT EXISTS customer_rank integer DEFAULT 0""", ) openupgrade.logged_query( @@ -193,7 +193,7 @@ def create_res_partner_ranks(env): openupgrade.logged_query( env.cr, """ ALTER TABLE res_partner - ADD COLUMN supplier_rank integer + ADD COLUMN IF NOT EXISTS supplier_rank integer DEFAULT 0""", ) openupgrade.logged_query( @@ -248,7 +248,7 @@ def add_helper_invoice_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move - ADD COLUMN old_invoice_id integer""", + ADD COLUMN IF NOT EXISTS old_invoice_id integer""", ) openupgrade.logged_query( env.cr, """ @@ -277,12 +277,12 @@ def add_helper_invoice_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line - ADD COLUMN old_invoice_line_id integer""", + ADD COLUMN IF NOT EXISTS old_invoice_line_id integer""", ) openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line - ADD COLUMN old_invoice_tax_id integer""", + ADD COLUMN IF NOT EXISTS old_invoice_tax_id integer""", ) # Create index for these columns, as they are going to be accessed frequently for table, field in [ @@ -298,7 +298,7 @@ def add_helper_voucher_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move - ADD COLUMN old_voucher_id integer""", + ADD COLUMN IF NOT EXISTS old_voucher_id integer""", ) openupgrade.logged_query( env.cr, """ @@ -311,7 +311,7 @@ def add_helper_voucher_move_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE account_move_line - ADD COLUMN old_voucher_line_id integer""", + ADD COLUMN IF NOT EXISTS old_voucher_line_id integer""", ) diff --git a/addons/crm/models/crm_lead.py b/addons/crm/models/crm_lead.py index 6623fe5f4f7f..c9974fca347a 100644 --- a/addons/crm/models/crm_lead.py +++ b/addons/crm/models/crm_lead.py @@ -163,7 +163,7 @@ def _auto_init(self): @api.model def _read_group_stage_ids(self, stages, domain, order): # retrieve team_id from the context and write the domain - # - ('id', 'in', stages.ids): add columns that should be present + # - ('id', 'in', stages.ids): ADD COLUMN IF NOT EXISTSs that should be present # - OR ('fold', '=', False): add default columns that are not folded # - OR ('team_ids', '=', team_id), ('fold', '=', False) if team_id: add team columns that are not folded team_id = self._context.get('default_team_id') diff --git a/addons/crm_iap_lead/migrations/13.0.1.0/post-migration.py b/addons/crm_iap_lead/migrations/13.0.1.0/post-migration.py index 2abfe7924bec..54ca40a8b354 100644 --- a/addons/crm_iap_lead/migrations/13.0.1.0/post-migration.py +++ b/addons/crm_iap_lead/migrations/13.0.1.0/post-migration.py @@ -7,7 +7,7 @@ def fix_crm_reveal_rule_contact_filter_type(env): openupgrade.logged_query( env.cr, """ ALTER TABLE crm_reveal_rule - ADD COLUMN contact_filter_type varchar""", + ADD COLUMN IF NOT EXISTS contact_filter_type varchar""", ) openupgrade.logged_query( env.cr, """ diff --git a/addons/im_livechat/migrations/13.0.1.0/pre-migration.py b/addons/im_livechat/migrations/13.0.1.0/pre-migration.py index 65f299d742b6..9b8d5064a4aa 100644 --- a/addons/im_livechat/migrations/13.0.1.0/pre-migration.py +++ b/addons/im_livechat/migrations/13.0.1.0/pre-migration.py @@ -24,7 +24,7 @@ def fill_mail_channel_livechat_operator_id(env): openupgrade.logged_query( env.cr, """ ALTER TABLE mail_channel - ADD COLUMN livechat_operator_id integer""", + ADD COLUMN IF NOT EXISTS livechat_operator_id integer""", ) openupgrade.logged_query( env.cr, """ diff --git a/addons/mrp/__init__.py b/addons/mrp/__init__.py index 7287f1058179..efd6106b2684 100644 --- a/addons/mrp/__init__.py +++ b/addons/mrp/__init__.py @@ -16,9 +16,9 @@ def _pre_init_mrp(cr): - Also set the default value for unit_factor in the same UPDATE query to save some SQL constraint checks""" # Openupgrade: the following lines will generate an errors, when running # openupgrade_records. - # cr.execute("""ALTER TABLE "stock_move" ADD COLUMN "unit_factor" float;""") - # cr.execute("""ALTER TABLE "stock_move" ADD COLUMN "is_done" bool;""") - # cr.execute("""ALTER TABLE "stock_move_line" ADD COLUMN "done_move" bool;""") + # cr.execute("""ALTER TABLE "stock_move" ADD COLUMN IF NOT EXISTS "unit_factor" float;""") + # cr.execute("""ALTER TABLE "stock_move" ADD COLUMN IF NOT EXISTS "is_done" bool;""") + # cr.execute("""ALTER TABLE "stock_move_line" ADD COLUMN IF NOT EXISTS "done_move" bool;""") # cr.execute("""UPDATE stock_move # SET is_done=COALESCE(state in ('done', 'cancel'), FALSE), # unit_factor=1.0;""") diff --git a/addons/mrp/migrations/13.0.2.0/pre-migration.py b/addons/mrp/migrations/13.0.2.0/pre-migration.py index ac622d5f5def..7c15f7762b0f 100644 --- a/addons/mrp/migrations/13.0.2.0/pre-migration.py +++ b/addons/mrp/migrations/13.0.2.0/pre-migration.py @@ -43,12 +43,12 @@ def fast_precreation_and_fill_mrp_bom_byproduct(env): openupgrade.logged_query( env.cr, """ ALTER TABLE mrp_bom_byproduct - ADD COLUMN company_id integer""", + ADD COLUMN IF NOT EXISTS company_id integer""", ) openupgrade.logged_query( env.cr, """ ALTER TABLE mrp_bom_byproduct - ADD COLUMN routing_id integer""", + ADD COLUMN IF NOT EXISTS routing_id integer""", ) openupgrade.logged_query( env.cr, """ @@ -63,7 +63,7 @@ def fast_precreation_and_fill_mrp_bom_line(env): openupgrade.logged_query( env.cr, """ ALTER TABLE mrp_bom_line - ADD COLUMN company_id integer""", + ADD COLUMN IF NOT EXISTS company_id integer""", ) openupgrade.logged_query( env.cr, """ diff --git a/addons/point_of_sale/migrations/13.0.1.0.1/end-migration.py b/addons/point_of_sale/migrations/13.0.1.0.1/end-migration.py index 3168c6f5b522..e82c7fc1a3c5 100644 --- a/addons/point_of_sale/migrations/13.0.1.0.1/end-migration.py +++ b/addons/point_of_sale/migrations/13.0.1.0.1/end-migration.py @@ -11,7 +11,7 @@ def create_pos_payment_methods(env): openupgrade.logged_query( env.cr, """ ALTER TABLE pos_payment_method - ADD COLUMN old_journal_id int""" + ADD COLUMN IF NOT EXISTS old_journal_id int""" ) env.cr.execute(""" SELECT DISTINCT journal_id diff --git a/addons/product/migrations/13.0.1.2/pre-migration.py b/addons/product/migrations/13.0.1.2/pre-migration.py index 39b155067afb..95610f75da41 100644 --- a/addons/product/migrations/13.0.1.2/pre-migration.py +++ b/addons/product/migrations/13.0.1.2/pre-migration.py @@ -168,7 +168,7 @@ def calculate_product_product_combination_indices(env): openupgrade.logged_query( env.cr, """ ALTER TABLE product_product - ADD COLUMN combination_indices varchar""", + ADD COLUMN IF NOT EXISTS combination_indices varchar""", ) openupgrade.logged_query( env.cr, """ @@ -222,7 +222,7 @@ def fill_product_template_attribute_value_attribute_line_id(env): openupgrade.logged_query( env.cr, "ALTER TABLE product_template_attribute_value " - "ADD COLUMN attribute_line_id INT4", + "ADD COLUMN IF NOT EXISTS attribute_line_id INT4", ) openupgrade.logged_query( env.cr, """ @@ -244,7 +244,7 @@ def fill_product_template_attribute_value__attribute_id_related(env): openupgrade.logged_query( env.cr, "ALTER TABLE product_template_attribute_value " - "ADD COLUMN attribute_id INT4", + "ADD COLUMN IF NOT EXISTS attribute_id INT4", ) openupgrade.logged_query( env.cr, """ diff --git a/addons/purchase/migrations/13.0.1.2/pre-migration.py b/addons/purchase/migrations/13.0.1.2/pre-migration.py index 52f1bd143971..e7706c8a4b39 100644 --- a/addons/purchase/migrations/13.0.1.2/pre-migration.py +++ b/addons/purchase/migrations/13.0.1.2/pre-migration.py @@ -30,7 +30,7 @@ def migrate(env, version): openupgrade.logged_query( env.cr, """ ALTER TABLE purchase_order_line - ADD COLUMN qty_received_method varchar""", + ADD COLUMN IF NOT EXISTS qty_received_method varchar""", ) # Add temporary table for avoiding the automatic launch of the compute method openupgrade.logged_query( diff --git a/addons/stock_account/migrations/13.0.1.1/post-migration.py b/addons/stock_account/migrations/13.0.1.1/post-migration.py index e2990ca07b9a..0a42d9a949c9 100644 --- a/addons/stock_account/migrations/13.0.1.1/post-migration.py +++ b/addons/stock_account/migrations/13.0.1.1/post-migration.py @@ -186,7 +186,7 @@ def generate_stock_valuation_layer(env): openupgrade.logged_query( env.cr, """ ALTER TABLE stock_valuation_layer - ADD COLUMN old_product_price_history_id integer""", + ADD COLUMN IF NOT EXISTS old_product_price_history_id integer""", ) company_obj = env["res.company"] product_obj = env["product.product"] diff --git a/addons/survey/migrations/13.0.3.0/pre-migration.py b/addons/survey/migrations/13.0.3.0/pre-migration.py index 5a7671ede1eb..d1d9ed1dd244 100644 --- a/addons/survey/migrations/13.0.3.0/pre-migration.py +++ b/addons/survey/migrations/13.0.3.0/pre-migration.py @@ -61,7 +61,7 @@ def add_helper_survey_question_page_rel(env): openupgrade.logged_query( env.cr, """ ALTER TABLE survey_question - ADD COLUMN old_page_id integer""", + ADD COLUMN IF NOT EXISTS old_page_id integer""", ) @@ -70,7 +70,7 @@ def fill_survey_user_input_line_question_sequence(env): openupgrade.logged_query( env.cr, """ ALTER TABLE survey_user_input_line - ADD COLUMN question_sequence integer""", + ADD COLUMN IF NOT EXISTS question_sequence integer""", ) openupgrade.logged_query( env.cr, """ diff --git a/addons/website/migrations/13.0.1.0/pre-migration.py b/addons/website/migrations/13.0.1.0/pre-migration.py index bf82c664d4ed..55c58f4e0937 100644 --- a/addons/website/migrations/13.0.1.0/pre-migration.py +++ b/addons/website/migrations/13.0.1.0/pre-migration.py @@ -25,7 +25,7 @@ def fill_website_rewrite_name(cr): openupgrade.logged_query( cr, """ ALTER TABLE website_rewrite - ADD COLUMN name varchar""", + ADD COLUMN IF NOT EXISTS name varchar""", ) openupgrade.logged_query( cr, """ diff --git a/addons/website_slides/migrations/13.0.2.0/post-migration.py b/addons/website_slides/migrations/13.0.2.0/post-migration.py index 233007d5f9d3..4408fb83ab66 100644 --- a/addons/website_slides/migrations/13.0.2.0/post-migration.py +++ b/addons/website_slides/migrations/13.0.2.0/post-migration.py @@ -71,7 +71,7 @@ def fill_slide_sequence(env): def convert_slide_categories(env): openupgrade.logged_query( - env.cr, "ALTER TABLE slide_slide ADD COLUMN old_category_id int4" + env.cr, "ALTER TABLE slide_slide ADD COLUMN IF NOT EXISTS old_category_id int4" ) openupgrade.logged_query( env.cr, sql.SQL(""" diff --git a/addons/website_slides/migrations/13.0.2.0/pre-migration.py b/addons/website_slides/migrations/13.0.2.0/pre-migration.py index f0ad75b2f409..fe6da4247e88 100644 --- a/addons/website_slides/migrations/13.0.2.0/pre-migration.py +++ b/addons/website_slides/migrations/13.0.2.0/pre-migration.py @@ -41,7 +41,7 @@ def migrate(env, version): openupgrade.rename_columns(env.cr, _column_renames) openupgrade.copy_columns(env.cr, _column_copies) openupgrade.rename_fields(env, _field_renames) - openupgrade.logged_query(env.cr, "ALTER TABLE slide_slide ADD COLUMN category_id int4") + openupgrade.logged_query(env.cr, "ALTER TABLE slide_slide ADD COLUMN IF NOT EXISTS category_id int4") openupgrade.rename_tables(env.cr, [("rel_channel_groups", "res_groups_slide_channel_rel")]) openupgrade.set_xml_ids_noupdate_value( env, diff --git a/odoo/tools/sql.py b/odoo/tools/sql.py index 6ed90b7d3e3e..6485ff9883bc 100644 --- a/odoo/tools/sql.py +++ b/odoo/tools/sql.py @@ -77,7 +77,7 @@ def column_exists(cr, tablename, columnname): def create_column(cr, tablename, columnname, columntype, comment=None): """ Create a column with the given type. """ coldefault = (columntype.upper()=='BOOLEAN') and 'DEFAULT false' or '' - cr.execute('ALTER TABLE "{}" ADD COLUMN "{}" {} {}'.format(tablename, columnname, columntype, coldefault)) + cr.execute('ALTER TABLE "{}" ADD COLUMN IF NOT EXISTS "{}" {} {}'.format(tablename, columnname, columntype, coldefault)) if comment: cr.execute('COMMENT ON COLUMN "{}"."{}" IS %s'.format(tablename, columnname), (comment,)) _schema.debug("Table %r: added column %r of type %s", tablename, columnname, columntype) @@ -97,7 +97,7 @@ def convert_column(cr, tablename, columnname, columntype): # can't do inplace change -> use a casted temp column query = ''' ALTER TABLE "{0}" RENAME COLUMN "{1}" TO __temp_type_cast; - ALTER TABLE "{0}" ADD COLUMN "{1}" {2}; + ALTER TABLE "{0}" ADD COLUMN IF NOT EXISTS "{1}" {2}; UPDATE "{0}" SET "{1}"= __temp_type_cast::{2}; ALTER TABLE "{0}" DROP COLUMN __temp_type_cast CASCADE; '''