lamindb_setup 0.77.1__py2.py3-none-any.whl → 0.77.3__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 (48) hide show
  1. lamindb_setup/__init__.py +1 -1
  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 +444 -440
  7. lamindb_setup/_delete.py +139 -137
  8. lamindb_setup/_django.py +41 -41
  9. lamindb_setup/_entry_points.py +22 -22
  10. lamindb_setup/_exportdb.py +68 -68
  11. lamindb_setup/_importdb.py +50 -50
  12. lamindb_setup/_init_instance.py +374 -374
  13. lamindb_setup/_migrate.py +239 -239
  14. lamindb_setup/_register_instance.py +36 -36
  15. lamindb_setup/_schema.py +27 -27
  16. lamindb_setup/_schema_metadata.py +411 -411
  17. lamindb_setup/_set_managed_storage.py +55 -55
  18. lamindb_setup/_setup_user.py +137 -134
  19. lamindb_setup/_silence_loggers.py +44 -44
  20. lamindb_setup/core/__init__.py +21 -21
  21. lamindb_setup/core/_aws_credentials.py +151 -151
  22. lamindb_setup/core/_aws_storage.py +48 -48
  23. lamindb_setup/core/_deprecated.py +55 -55
  24. lamindb_setup/core/_docs.py +14 -14
  25. lamindb_setup/core/_hub_client.py +1 -1
  26. lamindb_setup/core/_hub_core.py +590 -524
  27. lamindb_setup/core/_hub_crud.py +211 -211
  28. lamindb_setup/core/_hub_utils.py +109 -109
  29. lamindb_setup/core/_private_django_api.py +88 -88
  30. lamindb_setup/core/_settings.py +138 -138
  31. lamindb_setup/core/_settings_instance.py +467 -461
  32. lamindb_setup/core/_settings_load.py +105 -105
  33. lamindb_setup/core/_settings_save.py +81 -81
  34. lamindb_setup/core/_settings_storage.py +405 -393
  35. lamindb_setup/core/_settings_store.py +75 -73
  36. lamindb_setup/core/_settings_user.py +53 -53
  37. lamindb_setup/core/_setup_bionty_sources.py +101 -101
  38. lamindb_setup/core/cloud_sqlite_locker.py +232 -232
  39. lamindb_setup/core/django.py +114 -113
  40. lamindb_setup/core/exceptions.py +12 -12
  41. lamindb_setup/core/hashing.py +114 -114
  42. lamindb_setup/core/types.py +19 -19
  43. lamindb_setup/core/upath.py +779 -779
  44. {lamindb_setup-0.77.1.dist-info → lamindb_setup-0.77.3.dist-info}/METADATA +1 -1
  45. lamindb_setup-0.77.3.dist-info/RECORD +47 -0
  46. {lamindb_setup-0.77.1.dist-info → lamindb_setup-0.77.3.dist-info}/WHEEL +1 -1
  47. lamindb_setup-0.77.1.dist-info/RECORD +0 -47
  48. {lamindb_setup-0.77.1.dist-info → lamindb_setup-0.77.3.dist-info}/LICENSE +0 -0
