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

using AS OF SYSTEM TIME #281

Open
luto opened this issue May 13, 2023 · 1 comment
Open

using AS OF SYSTEM TIME #281

luto opened this issue May 13, 2023 · 1 comment

Comments

@luto
Copy link

luto commented May 13, 2023

Is there an any support for AS OF SYSTEM TIME in either the transaction variant:

BEGIN AS OF SYSTEM TIME '2019-04-09 18:02:52.0+00:00';

or the query variant:

SELECT * FROM t, u, v AS OF SYSTEM TIME '-4h';

?

It seems to me that there is currently no way to use this cockroach feature from within django. The code emitting BEGIN statements seems to be berried 3 dependencies down the line within psycopg.BaseConnection._get_tx_start_command, so I am not sure how to approach this elegantly.

The API could like like this for transactions (extended django.db.transaction.Atomic/.atomic):

from django_cockroachdb.transaction import atomic

with atomic(as_of_system_time="-4h"):
    pass

or like this for queries (modified QuerySet class):

Poll.objects.all().as_of_system_time("-4h")

Are there any plans to add this?

@luto
Copy link
Author

luto commented May 18, 2023

The following works, but completely sidesteps django's transaction management. So I am sure it will lead to hard to debug issues down the road in more complex apps.

from django.db import connection

class AsOfTransaction:
    def __init__(self, as_of_system_time):
        self.as_of_system_time = as_of_system_time

    def __enter__(self):
        connection.cursor().execute("BEGIN AS OF SYSTEM TIME %s", (self.as_of_system_time,))

    def __exit__(self, exc_type, exc_value, traceback):
        if exc_type is not None:
            connection.cursor().execute("ROLLBACK")
        else:
            connection.cursor().execute("COMMIT")

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

No branches or pull requests

1 participant