Skip to main content

DbDeclare

Python version License PyPI

A declarative layer for your database, built on SQLAlchemy.

The code is on GitHub.

The docs are hosted on GitHub pages.

The project is published on PyPI.

Overview

What is it?

DbDeclare is a Python package that helps you create and manage entities in your database cluster, like databases, roles, access control, and (eventually) more. It aims to fill the gap between SQLAlchemy (SQLA) and infrastructure as code (IaC).

Why use it?

DbDeclare does what SQLA does but for database entities beyond tables and columns. You can:

  • Declare desired state in Python
  • Avoid maintaining raw SQL
  • Tightly integrate your databases, roles, access control, and more with your tables

Future versions will have more features, and you will be able to:

  • Have version control over database changes (like Alembic)
  • Define upgrades/downgrades without explicitly defining the changes (like autogen)

Additionally, DbDeclare is:

  • Typed: Type-checking done via mypy
  • Thoroughly commented: There are docstrings for every method and class
  • Well-tested: Though this is a new package under active development, solid test coverage is a high priority

Running SQL scripts before SQLA and after IaC can be messy and hard to maintain. If you prefer to have databases, roles, and the like declared alongside your infrastructure, then there are great tools available for that, like Terraform and Pulumi's providers for Postgres and MySQL. So if you want it tied to that, great! But if, like me, you want it closer to your application code and alongside SQLA, this tool likely makes more sense for you.

Requirements

This requires a recent version of Python. Works with Python 3.11 or higher. We recommend pyenv to install any versions of Python you need and don't currently have installed.

This also requires a compatible driver/package for your database of choice, like psycopg for Postgres. You'll also need a functioning cluster to interact with (the example below shows a way to do so via Docker).

SQLAlchemy is a dependency and will be installed when you install DbDeclare. DbDeclare works with SQLAlchemy 2.0.0 or higher.

Installation

DbDeclare is published on PyPI. You can install it with pip or any tool that uses pip under the hood. This is typically installed in a virtual environment.

> pip install db-declare

Example

Here is a simple Postgres example. We will create a database and a user, and make sure the user can connect to the database. You need a Postgres cluster/instance and a python environment.

If needed, an easy way to spin up a Postgres instance is with Docker, specifically the official Postgres image:

> docker run --rm --name postgres -e POSTGRES_PASSWORD=postgres -p 127.0.0.1:5432:5432/tcp postgres

This spins up a Postgres instance with the default database name of postgres, an admin user of postgres with the password postgres, on port 5432.

Assuming you have a Python environment set up, DbDeclare installed, and psycopg installed (pip install psycopg), you can create a database and a user that can connect to it like this:

from sqlalchemy import create_engine

from dbdeclare.controller import Controller
from dbdeclare.data_structures import GrantOn, Privilege
from dbdeclare.entities import Database, Role


def main() -> None:
    # declare the database
    falafel_db = Database(name="falafel")
    # declare the user
    Role(
        name="hungry_user",
        login=True,  # (1)!
        password="fakepassword",  # (2)!
        grants=[GrantOn(privileges=[Privilege.CONNECT], on=[falafel_db])],  # (3)!
    )

    # create engine with admin user and default database
    engine = create_engine(url="postgresql+psycopg://postgres:postgres@127.0.0.1:5432/postgres")  # (4)!
    # create all entities and grant all privileges
    Controller.run_all(engine=engine)


if __name__ == "__main__":
    main()
  1. Make sure this role can log in (make it a user)
  2. Provide a password for the user to log in with
  3. Specify that this user can connect to the falafel database
  4. The engine to run DbDeclare must have admin privileges, so we use the postgres user here

After running this script, you should be able to access the falafel database as hungry_user. You can try it out with psql (if you don't have it installed, find it here). In a separate shell from where the docker run command is running, you can run:

> psql -h 127.0.0.1 -p 5432 -U hungry_user -d falafel

password for user hungry_user: ***

falafel=>

Voila! Check out the user guide for more involved use cases.

Contributing

Check out development, testing, and contributing guidance here.

License

This project is licensed under the terms of the MIT license.

Metadata

Release files for dbdeclare 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbdeclare 0.0.1
File Size Uploaded Provenance Actions
dbdeclare-0.0.1.tar.gz 18.2 kB Without provenance Details for dbdeclare-0.0.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dbdeclare 0.0.1
File Interpreter ABI Platform Actions
dbdeclare-0.0.1-py3-none-any.whl Python 3 none any Details for dbdeclare-0.0.1-py3-none-any.whl

Total release size: 41.5 kB

Release files / dbdeclare-0.0.1.tar.gz

Download URL dbdeclare-0.0.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
0c78bf5a558870d3cd4d111b1aa72fd60a59c9bc2ddb99e2cc90f70a477f99c2
BLAKE2b-256 checksum
How to use checksums
dc45fde4c15412014aa30bd843b3f89dbda62c15bb25c1f4af545decd69b3313
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Linux/5.4.0-139-generic

Release files / dbdeclare-0.0.1-py3-none-any.whl

Download URL dbdeclare-0.0.1-py3-none-any.whl
Size 23.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
34bb4caf2c0fde0cbcbff31fba5edee262da1ab2973baba44a2c72a6d565c2b6
BLAKE2b-256 checksum
How to use checksums
a62ba229266416d8ce04f741ac0fee5ca7d2cd0543db7f154b1c4385fac6e69e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.2.2 CPython/3.10.8 Linux/5.4.0-139-generic

Security for dbdeclare 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page