Upgrading to Newer Releases

Version 2.0

Breaking API Changes

  • The syntax query in db is not supported any more. Use db.contains(...) instead.
  • The ConcurrencyMiddleware has been removed due to a insecure implementation (see Issue #18). Consider tinyrecord instead.

Apart from that the API remains compatible to v1.4 and prior.


To improve the handling of IDs TinyDB changed the way it stores data (see Issue #13 for details). Opening an database from v1.4 or prior will most likely result in an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "tinydb\database.py", line 49, in __init__
    self._table = self.table('_default')
  File "tinydb\database.py", line 69, in table
    table = table_class(name, self, **options)
  File "tinydb\database.py", line 171, in __init__
    self._last_id = int(sorted(self._read().keys())[-1])
  File "tinydb\database.py", line 212, in _read
    data[eid] = Element(data[eid], eid)
TypeError: list indices must be integers, not dict

In this case you need to migrate the database to the recent scheme. TinyDB provides a migration script for the default JSON storage:

$ python -m tinydb.migrate db1.json db2.json
Processing db1.json ... Done
Processing db2.json ... Done

Migration with a custom storage

If you have database files that have been written using a custom storage class, you can write your own migration script that calls tinydb.migrate.migrate:

tinydb.migrate.migrate(*args, **kwargs)

Migrate a database to the scheme used in v2.0.

To migrate a db that uses a custom storage, use

>>> from tinydb.migrate import migrate
>>> args = [...]  # args for your storage class
>>> kwargs = {...}  # kwargs for your storage class
>>> migrate(*args, **kwargs, storage=YourStorageClass)
Parameters:storage – The class of the storage to use. Will be initialized with args and kwargs. Default: JSONStorage
Returns:True if the db has been migrated, False if the db didn’t need a migration.

« Changelog

Table Of Contents

Useful Links