Setting Up The Database With Proteus

Hi,

I want to play with automatic database setup and thought I could use ‘proteus’ for that. Since I’m going to have many iterations until the code does what it should do, I wanted to use SQLite’s memory database instead of PostgreSQL, which is very, very slow. However, when I follow the instructions given here:

https://docs.tryton.org/projects/client-library/en/latest/

I’ll only get a traceback saying that the ‘ir_module’ is not available. Since it’s an in-memory database, I also can’t use tryton-admin to set the database up. Is there a better way than just lifting all the relevant code from ‘tryton-admin’ and the modules it imports?

Thanks,
Toni

Hi,

You could check the demo script here:

Hi Maxime,
thank you for the idea, but the problem happens already at the secibd line from the documentation that I referenced:

>>> from proteus import config as pconfig
>>> config = pconfig.set_trytond('sqlite:///:memory:')
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/csb43/i18n/__init__.py:7: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.i
o/en/latest/pkg_resources.html
  import pkg_resources
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/csb43/i18n/__init__.py:21: DeprecationWarning: Use setlocale(), getencoding() and getlocale() instead
  lc, encoding = locale.getdefaultlocale()
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/braintree/dispute.py:80: DeprecationWarning: Use ProtectionLevel enum instead
  warnings.warn("Use ProtectionLevel enum instead", DeprecationWarning)
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/braintree/search.py:79: DeprecationWarning: Use protection_level parameter instead
  warnings.warn("Use protection_level parameter instead", DeprecationWarning)
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/zeep/utils.py:1: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
  import cgi
Traceback (most recent call last):
  File "<input>", line 1, in <module>
    config = pconfig.set_trytond('sqlite:///:memory:')
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/proteus/config.py", line 329, in set_trytond
    _CONFIG.current = TrytondConfig(database, user, config_file=config_file)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/proteus/config.py", line 282, in __init__
    self.pool.init()
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/pool.py", line 167, in init
    restart = not load_modules(
                  ^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/modules/__init__.py", line 388, in load_modules
    with Transaction().start(database_name, 0):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 216, in start
    Cache.sync(self)
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/cache.py", line 252, in sync
    cursor.execute(*table.select(
sqlite3.OperationalError: no such table: ir_cache

The script you recommended, also has the same problem. It even doesn’t recognize the database setting and insists on opening an SQLite database file in a completely unrelated location:

$ ./tryton_demo.py --config=test.conf
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/passlib/utils/__init__.py:854: DeprecationWarning: 'crypt' is deprecated and slated for removal in Python 3.13
  from crypt import crypt as _crypt
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/braintree/dispute.py:80: DeprecationWarning: Use ProtectionLevel enum instead
  warnings.warn("Use ProtectionLevel enum instead", DeprecationWarning)
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/braintree/search.py:79: DeprecationWarning: Use protection_level parameter instead
  warnings.warn("Use protection_level parameter instead", DeprecationWarning)
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/csb43/i18n/__init__.py:7: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html
  import pkg_resources
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/csb43/i18n/__init__.py:21: DeprecationWarning: Use setlocale(), getencoding() and getlocale() instead
  lc, encoding = locale.getdefaultlocale()
/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/zeep/utils.py:1: DeprecationWarning: 'cgi' is deprecated and slated for removal in Python 3.13
  import cgi
Traceback (most recent call last):
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 179, in start
    database = backend.Database(database_name).connect()
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/backend/sqlite/database.py", line 365, in connect
    self._make_uri(), uri=True,
    ^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/backend/sqlite/database.py", line 439, in _make_uri
    raise IOError("Database '%s' doesn't exist!" % db_path)
OSError: Database '/home/toni/db/demo.sqlite' doesn't exist!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 239, in stop
    self.rollback()
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 356, in rollback
    for datamanager in self._datamanagers:
                       ^^^^^^^^^^^^^^^^^^
AttributeError: 'Transaction' object has no attribute '_datamanagers'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/toni/mnt/tryton/setups/./tryton_demo.py", line 1014, in <module>
    main(options.database, options.modules, options.demo_password,
  File "/home/toni/mnt/tryton/setups/./tryton_demo.py", line 917, in main
    config = set_config(database, config_file)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/mnt/tryton/setups/./tryton_demo.py", line 26, in set_config
    return pconfig.set_trytond(database, config_file=config_file)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/proteus/config.py", line 329, in set_trytond
    _CONFIG.current = TrytondConfig(database, user, config_file=config_file)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/proteus/config.py", line 282, in __init__
    self.pool.init()
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/pool.py", line 167, in init
    restart = not load_modules(
                  ^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/modules/__init__.py", line 388, in load_modules
    with Transaction().start(database_name, 0):
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 218, in start
    self.stop(False)
  File "/home/toni/.virtualenvs/tryton/lib/python3.11/site-packages/trytond/transaction.py", line 241, in stop
    self.database.put_connection(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'put_connection'

FWIW, I am using a dedicated virtualenv with Tryton 7.0.3 on a Debian Bookworm machine.

Proteus does not setup the database. it must be setup by trytond-admin but this forbid to use memory database because they are only available to the current process.
One way to use the memory database is to use the create_db method of trytond.tests.test_tryton but importing the trytond.tests may have undesirable effect for a production setup.

Hi Cedric,

thank you for the hint! I want to use a memory database to speed up development of something, but not for production use. I have some clunky code that does the database setup, but was hoping to get rid of it. So the next stop is to figure whether ‘create_db’ is a better option. :slight_smile: