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

Add transaction model aiopg #219

Merged
merged 2 commits into from Dec 4, 2017

Conversation

vir-mir
Copy link
Member

@vir-mir vir-mir commented Nov 26, 2016

Todo task

@jettify
Copy link
Member

jettify commented Nov 26, 2016

Just for your information, you can put python3.5 tests into tests/pep492 folder to avoid syntax errors on older python versions.

@codecov-io
Copy link

codecov-io commented Nov 28, 2016

Codecov Report

Merging #219 into master will increase coverage by 0.3%.
The diff coverage is 98.21%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #219     +/-   ##
=========================================
+ Coverage   93.23%   93.53%   +0.3%     
=========================================
  Files          23       25      +2     
  Lines        3427     3648    +221     
  Branches      189      194      +5     
=========================================
+ Hits         3195     3412    +217     
- Misses        194      198      +4     
  Partials       38       38
Impacted Files Coverage Δ
aiopg/utils.py 82.99% <100%> (+1.79%) ⬆️
tests/test_cursor.py 100% <100%> (ø) ⬆️
tests/pep492/test_async_transaction.py 100% <100%> (ø)
aiopg/cursor.py 98.76% <100%> (+0.06%) ⬆️
tests/test_transaction.py 96.55% <96.55%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9382f02...96cf798. Read the comment docs.

@asvetlov
Copy link
Member

@vir-mir would you add missing tests?

@vir-mir
Copy link
Member Author

vir-mir commented Nov 28, 2016

@asvetlov hi! Yes.
I will finish, remove from the WIP (Work in process) title.

@vir-mir
Copy link
Member Author

vir-mir commented Nov 29, 2016

@asvetlov @jettify Done

sorry, I did not write the documentation.
But I added exemples/transactio.py and exemples/transactio_oldstyle.py

@vir-mir vir-mir changed the title WIP: add transaction model aiopg Add transaction model aiopg Nov 29, 2016
@vir-mir
Copy link
Member Author

vir-mir commented Dec 2, 2016

LGTM?

@jettify jettify mentioned this pull request Dec 2, 2016
@asvetlov
Copy link
Member

asvetlov commented Dec 2, 2016

please wait a bit

Copy link
Member

@asvetlov asvetlov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transaction class is perfect but I think we should add method factories to cursor for easy usage like

async with cur.begin():
    ...

async with cur.begin_nested():
    ...

For these methods we should choose appropriate default isolation level -- I have strong opinion that most of library users don't know about isolation levels at all.
At least it was very hard question on interviews provided by me.

__slots__ = ('_cur', '_is_begin', '_isolation', '_unique_id')

def __init__(self, cur, isolation_level: IsolationLevel,
readonly: bool = False, deferrable: bool = False):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Drop spaces around =.
Looks like bool=False is the preferable notation for function annotations.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good

@asvetlov
Copy link
Member

asvetlov commented Dec 4, 2016

@vir-mir we should decide: support typing annotations for the whole project or not.
I like it (while infrastructure is not really ready yet from my perspective).

If we will decide to support -- we should add annotations to every public aiopg method and check it by mypy tool strictly.
If not -- please don't add not checked annotations at all.
Would you be a volunteer for the task?

@jettify
Copy link
Member

jettify commented Dec 4, 2016

Exactly my thoughts, should we support external file like yarl or something inplace like here?

@jettify
Copy link
Member

jettify commented Dec 4, 2016

I believe we will add annotation eventually anyway, but lets drop it from this PR and add them separately with mypy supervision

@vir-mir
Copy link
Member Author

vir-mir commented Dec 5, 2016

Hi.
I propose to put the default isolation Read Committed
I add begin and begin_nested probably tomorrow.

@jettify
Copy link
Member

jettify commented Dec 5, 2016

Read Committed is default for PG so it is reasonable choice.

@vir-mir
Copy link
Member Author

vir-mir commented Dec 7, 2016

@asvetlov I'm add factories in cursor and drop spaces around

example

async def begin(engine):
    async with engine.cursor() as cur:
        async with cur.begin():
            await cur.execute("insert into tbl values(1, 'data')")

        async with cur.begin():
            await cur.execute('select * from tbl')
            row = await cur.fetchall()
            assert row == [(22, 'read only'), (1, 'data'), ]


async def begin_nested(engine):
    async with engine.cursor() as cur:
        async with cur.begin_nested():
            await cur.execute("insert into tbl values(1, 'data')")

            try:
                async with cur.begin_nested():
                    await cur.execute("insert into tbl values(1/0, 'no data')")
            except psycopg2.DataError:
                    pass

            async with cur.begin_nested():
                await cur.execute("insert into tbl values(2, 'data')")

        async with cur.begin_nested():
            await cur.execute('select * from tbl')
            row = await cur.fetchall()
            assert row == [(22, 'read only'), (1, 'data'),  (2, 'data'), ]

@vir-mir
Copy link
Member Author

vir-mir commented Dec 14, 2016

resolve conflicts

@asvetlov any news?

@drednout
Copy link

Hello, everyone!
What is the actual status of this PR? Is there any chances to have transaction functionality for raw SQL in aiopg upstream?

@vir-mir
Copy link
Member Author

vir-mir commented Nov 29, 2017

Dear all!
I rebuilt, I had to do push a force. Sorry. I also pull the master and corrected all the comments.

@asvetlov @jettify review, please

@@ -146,6 +147,16 @@ def callproc(self, procname, parameters=None, *, timeout=None):
else:
yield from self._conn._poll(waiter, timeout)

def begin(self):
return _TransactionBeginContextManager(self._transaction.begin())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not covered

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return _TransactionBeginContextManager(self._transaction.begin())

def begin_nested(self):
if not self._transaction.is_begin:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not called by tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done


@asyncio.coroutine
def __aexit__(self, exc_type, exc_val, exc_tb):
if exc_type is not None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not covered by tests

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@asvetlov
Copy link
Member

asvetlov commented Dec 4, 2017

Cool!
The code looks perfect but lacks documentation.

@jettify
Copy link
Member

jettify commented Dec 4, 2017 via email

@asvetlov
Copy link
Member

asvetlov commented Dec 4, 2017

Lets do docs in separate PR, this one already large.

Ok. @vir-mir please merge.

Documentation is a show stopper for next release.

@vir-mir
Copy link
Member Author

vir-mir commented Dec 4, 2017

@jettify @asvetlov I added code samples for the cursor.
docs of the class of transactions, I propose to pass in another PR

96cf798

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

5 participants