If your plugin wants to access existing data, this is quite straight forward. Just import the appropiate models and use the full power of SQLAlchemy. Take a look at the (upcoming) database section in the Developer’s Chapter.
If your plugin needs some new space to store data, you should create a new table. Please do not modify core tables. Not doing so might seem inefficient and possibly is. It will help keep things sane and easier to upgrade versions later.
So if you create a new plugin and need new tables, create a file named models.py in your plugin directory. You might take a look at the core’s db.models for some ideas. Here’s a simple one:
from mediagoblin.db.base import Base
from sqlalchemy import Column, Integer, Unicode, ForeignKey
class MediaSecurity(Base):
__tablename__ = "yourplugin__media_security"
# The primary key *and* reference to the main media_entry
media_entry = Column(Integer, ForeignKey('core__media_entries.id'),
primary_key=True)
get_media_entry = relationship("MediaEntry",
backref=backref("security_rating", cascade="all, delete-orphan"))
rating = Column(Unicode)
MODELS = [MediaSecurity]
That’s it.
Some notes:
If your plugin is in use and instances use it to store some data, changing the database design is a tricky thing.
Take a look at the core’s db/migrations.py for some good examples on what you might be able to do. Here’s a simple one to add one column:
from mediagoblin.db.migration_tools import RegisterMigration, inspect_table
from sqlalchemy import MetaData, Column, Integer
MIGRATIONS = {}
@RegisterMigration(1, MIGRATIONS)
def add_license_preference(db):
metadata = MetaData(bind=db.bind)
security_table = inspect_table(metadata, 'yourplugin__media_security')
col = Column('security_level', Integer)
col.create(security_table)
db.commit()