Skip to content

Commit b8e98ef

Browse files
Merge error regex with optional tokens, remove unnecessary whitespace from query.
1 parent e567ca1 commit b8e98ef

File tree

3 files changed

+22
-27
lines changed

3 files changed

+22
-27
lines changed

LNX-docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ services:
3232
interval: 1s
3333
fakeservices.datajoint.io:
3434
<<: *net
35-
image: datajoint/nginx:v0.0.15
35+
image: datajoint/nginx:v0.0.16
3636
environment:
3737
- ADD_db_TYPE=DATABASE
3838
- ADD_db_ENDPOINT=db:3306

datajoint/table.py

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,27 @@
1515
from . import blob
1616
from .utils import user_choice
1717
from .heading import Heading
18-
from .errors import DuplicateError, AccessError, DataJointError, UnknownAttributeError, IntegrityError
18+
from .errors import (DuplicateError, AccessError, DataJointError, UnknownAttributeError,
19+
IntegrityError)
1920
from .version import __version__ as version
2021

2122
logger = logging.getLogger(__name__)
2223

23-
foreign_key_full_error_regexp = re.compile(
24+
foreign_key_error_regexp = re.compile(
2425
r"[\w\s:]*\((?P<child>`[^`]+`.`[^`]+`), "
2526
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]+\))?")
2829

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())
3239

3340

3441
class _RenameMap(tuple):
@@ -348,31 +355,19 @@ def _delete_cascade(self):
348355
try:
349356
delete_count += self.delete_quick(get_count=True)
350357
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()
353359
if "`.`" not in match['child']: # if schema name missing, use self
354360
match['child'] = '{}.{}'.format(self.full_table_name.split(".")[0],
355361
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
357363
match['fk_attrs'] = [k.strip('`') for k in match['fk_attrs'].split(',')]
358364
match['pk_attrs'] = [k.strip('`') for k in match['pk_attrs'].split(',')]
359365
else: # only partially matched, querying with constraint to determine keys
360366
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())))
376371
match['parent'] = match['parent'][0]
377372
# restrict child by self if
378373
# 1. if self's restriction attributes are not in child's primary key

local-docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ services:
3434
interval: 1s
3535
fakeservices.datajoint.io:
3636
<<: *net
37-
image: datajoint/nginx:v0.0.15
37+
image: datajoint/nginx:v0.0.16
3838
environment:
3939
- ADD_db_TYPE=DATABASE
4040
- ADD_db_ENDPOINT=db:3306

0 commit comments

Comments
 (0)