Skip to content

Commit ac63823

Browse files
committed
api: native crud module support
Adds native api support for crud module [1] to use it from a connection object. 1. github.com/tarantool/crud Closes #205
1 parent 1154b3d commit ac63823

15 files changed

+2818
-1
lines changed

Diff for: .github/workflows/packing.yml

+13
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ jobs:
9999
- name: Install test requirements
100100
run: pip3 install -r requirements-test.txt
101101

102+
- name: Install the crud module for testing purposes
103+
run: tarantoolctl rocks install crud
104+
102105
- name: Run tests
103106
run: make test-pure-install
104107

@@ -157,8 +160,12 @@ jobs:
157160
- name: Setup test tarantool instance
158161
shell: wsl-bash_Ubuntu-20.04 {0}
159162
run: |
163+
sudo apt install -y unzip
164+
tarantoolctl rocks install crud
160165
rm -f ./tarantool.pid ./tarantool.log
166+
tarantool ./test/suites/lib/tarantool_python_ci.lua
161167
TNT_PID=$(tarantool ./test/suites/lib/tarantool_python_ci.lua > tarantool.log 2>&1 & echo $!)
168+
cat ./tarantool.log
162169
touch tarantool.pid
163170
echo $TNT_PID > ./tarantool.pid
164171
@@ -331,6 +338,9 @@ jobs:
331338
- name: Install test requirements
332339
run: pip3 install -r requirements-test.txt
333340

341+
- name: Install the crud module for testing purposes
342+
run: tarantoolctl rocks install crud
343+
334344
- name: Run tests
335345
run: make test-pure-install
336346

@@ -492,6 +502,9 @@ jobs:
492502
- name: Install test requirements
493503
run: pip3 install -r requirements-test.txt
494504

505+
- name: Install the crud module for testing purposes
506+
run: tarantoolctl rocks install crud
507+
495508
- name: Run tests
496509
run: make test-pure-install
497510

Diff for: .github/workflows/reusable_testing.yml

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ jobs:
3939
- name: Install test requirements
4040
run: pip3 install -r requirements-test.txt
4141

42+
- name: Install the crud module for testing purposes
43+
run: tarantoolctl rocks install crud
44+
4245
- run: make test

Diff for: .github/workflows/testing.yml

+19
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ jobs:
8585
- name: Install test requirements
8686
run: pip3 install -r requirements-test.txt
8787

88+
- name: Install the crud module for testing purposes
89+
run: tarantoolctl rocks install crud
90+
8891
- name: Run tests
8992
run: make test
9093

@@ -143,6 +146,11 @@ jobs:
143146
- name: Install test requirements
144147
run: pip3 install -r requirements-test.txt
145148

149+
- name: Install the crud module for testing purposes
150+
run: |
151+
source tarantool-enterprise/env.sh
152+
tarantoolctl rocks install crud
153+
146154
- name: Run tests
147155
run: |
148156
source tarantool-enterprise/env.sh
@@ -196,6 +204,9 @@ jobs:
196204
- name: Install test requirements
197205
run: pip3 install -r requirements-test.txt
198206

207+
- name: Install the crud module for testing purposes
208+
run: tarantoolctl rocks install crud
209+
199210
- name: Run tests
200211
run: make test-pure-install
201212

@@ -260,8 +271,12 @@ jobs:
260271
- name: Setup test tarantool instance
261272
shell: wsl-bash_Ubuntu-20.04 {0}
262273
run: |
274+
sudo apt install -y unzip
275+
tarantoolctl rocks install crud
263276
rm -f ./tarantool.pid ./tarantool.log
277+
tarantool ./test/suites/lib/tarantool_python_ci.lua
264278
TNT_PID=$(tarantool ./test/suites/lib/tarantool_python_ci.lua > tarantool.log 2>&1 & echo $!)
279+
cat ./tarantool.log
265280
touch tarantool.pid
266281
echo $TNT_PID > ./tarantool.pid
267282
@@ -332,8 +347,12 @@ jobs:
332347
- name: Setup test tarantool instance
333348
shell: wsl-bash_Ubuntu-20.04 {0}
334349
run: |
350+
sudo apt install -y unzip
351+
tarantoolctl rocks install crud
335352
rm -f ./tarantool.pid ./tarantool.log
353+
tarantool ./test/suites/lib/tarantool_python_ci.lua
336354
TNT_PID=$(tarantool ./test/suites/lib/tarantool_python_ci.lua > tarantool.log 2>&1 & echo $!)
355+
cat ./tarantool.log
337356
touch tarantool.pid
338357
echo $TNT_PID > ./tarantool.pid
339358

