Creating and dropping SQL databases

This module provides tools to create/drop and use databases specified by DB URL. This module only handles SQLite and PostgreSQL, but abstracts the differences between the two.

class clldutils.db.DB(url, log=None)[source]

A relational database specified by DB URL. Supported dialects are “sqlite” and “postgresql”.

Parameters:
  • url (str) –

  • log (typing.Optional[logging.Logger]) –

classmethod from_settings(settings, log=None)[source]

Instantiate a DB looking up the URL in a dict.

Parameters:

settings (dict) – A dict as returned - e.g. - by pyramid.paster.get_appsettings.

exists()[source]

Does the database exist?

Return type:

bool

create()[source]

Create the database

Raises:

ValueError – If the database already exists.

drop()[source]

Drop the database or remove the db file for sqlite.

class clldutils.db.TempDB(url, log=None)[source]

Context manager to use a temporary database.

>>> with TempDB(url) as db:
...     assert db.exists()
>>> assert not db.exists()
Parameters:
  • url (str) –

  • log (typing.Optional[logging.Logger]) –

class clldutils.db.FreshDB(url, log=None)[source]

Context manager to use a newly created database.

>>> with FreshDB(url) as db:
...     assert db.exists()
Parameters:
  • url (str) –

  • log (typing.Optional[logging.Logger]) –