Skip to content

Commit 219a939

Browse files
Merge pull request #822 from datajoint/master
Bringing Release013 in sync with master
2 parents 2baaed0 + 0dfa9b0 commit 219a939

File tree

10 files changed

+50
-46
lines changed

10 files changed

+50
-46
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ docker-compose.yml
2323
notebook
2424
.vscode
2525
__main__.py
26-
jupyter_custom.js
26+
jupyter_custom.js
27+
apk_requirements.txt

LNX-docker-compose.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# docker-compose -f LNX-docker-compose.yml --env-file LNX.env up --build --exit-code-from app
12
version: '2.2'
23
x-net: &net
34
networks:
@@ -31,7 +32,7 @@ services:
3132
interval: 1s
3233
fakeservices.datajoint.io:
3334
<<: *net
34-
image: raphaelguzman/nginx:v0.0.6
35+
image: raphaelguzman/nginx:v0.0.10
3536
environment:
3637
- ADD_db_TYPE=DATABASE
3738
- ADD_db_ENDPOINT=db:3306

datajoint/admin.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ def kill(restriction=None, connection=None, order_by=None): # pragma: no cover
4949
while True:
5050
print(' ID USER HOST STATE TIME INFO')
5151
print('+--+ +----------+ +-----------+ +-----------+ +-----+')
52-
cur = connection.query(query, as_dict=True)
52+
cur = ({k.lower(): v for k, v in elem.items()}
53+
for elem in connection.query(query, as_dict=True))
5354
for process in cur:
5455
try:
55-
print('{ID:>4d} {USER:<12s} {HOST:<12s} {STATE:<12s} {TIME:>7d} {INFO}'.format(**process))
56+
print('{id:>4d} {user:<12s} {host:<12s} {state:<12s} {time:>7d} {info}'.format(**process))
5657
except TypeError:
5758
print(process)
5859
response = input('process to kill or "q" to quit > ')
@@ -88,9 +89,10 @@ def kill_quick(restriction=None, connection=None):
8889
query = 'SELECT * FROM information_schema.processlist WHERE id <> CONNECTION_ID()' + (
8990
"" if restriction is None else ' AND (%s)' % restriction)
9091

91-
cur = connection.query(query, as_dict=True)
92+
cur = ({k.lower(): v for k, v in elem.items()}
93+
for elem in connection.query(query, as_dict=True))
9294
nkill = 0
9395
for process in cur:
94-
connection.query('kill %d' % process['ID'])
96+
connection.query('kill %d' % process['id'])
9597
nkill += 1
9698
return nkill

datajoint/connection.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
from . import errors
1313
from .dependencies import Dependencies
1414

15-
# client errors to catch
16-
client_errors = (client.err.InterfaceError, client.err.DatabaseError)
15+
logger = logging.getLogger(__name__)
16+
query_log_max_length = 300
1717

1818