lamindb_setup/_delete.py CHANGED
@@ -1,137 +1,139 @@
1
- from __future__ import annotations
2
-
3
- import shutil
4
- from typing import TYPE_CHECKING
5
- from uuid import UUID
6
-
7
- from lamin_utils import logger
8
-
9
- from ._connect_instance import _connect_instance, get_owner_name_from_identifier
10
- from .core._aws_credentials import HOSTED_BUCKETS
11
- from .core._hub_core import delete_instance as delete_instance_on_hub
12
- from .core._hub_core import get_storage_records_for_instance
13
- from .core._settings import settings
14
- from .core._settings_storage import StorageSettings
15
- from .core.upath import check_storage_is_empty
16
-
17
- if TYPE_CHECKING:
18
- from pathlib import Path
19
-
20
- from .core._settings_instance import InstanceSettings
21
-
22
-
23
- def delete_cache(cache_dir: Path):
24
- if cache_dir is not None and cache_dir.exists():
25
- shutil.rmtree(cache_dir)
26
-
27
-
28
- def delete_exclusion_dir(isettings: InstanceSettings) -> None:
29
- exclusion_dir = isettings.storage.root / f".lamindb/_exclusion/{isettings._id.hex}"
30
- if exclusion_dir.exists():
31
- exclusion_dir.rmdir()
32
-
33
-
34
- def delete_by_isettings(isettings: InstanceSettings) -> None:
35
- settings_file = isettings._get_settings_file()
36
- if settings_file.exists():
37
- settings_file.unlink()
38
- delete_cache(isettings.storage.cache_dir)
39
- if isettings.dialect == "sqlite":
40
- try:
41
- if isettings._sqlite_file.exists():
42
- isettings._sqlite_file.unlink()
43
- except PermissionError:
44
- logger.warning(
45
- "Did not have permission to delete SQLite file:"
46
- f" {isettings._sqlite_file}"
47
- )
48
- pass
49
- # unset the global instance settings
50
- if settings._instance_exists and isettings.slug == settings.instance.slug:
51
- if settings._instance_settings_path.exists():
52
- settings._instance_settings_path.unlink()
53
- settings._instance_settings = None
54
-
55
-
56
- def delete(slug: str, force: bool = False, require_empty: bool = True) -> int | None:
57
- """Delete a LaminDB instance.
58
-
59
- Args:
60
- slug: The instance slug `account_handle/instance_name` or URL.
61
- If the instance is owned by you, it suffices to pass the instance name.
62
- force: Whether to skip the confirmation prompt.
63
- require_empty: Whether to check if the instance is empty before deleting.
64
- """
65
- owner, name = get_owner_name_from_identifier(slug)
66
- isettings = _connect_instance(owner, name, raise_permission_error=False)
67
- if isettings.dialect != "sqlite":
68
- logger.warning(
69
- f"delete() does not yet affect your Postgres database at {isettings.db}"
70
- )
71
- if isettings.is_on_hub and settings.user.handle == "anonymous":
72
- logger.warning(
73
- "won't delete the hub component of this instance because you're not logged in"
74
- )
75
- if not force:
76
- valid_responses = ["y", "yes"]
77
- user_input = (
78
- input(f"Are you sure you want to delete instance {isettings.slug}? (y/n) ")
79
- .strip()
80
- .lower()
81
- )
82
- if user_input not in valid_responses:
83
- return -1
84
-
85
- # the actual deletion process begins here
86
- if isettings.dialect == "sqlite" and isettings.is_remote:
87
- # delete the exlusion dir first because it's hard to count its objects
88
- delete_exclusion_dir(isettings)
89
- if isettings.storage.type_is_cloud and isettings.storage.root_as_str.startswith(
90
- HOSTED_BUCKETS
91
- ):
92
- if not require_empty:
93
- logger.warning(
94
- "hosted storage always has to be empty, ignoring `require_empty`"
95
- )
96
- require_empty = True
97
- # first the default storage
98
- n_objects = check_storage_is_empty(
99
- isettings.storage.root,
100
- raise_error=require_empty,
101
- account_for_sqlite_file=isettings.dialect == "sqlite",
102
- )
103
- if isettings.storage._mark_storage_root.exists():
104
- isettings.storage._mark_storage_root.unlink(
105
- missing_ok=True # this is totally weird, but needed on Py3.11
106
- )
107
- # now everything that's on the hub
108
- if settings.user.handle != "anonymous":
109
- storage_records = get_storage_records_for_instance(isettings._id)
110
- for storage_record in storage_records:
111
- if storage_record["root"] == isettings.storage.root_as_str:
112
- continue
113
- ssettings = StorageSettings(storage_record["root"]) # type: ignore
114
- check_storage_is_empty(
115
- ssettings.root, # type: ignore
116
- raise_error=require_empty,
117
- )
118
- if ssettings._mark_storage_root.exists():
119
- ssettings._mark_storage_root.unlink(
120
- missing_ok=True # this is totally weird, but needed on Py3.11
121
- )
122
- logger.info(f"deleting instance {isettings.slug}")
123
- # below we can skip check_storage_is_empty() because we already called
124
- # it above
125
- if settings.user.handle != "anonymous" and isettings.is_on_hub:
126
- # start with deleting things on the hub
127
- # this will error if the user doesn't have permission
128
- delete_instance_on_hub(isettings._id, require_empty=False)
129
- delete_by_isettings(isettings)
130
- # if .lndb file was delete, then we might count -1
131
- if n_objects <= 0 and isettings.storage.type == "local":
132
- # dir is only empty after sqlite file was delete via delete_by_isettings
133
- if (isettings.storage.root / ".lamindb").exists():
134
- (isettings.storage.root / ".lamindb").rmdir()
135
- if isettings.storage.root.exists():
136
- isettings.storage.root.rmdir()
137
- return None
1
+ from __future__ import annotations
2
+
3
+ import shutil
4
+ from typing import TYPE_CHECKING
5
+ from uuid import UUID
6
+
7
+ from lamin_utils import logger
8
+
9
+ from ._connect_instance import _connect_instance, get_owner_name_from_identifier
10
+ from .core._aws_credentials import HOSTED_BUCKETS
11
+ from .core._hub_core import delete_instance as delete_instance_on_hub
12
+ from .core._hub_core import get_storage_records_for_instance
13
+ from .core._settings import settings
14
+ from .core._settings_storage import StorageSettings
15
+ from .core.upath import check_storage_is_empty
16
+
17
+ if TYPE_CHECKING:
18
+ from pathlib import Path
19
+
20
+ from .core._settings_instance import InstanceSettings
21
+
22
+
23
+ def delete_cache(isettings: InstanceSettings):
24
+ # avoid init of root
25
+ cache_dir = isettings.storage.cache_dir / isettings.storage._root_init
26
+ if cache_dir.exists():
27
+ shutil.rmtree(cache_dir)
28
+
29
+
30
+ def delete_exclusion_dir(isettings: InstanceSettings) -> None:
31
+ exclusion_dir = isettings.storage.root / f".lamindb/_exclusion/{isettings._id.hex}"
32
+ if exclusion_dir.exists():
33
+ exclusion_dir.rmdir()
34
+
35
+
36
+ def delete_by_isettings(isettings: InstanceSettings) -> None:
37
+ settings_file = isettings._get_settings_file()
38
+ if settings_file.exists():
39
+ settings_file.unlink()
40
+ delete_cache(isettings)
41
+ if isettings.dialect == "sqlite":
42
+ try:
43
+ if isettings._sqlite_file.exists():
44
+ isettings._sqlite_file.unlink()
45
+ except PermissionError:
46
+ logger.warning(
47
+ "Did not have permission to delete SQLite file:"
48
+ f" {isettings._sqlite_file}"
49
+ )
50
+ pass
51
+ # unset the global instance settings
52
+ if settings._instance_exists and isettings.slug == settings.instance.slug:
53
+ if settings._instance_settings_path.exists():
54
+ settings._instance_settings_path.unlink()
55
+ settings._instance_settings = None
56
+
57
+
58
+ def delete(slug: str, force: bool = False, require_empty: bool = True) -> int | None:
59
+ """Delete a LaminDB instance.
60
+
61
+ Args:
62
+ slug: The instance slug `account_handle/instance_name` or URL.
63
+ If the instance is owned by you, it suffices to pass the instance name.
64
+ force: Whether to skip the confirmation prompt.
65
+ require_empty: Whether to check if the instance is empty before deleting.
66
+ """
67
+ owner, name = get_owner_name_from_identifier(slug)
68
+ isettings = _connect_instance(owner, name, raise_permission_error=False)
69
+ if isettings.dialect != "sqlite":
70
+ logger.warning(
71
+ f"delete() does not yet affect your Postgres database at {isettings.db}"
72
+ )
73
+ if isettings.is_on_hub and settings.user.handle == "anonymous":
74
+ logger.warning(
75
+ "won't delete the hub component of this instance because you're not logged in"
76
+ )
77
+ if not force:
78
+ valid_responses = ["y", "yes"]
79
+ user_input = (
80
+ input(f"Are you sure you want to delete instance {isettings.slug}? (y/n) ")
81
+ .strip()
82
+ .lower()
83
+ )
84
+ if user_input not in valid_responses:
85
+ return -1
86
+
87
+ # the actual deletion process begins here
88
+ if isettings.dialect == "sqlite" and isettings.is_remote:
89
+ # delete the exlusion dir first because it's hard to count its objects
90
+ delete_exclusion_dir(isettings)
91
+ if isettings.storage.type_is_cloud and isettings.storage.root_as_str.startswith(
92
+ HOSTED_BUCKETS
93
+ ):
94
+ if not require_empty:
95
+ logger.warning(
96
+ "hosted storage always has to be empty, ignoring `require_empty`"
97
+ )
98
+ require_empty = True
99
+ # first the default storage
100
+ n_objects = check_storage_is_empty(
101
+ isettings.storage.root,
102
+ raise_error=require_empty,
103
+ account_for_sqlite_file=isettings.dialect == "sqlite",
104
+ )
105
+ if isettings.storage._mark_storage_root.exists():
106
+ isettings.storage._mark_storage_root.unlink(
107
+ missing_ok=True # this is totally weird, but needed on Py3.11
108
+ )
109
+ # now everything that's on the hub
110
+ if settings.user.handle != "anonymous":
111
+ storage_records = get_storage_records_for_instance(isettings._id)
112
+ for storage_record in storage_records:
113
+ if storage_record["root"] == isettings.storage.root_as_str:
114
+ continue
115
+ ssettings = StorageSettings(storage_record["root"]) # type: ignore
116
+ check_storage_is_empty(
117
+ ssettings.root, # type: ignore
118
+ raise_error=require_empty,
119
+ )
120
+ if ssettings._mark_storage_root.exists():
121
+ ssettings._mark_storage_root.unlink(
122
+ missing_ok=True # this is totally weird, but needed on Py3.11
123
+ )
124
+ logger.info(f"deleting instance {isettings.slug}")
125
+ # below we can skip check_storage_is_empty() because we already called
126
+ # it above
127
+ if settings.user.handle != "anonymous" and isettings.is_on_hub:
128
+ # start with deleting things on the hub
129
+ # this will error if the user doesn't have permission
130
+ delete_instance_on_hub(isettings._id, require_empty=False)
131
+ delete_by_isettings(isettings)
132
+ # if .lndb file was delete, then we might count -1
133
+ if n_objects <= 0 and isettings.storage.type == "local":
134
+ # dir is only empty after sqlite file was delete via delete_by_isettings
135
+ if (isettings.storage.root / ".lamindb").exists():
136
+ (isettings.storage.root / ".lamindb").rmdir()
137
+ if isettings.storage.root.exists():
138
+ isettings.storage.root.rmdir()
139
+ return None
lamindb_setup/_django.py CHANGED
@@ -1,41 +1,41 @@
1
- from __future__ import annotations
2
-
3
- from typing import Optional
4
-
5
- from .core._settings import settings
6
- from .core.django import setup_django
7
-
8
-
9
- def django(command: str, package_name: str | None = None, **kwargs):
10
- r"""Manage migrations.
11
-
12
- Examples:
13
-
14
- Reset auto-incrementing primary integer ids after a database import:
15
-
16
- >>> import lamindb as ln
17
- >>> ln.setup.django("sqlsequencereset", "lnschema_core")
18
- BEGIN;
19
- SELECT setval(pg_get_serial_sequence('"lnschema_core_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_user"; # noqa
20
- SELECT setval(pg_get_serial_sequence('"lnschema_core_storage"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_storage"; # noqa
21
- COMMIT;
22
-
23
- You can then run the SQL output that you'll see like so:
24
-
25
- >>> sql = \"\"\"BEGIN;
26
- SELECT setval(pg_get_serial_sequence('"lnschema_core_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_user"; # noqa
27
- SELECT setval(pg_get_serial_sequence('"lnschema_core_storage"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_storage"; # noqa
28
- COMMIT;\"\"\"
29
- >>> from django.db import connection
30
- >>> with connection.cursor() as cursor:
31
- cursor.execute(sql)
32
-
33
- """
34
- from django.core.management import call_command
35
-
36
- setup_django(settings.instance)
37
- if package_name is not None:
38
- args = [package_name]
39
- else:
40
- args = []
41
- call_command(command, *args, **kwargs)
1
+ from __future__ import annotations
2
+
3
+ from typing import Optional
4
+
5
+ from .core._settings import settings
6
+ from .core.django import setup_django
7
+
8
+
9
+ def django(command: str, package_name: str | None = None, **kwargs):
10
+ r"""Manage migrations.
11
+
12
+ Examples:
13
+
14
+ Reset auto-incrementing primary integer ids after a database import:
15
+
16
+ >>> import lamindb as ln
17
+ >>> ln.setup.django("sqlsequencereset", "lnschema_core")
18
+ BEGIN;
19
+ SELECT setval(pg_get_serial_sequence('"lnschema_core_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_user"; # noqa
20
+ SELECT setval(pg_get_serial_sequence('"lnschema_core_storage"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_storage"; # noqa
21
+ COMMIT;
22
+
23
+ You can then run the SQL output that you'll see like so:
24
+
25
+ >>> sql = \"\"\"BEGIN;
26
+ SELECT setval(pg_get_serial_sequence('"lnschema_core_user"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_user"; # noqa
27
+ SELECT setval(pg_get_serial_sequence('"lnschema_core_storage"','id'), coalesce(max("id"), 1), max("id") IS NOT null) FROM "lnschema_core_storage"; # noqa
28
+ COMMIT;\"\"\"
29
+ >>> from django.db import connection
30
+ >>> with connection.cursor() as cursor:
31
+ cursor.execute(sql)
32
+
33
+ """
34
+ from django.core.management import call_command
35
+
36
+ setup_django(settings.instance)
37
+ if package_name is not None:
38
+ args = [package_name]
39
+ else:
40
+ args = []
41
+ call_command(command, *args, **kwargs)
@@ -1,22 +1,22 @@
1
- import sys
2
- import warnings
3
- from importlib.metadata import entry_points
4
-
5
-
6
- def call_registered_entry_points(group, **kwargs):
7
- """load and call entry points registered under group."""
8
- if sys.version_info >= (3, 10):
9
- eps = entry_points(group=group)
10
- else:
11
- eps = entry_points().get(group, [])
12
-
13
- for ep in eps:
14
- func = ep.load()
15
- try:
16
- func(**kwargs)
17
- except BaseException as e:
18
- warnings.warn(
19
- f"Error loading entry point of group {group!r}: {ep} -> {e}",
20
- RuntimeWarning,
21
- stacklevel=2,
22
- )
1
+ import sys
2
+ import warnings
3
+ from importlib.metadata import entry_points
4
+
5
+
6
+ def call_registered_entry_points(group, **kwargs):
7
+ """load and call entry points registered under group."""
8
+ if sys.version_info >= (3, 10):
9
+ eps = entry_points(group=group)
10
+ else:
11
+ eps = entry_points().get(group, [])
12
+
13
+ for ep in eps:
14
+ func = ep.load()
15
+ try:
16
+ func(**kwargs)
17
+ except BaseException as e:
18
+ warnings.warn(
19
+ f"Error loading entry point of group {group!r}: {ep} -> {e}",
20
+ RuntimeWarning,
21
+ stacklevel=2,
22
+ )
@@ -1,68 +1,68 @@
1
- from __future__ import annotations
2
-
3
- from importlib import import_module
4
- from pathlib import Path
5
-
6
- MODELS = {
7
- "core": {
8
- "Collection": False,
9
- "Artifact": False,
10
- "Transform": False,
11
- "Run": True,
12
- "User": False,
13
- "Storage": False,
14
- "Feature": False,
15
- "FeatureSet": False,
16
- "ULabel": False,
17
- },
18
- # "bionty": {
19
- # "Organism": False,
20
- # "Gene": False,
21
- # "Protein": False,
22
- # "CellMarker": False,
23
- # "Tissue": False,
24
- # "CellType": False,
25
- # "Disease": False,
26
- # "CellLine": False,
27
- # "Phenotype": False,
28
- # "Pathway": False,
29
- # "ExperimentalFactor": False,
30
- # "DevelopmentalStage": False,
31
- # "Ethnicity": False,
32
- # "Source": False,
33
- # },
34
- # "wetlab": {
35
- # "ExperimentType": False,
36
- # "Experiment": False,
37
- # "Well": False,
38
- # "TreatmentTarget": False,
39
- # "Treatment": False,
40
- # "Biosample": False,
41
- # "Techsample": False,
42
- # },
43
- }
44
-
45
-
46
- def exportdb() -> None:
47
- directory = Path("./lamindb_export/")
48
- directory.mkdir(parents=True, exist_ok=True)
49
- import pandas as pd
50
-
51
- import lamindb_setup as ln_setup
52
-
53
- def export_registry(registry, directory):
54
- table_name = registry._meta.db_table
55
- df = pd.read_sql_table(table_name, ln_setup.settings.instance.db)
56
- df.to_parquet(directory / f"{table_name}.parquet", compression=None)
57
-
58
- # export data to parquet files
59
- print(f"\nexporting data to parquet files in: {directory}\n")
60
- for schema_name, models in MODELS.items():
61
- for model_name in models.keys():
62
- schema_module = import_module(f"lnschema_{schema_name}")
63
- registry = getattr(schema_module, model_name)
64
- export_registry(registry, directory)
65
- many_to_many_names = [field.name for field in registry._meta.many_to_many]
66
- for many_to_many_name in many_to_many_names:
67
- link_orm = getattr(registry, many_to_many_name).through
68
- export_registry(link_orm, directory)
1
+ from __future__ import annotations
2
+
3
+ from importlib import import_module
4
+ from pathlib import Path
5
+
6
+ MODELS = {
7
+ "core": {
8
+ "Collection": False,
9
+ "Artifact": False,
10
+ "Transform": False,
11
+ "Run": True,
12
+ "User": False,
13
+ "Storage": False,
14
+ "Feature": False,
15
+ "FeatureSet": False,
16
+ "ULabel": False,
17
+ },
18
+ # "bionty": {
19
+ # "Organism": False,
20
+ # "Gene": False,
21
+ # "Protein": False,
22
+ # "CellMarker": False,
23
+ # "Tissue": False,
24
+ # "CellType": False,
25
+ # "Disease": False,
26
+ # "CellLine": False,
27
+ # "Phenotype": False,
28
+ # "Pathway": False,
29
+ # "ExperimentalFactor": False,
30
+ # "DevelopmentalStage": False,
31
+ # "Ethnicity": False,
32
+ # "Source": False,
33
+ # },
34
+ # "wetlab": {
35
+ # "ExperimentType": False,
36
+ # "Experiment": False,
37
+ # "Well": False,
38
+ # "TreatmentTarget": False,
39
+ # "Treatment": False,
40
+ # "Biosample": False,
41
+ # "Techsample": False,
42
+ # },
43
+ }
44
+
45
+
46
+ def exportdb() -> None:
47
+ directory = Path("./lamindb_export/")
48
+ directory.mkdir(parents=True, exist_ok=True)
49
+ import pandas as pd
50
+
51
+ import lamindb_setup as ln_setup
52
+
53
+ def export_registry(registry, directory):
54
+ table_name = registry._meta.db_table
55
+ df = pd.read_sql_table(table_name, ln_setup.settings.instance.db)
56
+ df.to_parquet(directory / f"{table_name}.parquet", compression=None)
57
+
58
+ # export data to parquet files
59
+ print(f"\nexporting data to parquet files in: {directory}\n")
60
+ for schema_name, models in MODELS.items():
61
+ for model_name in models.keys():
62
+ schema_module = import_module(f"lnschema_{schema_name}")
63
+ registry = getattr(schema_module, model_name)
64
+ export_registry(registry, directory)
65
+ many_to_many_names = [field.name for field in registry._meta.many_to_many]
66
+ for many_to_many_name in many_to_many_names:
67
+ link_orm = getattr(registry, many_to_many_name).through
68
+ export_registry(link_orm, directory)
@@ -1,50 +1,50 @@
1
- from __future__ import annotations
2
-
3
- from importlib import import_module
4
- from pathlib import Path
5
-
6
- from ._exportdb import MODELS
7
-
8
-
9
- def import_registry(registry, directory, connection):
10
- import pandas as pd
11
-
12
- table_name = registry._meta.db_table
13
- df = pd.read_parquet(directory / f"{table_name}.parquet")
14
- old_foreign_key_columns = [
15
- column for column in df.columns if column.endswith("_old")
16
- ]
17
- for column in old_foreign_key_columns:
18
- df.drop(column, axis=1, inplace=True)
19
- df.to_sql(table_name, connection, if_exists="append", index=False)
20
-
21
-
22
- def importdb() -> None:
23
- # import data from parquet files
24
- directory = Path("./lamindb_export/")
25
- if directory.exists():
26
- response = input(
27
- f"\n\nDo you want to import registries from here: {directory}? (y/n)\n"
28
- )
29
- if response != "y":
30
- return None
31
- from sqlalchemy import create_engine, text
32
-
33
- import lamindb_setup as ln_setup
34
-
35
- engine = create_engine(ln_setup.settings.instance.db, echo=False)
36
- with engine.begin() as connection:
37
- if ln_setup.settings.instance.dialect == "postgresql":
38
- connection.execute(text("SET CONSTRAINTS ALL DEFERRED;"))
39
- for schema_name, models in MODELS.items():
40
- for model_name in models.keys():
41
- print(model_name)
42
- schema_module = import_module(f"lnschema_{schema_name}")
43
- registry = getattr(schema_module, model_name)
44
- import_registry(registry, directory, connection)
45
- many_to_many_names = [
46
- field.name for field in registry._meta.many_to_many
47
- ]
48
- for many_to_many_name in many_to_many_names:
49
- link_orm = getattr(registry, many_to_many_name).through
50
- import_registry(link_orm, directory, connection)
1
+ from __future__ import annotations
2
+
3
+ from importlib import import_module
4
+ from pathlib import Path
5
+
6
+ from ._exportdb import MODELS
7
+
8
+
9
+ def import_registry(registry, directory, connection):
10
+ import pandas as pd
11
+
12
+ table_name = registry._meta.db_table
13
+ df = pd.read_parquet(directory / f"{table_name}.parquet")
14
+ old_foreign_key_columns = [
15
+ column for column in df.columns if column.endswith("_old")
16
+ ]
17
+ for column in old_foreign_key_columns:
18
+ df.drop(column, axis=1, inplace=True)
19
+ df.to_sql(table_name, connection, if_exists="append", index=False)
20
+
21
+
22
+ def importdb() -> None:
23
+ # import data from parquet files
24
+ directory = Path("./lamindb_export/")
25
+ if directory.exists():
26
+ response = input(
27
+ f"\n\nDo you want to import registries from here: {directory}? (y/n)\n"
28
+ )
29
+ if response != "y":
30
+ return None
31
+ from sqlalchemy import create_engine, text
32
+
33
+ import lamindb_setup as ln_setup
34
+
35
+ engine = create_engine(ln_setup.settings.instance.db, echo=False)
36
+ with engine.begin() as connection:
37
+ if ln_setup.settings.instance.dialect == "postgresql":
38
+ connection.execute(text("SET CONSTRAINTS ALL DEFERRED;"))
39
+ for schema_name, models in MODELS.items():
40
+ for model_name in models.keys():
41
+ print(model_name)
42
+ schema_module = import_module(f"lnschema_{schema_name}")
43
+ registry = getattr(schema_module, model_name)
44
+ import_registry(registry, directory, connection)
45
+ many_to_many_names = [
46
+ field.name for field in registry._meta.many_to_many
47
+ ]
48
+ for many_to_many_name in many_to_many_names:
49
+ link_orm = getattr(registry, many_to_many_name).through
50
+ import_registry(link_orm, directory, connection)