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

Calculate New expires_at Using valid_for_iso From Items #1874

Merged
merged 1 commit into from
Jun 26, 2023

Conversation

pavelbrm
Copy link
Contributor

Summary

This PR is also aiming at addressing #1864.

Unlike #1870, which is very specific, this uses the maximum available valid_for_iso from the order's items instead of a hardcoded period in the query.

Note On Querying

The PR proposes a subquery for getting the period information instead of a JOIN. I ran both approaches with the planner, and think that the proposed approach is cleaner and efficient.

For comparison, this is that the PR is proposing:

SELECT
    COALESCE(last_paid_at, now()) +
    (SELECT COALESCE(MAX(valid_for_iso::interval), interval '1 month') FROM order_items WHERE order_id = $2)
AS expires_at FROM orders WHERE id = $1

A query using JOIN could be something like (which is what I checked):

SELECT COALESCE(last_paid_at, now()) + COALESCE(MAX(order_items.valid_for_iso::interval), interval '1 month') AS expires_at
FROM orders
INNER JOIN order_items ON order_items.order_id = orders.id
WHERE orders.id = $1
GROUP BY last_paid_at LIMIT 1;

The query plan in the second case contains more work, which is not necessary. I might be wrong though.

Type of Change

  • Product feature
  • Bug fix
  • Performance improvement
  • Refactor
  • Other

Tested Environments

  • Development
  • Staging
  • Production

Before Submitting

  • Does your code build cleanly without any errors or warnings?
  • Have you used auto closing keywords?
  • Have you added tests for new functionality?
  • Have validated query efficiency for new database queries?
  • Have documented new functionality in README or in comments?
  • Have you squashed all intermediate commits?
  • Is there a clear title that explains what the PR does?
  • Have you used intuitive function, variable and other naming?
  • Have you requested security / privacy review if needed
  • Have you performed a self review of this PR?

Manual Test Plan:

@pavelbrm pavelbrm self-assigned this Jun 23, 2023
@pavelbrm
Copy link
Contributor Author

pavelbrm commented Jun 23, 2023

Please note, this PR depends on #1873.

Base automatically changed from update-codeql-settings to master June 23, 2023 08:19
@pavelbrm pavelbrm merged commit a1656dd into master Jun 26, 2023
@pavelbrm pavelbrm deleted the fix-order-expires-at-02 branch June 26, 2023 06:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants