Skip to content

Commit 8d0c1f8

Browse files
authored
[SYNPY-1426] Deprecate create_snapshot_version in client (#1217)
* Add create_table_snapshot function and update snapshot methods in Table model - Implemented create_table_snapshot in table_services.py for creating table snapshots via the Synapse REST API. - Updated the Table model to use create_table_snapshot instead of the deprecated _create_table_snapshot method. - Added unit tests for snapshot creation in both asynchronous and synchronous test files.
1 parent 01e03f1 commit 8d0c1f8

File tree

6 files changed

+578
-13
lines changed

6 files changed

+578
-13
lines changed

synapseclient/api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
from .table_services import (
6969
ViewEntityType,
7070
ViewTypeMask,
71+
create_table_snapshot,
7172
get_columns,
7273
get_default_columns,
7374
post_columns,
@@ -181,4 +182,6 @@
181182
"get_user_profile_by_id",
182183
"get_user_profile_by_username",
183184
"is_user_certified",
185+
# table_services
186+
"create_table_snapshot",
184187
]

synapseclient/api/table_services.py

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55

66
import json
77
from enum import Enum
8-
from typing import TYPE_CHECKING, List, Optional
8+
from typing import TYPE_CHECKING, Dict, List, Optional, Union
9+
10+
from synapseclient.core.utils import delete_none_keys, id_of
911

1012
if TYPE_CHECKING:
1113
from synapseclient import Synapse
@@ -43,6 +45,54 @@ class ViewTypeMask(int, Enum):
4345
MATERIALIZED_VIEW = 0x200
4446

4547

48+
async def create_table_snapshot(
49+
table_id: str,
50+
comment: Optional[str] = None,
51+
label: Optional[str] = None,
52+
activity_id: Optional[str] = None,
53+
*,
54+
synapse_client: Optional["Synapse"] = None,
55+
) -> Dict[str, Union[str, int]]:
56+
"""
57+
Creates a table snapshot using the Synapse REST API.
58+
59+
Arguments:
60+
table_id: Table ID to create a snapshot for.
61+
comment: Optional snapshot comment.
62+
label: Optional snapshot label.
63+
activity_id: Optional activity ID or activity instance applied to snapshot version.
64+
synapse_client: If not passed in and caching was not disabled by
65+
`Synapse.allow_client_caching(False)` this will use the last created
66+
instance from the Synapse class constructor.
67+
68+
Returns:
69+
A dictionary containing the snapshot response.
70+
"""
71+
from synapseclient import Synapse
72+
73+
client = Synapse.get_client(synapse_client=synapse_client)
74+
75+
if activity_id and not isinstance(activity_id, str):
76+
activity_id = str(activity_id)
77+
78+
snapshot_body = {
79+
"snapshotComment": comment,
80+
"snapshotLabel": label,
81+
"snapshotActivityId": activity_id,
82+
}
83+
84+
delete_none_keys(snapshot_body)
85+
86+
table_id = id_of(table_id)
87+
uri = f"/entity/{table_id}/table/snapshot"
88+
89+
snapshot_response = await client.rest_post_async(
90+
uri, body=json.dumps(snapshot_body)
91+
)
92+
93+
return snapshot_response
94+
95+
4696
async def get_columns(
4797
table_id: str,
4898
*,

synapseclient/client.py

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5973,6 +5973,12 @@ def getColumns(self, x, limit=100, offset=0):
59735973
else:
59745974
ValueError("Can't get columns for a %s" % type(x))
59755975

5976+
@deprecated(
5977+
version="4.9.0",
5978+
reason="To be removed in 5.0.0. "
5979+
"Use the `.snapshot()` method on the `Table`, `EntityView`, `SubmissionView`, or `Dataset` classes instead. "
5980+
"Check the docstring for the replacement function example.",
5981+
)
59765982
def create_snapshot_version(
59775983
self,
59785984
table: typing.Union[
@@ -5983,7 +5989,11 @@ def create_snapshot_version(
59835989
activity: typing.Union[Activity, str] = None,
59845990
wait: bool = True,
59855991
) -> int:
5986-
"""Create a new Table Version, new View version, or new Dataset version.
5992+
"""
5993+
**Deprecated with replacement.** This method will be removed in 5.0.0.
5994+
Use the `.snapshot()` method on the `Table`, `EntityView`, `SubmissionView`, or `Dataset` classes instead.
5995+
5996+
Create a new Table Version, new View version, or new Dataset version.
59875997
59885998
Arguments:
59895999
table: The schema of the Table/View, or its ID.
@@ -5996,6 +6006,69 @@ def create_snapshot_version(
59966006
59976007
Returns:
59986008
The snapshot version number if wait=True, None if wait=False
6009+
6010+
Example: Using this function (DEPRECATED)
6011+
Creating a snapshot of a table
6012+
6013+
table_id = "syn1234"
6014+
snapshot_version = syn.create_snapshot_version(
6015+
table_id,
6016+
comment="This is a snapshot",
6017+
label="v1.0",
6018+
activity=my_activity
6019+
)
6020+
6021+
Example: Migration to new method
6022+
 
6023+
6024+
```python
6025+
from synapseclient import Synapse
6026+
from synapseclient.models import Table, EntityView, SubmissionView, Dataset
6027+
6028+
# Login to Synapse
6029+
syn = Synapse()
6030+
syn.login()
6031+
6032+
# For Tables
6033+
table = Table(id="syn1234")
6034+
snapshot_response = table.snapshot(
6035+
comment="This is a snapshot",
6036+
label="v1.0",
6037+
include_activity=True,
6038+
associate_activity_to_new_version=True
6039+
)
6040+
print(f"Snapshot version: {snapshot_response['snapshotVersionNumber']}")
6041+
6042+
# For Entity Views
6043+
view = EntityView(id="syn5678")
6044+
snapshot_transaction = view.snapshot(
6045+
comment="This is a snapshot",
6046+
label="v1.0",
6047+
include_activity=True,
6048+
associate_activity_to_new_version=True
6049+
)
6050+
print(f"Snapshot version: {snapshot_transaction.snapshot_version_number}")
6051+
6052+
# For Submission Views
6053+
submission_view = SubmissionView(id="syn9012")
6054+
snapshot_transaction = submission_view.snapshot(
6055+
comment="This is a snapshot",
6056+
label="v1.0",
6057+
include_activity=True,
6058+
associate_activity_to_new_version=True
6059+
)
6060+
print(f"Snapshot version: {snapshot_transaction.snapshot_version_number}")
6061+
6062+
# For Datasets
6063+
dataset = Dataset(id="syn3456")
6064+
snapshot_transaction = dataset.snapshot(
6065+
comment="This is a snapshot",
6066+
label="v1.0",
6067+
include_activity=True,
6068+
associate_activity_to_new_version=True
6069+
)
6070+
print(f"Snapshot version: {snapshot_transaction.snapshot_version_number}")
6071+
```
59996072
"""
60006073
ent = self.get(id_of(table), downloadFile=False)
60016074
if isinstance(ent, (EntityViewSchema, SubmissionViewSchema, Dataset)):
@@ -6023,6 +6096,10 @@ def create_snapshot_version(
60236096
# supply the snapshot version on an async table update without waiting
60246097
return result["snapshotVersionNumber"] if wait else None
60256098

6099+
@deprecated(
6100+
version="4.9.0",
6101+
reason="To be removed in 5.0.0. This is a private function and has no direct replacement.",
6102+
)
60266103
def _create_table_snapshot(
60276104
self,
60286105
table: typing.Union[Schema, str],
@@ -6065,6 +6142,10 @@ def _create_table_snapshot(
60656142
)
60666143
return snapshot
60676144

6145+
@deprecated(
6146+
version="4.9.0",
6147+
reason="To be removed in 5.0.0. This is a private function and has no direct replacement.",
6148+
)
60686149
def _async_table_update(
60696150
self,
60706151
table: typing.Union[EntityViewSchema, Schema, str, SubmissionViewSchema],

synapseclient/models/table.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import asyncio
21
import dataclasses
32
from collections import OrderedDict
43
from copy import deepcopy
@@ -10,6 +9,7 @@
109

1110
from synapseclient import Synapse
1211
from synapseclient import Table as Synapse_Table
12+
from synapseclient.api import create_table_snapshot
1313
from synapseclient.core.async_utils import async_to_sync
1414
from synapseclient.core.constants import concrete_types
1515
from synapseclient.core.utils import MB, delete_none_keys
@@ -1508,17 +1508,14 @@ async def main():
15081508
f"[{self.id}:{self.name}]: Creating a snapshot of the table."
15091509
)
15101510

1511-
loop = asyncio.get_event_loop()
1512-
snapshot_response = await loop.run_in_executor(
1513-
None,
1514-
lambda: client._create_table_snapshot(
1515-
table=self.id,
1516-
comment=comment,
1517-
label=label,
1518-
activity=(
1519-
self.activity.id if self.activity and include_activity else None
1520-
),
1511+
snapshot_response = await create_table_snapshot(
1512+
table_id=self.id,
1513+
comment=comment,
1514+
label=label,
1515+
activity_id=(
1516+
self.activity.id if self.activity and include_activity else None
15211517
),
1518+
synapse_client=synapse_client,
15221519
)
15231520

15241521
if associate_activity_to_new_version and self.activity:

0 commit comments

Comments
 (0)