From a9dac7c83df860ed66ef187411394d25afd77598 Mon Sep 17 00:00:00 2001 From: Elrond Date: Fri, 24 Feb 2012 21:16:02 +0100 Subject: [PATCH] Normalize MediaFile.name (make it a foreignkey) The name part of a MediaFile is only using a very limited number of items. Currently things like "original" or "thumb". So instead of storing the string on each entry, just store a short integer referencing the FileKeynames table and have the appropiate string there. --- mediagoblin/db/sql/models.py | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/mediagoblin/db/sql/models.py b/mediagoblin/db/sql/models.py index d0d1a32d..38c32e14 100644 --- a/mediagoblin/db/sql/models.py +++ b/mediagoblin/db/sql/models.py @@ -23,7 +23,7 @@ import datetime from sqlalchemy import ( Column, Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey, - UniqueConstraint) + UniqueConstraint, PrimaryKeyConstraint, SmallInteger) from sqlalchemy.orm import relationship from sqlalchemy.orm.collections import attribute_mapped_collection from sqlalchemy.sql.expression import desc @@ -177,6 +177,26 @@ class MediaEntry(Base, MediaEntryMixin): pass +class FileKeynames(Base): + """ + keywords for various places. + currently the MediaFile keys + """ + __tablename__ = "core__file_keynames" + id = Column(Integer, primary_key=True) + name = Column(Unicode, unique=True) + + def __repr__(self): + return "" % (self.id, self.name) + + @classmethod + def find_or_new(cls, name): + t = cls.query.filter_by(name=name).first() + if t is not None: + return t + return cls(name=name) + + class MediaFile(Base): """ TODO: Highly consider moving "name" into a new table. @@ -186,13 +206,22 @@ class MediaFile(Base): media_entry = Column( Integer, ForeignKey(MediaEntry.id), - nullable=False, primary_key=True) - name = Column(Unicode, primary_key=True) + nullable=False) + name_id = Column(SmallInteger, ForeignKey(FileKeynames.id), nullable=False) file_path = Column(PathTupleWithSlashes) + __table_args__ = ( + PrimaryKeyConstraint('media_entry', 'name_id'), + {}) + def __repr__(self): return "" % (self.name, self.file_path) + name_helper = relationship(FileKeynames, lazy="joined", innerjoin=True) + name = association_proxy('name_helper', 'name', + creator=FileKeynames.find_or_new + ) + class MediaAttachmentFile(Base): __tablename__ = "core__attachment_files" -- 2.25.1