Skip to content

Commit

Permalink
Merge pull request #88 from weaviate/rodrigo/delete_on_conflict
Browse files Browse the repository at this point in the history
Add deletion strategy option when creating classes
  • Loading branch information
jfrancoa authored Nov 7, 2024
2 parents ccaf06a + 0f16511 commit 23a3b52
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 5 deletions.
7 changes: 7 additions & 0 deletions test/integration/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def test_collection_lifecycle(collection_manager: CollectionManager):
auto_tenant_activation=False,
vectorizer="contextionary",
force_auto_schema=True,
replication_deletion_strategy=None,
)

# Verify collection exists
Expand All @@ -64,6 +65,7 @@ def test_collection_lifecycle(collection_manager: CollectionManager):
async_enabled=False,
auto_tenant_creation=None,
auto_tenant_activation=None,
replication_deletion_strategy=None,
)

# Get collection config to verify update
Expand Down Expand Up @@ -98,6 +100,7 @@ def test_multiple_collections(collection_manager: CollectionManager):
auto_tenant_activation=False,
vectorizer="contextionary",
force_auto_schema=True,
replication_deletion_strategy=None,
)
assert collection_manager.client.collections.exists(col)
finally:
Expand Down Expand Up @@ -129,6 +132,7 @@ def test_shard_operations(
auto_tenant_activation=False,
vectorizer="transformers",
force_auto_schema=True,
replication_deletion_strategy=None,
)

# Get shard info
Expand Down Expand Up @@ -170,6 +174,7 @@ def test_error_handling(collection_manager: CollectionManager):
auto_tenant_activation=False,
vectorizer="transformers",
force_auto_schema=True,
replication_deletion_strategy=None,
)

with pytest.raises(Exception):
Expand All @@ -186,6 +191,7 @@ def test_error_handling(collection_manager: CollectionManager):
auto_tenant_activation=False,
vectorizer="transformers",
force_auto_schema=True,
replication_deletion_strategy=None,
)
finally:
# Test deleting non-existent collection
Expand All @@ -204,4 +210,5 @@ def test_error_handling(collection_manager: CollectionManager):
async_enabled=True,
auto_tenant_creation=None,
auto_tenant_activation=None,
replication_deletion_strategy=None,
)
5 changes: 5 additions & 0 deletions test/unittests/test_managers/test_collection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ def test_create_collection(mock_client):
force_auto_schema=False,
shards=1,
vectorizer=None,
replication_deletion_strategy=None,
)

# Verify the collection creation was called with correct parameters
Expand Down Expand Up @@ -68,6 +69,7 @@ def test_create_existing_collection(mock_client):
force_auto_schema=False,
shards=1,
vectorizer=None,
replication_deletion_strategy=None,
)

# Verify the error message
Expand Down Expand Up @@ -104,6 +106,7 @@ def test_create_collection_failure(mock_client):
force_auto_schema=False,
shards=1,
vectorizer=None,
replication_deletion_strategy=None,
)

# Verify the error message
Expand Down Expand Up @@ -179,6 +182,7 @@ def test_update_collection(mock_client):
async_enabled=True,
auto_tenant_creation=True,
auto_tenant_activation=True,
replication_deletion_strategy="delete_on_conflict",
)

mock_collection.config.update.assert_called_once()
Expand Down Expand Up @@ -218,6 +222,7 @@ def test_update_nonexistent_collection(mock_client):
async_enabled=True,
auto_tenant_creation=True,
auto_tenant_activation=True,
replication_deletion_strategy="delete_on_conflict",
)

