Move db.sql.models* to db.models*
[mediagoblin.git] / mediagoblin / db / migrations.py
CommitLineData
70b44584 1# GNU MediaGoblin -- federated, autonomous media hosting
b781c3c9 2# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
70b44584
CAW
3#
4# This program is free software: you can redistribute it and/or modify
5# it under the terms of the GNU Affero General Public License as published by
6# the Free Software Foundation, either version 3 of the License, or
7# (at your option) any later version.
8#
9# This program is distributed in the hope that it will be useful,
10# but WITHOUT ANY WARRANTY; without even the implied warranty of
11# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12# GNU Affero General Public License for more details.
13#
14# You should have received a copy of the GNU Affero General Public License
15# along with this program. If not, see <http://www.gnu.org/licenses/>.
16
29fdd3bb
AW
17import datetime
18
88a9662b 19from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger,
316e1dfd 20 Integer, Unicode, UnicodeText, DateTime,
0f14c362
E
21 ForeignKey)
22from sqlalchemy.exc import ProgrammingError
316e1dfd 23from sqlalchemy.ext.declarative import declarative_base
0f14c362 24from migrate.changeset.constraint import UniqueConstraint
b781c3c9 25
1e46dc25 26from mediagoblin.db.util import RegisterMigration
b0c8328e 27from mediagoblin.db.models import MediaEntry, Collection, User
b781c3c9 28
3ea1cf36 29MIGRATIONS = {}
b781c3c9
JK
30
31
32@RegisterMigration(1, MIGRATIONS)
33def ogg_to_webm_audio(db_conn):
34 metadata = MetaData(bind=db_conn.bind)
35
36 file_keynames = Table('core__file_keynames', metadata, autoload=True,
37 autoload_with=db_conn.bind)
38
39 db_conn.execute(
38c6d441 40 file_keynames.update().where(file_keynames.c.name == 'ogg').
b781c3c9
JK
41 values(name='webm_audio')
42 )
b1055401 43 db_conn.commit()
38c6d441
JW
44
45
46@RegisterMigration(2, MIGRATIONS)
47def add_wants_notification_column(db_conn):
48 metadata = MetaData(bind=db_conn.bind)
49
50 users = Table('core__users', metadata, autoload=True,
51 autoload_with=db_conn.bind)
52
53 col = Column('wants_comment_notification', Boolean,
c4869eff 54 default=True, nullable=True)
38c6d441 55 col.create(users, populate_defaults=True)
b1055401 56 db_conn.commit()
64712915
JW
57
58
59@RegisterMigration(3, MIGRATIONS)
60def add_transcoding_progress(db_conn):
61 metadata = MetaData(bind=db_conn.bind)
62
63 media_entry = Table('core__media_entries', metadata, autoload=True,
64 autoload_with=db_conn.bind)
65
66 col = Column('transcoding_progress', SmallInteger)
67 col.create(media_entry)
68 db_conn.commit()
be5be115 69
88a9662b 70
316e1dfd
E
71class Collection_v0(declarative_base()):
72 __tablename__ = "core__collections"
73
74 id = Column(Integer, primary_key=True)
75 title = Column(Unicode, nullable=False)
76 slug = Column(Unicode)
77 created = Column(DateTime, nullable=False, default=datetime.datetime.now,
78 index=True)
79 description = Column(UnicodeText)
80 creator = Column(Integer, ForeignKey(User.id), nullable=False)
81 items = Column(Integer, default=0)
82
83class CollectionItem_v0(declarative_base()):
84 __tablename__ = "core__collection_items"
85
86 id = Column(Integer, primary_key=True)
87 media_entry = Column(
88 Integer, ForeignKey(MediaEntry.id), nullable=False, index=True)
89 collection = Column(Integer, ForeignKey(Collection.id), nullable=False)
90 note = Column(UnicodeText, nullable=True)
91 added = Column(DateTime, nullable=False, default=datetime.datetime.now)
92 position = Column(Integer)
93
94 ## This should be activated, normally.
95 ## But this would change the way the next migration used to work.
96 ## So it's commented for now.
0f14c362
E
97 __table_args__ = (
98 UniqueConstraint('collection', 'media_entry'),
99 {})
100
101collectionitem_unique_constraint_done = False
316e1dfd 102
be5be115 103@RegisterMigration(4, MIGRATIONS)
29fdd3bb 104def add_collection_tables(db_conn):
316e1dfd
E
105 Collection_v0.__table__.create(db_conn.bind)
106 CollectionItem_v0.__table__.create(db_conn.bind)
29fdd3bb 107
0f14c362
E
108 global collectionitem_unique_constraint_done
109 collectionitem_unique_constraint_done = True
110
29fdd3bb
AW
111 db_conn.commit()
112
88a9662b 113
29fdd3bb 114@RegisterMigration(5, MIGRATIONS)
59fb87c9 115def add_mediaentry_collected(db_conn):
be5be115
AW
116 metadata = MetaData(bind=db_conn.bind)
117
118 media_entry = Table('core__media_entries', metadata, autoload=True,
119 autoload_with=db_conn.bind)
120
d8984df8 121 col = Column('collected', Integer, default=0)
be5be115
AW
122 col.create(media_entry)
123 db_conn.commit()
5354f954
JW
124
125
316e1dfd
E
126class ProcessingMetaData_v0(declarative_base()):
127 __tablename__ = 'core__processing_metadata'
939d57a0 128
316e1dfd
E
129 id = Column(Integer, primary_key=True)
130 media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False,
131 index=True)
132 callback_url = Column(Unicode)
939d57a0 133
316e1dfd
E
134@RegisterMigration(6, MIGRATIONS)
135def create_processing_metadata_table(db):
136 ProcessingMetaData_v0.__table__.create(db.bind)
5354f954 137 db.commit()
0f14c362 138
ea91c183
E
139
140# Okay, problem being:
141# Migration #4 forgot to add the uniqueconstraint for the
142# new tables. While creating the tables from scratch had
143# the constraint enabled.
144#
a64abbb1 145# So we have four situations that should end up at the same
ea91c183
E
146# db layout:
147#
148# 1. Fresh install.
149# Well, easy. Just uses the tables in models.py
150# 2. Fresh install using a git version just before this migration
151# The tables are all there, the unique constraint is also there.
152# This migration should do nothing.
153# But as we can't detect the uniqueconstraint easily,
154# this migration just adds the constraint again.
155# And possibly fails very loud. But ignores the failure.
156# 3. old install, not using git, just releases.
157# This one will get the new tables in #4 (now with constraint!)
158# And this migration is just skipped silently.
159# 4. old install, always on latest git.
160# This one has the tables, but lacks the constraint.
a64abbb1 161# So this migration adds the constraint.
0f14c362
E
162@RegisterMigration(7, MIGRATIONS)
163def fix_CollectionItem_v0_constraint(db_conn):
164 """Add the forgotten Constraint on CollectionItem"""
165
166 global collectionitem_unique_constraint_done
167 if collectionitem_unique_constraint_done:
0f14c362
E
168 # Reset it. Maybe the whole thing gets run again
169 # For a different db?
170 collectionitem_unique_constraint_done = False
171 return
172
173 metadata = MetaData(bind=db_conn.bind)
174
175 CollectionItem_table = Table('core__collection_items',
176 metadata, autoload=True, autoload_with=db_conn.bind)
177
178 constraint = UniqueConstraint('collection', 'media_entry',
179 name='core__collection_items_collection_media_entry_key',
180 table=CollectionItem_table)
181
182 try:
183 constraint.create()
78fd5581
CAW
184 except ProgrammingError:
185 # User probably has an install that was run since the
186 # collection tables were added, so we don't need to run this migration.
187 pass
188
0f14c362 189 db_conn.commit()