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
DM-24355: make Database.sync (and methods that use it) transaction-friendly. #391
Conversation
8578324
to
75baaf0
Compare
def ensure(self, table: sqlalchemy.schema.Table, *rows: dict) -> int: | ||
# Docstring inherited. | ||
if not (self.isWriteable() or table.key in self._tempTables): | ||
raise ReadOnlyDatabaseError(f"Attempt to esnure into read-only database '{self}'.") |
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.
"esnure"
@@ -417,6 +433,17 @@ def replace(self, table: sqlalchemy.schema.Table, *rows: dict) -> None: | |||
) | |||
self._connection.execute(_Replace(table), *rows) | |||
|
|||
def ensure(self, table: sqlalchemy.schema.Table, *rows: dict) -> int: | |||
if not (self.isWriteable() or table.key in self._tempTables): |
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.
This check is starting to appear in many places. Could we instead do something like a
self.assertWriteable(table)
with an optional message string?
@@ -65,15 +65,16 @@ class _Replace(sqlalchemy.sql.Insert): | |||
pass | |||
|
|||
|
|||
# SQLite and PostgreSQL use similar syntax for their ON CONFLICT extension, | |||
# but SQLAlchemy only knows about PostgreSQL's, so we have to compile some |
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.
That seems odd that sqlite doesn't support it in sqlalchemy. Is it just that sqlalchemy team haven't got around to it or there are problems with it? Could we submit a patch?
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 think it's slightly new functionality in SQLite, but not that new. I imagine a patch would be appreciated, but it'd be a while before I got around to it.
...or whatever that database engine's equivalent is (PostgreSQL and SQLite both use ON CONFLICT DO NOTHING). This is a pretty straightforward variant of the existing replace() implementations, but while it's usable in its own right, my main goal is to use it to make sync transaction-friendly in a future commit.
The new implementation uses more engine-specific functionality - namely the new ensure() method and table locking - but it's much simpler, and the most importantly it can now be used inside transactions, which will be huge for making higher-level operations idempotent.
9ae1c2a
to
be984f6
Compare
No description provided.