1919
def translate_query_error(client_error, query):
@@ -23,39 +23,39 @@ def translate_query_error(client_error, query):
2323
:param query: sql query with placeholders
2424
:return: an instance of the corresponding subclass of datajoint.errors.DataJointError
2525
"""
26+
logger.debug('type: {}, args: {}'.format(type(client_error), client_error.args))
27+
28+
err, *args = client_error.args
29+
2630
# Loss of connection errors
27-
if isinstance(client_error, client.err.InterfaceError) and client_error.args[0] == "(0, '')":
28-
return errors.LostConnectionError('Server connection lost due to an interface error.', *client_error.args[1:])
29-
disconnect_codes = {
30-
2006: "Connection timed out",
31-
2013: "Server connection lost"}
32-
if isinstance(client_error, client.err.OperationalError) and client_error.args[0] in disconnect_codes:
33-
return errors.LostConnectionError(disconnect_codes[client_error.args[0]], *client_error.args[1:])
31+
if err in (0, "(0, '')"):
32+
return errors.LostConnectionError('Server connection lost due to an interface error.', *args)
33+
if err == 2006:
34+
return errors.LostConnectionError("Connection timed out", *args)
35+
if err == 2013:
36+
return errors.LostConnectionError("Server connection lost", *args)
3437
# Access errors
35-
if isinstance(client_error, client.err.OperationalError) and client_error.args[0] in (1044, 1142):
36-
return errors.AccessError('Insufficient privileges.', client_error.args[1], query)
38+
if err in (1044, 1142):
39+
return errors.AccessError('Insufficient privileges.', args[0], query)
3740
# Integrity errors
38-
if isinstance(client_error, client.err.IntegrityError) and client_error.args[0] == 1062:
39-
return errors.DuplicateError(*client_error.args[1:])
40-
if isinstance(client_error, client.err.IntegrityError) and client_error.args[0] == 1452:
41-
return errors.IntegrityError(*client_error.args[1:])
41+
if err == 1062:
42+
return errors.DuplicateError(*args)
43+
if err == 1452:
44+
return errors.IntegrityError(*args)
4245
# Syntax errors
43-
if isinstance(client_error, client.err.ProgrammingError) and client_error.args[0] == 1064:
44-
return errors.QuerySyntaxError(client_error.args[1], query)
46+
if err == 1064:
47+
return errors.QuerySyntaxError(args[0], query)
4548
# Existence errors
46-
if isinstance(client_error, client.err.ProgrammingError) and client_error.args[0] == 1146:
47-
return errors.MissingTableError(client_error.args[1], query)
48-
if isinstance(client_error, client.err.InternalError) and client_error.args[0] == 1364:
49-
return errors.MissingAttributeError(*client_error.args[1:])
50-
if isinstance(client_error, client.err.InternalError) and client_error.args[0] == 1054:
51-
return errors.UnknownAttributeError(*client_error.args[1:])
49+
if err == 1146:
50+
return errors.MissingTableError(args[0], query)
51+
if err == 1364:
52+
return errors.MissingAttributeError(*args)
53+
if err == 1054:
54+
return errors.UnknownAttributeError(*args)
5255
# all the other errors are re-raised in original form
5356
return client_error
5457

5558

56-
logger = logging.getLogger(__name__)
57-
58-
5959
def conn(host=None, user=None, password=None, *, init_fun=None, reset=False, use_tls=None):
6060
"""
6161
Returns a persistent connection object to be shared by multiple modules.
@@ -192,7 +192,7 @@ def _execute_query(cursor, query, args, cursor_class, suppress_warnings):
192192
# suppress all warnings arising from underlying SQL library
193193
warnings.simplefilter("ignore")
194194
cursor.execute(query, args)
195-
except client_errors as err:
195+
except client.err.Error as err:
196196
raise translate_query_error(err, query) from None
197197

