Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(versioning): scheduled changes incorrectly considered live #4119

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
12 changes: 11 additions & 1 deletion api/features/versioning/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from pathlib import Path

from django.db.models.query import QuerySet, RawQuerySet
from django.utils import timezone
from softdelete.models import SoftDeleteManager

if typing.TYPE_CHECKING:
Expand Down Expand Up @@ -53,5 +54,14 @@ def _get_latest_versions(

return self.raw(
get_latest_versions_sql,
params={"environment_id": environment_id, "api_key": environment_api_key},
params={
"environment_id": environment_id,
"api_key": environment_api_key,
# TODO:
# It seems as though there is a timezone issue when using postgres's
# built in now() function, so we pass in the current time from python.
# Using <= now() in the SQL query returns incorrect results.
# More investigation is needed here to understand the cause.
"live_from_before": timezone.now().isoformat(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer avoiding the use of Python's now() here. Maybe we can leave a TODO here to revisit it in the future?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the comment to use a TODO.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, so this works (after updating the test to schedule the other version for 2 hours later):

and efv2."live_from" <= now() + interval '90 minutes'

Indicating that it likely is some TZ issue here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, that's not true because even this works:

and efv2."live_from" <= now() + interval '1 second'

},
)
1 change: 1 addition & 0 deletions api/features/versioning/sql/get_latest_versions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ join (
where
efv2."deleted_at" is null
and efv2."published_at" is not null
and efv2."live_from" <= %(live_from_before)s
group by
efv2."feature_id",
efv2."environment_id"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import typing
from datetime import timedelta

import pytest
from django.utils import timezone
Expand Down Expand Up @@ -248,3 +249,31 @@ def test_update_version_webhooks_triggered_when_version_published(
kwargs={"environment_feature_version_uuid": str(new_version.uuid)},
delay_until=new_version.live_from,
)


def test_get_latest_versions_does_not_return_versions_scheduled_for_the_future(
environment_v2_versioning: Environment,
feature: "Feature",
admin_user: "FFAdminUser",
) -> None:
# Given
version_0 = EnvironmentFeatureVersion.objects.get(
environment=environment_v2_versioning, feature=feature
)

# Let's create a version scheduled for the future, that we'll publish
scheduled_version = EnvironmentFeatureVersion.objects.create(
environment=environment_v2_versioning,
feature=feature,
live_from=timezone.now() + timedelta(hours=1),
)
scheduled_version.publish(admin_user)

# When
latest_versions = EnvironmentFeatureVersion.objects.get_latest_versions_as_queryset(
environment_id=environment_v2_versioning.id
)

# Then
assert latest_versions.count() == 1
assert latest_versions.first() == version_0
Loading