-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
feat: add timezone in Prefer header #3024
Conversation
context "check behaviour of Prefer: timezone=America/Los_Angeles" $
it "should change timezone" $
request methodGet "/timezone_values?select=*"
[("Prefer", "handling=strict, timezone=America/Los_Angeles")]
""
`shouldRespondWith`
[json|[{"t":"2023-10-18T05:37:59.611-07:00"}, {"t":"2023-10-18T07:37:59.611-07:00"}, {"t":"2023-10-18T09:37:59.611-07:00"}]|]
{ matchStatus = 200
, matchHeaders = [matchContentTypeJson] } Strangely, this test is failing. But, if I run this test locally with
@steve-chavez @laurenceisla Is there something missing in the |
@taimoorzaeem Yes, the spec tests don't load the in-db config. This is done with AppState.reReadConfig, and only runs for the CLI. To make it work, the resulting Lines 75 to 84 in 1e0b205
But that might require some refactoring. Could be done in other PR.
That's another option too. It would be the simplest one for now. |
1e0b205
to
58ae646
Compare
@steve-chavez Hmm, the |
58ae646
to
8a08996
Compare
def test_prefer_timezone_with_invalid_timezone(defaultenv):
"timezone=America/XXX should set timezone to default timezone"
env = {**defaultenv, "PGRST_DB_CONFIG": "true", "PGRST_JWT_SECRET": SECRET}
headers = {
"Prefer": "handling=strict, timezone=America/XXX",
}
with run(env=env) as postgrest:
response = postgrest.session.get("/timezone_values", headers=headers)
response_body = '[{"t":"2023-10-18T17:37:59.611+05:00"}, \n {"t":"2023-10-18T19:37:59.611+05:00"}, \n {"t":"2023-10-18T21:37:59.611+05:00"}]'
assert response.text == response_body This test sets the timezone to default timezone for invalid timezones. On my system, we have PKT timezone GMT+05 and so the tests are working fine locally. However, on github CI the default timezone is |
Edited: According to the docs, the environment variable used to set a db timezone is postgrest/nix/tools/withTools.nix Line 75 in b235227
Replace Looks like it was setting the default time zone of the host, with this change that should be fixed. |
79c0822
to
c9db1eb
Compare
Looking good. Just those refactors and it should be ready to merge. BTW. Do you know why is it giving those IO errors? |
c9db1eb
to
9def5ec
Compare
Nope, I don't know why IO-tests are failing on some pg versions. |
7879124
to
b626142
Compare
b626142
to
965d345
Compare
@steve-chavez Good catch! The only thing left here I guess is that these tests don't belong to |
I'm thinking about this, can the result of the postgrest/src/PostgREST/SchemaCache.hs Lines 132 to 133 in f10d413
If I'm not mistaken, the Spec Tests should work OK then. |
@laurenceisla You're right. So the only reason to use the in-db config for db settings is to not expose them in the OpenAPI output (which uses the scheme cache). Some things like the @taimoorzaeem One more thing, the
This might be easier to test once you move the io tests to spec tests. |
@taimoorzaeem Also a note. When moving the timezones to the Schema Cache, you can just leave the instance JSON.ToJSON SchemaCache where
toJSON (SchemaCache tabs rels routs reps _ _) = JSON.object [
"dbTables" .= JSON.toJSON tabs
, "dbRelationships" .= JSON.toJSON rels
, "dbRoutines" .= JSON.toJSON routs
, "dbRepresentations" .= JSON.toJSON reps
, "dbMediaHandlers" .= JSON.emptyArray
, "dbTimezones" .= JSON.emptyArray
] We're working on a pure SQL generator for OpenAPI on https://github.com/PostgREST/postgrest-openapi/. So it's not worth the effort to do it on the Haskell side for now. |
hasTimezone p = BS.take 9 p == "timezone=" | ||
getTimezoneFromPrefs = listToMaybe [ PreferTimezone (BS.drop 9 p) | p <- prefs, hasTimezone p && S.member (BS.drop 9 p) acceptedTzNames] | ||
checkPrefs p = p `notElem` acceptedPrefs && BS.drop 9 p `S.notMember` acceptedTzNames |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will DRY this a bit. I think the BS.drop 9/BS.take 9
can be removed.
postJsonArrayTest "1000" "/perf_articles?columns=id,body" "15M" | ||
postJsonArrayTest "10000" "/perf_articles?columns=id,body" "15M" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get this locally too. I think the increased memory is because of the cached timezones (~1K).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@taimoorzaeem Great work! 💯
This should fix #2799.