Skip to content

Commit 501d525

Browse files
committed
Apply suggestions from code review
1 parent f39a78b commit 501d525

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

datajoint/user_tables.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
Hosts the table tiers, user relations should be derived from.
33
"""
44

5-
import inspect
65
from .table import Table
76
from .autopopulate import AutoPopulate
87
from .utils import from_camel_case, ClassProperty
@@ -189,14 +188,5 @@ def drop(self, force=False):
189188
raise DataJointError('Cannot drop a Part directly. Delete from master instead')
190189

191190
def alter(self, prompt=True, context=None):
192-
"""
193-
Alter the table definition from self.definition
194-
"""
195-
# map "master" keyword to master table in context
196-
if context is None:
197-
frame = inspect.currentframe().f_back
198-
context = dict(frame.f_globals, **frame.f_locals)
199-
del frame
200-
if self.master:
201-
context['master'] = self.master
202-
super().alter(prompt, context)
191+
# when there is no context, map "master" keyword to master table
192+
super().alter(prompt=prompt, context=context or self.declaration_context)

docs-parts/intro/Releases_lang1.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
* Bugfix - Fix assertion error when performing a union into a join (#930) PR #967
1616
* Bugfix - Fix regression issue with `DISTINCT` clause and `GROUP_BY` (#914) PR #963
1717
* Update `~jobs.error_stack` from blob to mediumblob to allow error stacks >64kB in jobs (#984) PR #986
18-
* Bugfix - Fix error when altering a part table that uses the "master" keyword (#936) PR #991
1918

2019
0.13.2 -- May 7, 2021
2120
----------------------

tests/test_alter.py

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from nose.tools import assert_equal, assert_not_equal
2+
import re
23
from .schema import *
34

45

@@ -28,6 +29,33 @@ class Experiment(dj.Imported):
2829
"""
2930

3031

32+
@schema
33+
class Parent(dj.Manual):
34+
definition = """
35+
parent_id: int
36+
"""
37+
38+
class Child(dj.Part):
39+
definition = """
40+
-> Parent
41+
"""
42+
definition_new = """
43+
-> master
44+
---
45+
child_id=null: int
46+
"""
47+
48+
class Grandchild(dj.Part):
49+
definition = """
50+
-> master.Child
51+
"""
52+
definition_new = """
53+
-> master.Child
54+
---
55+
grandchild_id=null: int
56+
"""
57+
58+
3159
def test_alter():
3260
original = schema.connection.query("SHOW CREATE TABLE " + Experiment.full_table_name).fetchone()[1]
3361
Experiment.definition = Experiment.definition1
@@ -41,24 +69,23 @@ def test_alter():
4169
assert_equal(original, restored)
4270

4371

44-
@schema
45-
class AlterMaster(dj.Manual):
46-
definition = """
47-
master_id : int
48-
"""
72+
def test_alter_part():
73+
# https://github.com/datajoint/datajoint-python/issues/936
4974

50-
class AlterPart(dj.Part):
51-
definition = """
52-
-> master
53-
"""
54-
55-
definition1 = """
56-
-> AlterMaster
57-
"""
75+
def verify_alter(table, attribute_sql):
76+
definition_original = schema.connection.query(
77+
f"SHOW CREATE TABLE {table.full_table_name}"
78+
).fetchone()[1]
79+
table.definition = table.definition_new
80+
table.alter(prompt=False)
81+
definition_new = schema.connection.query(
82+
f"SHOW CREATE TABLE {table.full_table_name}"
83+
).fetchone()[1]
84+
assert (
85+
re.sub(f"{attribute_sql},\n ", "", definition_new) == definition_original
86+
)
5887

59-
def test_alter_part_master_keyword():
60-
original = schema.connection.query("SHOW CREATE TABLE " + AlterMaster.AlterPart.full_table_name).fetchone()[1]
61-
AlterMaster.AlterPart.definition = AlterMaster.AlterPart.definition1
62-
AlterMaster.AlterPart.alter(prompt=False)
63-
altered = schema.connection.query("SHOW CREATE TABLE " + AlterMaster.AlterPart.full_table_name).fetchone()[1]
64-
assert_equal(original, altered)
88+
verify_alter(table=Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL")
89+
verify_alter(
90+
table=Parent.Grandchild, attribute_sql="`grandchild_id` .* DEFAULT NULL"
91+
)

0 commit comments

Comments
 (0)