198198
def query(self, query, args=(), *, as_dict=False, suppress_warnings=True, reconnect=None):
@@ -207,7 +207,7 @@ def query(self, query, args=(), *, as_dict=False, suppress_warnings=True, reconn
207207
"""
208208
if reconnect is None:
209209
reconnect = config['database.reconnect']
210-
logger.debug("Executing SQL:" + query[0:300])
210+
logger.debug("Executing SQL:" + query[:query_log_max_length])
211211
cursor_class = client.cursors.DictCursor if as_dict else client.cursors.Cursor
212212
cursor = self._conn.cursor(cursor=cursor_class)
213213
try:

datajoint/dependencies.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,15 @@ def load(self):
4343
self.add_node(n, primary_key=pk)
4444

4545
# load foreign keys
46-
keys = self._conn.query("""
46+
keys = ({k.lower(): v for k, v in elem.items()} for elem in self._conn.query("""
4747
SELECT constraint_name,
4848
concat('`', table_schema, '`.`', table_name, '`') as referencing_table,
4949
concat('`', referenced_table_schema, '`.`', referenced_table_name, '`') as referenced_table,
5050
column_name, referenced_column_name
5151
FROM information_schema.key_column_usage
5252
WHERE referenced_table_name NOT LIKE "~%%" AND (referenced_table_schema in ('{schemas}') OR
5353
referenced_table_schema is not NULL AND table_schema in ('{schemas}'))
54-
""".format(schemas="','".join(self._conn.schemas)), as_dict=True)
54+
""".format(schemas="','".join(self._conn.schemas)), as_dict=True))
5555
fks = defaultdict(lambda: dict(attr_map=OrderedDict()))
5656
for key in keys:
5757
d = fks[(key['constraint_name'], key['referencing_table'], key['referenced_table'])]

datajoint/external.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,11 +276,11 @@ def references(self):
276276
"""
277277
:return: generator of referencing table names and their referencing columns
278278
"""
279-
return self.connection.query("""
279+
return ({k.lower(): v for k, v in elem.items()} for elem in self.connection.query("""
280280
SELECT concat('`', table_schema, '`.`', table_name, '`') as referencing_table, column_name
281281
FROM information_schema.key_column_usage
282282
WHERE referenced_table_name="{tab}" and referenced_table_schema="{db}"
283-
""".format(tab=self.table_name, db=self.database), as_dict=True)
283+
""".format(tab=self.table_name, db=self.database), as_dict=True))
284284

285285
def fetch_external_paths(self, **fetch_kwargs):
286286
"""

datajoint/migrate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,14 @@ def _migrate_dj011_blob(schema, default_store):
4242
'`{db}`.`~external`'.format(db=schema.database))
4343

4444
# get referencing tables
45-
refs = query("""
45+
refs = [{k.lower(): v for k, v in elem.items()} for elem in query("""
4646
SELECT concat('`', table_schema, '`.`', table_name, '`')
4747
as referencing_table, column_name, constraint_name
4848
FROM information_schema.key_column_usage
4949
WHERE referenced_table_name="{tab}" and referenced_table_schema="{db}"
5050
""".format(
5151
tab=legacy_external.table_name,
52-
db=legacy_external.database), as_dict=True).fetchall()
52+
db=legacy_external.database), as_dict=True).fetchall()]
5353

5454
for ref in refs:
5555
# get comment
@@ -142,14 +142,14 @@ def _migrate_dj011_blob(schema, default_store):
142142

143143
# Drop the old external table but make sure it's no longer referenced
144144
# get referencing tables
145-
refs = query("""
145+
refs = [{k.lower(): v for k, v in elem.items()} for elem in query("""
146146
SELECT concat('`', table_schema, '`.`', table_name, '`') as
147147
referencing_table, column_name, constraint_name
148148
FROM information_schema.key_column_usage
149149
WHERE referenced_table_name="{tab}" and referenced_table_schema="{db}"
150150
""".format(
151151
tab=legacy_external.table_name,
152-
db=legacy_external.database), as_dict=True).fetchall()
152+
db=legacy_external.database), as_dict=True).fetchall()]
153153

154154
assert not refs, 'Some references still exist'
155155

datajoint/schemas.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ def __init__(self, schema_name, context=None, *, connection=None, create_schema=
7373
logger.info("Creating schema `{name}`.".format(name=schema_name))
7474
try:
7575
connection.query("CREATE DATABASE `{name}`".format(name=schema_name))
76-
logger.info('Creating schema `{name}`.'.format(name=schema_name))
7776
except pymysql.OperationalError:
7877
raise DataJointError(
7978
"Schema `{name}` does not exist and could not be created. "

local-docker-compose.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
# docker-compose -f local-docker-compose.yml --env-file LNX.env up --build
12
version: '2.2'
23
x-net: &net
34
networks:
@@ -33,7 +34,7 @@ services:
3334
interval: 1s
3435
fakeservices.datajoint.io:
3536
<<: *net
36-
image: raphaelguzman/nginx:v0.0.6
37+
image: raphaelguzman/nginx:v0.0.10
3738
environment:
3839
- ADD_db_TYPE=DATABASE
3940
- ADD_db_ENDPOINT=db:3306
@@ -106,4 +107,4 @@ services:
106107
# - ./notebook:/home/dja/notebooks
107108
# - ../dj-python-101/ch1:/home/dja/tutorials
108109
networks:
109-
main:
110+
main:

tests/test_schema.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def test_unauthorized_database():
6868
"""
6969
an attempt to create a database to which user has no privileges should raise an informative exception.
7070
"""
71-
dj.Schema('unauthorized_schema', connection=dj.conn(**CONN_INFO))
71+
dj.Schema('unauthorized_schema', connection=dj.conn(reset=True, **CONN_INFO))
7272

7373

7474
def test_drop_database():

0 commit comments

Comments
 (0)