lamindb_setup 1.14.1__py3-none-any.whl → 1.15.0__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.
- lamindb_setup/__init__.py +2 -2
- lamindb_setup/_connect_instance.py +1 -7
- lamindb_setup/_init_instance.py +4 -2
- lamindb_setup/_setup_user.py +5 -0
- lamindb_setup/core/__init__.py +15 -0
- lamindb_setup/core/_aws_options.py +8 -0
- lamindb_setup/io.py +194 -0
- {lamindb_setup-1.14.1.dist-info → lamindb_setup-1.15.0.dist-info}/METADATA +1 -1
- {lamindb_setup-1.14.1.dist-info → lamindb_setup-1.15.0.dist-info}/RECORD +11 -12
- lamindb_setup/_exportdb.py +0 -68
- lamindb_setup/_importdb.py +0 -50
- {lamindb_setup-1.14.1.dist-info → lamindb_setup-1.15.0.dist-info}/LICENSE +0 -0
- {lamindb_setup-1.14.1.dist-info → lamindb_setup-1.15.0.dist-info}/WHEEL +0 -0
lamindb_setup/__init__.py
CHANGED
|
@@ -35,7 +35,7 @@ Migration management
|
|
|
35
35
|
|
|
36
36
|
"""
|
|
37
37
|
|
|
38
|
-
__version__ = "1.
|
|
38
|
+
__version__ = "1.15.0" # denote a release candidate for 0.1.0 with 0.1rc1
|
|
39
39
|
|
|
40
40
|
import os
|
|
41
41
|
import warnings
|
|
@@ -47,7 +47,7 @@ warnings.filterwarnings("ignore", category=DeprecationWarning, module="postgrest
|
|
|
47
47
|
|
|
48
48
|
from packaging import version as packaging_version
|
|
49
49
|
|
|
50
|
-
from . import core, errors, types
|
|
50
|
+
from . import core, errors, io, types
|
|
51
51
|
from ._check_setup import _check_instance_setup
|
|
52
52
|
from ._connect_instance import connect
|
|
53
53
|
from ._delete import delete
|
|
@@ -204,18 +204,12 @@ def reset_django_module_variables():
|
|
|
204
204
|
app_names = {app.name for app in apps.get_app_configs()}
|
|
205
205
|
# always copy before iterations over sys.modules
|
|
206
206
|
# see https://docs.python.org/3/library/sys.html#sys.modules
|
|
207
|
+
# this whole thing runs about 50ms in a big env
|
|
207
208
|
for name, module in sys.modules.copy().items():
|
|
208
209
|
if (
|
|
209
210
|
module is not None
|
|
210
211
|
and (not name.startswith("__") or name == "__main__")
|
|
211
212
|
and name not in sys.builtin_module_names
|
|
212
|
-
and not (
|
|
213
|
-
hasattr(module, "__file__")
|
|
214
|
-
and module.__file__
|
|
215
|
-
and any(
|
|
216
|
-
path in module.__file__ for path in ["/lib/python", "\\lib\\python"]
|
|
217
|
-
)
|
|
218
|
-
)
|
|
219
213
|
):
|
|
220
214
|
try:
|
|
221
215
|
for k, v in vars(module).items():
|
lamindb_setup/_init_instance.py
CHANGED
|
@@ -7,8 +7,6 @@ from typing import TYPE_CHECKING, Literal
|
|
|
7
7
|
from uuid import UUID
|
|
8
8
|
|
|
9
9
|
import click
|
|
10
|
-
from django.core.exceptions import FieldError
|
|
11
|
-
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
|
|
12
10
|
from lamin_utils import logger
|
|
13
11
|
|
|
14
12
|
from ._disconnect import disconnect
|
|
@@ -71,6 +69,10 @@ def register_storage_in_instance(ssettings: StorageSettings) -> Storage:
|
|
|
71
69
|
|
|
72
70
|
|
|
73
71
|
def register_user(usettings: UserSettings, update_user: bool = True) -> None:
|
|
72
|
+
# we have to import this here dynamically because otherwise
|
|
73
|
+
# the except below will fail on re-connect due to reset
|
|
74
|
+
from django.core.exceptions import FieldError
|
|
75
|
+
from django.db.utils import IntegrityError, OperationalError, ProgrammingError
|
|
74
76
|
from lamindb.models import User
|
|
75
77
|
|
|
76
78
|
if not update_user and User.objects.filter(uid=usettings.uid).exists():
|
lamindb_setup/_setup_user.py
CHANGED
|
@@ -7,6 +7,7 @@ from lamin_utils import logger
|
|
|
7
7
|
|
|
8
8
|
from ._check_setup import _check_instance_setup
|
|
9
9
|
from ._init_instance import register_user
|
|
10
|
+
from .core._aws_options import reset_aws_options_cache
|
|
10
11
|
from .core._settings import settings
|
|
11
12
|
from .core._settings_load import load_user_settings
|
|
12
13
|
from .core._settings_save import save_user_settings
|
|
@@ -147,6 +148,8 @@ def login(
|
|
|
147
148
|
register_user(user_settings)
|
|
148
149
|
|
|
149
150
|
settings._user_settings = None
|
|
151
|
+
# aws s3 credentials are scoped to the user
|
|
152
|
+
reset_aws_options_cache()
|
|
150
153
|
return user_settings
|
|
151
154
|
|
|
152
155
|
|
|
@@ -163,6 +166,8 @@ def logout():
|
|
|
163
166
|
if current_user_settings_file().exists():
|
|
164
167
|
current_user_settings_file().unlink()
|
|
165
168
|
settings._user_settings = None
|
|
169
|
+
# aws s3 credentials are scoped to the user
|
|
170
|
+
reset_aws_options_cache()
|
|
166
171
|
logger.success("logged out")
|
|
167
172
|
else:
|
|
168
173
|
logger.important("already logged out")
|
lamindb_setup/core/__init__.py
CHANGED
|
@@ -1,8 +1,23 @@
|
|
|
1
1
|
"""Core setup library.
|
|
2
2
|
|
|
3
|
+
General
|
|
4
|
+
-------
|
|
5
|
+
|
|
3
6
|
.. autoclass:: SetupSettings
|
|
7
|
+
|
|
8
|
+
User
|
|
9
|
+
----
|
|
10
|
+
|
|
4
11
|
.. autoclass:: UserSettings
|
|
12
|
+
|
|
13
|
+
Instance
|
|
14
|
+
--------
|
|
15
|
+
|
|
5
16
|
.. autoclass:: InstanceSettings
|
|
17
|
+
|
|
18
|
+
Storage
|
|
19
|
+
-------
|
|
20
|
+
|
|
6
21
|
.. autoclass:: StorageSettings
|
|
7
22
|
|
|
8
23
|
"""
|
|
@@ -245,3 +245,11 @@ def get_aws_options_manager() -> AWSOptionsManager:
|
|
|
245
245
|
_aws_options_manager = AWSOptionsManager()
|
|
246
246
|
|
|
247
247
|
return _aws_options_manager
|
|
248
|
+
|
|
249
|
+
|
|
250
|
+
def reset_aws_options_cache():
|
|
251
|
+
global _aws_options_manager
|
|
252
|
+
|
|
253
|
+
if _aws_options_manager is not None:
|
|
254
|
+
_aws_options_manager._credentials_cache = {}
|
|
255
|
+
_aws_options_manager._parameters_cache = {}
|
lamindb_setup/io.py
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import json
|
|
4
|
+
import warnings
|
|
5
|
+
from importlib import import_module
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import TYPE_CHECKING
|
|
8
|
+
|
|
9
|
+
import pandas as pd
|
|
10
|
+
from django.db import models, transaction
|
|
11
|
+
from rich.progress import Progress
|
|
12
|
+
|
|
13
|
+
if TYPE_CHECKING:
|
|
14
|
+
from collections.abc import Sequence
|
|
15
|
+
from typing import Literal
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def _get_registries(module_name: str) -> list[str]:
|
|
19
|
+
"""Get registry class names from a module."""
|
|
20
|
+
schema_module = import_module(module_name)
|
|
21
|
+
exclude = {"SQLRecord", "BaseSQLRecord"}
|
|
22
|
+
|
|
23
|
+
if module_name == "lamindb":
|
|
24
|
+
module_filter = lambda cls, name: cls.__module__.startswith(
|
|
25
|
+
f"{module_name}.models."
|
|
26
|
+
) and name in dir(schema_module)
|
|
27
|
+
else:
|
|
28
|
+
module_filter = (
|
|
29
|
+
lambda cls, name: cls.__module__ == f"{module_name}.models"
|
|
30
|
+
and name in dir(schema_module)
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
return [
|
|
34
|
+
name
|
|
35
|
+
for name in dir(schema_module.models)
|
|
36
|
+
if (
|
|
37
|
+
name[0].isupper()
|
|
38
|
+
and isinstance(cls := getattr(schema_module.models, name, None), type)
|
|
39
|
+
and issubclass(cls, models.Model)
|
|
40
|
+
and module_filter(cls, name)
|
|
41
|
+
and name not in exclude
|
|
42
|
+
)
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _export_registry_to_parquet(registry: type[models.Model], directory: Path) -> None:
|
|
47
|
+
"""Export a single registry table to parquet."""
|
|
48
|
+
import lamindb_setup as ln_setup
|
|
49
|
+
|
|
50
|
+
table_name = registry._meta.db_table
|
|
51
|
+
with warnings.catch_warnings():
|
|
52
|
+
warnings.filterwarnings("ignore", message="Skipped unsupported reflection")
|
|
53
|
+
df = pd.read_sql_table(table_name, ln_setup.settings.instance.db)
|
|
54
|
+
df.to_parquet(directory / f"{table_name}.parquet", compression=None)
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def export_db(
|
|
58
|
+
module_names: Sequence[str] | None = None,
|
|
59
|
+
*,
|
|
60
|
+
output_dir: str | Path = "./lamindb_export/",
|
|
61
|
+
) -> None:
|
|
62
|
+
"""Export registry tables and many-to-many link tables to parquet files.
|
|
63
|
+
|
|
64
|
+
Ensure that you connect to postgres instances using `use_root_db_user=True`.
|
|
65
|
+
|
|
66
|
+
Args:
|
|
67
|
+
module_names: Module names to export (e.g., ["lamindb", "bionty", "wetlab"]).
|
|
68
|
+
Defaults to "lamindb" if not provided.
|
|
69
|
+
output_dir: Directory path for exported parquet files.
|
|
70
|
+
"""
|
|
71
|
+
directory = Path(output_dir)
|
|
72
|
+
directory.mkdir(parents=True, exist_ok=True)
|
|
73
|
+
|
|
74
|
+
module_names = module_names or ["lamindb"]
|
|
75
|
+
modules = {name: _get_registries(name) for name in module_names}
|
|
76
|
+
total_models = sum(len(models) for models in modules.values())
|
|
77
|
+
|
|
78
|
+
with Progress() as progress:
|
|
79
|
+
task = progress.add_task("Exporting", total=total_models)
|
|
80
|
+
for module_name, model_names in modules.items():
|
|
81
|
+
schema_module = import_module(module_name)
|
|
82
|
+
for model_name in model_names:
|
|
83
|
+
progress.update(task, description=f"[cyan]{module_name}.{model_name}")
|
|
84
|
+
registry = getattr(schema_module, model_name)
|
|
85
|
+
_export_registry_to_parquet(registry, directory)
|
|
86
|
+
for field in registry._meta.many_to_many:
|
|
87
|
+
link_orm = getattr(registry, field.name).through
|
|
88
|
+
_export_registry_to_parquet(link_orm, directory)
|
|
89
|
+
progress.advance(task)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def _import_registry(
|
|
93
|
+
registry: type[models.Model],
|
|
94
|
+
directory: Path,
|
|
95
|
+
if_exists: Literal["fail", "replace", "append"] = "replace",
|
|
96
|
+
) -> None:
|
|
97
|
+
"""Import a single registry table from parquet.
|
|
98
|
+
|
|
99
|
+
Uses raw SQL export instead of django to later circumvent FK constraints.
|
|
100
|
+
"""
|
|
101
|
+
table_name = registry._meta.db_table
|
|
102
|
+
parquet_file = directory / f"{table_name}.parquet"
|
|
103
|
+
|
|
104
|
+
if not parquet_file.exists():
|
|
105
|
+
print(f"Skipped {table_name} (file not found)")
|
|
106
|
+
return
|
|
107
|
+
|
|
108
|
+
df = pd.read_parquet(parquet_file)
|
|
109
|
+
|
|
110
|
+
old_foreign_key_columns = [col for col in df.columns if col.endswith("_old")]
|
|
111
|
+
if old_foreign_key_columns:
|
|
112
|
+
df = df.drop(columns=old_foreign_key_columns)
|
|
113
|
+
|
|
114
|
+
for col in df.columns:
|
|
115
|
+
if df[col].dtype == "object":
|
|
116
|
+
df[col] = df[col].apply(
|
|
117
|
+
lambda x: json.dumps(x) if isinstance(x, (dict, list)) else x
|
|
118
|
+
)
|
|
119
|
+
|
|
120
|
+
from django.db import connection
|
|
121
|
+
|
|
122
|
+
df.to_sql(table_name, connection.connection, if_exists=if_exists, index=False)
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def import_db(
|
|
126
|
+
module_names: Sequence[str] | None = None,
|
|
127
|
+
*,
|
|
128
|
+
input_dir: str | Path = "./lamindb_export/",
|
|
129
|
+
if_exists: Literal["fail", "replace", "append"] = "replace",
|
|
130
|
+
) -> None:
|
|
131
|
+
"""Import registry and link tables from parquet files.
|
|
132
|
+
|
|
133
|
+
Temporarily disables FK constraints to allow insertion in arbitrary order.
|
|
134
|
+
Requires superuser/RDS admin privileges for postgres databases.
|
|
135
|
+
|
|
136
|
+
Args:
|
|
137
|
+
input_dir: Directory containing parquet files to import.
|
|
138
|
+
module_names: Module names to import (e.g., ["lamindb", "bionty", "wetlab"]).
|
|
139
|
+
if_exists: How to behave if table exists: 'fail', 'replace', or 'append'.
|
|
140
|
+
"""
|
|
141
|
+
from django.db import connection
|
|
142
|
+
|
|
143
|
+
import lamindb_setup as ln_setup
|
|
144
|
+
|
|
145
|
+
directory = Path(input_dir)
|
|
146
|
+
|
|
147
|
+
if not directory.exists():
|
|
148
|
+
raise ValueError(f"Directory does not exist: {directory}")
|
|
149
|
+
|
|
150
|
+
if module_names is None:
|
|
151
|
+
parquet_files = list(directory.glob("*.parquet"))
|
|
152
|
+
detected_modules = {
|
|
153
|
+
f.name.split("_")[0] for f in parquet_files if "_" in f.name
|
|
154
|
+
}
|
|
155
|
+
module_names = sorted(detected_modules)
|
|
156
|
+
|
|
157
|
+
modules = {name: _get_registries(name) for name in module_names}
|
|
158
|
+
total_models = sum(len(models) for models in modules.values())
|
|
159
|
+
|
|
160
|
+
# Disable FK constraints to allow insertion in arbitrary order
|
|
161
|
+
if ln_setup.settings.instance.dialect == "sqlite":
|
|
162
|
+
with connection.cursor() as cursor:
|
|
163
|
+
if ln_setup.settings.instance.dialect == "postgresql":
|
|
164
|
+
cursor.execute("SET session_replication_role = 'replica'")
|
|
165
|
+
elif ln_setup.settings.instance.dialect == "sqlite":
|
|
166
|
+
cursor.execute("PRAGMA foreign_keys = OFF")
|
|
167
|
+
|
|
168
|
+
with transaction.atomic():
|
|
169
|
+
if ln_setup.settings.instance.dialect == "postgresql":
|
|
170
|
+
with connection.cursor() as cursor:
|
|
171
|
+
cursor.execute("SET CONSTRAINTS ALL DEFERRED")
|
|
172
|
+
|
|
173
|
+
with Progress() as progress:
|
|
174
|
+
task = progress.add_task("Importing", total=total_models)
|
|
175
|
+
for module_name, model_names in modules.items():
|
|
176
|
+
schema_module = import_module(module_name)
|
|
177
|
+
for model_name in model_names:
|
|
178
|
+
progress.update(
|
|
179
|
+
task, description=f"[cyan]{module_name}.{model_name}"
|
|
180
|
+
)
|
|
181
|
+
registry = getattr(schema_module, model_name)
|
|
182
|
+
_import_registry(registry, directory, if_exists=if_exists)
|
|
183
|
+
for field in registry._meta.many_to_many:
|
|
184
|
+
link_orm = getattr(registry, field.name).through
|
|
185
|
+
_import_registry(link_orm, directory, if_exists=if_exists)
|
|
186
|
+
progress.advance(task)
|
|
187
|
+
|
|
188
|
+
# Re-enable FK constraints again
|
|
189
|
+
if ln_setup.settings.instance.dialect == "sqlite":
|
|
190
|
+
with connection.cursor() as cursor:
|
|
191
|
+
if ln_setup.settings.instance.dialect == "postgresql":
|
|
192
|
+
cursor.execute("SET session_replication_role = 'origin'")
|
|
193
|
+
elif ln_setup.settings.instance.dialect == "sqlite":
|
|
194
|
+
cursor.execute("PRAGMA foreign_keys = ON")
|
|
@@ -1,27 +1,26 @@
|
|
|
1
|
-
lamindb_setup/__init__.py,sha256=
|
|
1
|
+
lamindb_setup/__init__.py,sha256=jFfiLt0yIvOHoum_g8DmraECRdU-EOd-iSmAKTZyG3c,3215
|
|
2
2
|
lamindb_setup/_cache.py,sha256=pGvDNVHGx4HWr_6w5ajqEJOdysmaGc6F221qFnXkT-k,2747
|
|
3
3
|
lamindb_setup/_check.py,sha256=28PcG8Kp6OpjSLSi1r2boL2Ryeh6xkaCL87HFbjs6GA,129
|
|
4
4
|
lamindb_setup/_check_setup.py,sha256=ToKMxsUq8dQBQh8baOrNVlSb1iC8h4zTg5dV8wMu0W4,6760
|
|
5
|
-
lamindb_setup/_connect_instance.py,sha256=
|
|
5
|
+
lamindb_setup/_connect_instance.py,sha256=Yp24MMAnypKE14fMzOYCE8oqW0EJTiRHrBuugubfkBM,17816
|
|
6
6
|
lamindb_setup/_delete.py,sha256=KS3r-xGFuDmAbzPUy-9JR-YnPShYdaHjDRQrAmXQ0qM,5863
|
|
7
7
|
lamindb_setup/_disconnect.py,sha256=FT8EpCm5XXDdhDH7QtAnkO3KPatq2HqT9VXGNjgJDbk,1232
|
|
8
8
|
lamindb_setup/_django.py,sha256=uIQflpkp8l3axyPaKURlk3kacgpElVP5KOKmFxYSMGk,1454
|
|
9
9
|
lamindb_setup/_entry_points.py,sha256=sKwXPX9xjOotoAjvgkU5LBwjjHLWVkh0ZGdiSsrch9k,522
|
|
10
|
-
lamindb_setup/
|
|
11
|
-
lamindb_setup/_importdb.py,sha256=fKv9ev5OOj_-bmzC8XZ1GxOcjIjI486yrHSHDWQrJeI,1874
|
|
12
|
-
lamindb_setup/_init_instance.py,sha256=8ejD6zjV0eF7KR-DvnmDAVJb9Ty0hjaPtIkFbyLDvA0,14806
|
|
10
|
+
lamindb_setup/_init_instance.py,sha256=zNXmZPUHYda1CfLGtsvo4gNhHprK9QVPfffUIfBlTxE,14938
|
|
13
11
|
lamindb_setup/_migrate.py,sha256=SN8uphuQX-8XShH5odLyzV8-eyXATDxB5hWoxwxmgBU,11264
|
|
14
12
|
lamindb_setup/_register_instance.py,sha256=RdUZxZWHLdbvdNZWpF8e0UWROb_T0cStWbzc5yUw34I,1047
|
|
15
13
|
lamindb_setup/_schema.py,sha256=b3uzhhWpV5mQtDwhMINc2MabGCnGLESy51ito3yl6Wc,679
|
|
16
14
|
lamindb_setup/_schema_metadata.py,sha256=af1Es7qFKGPRdNmk48384HiB2r-cDTdBPu0wB9qrga4,15526
|
|
17
15
|
lamindb_setup/_set_managed_storage.py,sha256=y5YQASsWNYVWUYeLgh3N2YBETYP7mBtbpxe3X_Vgb5I,2699
|
|
18
|
-
lamindb_setup/_setup_user.py,sha256=
|
|
16
|
+
lamindb_setup/_setup_user.py,sha256=ojq7UP2Aia8GTCr6m8fylFx9VSuvGu0HmvIJ8RzymE0,6108
|
|
19
17
|
lamindb_setup/_silence_loggers.py,sha256=AKF_YcHvX32eGXdsYK8MJlxEaZ-Uo2f6QDRzjKFCtws,1568
|
|
20
18
|
lamindb_setup/errors.py,sha256=lccF3X3M2mcbHVG_0HxfuJRFFpUE-42paccIxFOfefQ,1958
|
|
19
|
+
lamindb_setup/io.py,sha256=7wU3g2AH1h18wQerltA7p8VP0n47ZOsy32XlRcBbBAc,7321
|
|
21
20
|
lamindb_setup/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
22
21
|
lamindb_setup/types.py,sha256=fuQxZJnrGYe7a_Ju9n1RqO-HhkOAr1l1xjpAg9dmBu8,605
|
|
23
|
-
lamindb_setup/core/__init__.py,sha256=
|
|
24
|
-
lamindb_setup/core/_aws_options.py,sha256=
|
|
22
|
+
lamindb_setup/core/__init__.py,sha256=gMqbkPeD-T6o_KykonvNiUJJQYz5SCk2yGgFNWFUBNc,603
|
|
23
|
+
lamindb_setup/core/_aws_options.py,sha256=6SAs3dhKxNadQqt7Ce7uHSus0DsU_LcNZoCQWvhgBWA,9626
|
|
25
24
|
lamindb_setup/core/_aws_storage.py,sha256=QEtV-riQrwfivcwqHnXBbkJ-9YyNEXL4fLoCmOHZ1BI,2003
|
|
26
25
|
lamindb_setup/core/_clone.py,sha256=2NlXV04yykqg_k7z59C_kD1F1Hi4H-55H-JtNjhenQ0,3691
|
|
27
26
|
lamindb_setup/core/_deprecated.py,sha256=M3vpM4fZPOncxY2qsXQAPeaEph28xWdv7tYaueaUyAA,2554
|
|
@@ -45,7 +44,7 @@ lamindb_setup/core/exceptions.py,sha256=qjMzqy_uzPA7mCOdnoWnS_fdA6OWbdZGftz-YYpl
|
|
|
45
44
|
lamindb_setup/core/hashing.py,sha256=Y8Uc5uSGTfU6L2R_gb5w8DdHhGRog7RnkK-e9FEMjPY,3680
|
|
46
45
|
lamindb_setup/core/types.py,sha256=T7NwspfRHgIIpYsXDcApks8jkOlGeGRW-YbVLB7jNIo,67
|
|
47
46
|
lamindb_setup/core/upath.py,sha256=bi3k8AYeiGB_NtVTO9e9gHsfs2AFB4fXiVHcbNpnlpI,35780
|
|
48
|
-
lamindb_setup-1.
|
|
49
|
-
lamindb_setup-1.
|
|
50
|
-
lamindb_setup-1.
|
|
51
|
-
lamindb_setup-1.
|
|
47
|
+
lamindb_setup-1.15.0.dist-info/LICENSE,sha256=UOZ1F5fFDe3XXvG4oNnkL1-Ecun7zpHzRxjp-XsMeAo,11324
|
|
48
|
+
lamindb_setup-1.15.0.dist-info/WHEEL,sha256=CpUCUxeHQbRN5UGRQHYRJorO5Af-Qy_fHMctcQ8DSGI,82
|
|
49
|
+
lamindb_setup-1.15.0.dist-info/METADATA,sha256=aVm2bsYK6bWwBDEcp1IJR3kp8xrRnJb5CYK_KT09HCA,1798
|
|
50
|
+
lamindb_setup-1.15.0.dist-info/RECORD,,
|
lamindb_setup/_exportdb.py
DELETED
|
@@ -1,68 +0,0 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
from importlib import import_module
|
|
4
|
-
from pathlib import Path
|
|
5
|
-
|
|
6
|
-
MODELS = {
|
|
7
|
-
"lamindb": {
|
|
8
|
-
"Collection": False,
|
|
9
|
-
"Artifact": False,
|
|
10
|
-
"Transform": False,
|
|
11
|
-
"Run": True,
|
|
12
|
-
"User": False,
|
|
13
|
-
"Storage": False,
|
|
14
|
-
"Feature": False,
|
|
15
|
-
"Schema": 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 module_name, models in MODELS.items():
|
|
61
|
-
for model_name in models.keys():
|
|
62
|
-
schema_module = import_module(f"lnschema_{module_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)
|
lamindb_setup/_importdb.py
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
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 module_name, models in MODELS.items():
|
|
40
|
-
for model_name in models.keys():
|
|
41
|
-
print(model_name)
|
|
42
|
-
schema_module = import_module(f"lnschema_{module_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)
|
|
File without changes
|
|
File without changes
|