django-cockroachdb
django-cockroachdb copied to clipboard
using AS OF SYSTEM TIME
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?
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")