"""Delete foreign keys from tables.""" import os import mysql.connector cnx = mysql.connector.connect( host=os.environ.get('MYSQL_DB_HOST', '127.0.0.1'), port=os.environ.get('MYSQL_DB_PORT', '6050'), database=os.environ.get('MYSQL_DB_NAME', 'royalty_accounting'), user=os.environ.get('MYSQL_DB_USER', 'royalties'), password=os.environ.get('MYSQL_DB_PASS', '1234'), ) foreign_keys_to_drop = [ ('accounting_run', 'fk_acc_run_run_ctrl'), ('accounting_run', 'fk_acct_run_acct_period'), ('run_controller_contract', 'fk_run_controller_contract_contract'), ('run_controller_contract', 'fk_run_controller_contract_run_controller'), ('sales_file', 'fk_sales_file_acct_period'), ('statement_attachment', 'fk_statement_attachment_statement_period'), ('abacus_state_history', 'fk_abacus_state_history'), ('contract_history', 'FK_contract_history_contract'), ('account_contract', 'fk_account_contract_contract_id'), ('contract_advance', 'fk_contract_contract_advance'), ('ledger_contract_advance_applied', 'fk_contract_contract_advance_applied'), ('ledger_contract_advance_applied', 'fk_statement_period_contract_advance_applied'), ('contract_advance_history', 'fk_contract_contract_advance_history'), ('contract_exclusion', 'fk_contract_exclusion_contract'), ('ledger_accounting_run_balance', 'fk_contract_ledger'), ('ledger_deposit', 'fk_contract_ledger_deposit'), ('contract_party', 'fk_contract_party_contract'), ('contract_party_history', 'fk_contract_party_history_contract'), ('contract_reserve', 'fk_contract_reserve_contract_id'), ('contract_term', 'fk_contract_term_contract'), ('worksheet_correction', 'fk_contract_worksheet_correction'), ('ledger_account', 'fk_ledger_account_contract'), ('ledger_account_contract', 'fk_ledger_account_contract_contract'), ( 'ledger_account_contract_current_balance', 'fk_ledger_account_contract_current_balance_contract', ), ('ledger_contract_flowthrough', 'fk_ledger_contract_flowthrough_contract'), ('ledger_contract_flowthrough_current_balance', 'fk_lcf_current_balance_contract'), ('ledger_accounting_run_vat', 'fk_ledger_accounting_run_vat_contract'), ('ledger_adjustment_applied', 'fk_ledger_adjustment_applied_contract'), ('ledger_adjustment', 'fk_ledger_adjustment_contract'), ('ledger_adjustment_detail', 'fk_ledger_adjustment_detail_contract'), ('ledger_correction', 'fk_ledger_correction_contract'), ('ledger_vat_summary', 'fk_ledger_vat_summary_contract'), ('legacy_contract', 'fk_legacy_contract_contract'), ('account_contract_vat_detail', 'fk_vat_detail_contract_id'), ('worksheet_adjustment', 'fk_worksheet_adjustment_contract'), ('worksheet_adjustment_detail', 'fk_worksheet_adjustment_detail_contract'), ('account_payment_term_history', 'fk_account_payment_term_history_payment_term_id'), ( 'worksheet_adjustment_detail', 'fk_worksheet_adjustment_detail_statement_period_adjustment_file', ), ( 'worksheet_adjustment', 'fk_worksheet_adjustment_statement_period_adjustment_file', ), ('worksheet_account_contract_closing_balance', 'fk_waccb_contract'), ('worksheet_account_contract_payable_after_tax', 'fk_wacpat_contract'), ('worksheet_account_contract_payable_after_tax', 'fk_wacpat_account'), ('payment_group_payment_account_detail', 'fk_pgpad_contract'), ('statement_period_payment_entity', 'fk_statement_period_reference_payment_entity'), ('worksheet_account_contract_payable_details', 'fk_wacpd_contract'), ('contract_lifecycle_schedule', 'fk_contract_id'), ('contract_lifecycle', 'fk_contract_lifecycle_contract'), ('worksheet_tax_correction', 'fk_tc_contract'), ('worksheet_account_contract_taxable_revenue', 'fk_wactr_contract'), ('worksheet_tax_correction_vat', 'fk_tcv_contract'), ('contract_mechanical_deduction', 'fk_contract_mechanical_deduction_contract'), ('contract_flowthrough', 'fk_contract_flowthrough_contract'), ('reference_signing_entity', 'fk_reference_sap_profit_center_signing_entity'), ('contract', 'fk_contract_reference_sap_profit_center'), ('contract_history', 'fk_contract_history_reference_sap_profit_center'), ('signing_entity_sap_profit_center', 'fk_sespc_signing_entity'), ('signing_entity_sap_profit_center', 'fk_sespc_sap_profit_center'), ('abacus_event', 'fk_abacus_event_statement_period'), ( 'ledger_adjustment_detail_applied', 'fk_ledger_adjustment_detail_worksheet_adjustment', ), ('ledger_adjustment_applied', 'fk_ledger_adjustment_applied_worksheet_adjustment'), ( 'worksheet_adjustment_detail', 'fk_worksheet_adjustment_detail_worksheet_adjustment', ), ('account_payment_term', 'fk_account_payment_term_payment_entity'), ('payment_entity_payoneer_program', 'fk_payoneer_program_reference_payment_entity'), ('account_payment_term_history', 'fk_account_payment_term_history_payment_entity'), ('worksheet_account_contract_closing_balance', 'fk_waccb_reference_payment_entity'), ('worksheet_account_contract_taxable_revenue', 'fk_wactr_ref_payment_entity'), ('reference_signing_entity', 'fk_reference_signing_entity_payment_entity'), ('worksheet_payment_custom', 'fk_worksheet_payment_custom_contract'), ('worksheet_flowthrough', 'fk_worksheet_flowthrough_contract'), ('earnings_transfer', 'fk_earnings_transfer_from_contract'), ('earnings_transfer', 'fk_earnings_transfer_to_contract'), ('file_upload', 'fk_file_upload_config'), ('payment_allocation', 'fk_payment_allocation_contract'), ] cursor = cnx.cursor() for table, fk in foreign_keys_to_drop: sql = f'ALTER TABLE `{table}` DROP FOREIGN KEY `{fk}`;' print(f'Executing: {sql}') try: cursor.execute(sql) except mysql.connector.errors.ProgrammingError as e: # The integration test DB may persist across runs, so previously-dropped FKs raise # "Can't DROP ..." (errno 1091) on subsequent invocations. Treat as no-op so the # script is idempotent. if e.errno == 1091: print(f' skipped: {fk} already absent on {table}') else: raise cnx.commit() cursor.close() cnx.close()