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

Blogs

Check out the latest blogs from LambdaTest on this topic:

Scala Testing: A Comprehensive Guide

Before we discuss Scala testing, let us understand the fundamentals of Scala and how this programming language is a preferred choice for your development requirements.The popularity and usage of Scala are rapidly rising, evident by the ever-increasing open positions for Scala developers.

What Agile Testing (Actually) Is

So, now that the first installment of this two fold article has been published (hence you might have an idea of what Agile Testing is not in my opinion), I’ve started feeling the pressure to explain what Agile Testing actually means to me.

How To Choose The Right Mobile App Testing Tools

Did you know that according to Statista, the number of smartphone users will reach 18.22 billion by 2025? Let’s face it, digital transformation is skyrocketing and will continue to do so. This swamps the mobile app development market with various options and gives rise to the need for the best mobile app testing tools

A Complete Guide To CSS Houdini

As a developer, checking the cross browser compatibility of your CSS properties is of utmost importance when building your website. I have often found myself excited to use a CSS feature only to discover that it’s still not supported on all browsers. Even if it is supported, the feature might be experimental and not work consistently across all browsers. Ask any front-end developer about using a CSS feature whose support is still in the experimental phase in most prominent web browsers. ????

Appium Testing Tutorial For Mobile Applications

The count of mobile users is on a steep rise. According to the research, by 2025, it is expected to reach 7.49 billion users worldwide. 70% of all US digital media time comes from mobile apps, and to your surprise, the average smartphone owner uses ten apps per day and 30 apps each month.

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