lamindb_setup 0.76.7__py2.py3-none-any.whl → 0.77.0__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (47) hide show
  1. lamindb_setup/__init__.py +6 -7
  2. lamindb_setup/_cache.py +34 -34
  3. lamindb_setup/_check.py +7 -7
  4. lamindb_setup/_check_setup.py +79 -79
  5. lamindb_setup/_close.py +35 -35
  6. lamindb_setup/_connect_instance.py +440 -433
  7. lamindb_setup/_delete.py +137 -137
  8. lamindb_setup/_django.py +41 -41
  9. lamindb_setup/_exportdb.py +68 -68
  10. lamindb_setup/_importdb.py +50 -50
  11. lamindb_setup/_init_instance.py +374 -374
  12. lamindb_setup/_migrate.py +239 -239
  13. lamindb_setup/_register_instance.py +36 -36
  14. lamindb_setup/_schema.py +27 -27
  15. lamindb_setup/_schema_metadata.py +411 -391
  16. lamindb_setup/_set_managed_storage.py +55 -55
  17. lamindb_setup/_setup_user.py +134 -118
  18. lamindb_setup/_silence_loggers.py +44 -44
  19. lamindb_setup/core/__init__.py +21 -21
  20. lamindb_setup/core/_aws_credentials.py +151 -151
  21. lamindb_setup/core/_aws_storage.py +48 -48
  22. lamindb_setup/core/_deprecated.py +55 -55
  23. lamindb_setup/core/_docs.py +14 -14
  24. lamindb_setup/core/_hub_client.py +173 -164
  25. lamindb_setup/core/_hub_core.py +524 -473
  26. lamindb_setup/core/_hub_crud.py +211 -211
  27. lamindb_setup/core/_hub_utils.py +109 -109
  28. lamindb_setup/core/_private_django_api.py +88 -88
  29. lamindb_setup/core/_settings.py +138 -138
  30. lamindb_setup/core/_settings_instance.py +461 -461
  31. lamindb_setup/core/_settings_load.py +105 -100
  32. lamindb_setup/core/_settings_save.py +81 -81
  33. lamindb_setup/core/_settings_storage.py +393 -393
  34. lamindb_setup/core/_settings_store.py +73 -72
  35. lamindb_setup/core/_settings_user.py +53 -51
  36. lamindb_setup/core/_setup_bionty_sources.py +101 -99
  37. lamindb_setup/core/cloud_sqlite_locker.py +232 -232
  38. lamindb_setup/core/django.py +113 -113
  39. lamindb_setup/core/exceptions.py +12 -12
  40. lamindb_setup/core/hashing.py +114 -114
  41. lamindb_setup/core/types.py +19 -19
  42. lamindb_setup/core/upath.py +779 -779
  43. {lamindb_setup-0.76.7.dist-info → lamindb_setup-0.77.0.dist-info}/METADATA +1 -1
  44. lamindb_setup-0.77.0.dist-info/RECORD +46 -0
  45. {lamindb_setup-0.76.7.dist-info → lamindb_setup-0.77.0.dist-info}/WHEEL +1 -1
  46. lamindb_setup-0.76.7.dist-info/RECORD +0 -46
  47. {lamindb_setup-0.76.7.dist-info → lamindb_setup-0.77.0.dist-info}/LICENSE +0 -0
