Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("experimentation", "0002_add_config_and_created_status"),
]

operations = [
migrations.RemoveConstraint(
model_name="warehouseconnection",
name="unique_active_warehouse_per_type_and_env",
),
migrations.AddConstraint(
model_name="warehouseconnection",
constraint=models.UniqueConstraint(
condition=models.Q(("deleted_at__isnull", True)),
fields=("environment",),
name="unique_active_warehouse_per_env",
),
),
Comment thread
gagantrivedi marked this conversation as resolved.
]
4 changes: 2 additions & 2 deletions api/experimentation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class WarehouseConnection(LifecycleModelMixin, SoftDeleteExportableModel): # ty
class Meta:
constraints = [
models.UniqueConstraint(
fields=["warehouse_type", "environment"],
fields=["environment"],
condition=models.Q(deleted_at__isnull=True),
name="unique_active_warehouse_per_type_and_env",
name="unique_active_warehouse_per_env",
),
]
6 changes: 3 additions & 3 deletions api/experimentation/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ def create(self, request: Request, *args: object, **kwargs: object) -> Response:
serializer = self.get_serializer(data=request.data)
serializer.is_valid(raise_exception=True)

warehouse_type = serializer.validated_data["warehouse_type"]
if WarehouseConnection.objects.filter(
environment=environment,
warehouse_type=warehouse_type,
).exists():
return Response(
{"detail": "Warehouse connection already exists."},
{
"detail": "This environment already has an active warehouse connection."
},
status=409,
)

Expand Down
72 changes: 71 additions & 1 deletion api/tests/unit/experimentation/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,39 @@ def test_post__already_exists__returns_409(

# Then
assert response.status_code == status.HTTP_409_CONFLICT
assert response.json()["detail"] == "Warehouse connection already exists."
assert (
response.json()["detail"]
== "This environment already has an active warehouse connection."
)


def test_post__different_type_already_exists__returns_409(
admin_client: APIClient,
environment: Environment,
enable_features: EnableFeaturesFixture,
warehouse_connection: WarehouseConnection,
warehouse_connection_url: str,
) -> None:
# Given
enable_features("experimentation_warehouse_connection")

# When
response = admin_client.post(
warehouse_connection_url,
data={
"warehouse_type": "snowflake",
"name": "My Snowflake",
"config": {"account_identifier": "xy12345.us-east-1"},
},
format="json",
)

# Then
assert response.status_code == status.HTTP_409_CONFLICT
assert (
response.json()["detail"]
== "This environment already has an active warehouse connection."
)


def test_post__soft_deleted_exists__resurrects_and_returns_201(
Expand Down Expand Up @@ -499,6 +531,44 @@ def test_post__snowflake_soft_deleted__resurrects_with_new_config(
assert data["config"]["account_identifier"] == "new.us-west-2"


def test_post__different_type_soft_deleted__creates_new_record(
admin_client: APIClient,
environment: Environment,
enable_features: EnableFeaturesFixture,
warehouse_connection_url: str,
) -> None:
# Given
enable_features("experimentation_warehouse_connection")
create_response = admin_client.post(
warehouse_connection_url,
data={
"warehouse_type": "snowflake",
"name": "Old Snowflake",
"config": {"account_identifier": "xy12345.us-east-1"},
},
format="json",
)
original_id = create_response.json()["id"]
url = reverse(
"api-v1:environments:experimentation:warehouse-connections-detail",
args=[environment.api_key, original_id],
)
admin_client.delete(url)

# When
response = admin_client.post(
warehouse_connection_url,
data={"warehouse_type": "flagsmith"},
format="json",
)

# Then
assert response.status_code == status.HTTP_201_CREATED
data = response.json()
assert data["id"] != original_id
assert data["warehouse_type"] == "flagsmith"


def test_patch__snowflake_update_config__returns_200(
admin_client: APIClient,
environment: Environment,
Expand Down
Loading