Skip to content

Commit 4142515

Browse files
Merge pull request #12 from A-Baji/fix_renaming_attributes
Fix renaming attributes
2 parents b9d3334 + cec702f commit 4142515

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
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.18
35+
image: datajoint/nginx:v0.0.19
3636
environment:
3737
- ADD_db_TYPE=DATABASE
3838
- ADD_db_ENDPOINT=db:3306

datajoint/declare.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
UUID_DATA_TYPE = 'binary(16)'
1313
MAX_TABLE_NAME_LENGTH = 64
14-
CONSTANT_LITERALS = {'CURRENT_TIMESTAMP'} # SQL literals to be used without quotes (case insensitive)
14+
CONSTANT_LITERALS = {'CURRENT_TIMESTAMP', 'NULL'} # SQL literals to be used without quotes (case insensitive)
1515
EXTERNAL_TABLE_ROOT = '~external'
1616

1717
TYPE_PATTERN = {k: re.compile(v, re.I) for k, v in dict(

datajoint/expression.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from .preview import preview, repr_html
1010
from .condition import AndList, Not, \
1111
make_condition, assert_join_compatibility, extract_column_names, PromiscuousOperand
12+
from .declare import CONSTANT_LITERALS
1213

1314
logger = logging.getLogger(__name__)
1415

@@ -313,9 +314,9 @@ def proj(self, *attributes, **named_attributes):
313314
Each attribute name can only be used once.
314315
"""
315316
# new attributes in parentheses are included again with the new name without removing original
316-
duplication_pattern = re.compile(r'\s*\(\s*(?P<name>\w+)\s*\)\s*$')
317+
duplication_pattern = re.compile(fr'^\s*\(\s*(?!{"|".join(CONSTANT_LITERALS)})(?P<name>[a-zA-Z_]\w*)\s*\)\s*$')
317318
# attributes without parentheses renamed
318-
rename_pattern = re.compile(r'\s*(?P<name>\w+)\s*$')
319+
rename_pattern = re.compile(fr'^\s*(?!{"|".join(CONSTANT_LITERALS)})(?P<name>[a-zA-Z_]\w*)\s*$')
319320
replicate_map = {k: m.group('name')
320321
for k, m in ((k, duplication_pattern.match(v)) for k, v in named_attributes.items()) if m}
321322
rename_map = {k: m.group('name')

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.18
37+
image: datajoint/nginx:v0.0.19
3838
environment:
3939
- ADD_db_TYPE=DATABASE
4040
- ADD_db_ENDPOINT=db:3306

tests/test_relational_operand.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
from .schema import (Experiment, TTest3, Trial, Ephys, Child, Parent, SubjectA, SessionA,
1414
SessionStatusA, SessionDateA)
1515

16+
from . import PREFIX, CONN_INFO
17+
1618

1719
def setup():
1820
"""
@@ -177,6 +179,15 @@ def test_project():
177179
assert_equal(len((D() & cond).proj()), len((D() & cond)),
178180
'projection failed: altered its argument''s cardinality')
179181

182+
@staticmethod
183+
def test_rename_non_dj_attribute():
184+
schema = PREFIX + '_test1'
185+
connection = dj.conn(**CONN_INFO)
186+
connection.query(f'CREATE TABLE {schema}.test_table (oldID int PRIMARY KEY)').fetchall()
187+
mySchema = dj.VirtualModule(schema, schema)
188+
assert 'oldID' not in mySchema.TestTable.proj(new_name='oldID').heading.attributes.keys(), 'Failed to rename attribute correctly'
189+
connection.query(f'DROP TABLE {schema}.test_table')
190+
180191
@staticmethod
181192
def test_union():
182193
x = set(zip(*IJ.fetch('i', 'j')))

0 commit comments

Comments
 (0)