Skip to content

Commit 3320e22

Browse files
Merge pull request #991 from rly/fix/alter_part
Fix error when altering Part table that uses "master" keyword
2 parents 4360b17 + 1d4e2b6 commit 3320e22

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
## Release notes
22

3+
### Upcoming
4+
- Fixed - Fix altering a part table that uses the "master" keyword - PR [#991](https://github.com/datajoint/datajoint-python/pull/991)
5+
36
### 0.14.0 -- Feb 13, 2023
47
- Added - `json` data type ([#245](https://github.com/datajoint/datajoint-python/issues/245)) PR [#1051](https://github.com/datajoint/datajoint-python/pull/1051)
58
- Fixed - Activating a schema requires all tables to exist even if `create_tables=False` PR [#1058](https://github.com/datajoint/datajoint-python/pull/1058)

datajoint/user_tables.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,3 +238,7 @@ def drop(self, force=False):
238238
raise DataJointError(
239239
"Cannot drop a Part directly. Delete from master instead"
240240
)
241+
242+
def alter(self, prompt=True, context=None):
243+
# without context, use declaration context which maps master keyword to master table
244+
super().alter(prompt=prompt, context=context or self.declaration_context)

tests/test_alter.py

Lines changed: 50 additions & 0 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

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

2930

31+
@schema
32+
class Parent(dj.Manual):
33+
definition = """
34+
parent_id: int
35+
"""
36+
37+
class Child(dj.Part):
38+
definition = """
39+
-> Parent
40+
"""
41+
definition_new = """
42+
-> master
43+
---
44+
child_id=null: int
45+
"""
46+
47+
class Grandchild(dj.Part):
48+
definition = """
49+
-> master.Child
50+
"""
51+
definition_new = """
52+
-> master.Child
53+
---
54+
grandchild_id=null: int
55+
"""
56+
57+
3058
def test_alter():
3159
original = schema.connection.query(
3260
"SHOW CREATE TABLE " + Experiment.full_table_name
@@ -44,3 +72,25 @@ def test_alter():
4472
).fetchone()[1]
4573
assert_not_equal(altered, restored)
4674
assert_equal(original, restored)
75+
76+
77+
def test_alter_part():
78+
# https://github.com/datajoint/datajoint-python/issues/936
79+
80+
def verify_alter(table, attribute_sql):
81+
definition_original = schema.connection.query(
82+
f"SHOW CREATE TABLE {table.full_table_name}"
83+
).fetchone()[1]
84+
table.definition = table.definition_new
85+
table.alter(prompt=False)
86+
definition_new = schema.connection.query(
87+
f"SHOW CREATE TABLE {table.full_table_name}"
88+
).fetchone()[1]
89+
assert (
90+
re.sub(f"{attribute_sql},\n ", "", definition_new) == definition_original
91+
)
92+
93+
verify_alter(table=Parent.Child, attribute_sql="`child_id` .* DEFAULT NULL")
94+
verify_alter(
95+
table=Parent.Grandchild, attribute_sql="`grandchild_id` .* DEFAULT NULL"
96+
)

0 commit comments

Comments
 (0)