MusicBrainz Database Tools
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file mbdata-30.0.1.tar.gz
.
File metadata
- Download URL: mbdata-30.0.1.tar.gz
- Upload date:
- Size: 200.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
04915b5475a4db9abd77d1302a7b79f511cc06e5c956fd1ec76df4fcef2d0016
|
|
MD5 |
ae5b5fa267ca8141b63368943b40da80
|
|
BLAKE2b-256 |
3455239c58b65694c8cff925361efb24ecade6a7acccaec5f397ded91f4e39e4
|
Provenance
The following attestation bundles were made for mbdata-30.0.1.tar.gz
:
Publisher:
publish.yml
on metabrainz/mbdata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
mbdata-30.0.1.tar.gz
-
Subject digest:
04915b5475a4db9abd77d1302a7b79f511cc06e5c956fd1ec76df4fcef2d0016
- Sigstore transparency entry: 214577985
- Sigstore integration time:
-
Permalink:
metabrainz/mbdata@648c34e19327fbc12ccfec6f363f456315da1024
-
Branch / Tag:
refs/tags/v30.0.1
- Owner: https://github.com/metabrainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish.yml@648c34e19327fbc12ccfec6f363f456315da1024
-
Trigger Event:
release
-
Statement type:
File details
Details for the file mbdata-30.0.1-py3-none-any.whl
.
File metadata
- Download URL: mbdata-30.0.1-py3-none-any.whl
- Upload date:
- Size: 246.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
acf56766ce31225ebad99f6af6ee322debebb744ba4cae67de3ff46254a3a6da
|
|
MD5 |
a8f98336c076ba81e38b63cb0056af29
|
|
BLAKE2b-256 |
04c41d142c2d604da0b455c9ea851553b5be253baec97778b467b10d70e2235c
|
Provenance
The following attestation bundles were made for mbdata-30.0.1-py3-none-any.whl
:
Publisher:
publish.yml
on metabrainz/mbdata
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1
-
Predicate type:
https://docs.pypi.org/attestations/publish/v1
-
Subject name:
mbdata-30.0.1-py3-none-any.whl
-
Subject digest:
acf56766ce31225ebad99f6af6ee322debebb744ba4cae67de3ff46254a3a6da
- Sigstore transparency entry: 214577987
- Sigstore integration time:
-
Permalink:
metabrainz/mbdata@648c34e19327fbc12ccfec6f363f456315da1024
-
Branch / Tag:
refs/tags/v30.0.1
- Owner: https://github.com/metabrainz
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com
-
Runner Environment:
github-hosted
-
Publication workflow:
publish.yml@648c34e19327fbc12ccfec6f363f456315da1024
-
Trigger Event:
release
-
Statement type: