Skip to content

Commit 8a1d3d1

Browse files
committed
Simplify collection alias code.
Now it's possible to query schema using the alias so we don't need to keep the alias and collection referenced to do either one operation or the other. Also, add support for the override_alias when restoring the backup.
1 parent 63ee163 commit 8a1d3d1

File tree

5 files changed

+19
-60
lines changed

5 files changed

+19
-60
lines changed

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ classifiers =
3737
include_package_data = True
3838
python_requires = >=3.9
3939
install_requires =
40-
weaviate-client>=4.16.0
40+
weaviate-client==v4.17.0-rc1
4141
click==8.1.7
4242
semver>=3.0.2
4343
numpy>=1.24.0

weaviate_cli/commands/restore.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,13 @@ def restore() -> None:
3838
default=RestoreBackupDefaults.exclude,
3939
help="Collection to exclude in backup (default: None).",
4040
)
41+
@click.option(
42+
"--override-alias",
43+
is_flag=True,
44+
help="Override the alias of the collection (default: False).",
45+
)
4146
@click.pass_context
42-
def restore_backup_cli(ctx, backend, include, exclude, backup_id, wait):
47+
def restore_backup_cli(ctx, backend, include, exclude, backup_id, wait, override_alias):
4348
"""Restore a backup in Weaviate."""
4449

4550
client = None
@@ -52,6 +57,7 @@ def restore_backup_cli(ctx, backend, include, exclude, backup_id, wait):
5257
include=include,
5358
exclude=exclude,
5459
wait=wait,
60+
override_alias=override_alias,
5561
)
5662
except Exception as e:
5763
click.echo(f"Error: {e}")

weaviate_cli/defaults.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,7 @@ class RestoreBackupDefaults:
200200
wait: bool = False
201201
include: Optional[str] = None
202202
exclude: Optional[str] = None
203+
override_alias: bool = False
203204

204205

205206
@dataclass

weaviate_cli/managers/backup_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
class BackupManager:
1515
def __init__(self, client: WeaviateClient) -> None:
16-
self.client = client
16+
self.client: WeaviateClient = client
1717

