|
15 | 15 | from . import blob |
16 | 16 | from .utils import user_choice |
17 | 17 | from .heading import Heading |
18 | | -from .errors import DuplicateError, AccessError, DataJointError, UnknownAttributeError, IntegrityError |
| 18 | +from .errors import (DuplicateError, AccessError, DataJointError, UnknownAttributeError, |
| 19 | + IntegrityError) |
19 | 20 | from .version import __version__ as version |
20 | 21 |
|
21 | 22 | logger = logging.getLogger(__name__) |
22 | 23 |
|
23 | | -foreign_key_full_error_regexp = re.compile( |
| 24 | +foreign_key_error_regexp = re.compile( |
24 | 25 | r"[\w\s:]*\((?P<child>`[^`]+`.`[^`]+`), " |
25 | 26 | r"CONSTRAINT (?P<name>`[^`]+`) " |
26 | | - r"FOREIGN KEY \((?P<fk_attrs>[^)]+)\) " |
27 | | - r"REFERENCES (?P<parent>`[^`]+`(\.`[^`]+`)?) \((?P<pk_attrs>[^)]+)\)[\s\w]+\)") |
| 27 | + r"(FOREIGN KEY \((?P<fk_attrs>[^)]+)\) " |
| 28 | + r"REFERENCES (?P<parent>`[^`]+`(\.`[^`]+`)?) \((?P<pk_attrs>[^)]+)\)[\s\w]+\))?") |
28 | 29 |
|
29 | | -foreign_key_partial_error_regexp = re.compile( |
30 | | - r"[\w\s:]*\((?P<child>`[^`]+?`.`[^`]+?`), " |
31 | | - r"CONSTRAINT (?P<name>`[^`]+?`) ") |
| 30 | +constraint_info_query = ' '.join(""" |
| 31 | + SELECT |
| 32 | + COLUMN_NAME as fk_attrs, |
| 33 | + CONCAT('`', REFERENCED_TABLE_SCHEMA, '`.`', REFERENCED_TABLE_NAME, '`') as parent, |
| 34 | + REFERENCED_COLUMN_NAME as pk_attrs |
| 35 | + FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
| 36 | + WHERE |
| 37 | + CONSTRAINT_NAME = %s AND TABLE_SCHEMA = %s AND TABLE_NAME = %s; |
| 38 | + """.split()) |
32 | 39 |
|
33 | 40 |
|
34 | 41 | class _RenameMap(tuple): |
@@ -348,31 +355,19 @@ def _delete_cascade(self): |
348 | 355 | try: |
349 | 356 | delete_count += self.delete_quick(get_count=True) |
350 | 357 | except IntegrityError as error: |
351 | | - match = (foreign_key_full_error_regexp.match(error.args[0]) or |
352 | | - foreign_key_partial_error_regexp.match(error.args[0])).groupdict() |
| 358 | + match = foreign_key_error_regexp.match(error.args[0]).groupdict() |
353 | 359 | if "`.`" not in match['child']: # if schema name missing, use self |
354 | 360 | match['child'] = '{}.{}'.format(self.full_table_name.split(".")[0], |
355 | 361 | match['child']) |
356 | | - if 'pk_attrs' in match: # fullly matched, adjusting the keys |
| 362 | + if match['pk_attrs'] is not None: # fully matched, adjusting the keys |
357 | 363 | match['fk_attrs'] = [k.strip('`') for k in match['fk_attrs'].split(',')] |
358 | 364 | match['pk_attrs'] = [k.strip('`') for k in match['pk_attrs'].split(',')] |
359 | 365 | else: # only partially matched, querying with constraint to determine keys |
360 | 366 | match['fk_attrs'], match['parent'], match['pk_attrs'] = list(map( |
361 | | - list, zip(*self.connection.query( |
362 | | - """ |
363 | | - SELECT |
364 | | - COLUMN_NAME as fk_attrs, |
365 | | - CONCAT('`', REFERENCED_TABLE_SCHEMA, '`.`', |
366 | | - REFERENCED_TABLE_NAME, '`') as parent, |
367 | | - REFERENCED_COLUMN_NAME as pk_attrs |
368 | | - FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE |
369 | | - WHERE |
370 | | - CONSTRAINT_NAME = %s AND TABLE_SCHEMA = %s |
371 | | - AND TABLE_NAME = %s; |
372 | | - """, |
373 | | - args=(match['name'].strip('`'), |
374 | | - *[_.strip('`') for _ in match['child'].split('`.`')]) |
375 | | - ).fetchall()))) |
| 367 | + list, zip(*self.connection.query(constraint_info_query, args=( |
| 368 | + match['name'].strip('`'), |
| 369 | + *[_.strip('`') for _ in match['child'].split('`.`')] |
| 370 | + )).fetchall()))) |
376 | 371 | match['parent'] = match['parent'][0] |
377 | 372 | # restrict child by self if |
378 | 373 | # 1. if self's restriction attributes are not in child's primary key |
|
0 commit comments