Skip to content

Commit 16d58ea

Browse files
Merge pull request #1058 from guzman-raphael/bug-activate
Schema activate ignores `create_tables=False`
2 parents 03dfbaf + 79237c2 commit 16d58ea

File tree

7 files changed

+110
-12
lines changed

7 files changed

+110
-12
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+
### 0.14.0 -- TBA
4+
* Bugfix - Activating a schema requires all tables to exist even if `create_tables=False` PR [#1058](https://github.com/datajoint/datajoint-python/pull/1058)
5+
36
### 0.13.8 -- Sep 21, 2022
47
* Add - New documentation structure based on markdown PR [#1052](https://github.com/datajoint/datajoint-python/pull/1052)
58
* Bugfix - Fix queries with backslashes ([#999](https://github.com/datajoint/datajoint-python/issues/999)) PR [#1052](https://github.com/datajoint/datajoint-python/pull/1052)

datajoint/schemas.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def __init__(
4747
connection=None,
4848
create_schema=True,
4949
create_tables=True,
50-
add_objects=None
50+
add_objects=None,
5151
):
5252
"""
5353
Associate database schema `schema_name`. If the schema does not exist, attempt to
@@ -87,7 +87,7 @@ def activate(
8787
connection=None,
8888
create_schema=None,
8989
create_tables=None,
90-
add_objects=None
90+
add_objects=None,
9191
):
9292
"""
9393
Associate database schema `schema_name`. If the schema does not exist, attempt to
@@ -222,9 +222,7 @@ def _decorate_table(self, table_class, context, assert_declared=False):
222222
# instantiate the class, declare the table if not already
223223
instance = table_class()
224224
is_declared = instance.is_declared
225-
if not is_declared:
226-
if not self.create_tables or assert_declared:
227-
raise DataJointError("Table `%s` not declared" % instance.table_name)
225+
if not is_declared and not assert_declared and self.create_tables:
228226
instance.declare(context)
229227
self.connection.dependencies.clear()
230228
is_declared = is_declared or instance.is_declared
@@ -506,7 +504,7 @@ def __init__(
506504
create_schema=False,
507505
create_tables=False,
508506
connection=None,
509-
add_objects=None
507+
add_objects=None,
510508
):
511509
"""
512510
Creates a python module with the given name from the name of a schema on the server and

datajoint/version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
__version__ = "0.13.8"
1+
__version__ = "0.14.0"
22

33
assert len(__version__) <= 10 # The log table limits version to the 10 characters

local-docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ services:
9595
## Interactive Jupyter Notebook environment
9696
jupyter notebook &
9797
## Remote debugger
98+
set +e
9899
while true
99100
do
100101
python -m ptvsd --host 0.0.0.0 --port 5678 --wait .

tests/schema_privileges.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import datajoint as dj
2+
3+
schema = dj.Schema()
4+
5+
6+
@schema
7+
class Parent(dj.Lookup):
8+
definition = """
9+
id: int
10+
"""
11+
contents = [(1,)]
12+
13+
14+
@schema
15+
class Child(dj.Computed):
16+
definition = """
17+
-> Parent
18+
"""
19+
20+
def make(self, key):
21+
self.insert1(key)
22+
23+
24+
@schema
25+
class NoAccess(dj.Lookup):
26+
definition = """
27+
string: varchar(10)
28+
"""
29+
30+
31+
@schema
32+
class NoAccessAgain(dj.Manual):
33+
definition = """
34+
-> NoAccess
35+
"""

tests/test_blob.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ def test_insert_longblob():
230230

231231
def test_datetime_serialization_speed():
232232
# If this fails that means for some reason deserializing/serializing
233-
# np arrays of np.datetime64 types is now slower than regular arrays of datetime64
233+
# np arrays of np.datetime64 types is now slower than regular arrays of datetime
234234

235235
optimized_exe_time = timeit.timeit(
236236
setup="myarr=pack(np.array([np.datetime64('2022-10-13 03:03:13') for _ in range(0, 10000)]))",
@@ -247,4 +247,4 @@ def test_datetime_serialization_speed():
247247
)
248248
print(f"python time {baseline_exe_time}")
249249

250-
assert optimized_exe_time * 1000 < baseline_exe_time
250+
assert optimized_exe_time * 900 < baseline_exe_time

tests/test_privileges.py

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
from nose.tools import assert_true, raises
1+
import importlib
22
import datajoint as dj
3-
from . import schema, CONN_INFO
3+
from . import schema, CONN_INFO_ROOT, PREFIX
4+
from . import schema_privileges as pipeline
5+
from nose.tools import assert_true, raises
46

57
namespace = locals()
68

@@ -10,7 +12,7 @@ class TestUnprivileged:
1012
def setup_class(cls):
1113
"""A connection with only SELECT privilege to djtest schemas"""
1214
cls.connection = dj.conn(
13-
host=CONN_INFO["host"], user="djview", password="djview", reset=True
15+
host=CONN_INFO_ROOT["host"], user="djview", password="djview", reset=True
1416
)
1517

1618
@raises(dj.DataJointError)
@@ -46,3 +48,62 @@ class Try(dj.Manual):
4648
"""
4749

4850
Try().insert1((1, 1.5))
51+
52+
53+
class TestSubset:
54+
USER = "djsubset"
55+
56+
@classmethod
57+
def setup_class(cls):
58+
conn = dj.conn(
59+
host=CONN_INFO_ROOT["host"],
60+
user=CONN_INFO_ROOT["user"],
61+
password=CONN_INFO_ROOT["password"],
62+
reset=True,
63+
)
64+
pipeline.schema.activate(f"{PREFIX}_pipeline")
65+
conn.query(
66+
f"""
67+
CREATE USER IF NOT EXISTS '{cls.USER}'@'%%'
68+
IDENTIFIED BY '{cls.USER}'
69+
"""
70+
)
71+
conn.query(
72+
f"""
73+
GRANT SELECT, INSERT, UPDATE, DELETE
74+
ON `{PREFIX}_pipeline`.`#parent`
75+
TO '{cls.USER}'@'%%'
76+
"""
77+
)
78+
conn.query(
79+
f"""
80+
GRANT SELECT, INSERT, UPDATE, DELETE
81+
ON `{PREFIX}_pipeline`.`__child`
82+
TO '{cls.USER}'@'%%'
83+
"""
84+
)
85+
cls.connection = dj.conn(
86+
host=CONN_INFO_ROOT["host"],
87+
user=cls.USER,
88+
password=cls.USER,
89+
reset=True,
90+
)
91+
92+
@classmethod
93+
def teardown_class(cls):
94+
conn = dj.conn(
95+
host=CONN_INFO_ROOT["host"],
96+
user=CONN_INFO_ROOT["user"],
97+
password=CONN_INFO_ROOT["password"],
98+
reset=True,
99+
)
100+
conn.query(f"DROP USER {cls.USER}")
101+
conn.query(f"DROP DATABASE {PREFIX}_pipeline")
102+
103+
def test_populate_activate(self):
104+
importlib.reload(pipeline)
105+
pipeline.schema.activate(
106+
f"{PREFIX}_pipeline", create_schema=True, create_tables=False
107+
)
108+
pipeline.Child.populate()
109+
assert pipeline.Child.progress(display=False)[0] == 0

0 commit comments

Comments
 (0)