1818
def create_backup(
1919
self,
@@ -68,13 +68,15 @@ def restore_backup(
6868
include: Optional[str] = RestoreBackupDefaults.include,
6969
exclude: Optional[str] = RestoreBackupDefaults.exclude,
7070
wait: bool = RestoreBackupDefaults.wait,
71+
override_alias: bool = RestoreBackupDefaults.override_alias,
7172
) -> None:
7273

7374
result = self.client.backup.restore(
7475
backup_id=backup_id,
7576
backend=backend,
7677
include_collections=include.split(",") if include else None,
7778
exclude_collections=exclude.split(",") if exclude else None,
79+
overwrite_alias=override_alias,
7880
wait_for_completion=wait,
7981
)
8082

weaviate_cli/managers/data_manager.py

Lines changed: 7 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -237,18 +237,10 @@ def __import_json(
237237
file_name: str,
238238
cl: wvc.ConsistencyLevel,
239239
num_objects: Optional[int] = None,
240-
alias: Optional[str] = None,
241240
) -> int:
242241
counter = 0
243-
if alias is None:
244-
properties: List[wvc.Property] = collection.config.get().properties
245-
else:
246-
collection_from_alias = self.client.alias.get(alias_name=alias).collection
247-
properties: List[wvc.Property] = (
248-
self.client.collections.get(collection_from_alias)
249-
.config.get()
250-
.properties
251-
)
242+
243+
properties: List[wvc.Property] = collection.config.get().properties
252244

253245
try:
254246
with (
@@ -451,20 +443,13 @@ def __ingest_data(
451443
uuid: Optional[str] = None,
452444
verbose: bool = False,
453445
multi_vector: bool = False,
454-
alias: Optional[str] = None,
455446
) -> Collection:
456447
if randomize:
457448
click.echo(f"Generating {num_objects} objects")
458449
start_time = time.time()
459450

460451
# Determine vector dimensions based on vectorizer
461-
if alias is None:
462-
config = collection.config.get()
463-
else:
464-
collection_from_alias = self.client.alias.get(
465-
alias_name=alias
466-
).collection
467-
config = self.client.collections.get(collection_from_alias).config.get()
452+
config = collection.config.get()
468453

469454
if not config.vectorizer and config.vector_config:
470455
# Named vectors
@@ -599,11 +584,9 @@ def __ingest_data(
599584
)
600585
return cl_collection
601586
else:
602-
click.echo(
603-
f"Importing {num_objects} objects from Movies dataset {'using alias' if alias else ''}"
604-
)
587+
click.echo(f"Importing {num_objects} objects from Movies dataset")
605588
num_objects_inserted = self.__import_json(
606-
collection, "movies.json", cl, num_objects, alias
589+
collection, "movies.json", cl, num_objects
607590
)
608591
print(
609592
f"Inserted {num_objects_inserted} objects into class '{collection.name}'"
@@ -627,16 +610,12 @@ def create_data(
627610
multi_vector: bool = CreateDataDefaults.multi_vector,
628611
) -> Collection:
629612

630-
alias = None
631613
if not self.client.collections.exists(collection):
632614
alias_list = self.client.alias.list_all()
633615
if collection not in alias_list.keys():
634616
raise Exception(
635617
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command"
636618
)
637-
else:
638-
alias = collection
639-
collection = str(alias_list[collection].collection)
640619

641620
col: Collection = self.client.collections.get(collection)
642621
mt_enabled = col.config.get().multi_tenancy_config.enabled
@@ -705,9 +684,7 @@ def create_data(
705684
if tenant == "None":
706685
initial_length = len(col)
707686
collection = self.__ingest_data(
708-
collection=(
709-
col if alias is None else self.client.collections.get(alias)
710-
),
687+
collection=col,
711688
num_objects=limit,
712689
cl=cl_map[consistency_level],
713690
randomize=randomize,
@@ -716,7 +693,6 @@ def create_data(
716693
uuid=uuid,
717694
verbose=verbose,
718695
multi_vector=multi_vector,
719-
alias=alias,
720696
)
721697
after_length = len(col)
722698
else:
@@ -739,11 +715,7 @@ def create_data(
739715
initial_length = len(col.with_tenant(tenant))
740716
click.echo(f"Processing objects for tenant '{tenant}'")
741717
collection = self.__ingest_data(
742-
collection=(
743-
col.with_tenant(tenant)
744-
if alias is None
745-
else self.client.collections.get(alias).with_tenant(tenant)
746-
),
718+
collection=col.with_tenant(tenant),
747719
num_objects=limit,
748720
cl=cl_map[consistency_level],
749721
randomize=randomize,
@@ -752,7 +724,6 @@ def create_data(
752724
uuid=uuid,
753725
verbose=verbose,
754726
multi_vector=multi_vector,
755-
alias=alias,
756727
)
757728
after_length = len(col.with_tenant(tenant))
758729
if wait_for_indexing:
@@ -983,16 +954,12 @@ def update_data(
983954
verbose: bool = UpdateDataDefaults.verbose,
984955
) -> None:
985956

986-
alias = None
987957
if not self.client.collections.exists(collection):
988958
alias_list = self.client.alias.list_all()
989959
if collection not in alias_list.keys():
990960
raise Exception(
991961
f"Class '{collection}' does not exist in Weaviate. Create first using ./create_class.py"
992962
)
993-
else:
994-
alias = collection
995-
collection = str(alias_list[collection].collection)
996963

997964
col: Collection = self.client.collections.get(collection)
998965
try:
@@ -1012,8 +979,6 @@ def update_data(
1012979
}
1013980

1014981
click.echo(f"Preparing to update {limit} objects into class '{col.name}'")
1015-
# Override collection if alias is provided
1016-
col = col if alias is None else self.client.collections.get(alias)
1017982
for tenant in tenants:
1018983
if tenant == "None":
1019984
ret = self.__update_data(
@@ -1130,18 +1095,12 @@ def delete_data(
11301095
verbose: bool = DeleteDataDefaults.verbose,
11311096
) -> None:
11321097

1133-
alias = None
11341098
if not self.client.collections.exists(collection):
11351099
alias_list = self.client.alias.list_all()
11361100
if collection not in alias_list.keys():
11371101
raise Exception(
11381102
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
11391103
)
1140-
else:
1141-
alias = collection
1142-
collection = str(alias_list[collection].collection)
1143-
1144-
return 1
11451104

11461105
col: Collection = self.client.collections.get(collection)
11471106
mt_enabled = col.config.get().multi_tenancy_config.enabled
@@ -1161,9 +1120,6 @@ def delete_data(
11611120
else:
11621121
tenants = existing_tenants
11631122

1164-
# Override collection if alias is provided
1165-
col = col if alias is None else self.client.collections.get(alias)
1166-
11671123
for tenant in tenants:
11681124
if tenant == "None":
11691125
ret = self.__delete_data(
@@ -1264,16 +1220,12 @@ def query_data(
12641220
target_vector: Optional[str] = QueryDataDefaults.target_vector,
12651221
) -> None:
12661222

1267-
alias = None
12681223
if not self.client.collections.exists(collection):
12691224
alias_list = self.client.alias.list_all()
12701225
if collection not in alias_list.keys():
12711226
raise Exception(
12721227
f"Class '{collection}' does not exist in Weaviate. Create first using <create class> command."
12731228
)
1274-
else:
1275-
alias = collection
1276-
collection = str(alias_list[collection].collection)
12771229

12781230
col: Collection = self.client.collections.get(collection)
12791231
mt_enabled = col.config.get().multi_tenancy_config.enabled
@@ -1303,8 +1255,6 @@ def query_data(
13031255
"all": wvc.ConsistencyLevel.ALL,
13041256
"one": wvc.ConsistencyLevel.ONE,
13051257
}
1306-
# Override collection if alias is provided
1307-
col = col if alias is None else self.client.collections.get(alias)
13081258

13091259
for tenant in existing_tenants:
13101260
if tenant == "None":

0 commit comments

Comments
 (0)