Fixing our broken CollectionItem unique constraint.

This one seems to work nicely in all relevant situations.
See comments inside the source.
This commit is contained in:
Elrond 2012-11-28 16:44:43 +01:00 committed by Christopher Allan Webber
parent afe0afdb88
commit 0f14c362c3

View File

@ -18,8 +18,10 @@ import datetime
from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger, from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger,
Integer, Unicode, UnicodeText, DateTime, Integer, Unicode, UnicodeText, DateTime,
ForeignKey, UniqueConstraint) ForeignKey)
from sqlalchemy.exc import ProgrammingError
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from migrate.changeset.constraint import UniqueConstraint
from mediagoblin.db.sql.util import RegisterMigration from mediagoblin.db.sql.util import RegisterMigration
from mediagoblin.db.sql.models import MediaEntry, Collection, User from mediagoblin.db.sql.models import MediaEntry, Collection, User
@ -92,15 +94,20 @@ class CollectionItem_v0(declarative_base()):
## This should be activated, normally. ## This should be activated, normally.
## But this would change the way the next migration used to work. ## But this would change the way the next migration used to work.
## So it's commented for now. ## So it's commented for now.
# __table_args__ = ( __table_args__ = (
# UniqueConstraint('collection', 'media_entry'), UniqueConstraint('collection', 'media_entry'),
# {}) {})
collectionitem_unique_constraint_done = False
@RegisterMigration(4, MIGRATIONS) @RegisterMigration(4, MIGRATIONS)
def add_collection_tables(db_conn): def add_collection_tables(db_conn):
Collection_v0.__table__.create(db_conn.bind) Collection_v0.__table__.create(db_conn.bind)
CollectionItem_v0.__table__.create(db_conn.bind) CollectionItem_v0.__table__.create(db_conn.bind)
global collectionitem_unique_constraint_done
collectionitem_unique_constraint_done = True
db_conn.commit() db_conn.commit()
@ -128,3 +135,34 @@ class ProcessingMetaData_v0(declarative_base()):
def create_processing_metadata_table(db): def create_processing_metadata_table(db):
ProcessingMetaData_v0.__table__.create(db.bind) ProcessingMetaData_v0.__table__.create(db.bind)
db.commit() db.commit()
@RegisterMigration(7, MIGRATIONS)
def fix_CollectionItem_v0_constraint(db_conn):
"""Add the forgotten Constraint on CollectionItem"""
global collectionitem_unique_constraint_done
if collectionitem_unique_constraint_done:
print "Okay, already done, short circuit"
# Reset it. Maybe the whole thing gets run again
# For a different db?
collectionitem_unique_constraint_done = False
return
metadata = MetaData(bind=db_conn.bind)
CollectionItem_table = Table('core__collection_items',
metadata, autoload=True, autoload_with=db_conn.bind)
constraint = UniqueConstraint('collection', 'media_entry',
name='core__collection_items_collection_media_entry_key',
table=CollectionItem_table)
try:
constraint.create()
except ProgrammingError as exc:
print "***", repr(exc)
print "***", repr(exc.statement)
print "***", repr(exc.params)
print "***", repr(exc.orig)
print "*** Ignoring it, you probably have a fresh install from a recent git."
db_conn.commit()