Skip to content

Configure the Path of a SQLite Database

Overview

We want to use different databases in the different environments:

  • development
  • tests
  • production / live

We use pydantic.Settings and an environment variable to make the path configurable.

  • For development: set the environment variable.
  • For tests: Use a pytest fixture with tmpdir to define a custom path.

Config

In a config.py:

from pathlib import Path

from pydantic import BaseSettings, Field
from sqlite_utils import Database

from pycon_catalogue.initialize import init_db


class Settings(BaseSettings):
    db_path: Path = Field(
        Path.home() / "pycon-catalogue.db", env="APP_NAME_DB_PATH"
    )

    def db(self) -> Database:
        return Database(self.db_path)


settings: Settings = Settings()

Instead of Path.home(), you can also use a more sophisticated default value:

Setting a Development Database

export APP_NAME_DB_PATH=$HOME/dev-app-name.db

Using a Temporary Database for Tests

Use a db in tmpdir.

from pathlib import Path

import pytest

from app_name import config


@pytest.fixture(autouse=True)
def app_db_path(tmpdir):
    config.settings.db_path = Path(tmpdir / "test-app-name.db")

More Info