assert "Error: Collection 'TestCollection' does not exist in Weaviate." in str(
Expand Down
8 changes: 8 additions & 0 deletions weaviate_cli/commands/create.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ def create() -> None:
type=click.Choice(["contextionary", "transformers", "openai", "ollama"]),
help="Vectorizer to use.",
)
@click.option(
"--replication_deletion_strategy",
default="delete_on_conflict",
type=click.Choice(["delete_on_conflict", "no_automated_resolution"]),
help="Replication deletion strategy (default: 'delete_on_conflict').",
)
@click.pass_context
def create_collection_cli(
ctx: click.Context,
Expand All @@ -84,6 +90,7 @@ def create_collection_cli(
force_auto_schema: bool,
shards: int,
vectorizer: Optional[str],
replication_deletion_strategy: str,
) -> None:
"""Create a collection in Weaviate."""

Expand All @@ -105,6 +112,7 @@ def create_collection_cli(
force_auto_schema=force_auto_schema,
shards=shards,
vectorizer=vectorizer,
replication_deletion_strategy=replication_deletion_strategy,
)
except Exception as e:
click.echo(f"Error: {e}")
Expand Down
8 changes: 8 additions & 0 deletions weaviate_cli/commands/update.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ def update() -> None:
type=bool,
help="Enable auto tenant activation (default: None).",
)
@click.option(
"--replication_deletion_strategy",
default=None,
type=click.Choice(["delete_on_conflict", "no_automated_resolution"]),
help="Replication deletion strategy.",
)
@click.pass_context
def update_collection_cli(
ctx: click.Context,
Expand All @@ -59,6 +65,7 @@ def update_collection_cli(
training_limit: int,
auto_tenant_creation: Optional[bool],
auto_tenant_activation: Optional[bool],
replication_deletion_strategy: Optional[str],
) -> None:
"""Update a collection in Weaviate."""

Expand All @@ -75,6 +82,7 @@ def update_collection_cli(
training_limit=training_limit,
auto_tenant_creation=auto_tenant_creation,
auto_tenant_activation=auto_tenant_activation,
replication_deletion_strategy=replication_deletion_strategy,
)
except Exception as e:
click.echo(f"Error: {e}")
Expand Down
32 changes: 27 additions & 5 deletions weaviate_cli/managers/collection_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def create_collection(
force_auto_schema: bool,
shards: int,
vectorizer: Optional[str],
replication_deletion_strategy: Optional[str],
) -> None:

if self.client.collections.exists(collection):
Expand Down Expand Up @@ -133,6 +134,11 @@ def create_collection(
wvc.Property(name="status", data_type=wvc.DataType.TEXT),
]

rds_map = {
"delete_on_conflict": wvc.ReplicationDeletionStrategy.DELETE_ON_CONFLICT,
"no_automated_resolution": wvc.ReplicationDeletionStrategy.NO_AUTOMATED_RESOLUTION,
}

try:
self.client.collections.create(
name=collection,
Expand All @@ -141,7 +147,13 @@ def create_collection(
inverted_index_map[inverted_index] if inverted_index else None
),
replication_config=wvc.Configure.replication(
factor=replication_factor, async_enabled=async_enabled
factor=replication_factor,
async_enabled=async_enabled,
deletion_strategy=(
rds_map[replication_deletion_strategy]
if replication_deletion_strategy
else None
),
),
sharding_config=(
wvc.Configure.sharding(desired_count=shards) if shards > 1 else None
Expand Down Expand Up @@ -171,6 +183,7 @@ def update_collection(
async_enabled: Optional[bool],
auto_tenant_creation: Optional[bool],
auto_tenant_activation: Optional[bool],
replication_deletion_strategy: Optional[str],
) -> None:

if not self.client.collections.exists(collection):
Expand Down Expand Up @@ -202,6 +215,10 @@ def update_collection(

col_obj: Collection = self.client.collections.get(collection)
rf = col_obj.config.get().replication_config.factor
rds_map = {
"delete_on_conflict": wvc.ReplicationDeletionStrategy.DELETE_ON_CONFLICT,
"no_automated_resolution": wvc.ReplicationDeletionStrategy.NO_AUTOMATED_RESOLUTION,
}
mt = col_obj.config.get().multi_tenancy_config.enabled
auto_tenant_creation = (
auto_tenant_creation
Expand All @@ -213,16 +230,21 @@ def update_collection(
if auto_tenant_activation is not None
else col_obj.config.get().multi_tenancy_config.auto_tenant_activation
)

col_obj.config.update(
description=description,
vectorizer_config=(
vector_index_map[vector_index] if vector_index else None
),
replication_config=(
wvc.Reconfigure.replication(factor=rf, async_enabled=async_enabled)
if async_enabled is not None
else None
wvc.Reconfigure.replication(
factor=rf,
async_enabled=async_enabled if async_enabled is not None else None,
deletion_strategy=(
rds_map[replication_deletion_strategy]
if replication_deletion_strategy
else None
),
)
),
multi_tenancy_config=(
wvc.Reconfigure.multi_tenancy(
Expand Down

0 comments on commit 23a3b52

Please sign in to comment.