Skip to main content

MusicBrainz Database Tools

Project description

pypi badge

If you are developing a Python application that needs access to the MusicBrainz data, you can use the mbdata.models module to get SQLAlchemy models mapped to the MusicBrainz database tables.

All tables from the MusicBrainz database are mapped, all foreign keys have one-way relationships set up and some models, where it’s essential to access their related models, have two-way relationships (collections) set up.

In order to work with the relationships efficiently, you should use the appropriate kind of eager loading.

Example usage of the models:

>>> from sqlalchemy import create_engine
>>> from sqlalchemy.orm import sessionmaker
>>> from mbdata.models import Artist
>>> engine = create_engine('postgresql://musicbrainz:musicbrainz@127.0.0.1/musicbrainz', echo=True)
>>> Session = sessionmaker(bind=engine)
>>> session = Session()
>>> artist = session.query(Artist).filter_by(gid='8970d868-0723-483b-a75b-51088913d3d4').first()
>>> print artist.name

If you use the models in your own application and want to define foreign keys from your own models to the MusicBrainz schema, you will need to let mbdata know which metadata object to add the MusicBrainz tables to:

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

# this should be the first place where you import anything from mbdata
import mbdata.config
mbdata.config.configure(base_class=Base)

# now you can import and use the mbdata models
import mbdata.models

You can also use mbdata.config to re-map the MusicBrainz schema names, if your database doesn’t follow the original structure:

import mbdata.config
mbdata.config.configure(schema='my_own_mb_schema')

If you need sample MusicBrainz data for your tests, you can use mbdata.sample_data:

from mbdata.sample_data import create_sample_data
create_sample_data(session)

Development

Normally you should work against a regular PostgreSQL database with MusicBrainz data, but for testing purposes, you can use a SQLite database with small data sub-set used in unit tests. You can create the database using:

./bin/create_sample_db.py sample.db

Then you can change your configuration:

DATABASE_URI = 'sqlite:///sample.db'

Running tests:

pytest -v

If you want to see the SQL queries from a failed test, you can use the following:

MBDATA_DATABASE_ECHO=1 pytest -v

GitHub Actions workflow that automatically runs the tests after each commit is here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

mbdata-31.0.0.tar.gz (200.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

mbdata-31.0.0-py3-none-any.whl (247.3 kB view details)

Uploaded Python 3

File details

Details for the file mbdata-31.0.0.tar.gz.

File metadata

  • Download URL: mbdata-31.0.0.tar.gz
  • Upload date:
  • Size: 200.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mbdata-31.0.0.tar.gz
Algorithm Hash digest
SHA256 9d624cbd4add8d8eb20e3cd59bc04dd64a26c01f98a72571bdefea0ea7a6be3e
MD5 a7d3d18f894bb63bf12f028574407ecc
BLAKE2b-256 47f58b7ef1c2a52814f454d818cc15a8f79eee131ab31228018697db300dc40a

See more details on using hashes here.

Provenance

The following attestation bundles were made for mbdata-31.0.0.tar.gz:

Publisher: publish.yml on metabrainz/mbdata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file mbdata-31.0.0-py3-none-any.whl.

File metadata

  • Download URL: mbdata-31.0.0-py3-none-any.whl
  • Upload date:
  • Size: 247.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for mbdata-31.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0384eb8951ad71d2d3127bd6d44047f0b055017d3286f885a94330e336b5af6d
MD5 cb7858a681c25c8336de1dace872c566
BLAKE2b-256 71610c43aa4d9cabdee02c8dc8a1e4ee514395d8866e105b4bd7f2eb14f5f5cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for mbdata-31.0.0-py3-none-any.whl:

Publisher: publish.yml on metabrainz/mbdata

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page