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

DM-24355: make Database.sync (and methods that use it) transaction-friendly. #391

Merged
merged 4 commits into from Oct 16, 2020

Conversation

TallJimbo
Copy link
Member

No description provided.

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}'.")
Copy link
Member

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):
Copy link
Member

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
Copy link
Member

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?

Copy link
Member Author

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.
@TallJimbo TallJimbo merged commit 77e5f26 into master Oct 16, 2020
@TallJimbo TallJimbo deleted the tickets/DM-24355 branch October 16, 2020 14:27
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.

None yet

2 participants