Diff for: CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99
### Added
1010
- Support custom packer and unpacker factories (#191).
1111

12+
- Support [crud module](https://github.com/tarantool/crud) native API (#205).
13+
1214
### Changed
1315

1416
### Fixed

Diff for: docs/source/api/submodule-crud.rst

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module :py:mod:`tarantool.crud`
2+
================================
3+
4+
.. automodule:: tarantool.crud

Diff for: docs/source/index.rst

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ API Reference
6363
api/module-tarantool.rst
6464
api/submodule-connection.rst
6565
api/submodule-connection-pool.rst
66+
api/submodule-crud.rst
6667
api/submodule-dbapi.rst
6768
api/submodule-error.rst
6869
api/submodule-mesh-connection.rst

Diff for: docs/source/quick-start.rst

+137
Original file line numberDiff line numberDiff line change
@@ -347,3 +347,140 @@ with out-of-band message processing:
347347
348348
print(callback_res)
349349
>>> [[[100, 1]], [[200, 1]], [[300, 1]]]
350+
351+
352+
Interaction with the crud module
353+
----------------------------------
354+
355+
Through the :class:`~tarantool.Connection` object, you can access
356+
`crud module <https://github.com/tarantool/crud>`_ methods:
357+
358+
.. code-block:: python
359+
360+
>>> import tarantool
361+
>>> from tarantool.error import CrudModuleError, DatabaseError
362+
>>> conn = tarantool.Connection(host='localhost',port=3301)
363+
364+
>>> conn.crud_
365+
conn.crud_count( conn.crud_insert( conn.crud_insert_object_many(
366+
conn.crud_min( conn.crud_replace_object( conn.crud_stats(
367+
conn.crud_unflatten_rows( conn.crud_upsert_many( conn.crud_delete(
368+
conn.crud_insert_many( conn.crud_len( conn.crud_replace(
369+
conn.crud_replace_object_many( conn.crud_storage_info( conn.crud_update(
370+
conn.crud_upsert_object( conn.crud_get( conn.crud_insert_object(
371+
conn.crud_max( conn.crud_replace_many( conn.crud_select(
372+
conn.crud_truncate( conn.crud_upsert( conn.crud_upsert_object_many(
373+
374+
As an example, consider :meth:`~tarantool.Connection.crud_insert` and :meth:`~tarantool.Connection.crud_insert_object_many`.
375+
It is recommended to enclose calls in the try-except construction as follows:
376+
377+
.. code-block:: python
378+
379+
# Insert without exception:
380+
>>> res = conn.crud_insert('tester', (3500,300,'Rob'))
381+
>>> res
382+
<tarantool.crud.CrudResult object at 0x11a56e320>
383+
>>> res.
384+
res.metadata res.rows # fields like in Lua implementation.
385+
>>> res.rows
386+
[[3500, 300, 'Rob']]
387+
>>> res.metadata
388+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
389+
390+
# Insert with exception (duplicate key exists):
391+
>>> try:
392+
... res = conn.crud_insert('tester', (3500,300,'Rob'))
393+
... except CrudModuleError as e:
394+
... exc_crud = e
395+
...
396+
>>> exc_crud
397+
CrudModuleError(None, <tarantool.crud.CrudError object at 0x10a276950>)
398+
>>> exc_crud.
399+
exc_crud.args exc_crud.err exc_crud.res exc_crud.with_traceback(
400+
>>> exc_crud.args
401+
(None, <tarantool.crud.CrudError object at 0x10a276950>)
402+
>>> exc_crud.err
403+
<tarantool.crud.CrudError object at 0x10a276950>
404+
>>> exc_crud.err. # fields like in Lua implementation.
405+
exc_crud.err.class_name exc_crud.err.err exc_crud.err.file exc_crud.err.line exc_crud.err.str
406+
>>> exc_crud.err.class_name
407+
'InsertError'
408+
>>> exc_crud.err.str
409+
'InsertError: Failed to insert: Duplicate key exists in unique index "primary_index" in space "tester" with old tuple - [3500, 300, "Rob"] and new tuple - [3500, 300, "Rob"]'
410+
>>> exc_crud.res is None # using only in case of *_many.
411+
True
412+
413+
# In case of batch operation (*_many), CrudModuleError contains both result and errors:
414+
>>> try:
415+
... res = conn.crud_insert_object_many('tester', ({'id':1800,'bucket_id':100,'name':'Mike'}, {'id':2800,'bucket_id':100,'second_name':'Bill'}, {'id':3800,'bucket_id':100,'name':'Bob'}, {'id':1,'bucket_id':100,'name':'Rob'}), {'timeout':100, 'rollback_on_error':False})
416+
... except CrudModuleError as e:
417+
... exc_crud = e
418+
...
419+
>>> exc_crud
420+
CrudModuleError(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
421+
>>> exc_crud.
422+
exc_crud.args exc_crud.errs exc_crud.res exc_crud.with_traceback(
423+
>>> exc_crud.res # some of the rows were inserted.
424+
<tarantool.crud.CrudResult object at 0x11a56f310>
425+
>>> exc_crud.res.rows
426+
[[1800, 100, 'Mike'], [3800, 100, 'Bob']]
427+
>>> exc_crud.errs # some of the rows were not inserted.
428+
[<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>]
429+
>>> exc_crud.errs[0].str
430+
'CallError: Failed for 56d9c861-a8ac-459c-9715-09094ebb94a7: Function returned an error: Duplicate key exists in unique index "primary_index" in space "tester" with old tuple - [1, 100, "Mike"] and new tuple - [1, 100, "Rob"]'
431+
>>> exc_crud.errs[1].str
432+
'InsertManyError: Failed to flatten object: FlattenError: Object is specified in bad format: FlattenError: Unknown field "second_name" is specified'
433+
>>> exc_crud.args
434+
(<tarantool.crud.CrudResult object at 0x11a56f310>, [<tarantool.crud.CrudError object at 0x11a56e9e0>, <tarantool.crud.CrudError object at 0x11a56f490>])
435+
436+
If module crud not found on the router:
437+
438+
.. code-block:: python
439+
440+
>>> try:
441+
... res = conn.crud_insert('tester', (22221,300,'Rob'))
442+
... except CrudModuleError as e:
443+
... exc_crud = e
444+
... except DatabaseError as e:
445+
... exc_db = e
446+
...
447+
>>> exc_db
448+
DatabaseError(33, "Procedure 'crud.insert' is not defined. Ensure that you're calling crud.router and user have enough grants")
449+
450+
Using :meth:`~tarantool.Connection.crud_select` and :meth:`~tarantool.Connection.crud_unflatten_rows`:
451+
452+
.. code-block:: python
453+
454+
>>> try:
455+
... res = conn.crud_select('tester', {}, {'first':2})
456+
... except CrudModuleError as e:
457+
... exc_crud = e
458+
...
459+
>>> res
460+
<tarantool.crud.CrudResult object at 0x10a276d10>
461+
>>> res.
462+
res.metadata res.rows
463+
>>> res.rows
464+
[[1, 100, 'Mike'], [2, 100, 'Mike']]
465+
>>> res.metadata
466+
[{'name': 'id', 'type': 'unsigned'}, {'name': 'bucket_id', 'type': 'unsigned'}, {'name': 'name', 'type': 'string'}]
467+
>>> r = conn.crud_unflatten_rows(res.rows, res.metadata)
468+
>>> r
469+
[{'id': 1, 'bucket_id': 100, 'name': 'Mike'}, {'id': 2, 'bucket_id': 100, 'name': 'Mike'}]
470+
471+
Using :meth:`~tarantool.Connection.crud_truncate` and :meth:`~tarantool.Connection.crud_len`:
472+
473+
.. code-block:: python
474+
475+
>>> try:
476+
... res = conn.crud_len('tester')
477+
... except CrudModuleError as e:
478+
... exc_crud = e
479+
>>> res
480+
26
481+
>>> try:
482+
... res = conn.crud_truncate('tester')
483+
... except CrudModuleError as e:
484+
... exc_crud = e
485+
>>> res
486+
True

0 commit comments

Comments
 (0)