@@ -1,473 +1,524 @@
1
- from __future__ import annotations
2
-
3
- import json
4
- import os
5
- import uuid
6
- from importlib import metadata
7
- from typing import TYPE_CHECKING, Literal
8
- from uuid import UUID
9
-
10
- from lamin_utils import logger
11
- from postgrest.exceptions import APIError
12
-
13
- from ._hub_client import (
14
- call_with_fallback,
15
- call_with_fallback_auth,
16
- connect_hub,
17
- )
18
- from ._hub_crud import (
19
- _delete_instance_record,
20
- select_account_by_handle,
21
- select_db_user_by_instance,
22
- select_default_storage_by_instance_id,
23
- select_instance_by_id_with_storage,
24
- select_instance_by_name,
25
- select_instance_by_owner_name,
26
- )
27
- from ._hub_crud import update_instance as _update_instance_record
28
- from ._hub_utils import (
29
- LaminDsn,
30
- LaminDsnModel,
31
- )
32
- from ._settings import settings
33
- from ._settings_storage import StorageSettings, base62
34
-
35
- if TYPE_CHECKING:
36
- from supabase import Client # type: ignore
37
-
38
- from ._settings_instance import InstanceSettings
39
-
40
-
41
- def delete_storage_record(
42
- storage_uuid: UUID,
43
- ) -> None:
44
- return call_with_fallback_auth(
45
- _delete_storage_record,
46
- storage_uuid=storage_uuid,
47
- )
48
-
49
-
50
- def _delete_storage_record(storage_uuid: UUID, client: Client) -> None:
51
- if storage_uuid is None:
52
- return None
53
- response = client.table("storage").delete().eq("id", storage_uuid.hex).execute()
54
- if response.data:
55
- logger.important(f"deleted storage record on hub {storage_uuid.hex}")
56
- else:
57
- raise PermissionError(
58
- f"Deleting of storage with {storage_uuid.hex} was not successful. Probably, you"
59
- " don't have sufficient permissions."
60
- )
61
-
62
-
63
- def update_instance_record(instance_uuid: UUID, fields: dict) -> None:
64
- return call_with_fallback_auth(
65
- _update_instance_record, instance_id=instance_uuid.hex, instance_fields=fields
66
- )
67
-
68
-
69
- def get_storage_records_for_instance(
70
- instance_id: UUID,
71
- ) -> list[dict[str, str | int]]:
72
- return call_with_fallback_auth(
73
- _get_storage_records_for_instance,
74
- instance_id=instance_id,
75
- )
76
-
77
-
78
- def _get_storage_records_for_instance(
79
- instance_id: UUID, client: Client
80
- ) -> list[dict[str, str | int]]:
81
- response = (
82
- client.table("storage").select("*").eq("instance_id", instance_id.hex).execute()
83
- )
84
- return response.data
85
-
86
-
87
- def _select_storage(
88
- ssettings: StorageSettings, update_uid: bool, client: Client
89
- ) -> bool:
90
- root = ssettings.root_as_str
91
- response = client.table("storage").select("*").eq("root", root).execute()
92
- if not response.data:
93
- return False
94
- else:
95
- existing_storage = response.data[0]
96
- if existing_storage["instance_id"] is not None:
97
- if ssettings._instance_id is not None:
98
- # consider storage settings that are meant to be managed by an instance
99
- if UUID(existing_storage["instance_id"]) != ssettings._instance_id:
100
- # everything is alright if the instance_id matches
101
- # we're probably just switching storage locations
102
- # below can be turned into a warning and then delegate the error
103
- # to a unique constraint violation below
104
- raise ValueError(
105
- f"Storage root {root} is already managed by instance {existing_storage['instance_id']}."
106
- )
107
- else:
108
- # if the request is agnostic of the instance, that's alright,
109
- # we'll update the instance_id with what's stored in the hub
110
- ssettings._instance_id = UUID(existing_storage["instance_id"])
111
- ssettings._uuid_ = UUID(existing_storage["id"])
112
- if update_uid:
113
- ssettings._uid = existing_storage["lnid"]
114
- else:
115
- assert ssettings._uid == existing_storage["lnid"]
116
- return True
117
-
118
-
119
- def init_storage(
120
- ssettings: StorageSettings,
121
- auto_populate_instance: bool = True,
122
- ) -> Literal["hub-record-retireved", "hub-record-created"]:
123
- if settings.user.handle != "anonymous":
124
- return call_with_fallback_auth(
125
- _init_storage,
126
- ssettings=ssettings,
127
- auto_populate_instance=auto_populate_instance,
128
- )
129
- else:
130
- storage_exists = call_with_fallback(
131
- _select_storage, ssettings=ssettings, update_uid=True
132
- )
133
- if storage_exists:
134
- return "hub-record-retireved"
135
- else:
136
- raise ValueError("Log in to create a storage location on the hub.")
137
-
138
-
139
- def _init_storage(
140
- ssettings: StorageSettings, auto_populate_instance: bool, client: Client
141
- ) -> Literal["hub-record-retireved", "hub-record-created"]:
142
- from lamindb_setup import settings
143
-
144
- # storage roots are always stored without the trailing slash in the SQL
145
- # database
146
- root = ssettings.root_as_str
147
- if _select_storage(ssettings, update_uid=True, client=client):
148
- return "hub-record-retireved"
149
- if ssettings.type_is_cloud:
150
- id = uuid.uuid5(uuid.NAMESPACE_URL, root)
151
- else:
152
- id = uuid.uuid4()
153
- if (
154
- ssettings._instance_id is None
155
- and settings._instance_exists
156
- and auto_populate_instance
157
- ):
158
- logger.warning(
159
- f"will manage storage location {ssettings.root_as_str} with instance {settings.instance.slug}"
160
- )
161
- ssettings._instance_id = settings.instance._id
162
- instance_id_hex = (
163
- ssettings._instance_id.hex
164
- if (ssettings._instance_id is not None and auto_populate_instance)
165
- else None
166
- )
167
- fields = {
168
- "id": id.hex,
169
- "lnid": ssettings.uid,
170
- "created_by": settings.user._uuid.hex, # type: ignore
171
- "root": root,
172
- "region": ssettings.region,
173
- "type": ssettings.type,
174
- "instance_id": instance_id_hex,
175
- # the empty string is important as we want the user flow to be through LaminHub
176
- # if this errors with unique constraint error, the user has to update
177
- # the description in LaminHub
178
- "description": "",
179
- }
180
- # TODO: add error message for violated unique constraint
181
- # on root & description
182
- client.table("storage").upsert(fields).execute()
183
- ssettings._uuid_ = id
184
- return "hub-record-created"
185
-
186
-
187
- def delete_instance(identifier: UUID | str, require_empty: bool = True) -> str | None:
188
- return call_with_fallback_auth(
189
- _delete_instance, identifier=identifier, require_empty=require_empty
190
- )
191
-
192
-
193
- def _delete_instance(
194
- identifier: UUID | str, require_empty: bool, client: Client
195
- ) -> str | None:
196
- """Fully delete an instance in the hub.
197
-
198
- This function deletes the relevant instance and storage records in the hub,
199
- conditional on the emptiness of the storage location.
200
- """
201
- from ._settings_storage import mark_storage_root
202
- from .upath import check_storage_is_empty, create_path
203
-
204
- # the "/" check is for backward compatibility with the old identifier format
205
- if isinstance(identifier, UUID) or "/" not in identifier:
206
- if isinstance(identifier, UUID):
207
- instance_id_str = identifier.hex
208
- else:
209
- instance_id_str = identifier
210
- instance_with_storage = select_instance_by_id_with_storage(
211
- instance_id=instance_id_str, client=client
212
- )
213
- else:
214
- owner, name = identifier.split("/")
215
- instance_with_storage = select_instance_by_owner_name(
216
- owner=owner, name=name, client=client
217
- )
218
-
219
- if instance_with_storage is None:
220
- logger.important("not deleting instance from hub as instance not found there")
221
- return "instance-not-found"
222
-
223
- storage_records = _get_storage_records_for_instance(
224
- UUID(instance_with_storage["id"]),
225
- client,
226
- )
227
- if require_empty:
228
- for storage_record in storage_records:
229
- account_for_sqlite_file = (
230
- instance_with_storage["db_scheme"] is None
231
- and instance_with_storage["storage"]["root"] == storage_record["root"]
232
- )
233
- root_string = storage_record["root"]
234
- # gate storage and instance deletion on empty storage location for
235
- if client.auth.get_session() is not None:
236
- access_token = client.auth.get_session().access_token
237
- else:
238
- access_token = None
239
- root_path = create_path(root_string, access_token)
240
- mark_storage_root(
241
- root_path,
242
- storage_record["lnid"], # type: ignore
243
- ) # address permission error
244
- check_storage_is_empty(
245
- root_path, account_for_sqlite_file=account_for_sqlite_file
246
- )
247
- _update_instance_record(instance_with_storage["id"], {"storage_id": None}, client)
248
- # first delete the storage records because we will turn instance_id on
249
- # storage into a FK soon
250
- for storage_record in storage_records:
251
- _delete_storage_record(UUID(storage_record["id"]), client) # type: ignore
252
- _delete_instance_record(UUID(instance_with_storage["id"]), client)
253
- return None
254
-
255
-
256
- def delete_instance_record(
257
- instance_id: UUID,
258
- ) -> None:
259
- return call_with_fallback_auth(
260
- _delete_instance_record,
261
- instance_id=instance_id,
262
- )
263
-
264
-
265
- def init_instance(isettings: InstanceSettings) -> None:
266
- return call_with_fallback_auth(_init_instance, isettings=isettings)
267
-
268
-
269
- def _init_instance(isettings: InstanceSettings, client: Client) -> None:
270
- from ._settings import settings
271
-
272
- try:
273
- lamindb_version = metadata.version("lamindb")
274
- except metadata.PackageNotFoundError:
275
- lamindb_version = None
276
- fields = {
277
- "id": isettings._id.hex,
278
- "account_id": settings.user._uuid.hex, # type: ignore
279
- "name": isettings.name,
280
- "storage_id": isettings.storage._uuid.hex, # type: ignore
281
- "lnid": isettings.uid,
282
- "schema_str": isettings._schema_str,
283
- "lamindb_version": lamindb_version,
284
- "public": False,
285
- }
286
- if isettings.dialect != "sqlite":
287
- db_dsn = LaminDsnModel(db=isettings.db)
288
- db_fields = {
289
- "db_scheme": db_dsn.db.scheme,
290
- "db_host": db_dsn.db.host,
291
- "db_port": db_dsn.db.port,
292
- "db_database": db_dsn.db.database,
293
- }
294
- fields.update(db_fields)
295
- # I'd like the following to be an upsert, but this seems to violate RLS
296
- # Similarly, if we don't specify `returning="minimal"`, we'll violate RLS
297
- # we could make this idempotent by catching an error, but this seems dangerous
298
- # as then init_instance is no longer idempotent
299
- try:
300
- client.table("instance").insert(fields, returning="minimal").execute()
301
- except APIError:
302
- logger.warning(
303
- f"instance already existed at: https://lamin.ai/{isettings.owner}/{isettings.name}"
304
- )
305
- return None
306
- client.table("storage").update(
307
- {"instance_id": isettings._id.hex, "is_default": True}
308
- ).eq("id", isettings.storage._uuid.hex).execute() # type: ignore
309
- logger.important(f"go to: https://lamin.ai/{isettings.owner}/{isettings.name}")
310
-
311
-
312
- def connect_instance(
313
- *,
314
- owner: str, # account_handle
315
- name: str, # instance_name
316
- ) -> tuple[dict, dict] | str:
317
- from ._settings import settings
318
-
319
- if settings.user.handle != "anonymous":
320
- return call_with_fallback_auth(_connect_instance, owner=owner, name=name)
321
- else:
322
- return call_with_fallback(_connect_instance, owner=owner, name=name)
323
-
324
-
325
- def _connect_instance(
326
- *,
327
- owner: str, # account_handle
328
- name: str, # instance_name
329
- client: Client,
330
- ) -> tuple[dict, dict] | str:
331
- instance_account_storage = select_instance_by_owner_name(owner, name, client)
332
- if instance_account_storage is None:
333
- # try the via single requests, will take more time
334
- account = select_account_by_handle(owner, client)
335
- if account is None:
336
- return "account-not-exists"
337
- instance = select_instance_by_name(account["id"], name, client)
338
- if instance is None:
339
- return "instance-not-found"
340
- # get default storage
341
- storage = select_default_storage_by_instance_id(instance["id"], client)
342
- if storage is None:
343
- return "storage-does-not-exist-on-hub"
344
- else:
345
- account = instance_account_storage.pop("account")
346
- storage = instance_account_storage.pop("storage")
347
- instance = instance_account_storage
348
- # check if is postgres instance
349
- # this used to be a check for `instance["db"] is not None` in earlier versions
350
- # removed this on 2022-10-25 and can remove from the hub probably for lamindb 1.0
351
- if instance["db_scheme"] is not None:
352
- db_user = select_db_user_by_instance(instance["id"], client)
353
- if db_user is None:
354
- name, password = "none", "none"
355
- else:
356
- name, password = db_user["db_user_name"], db_user["db_user_password"]
357
- # construct dsn from instance and db_account fields
358
- db_dsn = LaminDsn.build(
359
- scheme=instance["db_scheme"],
360
- user=name,
361
- password=password,
362
- host=instance["db_host"],
363
- port=instance["db_port"],
364
- database=instance["db_database"],
365
- )
366
- instance["db"] = db_dsn
367
- return instance, storage # type: ignore
368
-
369
-
370
- def access_aws(storage_root: str, access_token: str | None = None) -> dict[str, dict]:
371
- from ._settings import settings
372
-
373
- if settings.user.handle != "anonymous" or access_token is not None:
374
- storage_root_info = call_with_fallback_auth(
375
- _access_aws, storage_root=storage_root, access_token=access_token
376
- )
377
- return storage_root_info
378
- else:
379
- raise RuntimeError("Can only get access to AWS if authenticated.")
380
-
381
-
382
- def _access_aws(*, storage_root: str, client: Client) -> dict[str, dict]:
383
- import lamindb_setup
384
-
385
- storage_root_info: dict[str, dict] = {"credentials": {}, "accessibility": {}}
386
- response = client.functions.invoke(
387
- "access-aws",
388
- invoke_options={"body": {"storage_root": storage_root}},
389
- )
390
- if response is not None and response != b"{}":
391
- data = json.loads(response)
392
-
393
- loaded_credentials = data["Credentials"]
394
- loaded_accessibility = data["StorageAccessibility"]
395
-
396
- credentials = storage_root_info["credentials"]
397
- credentials["key"] = loaded_credentials["AccessKeyId"]
398
- credentials["secret"] = loaded_credentials["SecretAccessKey"]
399
- credentials["token"] = loaded_credentials["SessionToken"]
400
-
401
- accessibility = storage_root_info["accessibility"]
402
- accessibility["storage_root"] = loaded_accessibility["storageRoot"]
403
- accessibility["is_managed"] = loaded_accessibility["isManaged"]
404
- return storage_root_info
405
-
406
-
407
- def get_lamin_site_base_url():
408
- if "LAMIN_ENV" in os.environ:
409
- if os.environ["LAMIN_ENV"] == "local":
410
- return "http://localhost:3000"
411
- elif os.environ["LAMIN_ENV"] == "staging":
412
- return "https://staging.lamin.ai"
413
- return "https://lamin.ai"
414
-
415
-
416
- def sign_up_local_hub(email) -> str | tuple[str, str, str]:
417
- # raises gotrue.errors.AuthApiError: User already registered
418
- password = base62(40) # generate new password
419
- sign_up_kwargs = {"email": email, "password": password}
420
- client = connect_hub()
421
- auth_response = client.auth.sign_up(sign_up_kwargs)
422
- client.auth.sign_out()
423
- return (
424
- password,
425
- auth_response.session.user.id,
426
- auth_response.session.access_token,
427
- )
428
-
429
-
430
- def _sign_in_hub(email: str, password: str, handle: str | None, client: Client):
431
- auth = client.auth.sign_in_with_password(
432
- {
433
- "email": email,
434
- "password": password,
435
- }
436
- )
437
- data = client.table("account").select("*").eq("id", auth.user.id).execute().data
438
- if data: # sync data from hub to local cache in case it was updated on the hub
439
- user_uuid = UUID(data[0]["id"])
440
- user_id = data[0]["lnid"]
441
- user_handle = data[0]["handle"]
442
- user_name = data[0]["name"]
443
- if handle is not None and handle != user_handle:
444
- logger.warning(
445
- f"using account handle {user_handle} (cached handle was {handle})"
446
- )
447
- else: # user did not complete signup as usermeta has no matching row
448
- logger.error("complete signup on your account page.")
449
- return "complete-signup"
450
- return (
451
- user_uuid,
452
- user_id,
453
- user_handle,
454
- user_name,
455
- auth.session.access_token,
456
- )
457
-
458
-
459
- def sign_in_hub(
460
- email: str, password: str, handle: str | None = None
461
- ) -> Exception | tuple[UUID, str, str, str, str]:
462
- try:
463
- result = call_with_fallback(
464
- _sign_in_hub, email=email, password=password, handle=handle
465
- )
466
- except Exception as exception: # this is bad, but I don't find APIError right now
467
- logger.error(exception)
468
- logger.error(
469
- "Could not login. Probably your password is wrong or you didn't complete"
470
- " signup."
471
- )
472
- return exception
473
- return result
1
+ from __future__ import annotations
2
+
3
+ import json
4
+ import os
5
+ import uuid
6
+ from importlib import metadata
7
+ from typing import TYPE_CHECKING, Literal
8
+ from uuid import UUID
9
+
10
+ from lamin_utils import logger
11
+ from postgrest.exceptions import APIError
12
+
13
+ from ._hub_client import (
14
+ call_with_fallback,
15
+ call_with_fallback_auth,
16
+ connect_hub,
17
+ )
18
+ from ._hub_crud import (
19
+ _delete_instance_record,
20
+ select_account_by_handle,
21
+ select_db_user_by_instance,
22
+ select_default_storage_by_instance_id,
23
+ select_instance_by_id_with_storage,
24
+ select_instance_by_name,
25
+ select_instance_by_owner_name,
26
+ )
27
+ from ._hub_crud import update_instance as _update_instance_record
28
+ from ._hub_utils import (
29
+ LaminDsn,
30
+ LaminDsnModel,
31
+ )
32
+ from ._settings import settings
33
+ from ._settings_storage import StorageSettings, base62
34
+
35
+ if TYPE_CHECKING:
36
+ from supabase import Client # type: ignore
37
+
38
+ from ._settings_instance import InstanceSettings
39
+
40
+
41
+ def delete_storage_record(
42
+ storage_uuid: UUID,
43
+ ) -> None:
44
+ return call_with_fallback_auth(
45
+ _delete_storage_record,
46
+ storage_uuid=storage_uuid,
47
+ )
48
+
49
+
50
+ def _delete_storage_record(storage_uuid: UUID, client: Client) -> None:
51
+ if storage_uuid is None:
52
+ return None
53
+ response = client.table("storage").delete().eq("id", storage_uuid.hex).execute()
54
+ if response.data:
55
+ logger.important(f"deleted storage record on hub {storage_uuid.hex}")
56
+ else:
57
+ raise PermissionError(
58
+ f"Deleting of storage with {storage_uuid.hex} was not successful. Probably, you"
59
+ " don't have sufficient permissions."
60
+ )
61
+
62
+
63
+ def update_instance_record(instance_uuid: UUID, fields: dict) -> None:
64
+ return call_with_fallback_auth(
65
+ _update_instance_record, instance_id=instance_uuid.hex, instance_fields=fields
66
+ )
67
+
68
+
69
+ def get_storage_records_for_instance(
70
+ instance_id: UUID,
71
+ ) -> list[dict[str, str | int]]:
72
+ return call_with_fallback_auth(
73
+ _get_storage_records_for_instance,
74
+ instance_id=instance_id,
75
+ )
76
+
77
+
78
+ def _get_storage_records_for_instance(
79
+ instance_id: UUID, client: Client
80
+ ) -> list[dict[str, str | int]]:
81
+ response = (
82
+ client.table("storage").select("*").eq("instance_id", instance_id.hex).execute()
83
+ )
84
+ return response.data
85
+
86
+
87
+ def _select_storage(
88
+ ssettings: StorageSettings, update_uid: bool, client: Client
89
+ ) -> bool:
90
+ root = ssettings.root_as_str
91
+ response = client.table("storage").select("*").eq("root", root).execute()
92
+ if not response.data:
93
+ return False
94
+ else:
95
+ existing_storage = response.data[0]
96
+ if existing_storage["instance_id"] is not None:
97
+ if ssettings._instance_id is not None:
98
+ # consider storage settings that are meant to be managed by an instance
99
+ if UUID(existing_storage["instance_id"]) != ssettings._instance_id:
100
+ # everything is alright if the instance_id matches
101
+ # we're probably just switching storage locations
102
+ # below can be turned into a warning and then delegate the error
103
+ # to a unique constraint violation below
104
+ raise ValueError(
105
+ f"Storage root {root} is already managed by instance {existing_storage['instance_id']}."
106
+ )
107
+ else:
108
+ # if the request is agnostic of the instance, that's alright,
109
+ # we'll update the instance_id with what's stored in the hub
110
+ ssettings._instance_id = UUID(existing_storage["instance_id"])
111
+ ssettings._uuid_ = UUID(existing_storage["id"])
112
+ if update_uid:
113
+ ssettings._uid = existing_storage["lnid"]
114
+ else:
115
+ assert ssettings._uid == existing_storage["lnid"]
116
+ return True
117
+
118
+
119
+ def init_storage(
120
+ ssettings: StorageSettings,
121
+ auto_populate_instance: bool = True,
122
+ ) -> Literal["hub-record-retireved", "hub-record-created"]:
123
+ if settings.user.handle != "anonymous":
124
+ return call_with_fallback_auth(
125
+ _init_storage,
126
+ ssettings=ssettings,
127
+ auto_populate_instance=auto_populate_instance,
128
+ )
129
+ else:
130
+ storage_exists = call_with_fallback(
131
+ _select_storage, ssettings=ssettings, update_uid=True
132
+ )
133
+ if storage_exists:
134
+ return "hub-record-retireved"
135
+ else:
136
+ raise ValueError("Log in to create a storage location on the hub.")
137
+
138
+
139
+ def _init_storage(
140
+ ssettings: StorageSettings, auto_populate_instance: bool, client: Client
141
+ ) -> Literal["hub-record-retireved", "hub-record-created"]:
142
+ from lamindb_setup import settings
143
+
144
+ # storage roots are always stored without the trailing slash in the SQL
145
+ # database
146
+ root = ssettings.root_as_str
147
+ if _select_storage(ssettings, update_uid=True, client=client):
148
+ return "hub-record-retireved"
149
+ if ssettings.type_is_cloud:
150
+ id = uuid.uuid5(uuid.NAMESPACE_URL, root)
151
+ else:
152
+ id = uuid.uuid4()
153
+ if (
154
+ ssettings._instance_id is None
155
+ and settings._instance_exists
156
+ and auto_populate_instance
157
+ ):
158
+ logger.warning(
159
+ f"will manage storage location {ssettings.root_as_str} with instance {settings.instance.slug}"
160
+ )
161
+ ssettings._instance_id = settings.instance._id
162
+ instance_id_hex = (
163
+ ssettings._instance_id.hex
164
+ if (ssettings._instance_id is not None and auto_populate_instance)
165
+ else None
166
+ )
167
+ fields = {
168
+ "id": id.hex,
169
+ "lnid": ssettings.uid,
170
+ "created_by": settings.user._uuid.hex, # type: ignore
171
+ "root": root,
172
+ "region": ssettings.region,
173
+ "type": ssettings.type,
174
+ "instance_id": instance_id_hex,
175
+ # the empty string is important as we want the user flow to be through LaminHub
176
+ # if this errors with unique constraint error, the user has to update
177
+ # the description in LaminHub
178
+ "description": "",
179
+ }
180
+ # TODO: add error message for violated unique constraint
181
+ # on root & description
182
+ client.table("storage").upsert(fields).execute()
183
+ ssettings._uuid_ = id
184
+ return "hub-record-created"
185
+
186
+
187
+ def delete_instance(identifier: UUID | str, require_empty: bool = True) -> str | None:
188
+ return call_with_fallback_auth(
189
+ _delete_instance, identifier=identifier, require_empty=require_empty
190
+ )
191
+
192
+
193
+ def _delete_instance(
194
+ identifier: UUID | str, require_empty: bool, client: Client
195
+ ) -> str | None:
196
+ """Fully delete an instance in the hub.
197
+
198
+ This function deletes the relevant instance and storage records in the hub,
199
+ conditional on the emptiness of the storage location.
200
+ """
201
+ from ._settings_storage import mark_storage_root
202
+ from .upath import check_storage_is_empty, create_path
203
+
204
+ # the "/" check is for backward compatibility with the old identifier format
205
+ if isinstance(identifier, UUID) or "/" not in identifier:
206
+ if isinstance(identifier, UUID):
207
+ instance_id_str = identifier.hex
208
+ else:
209
+ instance_id_str = identifier
210
+ instance_with_storage = select_instance_by_id_with_storage(
211
+ instance_id=instance_id_str, client=client
212
+ )
213
+ else:
214
+ owner, name = identifier.split("/")
215
+ instance_with_storage = select_instance_by_owner_name(
216
+ owner=owner, name=name, client=client
217
+ )
218
+
219
+ if instance_with_storage is None:
220
+ logger.important("not deleting instance from hub as instance not found there")
221
+ return "instance-not-found"
222
+
223
+ storage_records = _get_storage_records_for_instance(
224
+ UUID(instance_with_storage["id"]),
225
+ client,
226
+ )
227
+ if require_empty:
228
+ for storage_record in storage_records:
229
+ account_for_sqlite_file = (
230
+ instance_with_storage["db_scheme"] is None
231
+ and instance_with_storage["storage"]["root"] == storage_record["root"]
232
+ )
233
+ root_string = storage_record["root"]
234
+ # gate storage and instance deletion on empty storage location for
235
+ if client.auth.get_session() is not None:
236
+ access_token = client.auth.get_session().access_token
237
+ else:
238
+ access_token = None
239
+ root_path = create_path(root_string, access_token)
240
+ mark_storage_root(
241
+ root_path,
242
+ storage_record["lnid"], # type: ignore
243
+ ) # address permission error
244
+ check_storage_is_empty(
245
+ root_path, account_for_sqlite_file=account_for_sqlite_file
246
+ )
247
+ _update_instance_record(instance_with_storage["id"], {"storage_id": None}, client)
248
+ # first delete the storage records because we will turn instance_id on
249
+ # storage into a FK soon
250
+ for storage_record in storage_records:
251
+ _delete_storage_record(UUID(storage_record["id"]), client) # type: ignore
252
+ _delete_instance_record(UUID(instance_with_storage["id"]), client)
253
+ return None
254
+
255
+
256
+ def delete_instance_record(
257
+ instance_id: UUID,
258
+ ) -> None:
259
+ return call_with_fallback_auth(
260
+ _delete_instance_record,
261
+ instance_id=instance_id,
262
+ )
263
+
264
+
265
+ def init_instance(isettings: InstanceSettings) -> None:
266
+ return call_with_fallback_auth(_init_instance, isettings=isettings)
267
+
268
+
269
+ def _init_instance(isettings: InstanceSettings, client: Client) -> None:
270
+ from ._settings import settings
271
+
272
+ try:
273
+ lamindb_version = metadata.version("lamindb")
274
+ except metadata.PackageNotFoundError:
275
+ lamindb_version = None
276
+ fields = {
277
+ "id": isettings._id.hex,
278
+ "account_id": settings.user._uuid.hex, # type: ignore
279
+ "name": isettings.name,
280
+ "storage_id": isettings.storage._uuid.hex, # type: ignore
281
+ "lnid": isettings.uid,
282
+ "schema_str": isettings._schema_str,
283
+ "lamindb_version": lamindb_version,
284
+ "public": False,
285
+ }
286
+ if isettings.dialect != "sqlite":
287
+ db_dsn = LaminDsnModel(db=isettings.db)
288
+ db_fields = {
289
+ "db_scheme": db_dsn.db.scheme,
290
+ "db_host": db_dsn.db.host,
291
+ "db_port": db_dsn.db.port,
292
+ "db_database": db_dsn.db.database,
293
+ }
294
+ fields.update(db_fields)
295
+ # I'd like the following to be an upsert, but this seems to violate RLS
296
+ # Similarly, if we don't specify `returning="minimal"`, we'll violate RLS
297
+ # we could make this idempotent by catching an error, but this seems dangerous
298
+ # as then init_instance is no longer idempotent
299
+ try:
300
+ client.table("instance").insert(fields, returning="minimal").execute()
301
+ except APIError:
302
+ logger.warning(
303
+ f"instance already existed at: https://lamin.ai/{isettings.owner}/{isettings.name}"
304
+ )
305
+ return None
306
+ client.table("storage").update(
307
+ {"instance_id": isettings._id.hex, "is_default": True}
308
+ ).eq("id", isettings.storage._uuid.hex).execute() # type: ignore
309
+ logger.important(f"go to: https://lamin.ai/{isettings.owner}/{isettings.name}")
310
+
311
+
312
+ def connect_instance(
313
+ *,
314
+ owner: str, # account_handle
315
+ name: str, # instance_name
316
+ ) -> tuple[dict, dict] | str:
317
+ from ._settings import settings
318
+
319
+ if settings.user.handle != "anonymous":
320
+ return call_with_fallback_auth(_connect_instance, owner=owner, name=name)
321
+ else:
322
+ return call_with_fallback(_connect_instance, owner=owner, name=name)
323
+
324
+
325
+ def _connect_instance(
326
+ *,
327
+ owner: str, # account_handle
328
+ name: str, # instance_name
329
+ client: Client,
330
+ ) -> tuple[dict, dict] | str:
331
+ instance_account_storage = select_instance_by_owner_name(owner, name, client)
332
+ if instance_account_storage is None:
333
+ # try the via single requests, will take more time
334
+ account = select_account_by_handle(owner, client)
335
+ if account is None:
336
+ return "account-not-exists"
337
+ instance = select_instance_by_name(account["id"], name, client)
338
+ if instance is None:
339
+ return "instance-not-found"
340
+ # get default storage
341
+ storage = select_default_storage_by_instance_id(instance["id"], client)
342
+ if storage is None:
343
+ return "storage-does-not-exist-on-hub"
344
+ else:
345
+ account = instance_account_storage.pop("account")
346
+ storage = instance_account_storage.pop("storage")
347
+ instance = instance_account_storage
348
+ # check if is postgres instance
349
+ # this used to be a check for `instance["db"] is not None` in earlier versions
350
+ # removed this on 2022-10-25 and can remove from the hub probably for lamindb 1.0
351
+ if instance["db_scheme"] is not None:
352
+ db_user = select_db_user_by_instance(instance["id"], client)
353
+ if db_user is None:
354
+ name, password = "none", "none"
355
+ else:
356
+ name, password = db_user["db_user_name"], db_user["db_user_password"]
357
+ # construct dsn from instance and db_account fields
358
+ db_dsn = LaminDsn.build(
359
+ scheme=instance["db_scheme"],
360
+ user=name,
361
+ password=password,
362
+ host=instance["db_host"],
363
+ port=instance["db_port"],
364
+ database=instance["db_database"],
365
+ )
366
+ instance["db"] = db_dsn
367
+ return instance, storage # type: ignore
368
+
369
+
370
+ def access_aws(storage_root: str, access_token: str | None = None) -> dict[str, dict]:
371
+ from ._settings import settings
372
+
373
+ if settings.user.handle != "anonymous" or access_token is not None:
374
+ storage_root_info = call_with_fallback_auth(
375
+ _access_aws, storage_root=storage_root, access_token=access_token
376
+ )
377
+ return storage_root_info
378
+ else:
379
+ raise RuntimeError("Can only get access to AWS if authenticated.")
380
+
381
+
382
+ def _access_aws(*, storage_root: str, client: Client) -> dict[str, dict]:
383
+ import lamindb_setup
384
+
385
+ storage_root_info: dict[str, dict] = {"credentials": {}, "accessibility": {}}
386
+ response = client.functions.invoke(
387
+ "access-aws",
388
+ invoke_options={"body": {"storage_root": storage_root}},
389
+ )
390
+ if response is not None and response != b"{}":
391
+ data = json.loads(response)
392
+
393
+ loaded_credentials = data["Credentials"]
394
+ loaded_accessibility = data["StorageAccessibility"]
395
+
396
+ credentials = storage_root_info["credentials"]
397
+ credentials["key"] = loaded_credentials["AccessKeyId"]
398
+ credentials["secret"] = loaded_credentials["SecretAccessKey"]
399
+ credentials["token"] = loaded_credentials["SessionToken"]
400
+
401
+ accessibility = storage_root_info["accessibility"]
402
+ accessibility["storage_root"] = loaded_accessibility["storageRoot"]
403
+ accessibility["is_managed"] = loaded_accessibility["isManaged"]
404
+ return storage_root_info
405
+
406
+
407
+ def get_lamin_site_base_url():
408
+ if "LAMIN_ENV" in os.environ:
409
+ if os.environ["LAMIN_ENV"] == "local":
410
+ return "http://localhost:3000"
411
+ elif os.environ["LAMIN_ENV"] == "staging":
412
+ return "https://staging.lamin.ai"
413
+ return "https://lamin.ai"
414
+
415
+
416
+ def sign_up_local_hub(email) -> str | tuple[str, str, str]:
417
+ # raises gotrue.errors.AuthApiError: User already registered
418
+ password = base62(40) # generate new password
419
+ sign_up_kwargs = {"email": email, "password": password}
420
+ client = connect_hub()
421
+ auth_response = client.auth.sign_up(sign_up_kwargs)
422
+ client.auth.sign_out()
423
+ return (
424
+ password,
425
+ auth_response.session.user.id,
426
+ auth_response.session.access_token,
427
+ )
428
+
429
+
430
+ def _sign_in_hub(email: str, password: str, handle: str | None, client: Client):
431
+ auth = client.auth.sign_in_with_password(
432
+ {
433
+ "email": email,
434
+ "password": password,
435
+ }
436
+ )
437
+ data = client.table("account").select("*").eq("id", auth.user.id).execute().data
438
+ if data: # sync data from hub to local cache in case it was updated on the hub
439
+ user = data[0]
440
+ user_uuid = UUID(user["id"])
441
+ user_id = user["lnid"]
442
+ user_handle = user["handle"]
443
+ user_name = user["name"]
444
+ if handle is not None and handle != user_handle:
445
+ logger.warning(
446
+ f"using account handle {user_handle} (cached handle was {handle})"
447
+ )
448
+ else: # user did not complete signup as usermeta has no matching row
449
+ logger.error("complete signup on your account page.")
450
+ return "complete-signup"
451
+ return (
452
+ user_uuid,
453
+ user_id,
454
+ user_handle,
455
+ user_name,
456
+ auth.session.access_token,
457
+ )
458
+
459
+
460
+ def sign_in_hub(
461
+ email: str, password: str, handle: str | None = None
462
+ ) -> Exception | str | tuple[UUID, str, str, str, str]:
463
+ try:
464
+ result = call_with_fallback(
465
+ _sign_in_hub, email=email, password=password, handle=handle
466
+ )
467
+ except Exception as exception: # this is bad, but I don't find APIError right now
468
+ logger.error(exception)
469
+ logger.error(
470
+ "Could not login. Probably your password is wrong or you didn't complete"
471
+ " signup."
472
+ )
473
+ return exception
474
+ return result
475
+
476
+
477
+ def _sign_in_hub_api_key(api_key: str, client: Client):
478
+ response = client.functions.invoke(
479
+ "create-jwt",
480
+ invoke_options={"body": {"api_key": api_key}},
481
+ )
482
+ access_token = json.loads(response)["accessToken"]
483
+ # probably need more info here to avoid additional queries
484
+ # like handle, uid etc
485
+ account_id = client.auth._decode_jwt(access_token)["sub"]
486
+ client.postgrest.auth(access_token)
487
+ # normally public.account.id is equal to auth.user.id
488
+ data = client.table("account").select("*").eq("id", account_id).execute().data
489
+ if data:
490
+ user = data[0]
491
+ user_uuid = UUID(user["id"])
492
+ user_id = user["lnid"]
493
+ user_handle = user["handle"]
494
+ user_name = user["name"]
495
+ else:
496
+ logger.error("Invalid API key.")
497
+ return "invalid-api-key"
498
+ return (user_uuid, user_id, user_handle, user_name, access_token)
499
+
500
+
501
+ def sign_in_hub_api_key(
502
+ api_key: str,
503
+ ) -> Exception | str | tuple[UUID, str, str, str, str]:
504
+ try:
505
+ result = call_with_fallback(_sign_in_hub_api_key, api_key=api_key)
506
+ except Exception as exception:
507
+ logger.error(exception)
508
+ logger.error("Could not login. Probably your API key is wrong.")
509
+ return exception
510
+ return result
511
+
512
+
513
+ def _create_api_key(body: dict, client: Client) -> str:
514
+ response = client.functions.invoke(
515
+ "create-api-key",
516
+ invoke_options={"body": body},
517
+ )
518
+ api_key = json.loads(response)["apiKey"]
519
+ return api_key
520
+
521
+
522
+ def create_api_key(body: dict) -> str:
523
+ api_key = call_with_fallback_auth(_create_api_key, body=body)
524
+ return api_key