Skip to content

Disable hidden timestamp attribute by default #1188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions datajoint/declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from .errors import DataJointError, _support_filepath_types, FILEPATH_FEATURE_SWITCH
from .attribute_adapter import get_adapter
from .condition import translate_attribute
from .settings import config

UUID_DATA_TYPE = "binary(16)"
MAX_TABLE_NAME_LENGTH = 64
Expand Down Expand Up @@ -311,17 +312,18 @@ def declare(full_table_name, definition, context):
external_stores,
) = prepare_declare(definition, context)

metadata_attr_sql = [
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
]
attribute_sql.extend(
attr.format(
full_table_name=sha1(
full_table_name.replace("`", "").encode("utf-8")
).hexdigest()
if config.get("enable_hidden_attributes", False):
metadata_attr_sql = [
"`_{full_table_name}_timestamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP"
]
attribute_sql.extend(
attr.format(
full_table_name=sha1(
full_table_name.replace("`", "").encode("utf-8")
).hexdigest()
)
for attr in metadata_attr_sql
)
for attr in metadata_attr_sql
)

if not primary_key:
raise DataJointError("Table must have a primary key")
Expand Down
1 change: 1 addition & 0 deletions datajoint/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"display.show_tuple_count": True,
"database.use_tls": None,
"enable_python_native_blobs": True, # python-native/dj0 encoding support
"enable_hidden_attributes": False,
"filepath_checksum_size_limit": None, # file size limit for when to disable checksums
}
)
Expand Down
64 changes: 59 additions & 5 deletions tests/test_declare.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,25 @@
import datajoint as dj
import inspect
from datajoint.declare import declare
from datajoint.settings import config


@pytest.fixture(scope="function")
def enable_hidden_attributes():
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = True
yield
if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


@pytest.fixture(scope="function")
def disable_hidden_attributes():
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = False
yield
if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


def test_schema_decorator(schema_any):
Expand Down Expand Up @@ -373,9 +392,44 @@ class Table_With_Underscores(dj.Manual):
schema_any(Table_With_Underscores)


def test_hidden_attributes(schema_any):
def test_hidden_attributes_default_value():
config_val = config.get("enable_hidden_attributes")
assert (
list(Experiment().heading._attributes.keys())[-1].split("_")[2] == "timestamp"
)
assert any(a.is_hidden for a in Experiment().heading._attributes.values())
assert not any(a.is_hidden for a in Experiment().heading.attributes.values())
config_val is not None and not config_val
), "Default value for enable_hidden_attributes is not False"


def test_hidden_attributes_enabled(enable_hidden_attributes, schema_any):
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = True

msg = f"{Experiment().heading._attributes=}"
assert any(
a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()
), msg
assert any(
a.name.startswith("_") for a in Experiment().heading._attributes.values()
), msg
assert any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg

if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val


def test_hidden_attributes_disabled(disable_hidden_attributes, schema_any):
orig_config_val = config.get("enable_hidden_attributes")
config["enable_hidden_attributes"] = False

msg = f"{Experiment().heading._attributes=}"
assert not any(
a.name.endswith("_timestamp") for a in Experiment().heading._attributes.values()
), msg
assert not any(
a.name.startswith("_") for a in Experiment().heading._attributes.values()
), msg
assert not any(a.is_hidden for a in Experiment().heading._attributes.values()), msg
assert not any(a.is_hidden for a in Experiment().heading.attributes.values()), msg

if orig_config_val is not None:
config["enable_hidden_attributes"] = orig_config_val
Loading