How to use delete_invalid_foriegn_keys method in autotest

Best Python code snippet using autotest_python

037_db_constraints.py

Source:037_db_constraints.py Github

copy

Full Screen

...14 (table, first_id, second_id),15 first_id_value, second_id_value)16 if rows:17 print 'Deleted %s duplicate rows from %s' % (len(rows), table)18def delete_invalid_foriegn_keys(manager, pivot_table, foreign_key_field,19 destination_table):20 manager.execute(21 'DELETE %(table)s.* FROM %(table)s '22 'LEFT JOIN %(destination_table)s '23 'ON %(table)s.%(field)s = %(destination_table)s.id '24 'WHERE %(destination_table)s.id IS NULL' %25 dict(table=pivot_table, field=foreign_key_field,26 destination_table=destination_table))27 deleted_count = manager._database.rowcount28 if deleted_count:29 print ('Deleted %s invalid foreign key references from %s (%s)' %30 (deleted_count, pivot_table, foreign_key_field))31def unique_index_name(table):32 return table + '_both_ids'33def basic_index_name(table, field):34 if field == 'aclgroup_id':35 field = 'acl_group_id'36 return table + '_' + field37def create_unique_index(manager, pivot_table, first_field, second_field):38 index_name = unique_index_name(pivot_table)39 manager.execute('CREATE UNIQUE INDEX %s ON %s (%s, %s)' %40 (index_name, pivot_table, first_field, second_field))41 # these indices are in the migrations but may not exist for historical42 # reasons43 old_index_name = basic_index_name(pivot_table, first_field)44 execute_safely(manager, 'DROP INDEX %s ON %s' %45 (old_index_name, pivot_table))46def drop_unique_index(manager, pivot_table, first_field):47 index_name = unique_index_name(pivot_table)48 manager.execute('DROP INDEX %s ON %s' % (index_name, pivot_table))49 old_index_name = basic_index_name(pivot_table, first_field)50 manager.execute('CREATE INDEX %s ON %s (%s)' %51 (old_index_name, pivot_table, first_field))52def foreign_key_name(table, field):53 return '_'.join([table, field, 'fk'])54def create_foreign_key_constraint(manager, table, field, destination_table):55 key_name = foreign_key_name(table, field)56 manager.execute('ALTER TABLE %s ADD CONSTRAINT %s FOREIGN KEY (%s) '57 'REFERENCES %s (id) ON DELETE NO ACTION' %58 (table, key_name, field, destination_table))59def drop_foreign_key_constraint(manager, table, field):60 key_name = foreign_key_name(table, field)61 manager.execute('ALTER TABLE %s DROP FOREIGN KEY %s' % (table, key_name))62def cleanup_m2m_pivot(manager, pivot_table, first_field, first_table,63 second_field, second_table, create_unique):64 delete_duplicates(manager, pivot_table, first_field, second_field)65 delete_invalid_foriegn_keys(manager, pivot_table, first_field, first_table)66 delete_invalid_foriegn_keys(manager, pivot_table, second_field,67 second_table)68 if create_unique:69 # first field is the more commonly used one, so we'll replace the70 # less-commonly-used index with the larger unique index71 create_unique_index(manager, pivot_table, second_field, first_field)72 create_foreign_key_constraint(manager, pivot_table, first_field,73 first_table)74 create_foreign_key_constraint(manager, pivot_table, second_field,75 second_table)76def reverse_cleanup_m2m_pivot(manager, pivot_table, first_field, second_field,77 drop_unique):78 drop_foreign_key_constraint(manager, pivot_table, second_field)79 drop_foreign_key_constraint(manager, pivot_table, first_field)80 if drop_unique:...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful