Commit | Line | Data |
---|---|---|
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 | 17 | import datetime |
34d8bc98 | 18 | import uuid |
29fdd3bb | 19 | |
88a9662b | 20 | from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger, |
316e1dfd | 21 | Integer, Unicode, UnicodeText, DateTime, |
0f14c362 E |
22 | ForeignKey) |
23 | from sqlalchemy.exc import ProgrammingError | |
316e1dfd | 24 | from sqlalchemy.ext.declarative import declarative_base |
ab1f65e6 | 25 | from sqlalchemy.sql import and_ |
0f14c362 | 26 | from migrate.changeset.constraint import UniqueConstraint |
b781c3c9 | 27 | |
8e3bf978 JT |
28 | |
29 | from mediagoblin.db.extratypes import JSONEncoded | |
c130e3ee | 30 | from mediagoblin.db.migration_tools import RegisterMigration, inspect_table |
7271b062 | 31 | from mediagoblin.db.models import MediaEntry, Collection, User, MediaComment |
b781c3c9 | 32 | |
3ea1cf36 | 33 | MIGRATIONS = {} |
b781c3c9 JK |
34 | |
35 | ||
36 | @RegisterMigration(1, MIGRATIONS) | |
37 | def ogg_to_webm_audio(db_conn): | |
38 | metadata = MetaData(bind=db_conn.bind) | |
39 | ||
40 | file_keynames = Table('core__file_keynames', metadata, autoload=True, | |
41 | autoload_with=db_conn.bind) | |
42 | ||
43 | db_conn.execute( | |
38c6d441 | 44 | file_keynames.update().where(file_keynames.c.name == 'ogg'). |
b781c3c9 JK |
45 | values(name='webm_audio') |
46 | ) | |
b1055401 | 47 | db_conn.commit() |
38c6d441 JW |
48 | |
49 | ||
50 | @RegisterMigration(2, MIGRATIONS) | |
51 | def add_wants_notification_column(db_conn): | |
52 | metadata = MetaData(bind=db_conn.bind) | |
53 | ||
54 | users = Table('core__users', metadata, autoload=True, | |
55 | autoload_with=db_conn.bind) | |
56 | ||
57 | col = Column('wants_comment_notification', Boolean, | |
c4869eff | 58 | default=True, nullable=True) |
38c6d441 | 59 | col.create(users, populate_defaults=True) |
b1055401 | 60 | db_conn.commit() |
64712915 JW |
61 | |
62 | ||
63 | @RegisterMigration(3, MIGRATIONS) | |
64 | def add_transcoding_progress(db_conn): | |
65 | metadata = MetaData(bind=db_conn.bind) | |
66 | ||
c4466cb4 | 67 | media_entry = inspect_table(metadata, 'core__media_entries') |
64712915 JW |
68 | |
69 | col = Column('transcoding_progress', SmallInteger) | |
70 | col.create(media_entry) | |
71 | db_conn.commit() | |
be5be115 | 72 | |
88a9662b | 73 | |
316e1dfd E |
74 | class Collection_v0(declarative_base()): |
75 | __tablename__ = "core__collections" | |
76 | ||
77 | id = Column(Integer, primary_key=True) | |
78 | title = Column(Unicode, nullable=False) | |
79 | slug = Column(Unicode) | |
80 | created = Column(DateTime, nullable=False, default=datetime.datetime.now, | |
81 | index=True) | |
82 | description = Column(UnicodeText) | |
83 | creator = Column(Integer, ForeignKey(User.id), nullable=False) | |
84 | items = Column(Integer, default=0) | |
85 | ||
86 | class CollectionItem_v0(declarative_base()): | |
87 | __tablename__ = "core__collection_items" | |
88 | ||
89 | id = Column(Integer, primary_key=True) | |
90 | media_entry = Column( | |
91 | Integer, ForeignKey(MediaEntry.id), nullable=False, index=True) | |
92 | collection = Column(Integer, ForeignKey(Collection.id), nullable=False) | |
93 | note = Column(UnicodeText, nullable=True) | |
94 | added = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
95 | position = Column(Integer) | |
96 | ||
97 | ## This should be activated, normally. | |
98 | ## But this would change the way the next migration used to work. | |
99 | ## So it's commented for now. | |
0f14c362 E |
100 | __table_args__ = ( |
101 | UniqueConstraint('collection', 'media_entry'), | |
102 | {}) | |
103 | ||
104 | collectionitem_unique_constraint_done = False | |
316e1dfd | 105 | |
be5be115 | 106 | @RegisterMigration(4, MIGRATIONS) |
29fdd3bb | 107 | def add_collection_tables(db_conn): |
316e1dfd E |
108 | Collection_v0.__table__.create(db_conn.bind) |
109 | CollectionItem_v0.__table__.create(db_conn.bind) | |
29fdd3bb | 110 | |
0f14c362 E |
111 | global collectionitem_unique_constraint_done |
112 | collectionitem_unique_constraint_done = True | |
113 | ||
29fdd3bb AW |
114 | db_conn.commit() |
115 | ||
88a9662b | 116 | |
29fdd3bb | 117 | @RegisterMigration(5, MIGRATIONS) |
59fb87c9 | 118 | def add_mediaentry_collected(db_conn): |
be5be115 AW |
119 | metadata = MetaData(bind=db_conn.bind) |
120 | ||
c4466cb4 | 121 | media_entry = inspect_table(metadata, 'core__media_entries') |
be5be115 | 122 | |
d8984df8 | 123 | col = Column('collected', Integer, default=0) |
be5be115 AW |
124 | col.create(media_entry) |
125 | db_conn.commit() | |
5354f954 JW |
126 | |
127 | ||
316e1dfd E |
128 | class ProcessingMetaData_v0(declarative_base()): |
129 | __tablename__ = 'core__processing_metadata' | |
939d57a0 | 130 | |
316e1dfd E |
131 | id = Column(Integer, primary_key=True) |
132 | media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False, | |
133 | index=True) | |
134 | callback_url = Column(Unicode) | |
939d57a0 | 135 | |
316e1dfd E |
136 | @RegisterMigration(6, MIGRATIONS) |
137 | def create_processing_metadata_table(db): | |
138 | ProcessingMetaData_v0.__table__.create(db.bind) | |
5354f954 | 139 | db.commit() |
0f14c362 | 140 | |
ea91c183 E |
141 | |
142 | # Okay, problem being: | |
143 | # Migration #4 forgot to add the uniqueconstraint for the | |
144 | # new tables. While creating the tables from scratch had | |
145 | # the constraint enabled. | |
146 | # | |
a64abbb1 | 147 | # So we have four situations that should end up at the same |
ea91c183 E |
148 | # db layout: |
149 | # | |
150 | # 1. Fresh install. | |
151 | # Well, easy. Just uses the tables in models.py | |
152 | # 2. Fresh install using a git version just before this migration | |
153 | # The tables are all there, the unique constraint is also there. | |
154 | # This migration should do nothing. | |
155 | # But as we can't detect the uniqueconstraint easily, | |
156 | # this migration just adds the constraint again. | |
157 | # And possibly fails very loud. But ignores the failure. | |
158 | # 3. old install, not using git, just releases. | |
159 | # This one will get the new tables in #4 (now with constraint!) | |
160 | # And this migration is just skipped silently. | |
161 | # 4. old install, always on latest git. | |
162 | # This one has the tables, but lacks the constraint. | |
a64abbb1 | 163 | # So this migration adds the constraint. |
0f14c362 E |
164 | @RegisterMigration(7, MIGRATIONS) |
165 | def fix_CollectionItem_v0_constraint(db_conn): | |
166 | """Add the forgotten Constraint on CollectionItem""" | |
167 | ||
168 | global collectionitem_unique_constraint_done | |
169 | if collectionitem_unique_constraint_done: | |
0f14c362 E |
170 | # Reset it. Maybe the whole thing gets run again |
171 | # For a different db? | |
172 | collectionitem_unique_constraint_done = False | |
173 | return | |
174 | ||
175 | metadata = MetaData(bind=db_conn.bind) | |
176 | ||
c4466cb4 | 177 | CollectionItem_table = inspect_table(metadata, 'core__collection_items') |
0f14c362 E |
178 | |
179 | constraint = UniqueConstraint('collection', 'media_entry', | |
180 | name='core__collection_items_collection_media_entry_key', | |
181 | table=CollectionItem_table) | |
182 | ||
183 | try: | |
184 | constraint.create() | |
78fd5581 CAW |
185 | except ProgrammingError: |
186 | # User probably has an install that was run since the | |
187 | # collection tables were added, so we don't need to run this migration. | |
188 | pass | |
189 | ||
0f14c362 | 190 | db_conn.commit() |
dc4dfbde MH |
191 | |
192 | ||
193 | @RegisterMigration(8, MIGRATIONS) | |
194 | def add_license_preference(db): | |
195 | metadata = MetaData(bind=db.bind) | |
196 | ||
0c871f81 | 197 | user_table = inspect_table(metadata, 'core__users') |
dc4dfbde | 198 | |
0c871f81 | 199 | col = Column('license_preference', Unicode) |
dc4dfbde MH |
200 | col.create(user_table) |
201 | db.commit() | |
e66431f4 CAW |
202 | |
203 | ||
204 | @RegisterMigration(9, MIGRATIONS) | |
205 | def mediaentry_new_slug_era(db): | |
206 | """ | |
207 | Update for the new era for media type slugs. | |
208 | ||
209 | Entries without slugs now display differently in the url like: | |
210 | /u/cwebber/m/id=251/ | |
211 | ||
212 | ... because of this, we should back-convert: | |
213 | - entries without slugs should be converted to use the id, if possible, to | |
214 | make old urls still work | |
215 | - slugs with = (or also : which is now also not allowed) to have those | |
216 | stripped out (small possibility of breakage here sadly) | |
217 | """ | |
e66431f4 CAW |
218 | |
219 | def slug_and_user_combo_exists(slug, uploader): | |
e66431f4 CAW |
220 | return db.execute( |
221 | media_table.select( | |
ab1f65e6 | 222 | and_(media_table.c.uploader==uploader, |
aecd65b7 | 223 | media_table.c.slug==slug))).first() is not None |
e66431f4 CAW |
224 | |
225 | def append_garbage_till_unique(row, new_slug): | |
226 | """ | |
227 | Attach junk to this row until it's unique, then save it | |
228 | """ | |
229 | if slug_and_user_combo_exists(new_slug, row.uploader): | |
230 | # okay, still no success; | |
231 | # let's whack junk on there till it's unique. | |
232 | new_slug += '-' + uuid.uuid4().hex[:4] | |
233 | # keep going if necessary! | |
234 | while slug_and_user_combo_exists(new_slug, row.uploader): | |
235 | new_slug += uuid.uuid4().hex[:4] | |
236 | ||
237 | db.execute( | |
238 | media_table.update(). \ | |
239 | where(media_table.c.id==row.id). \ | |
240 | values(slug=new_slug)) | |
241 | ||
242 | metadata = MetaData(bind=db.bind) | |
243 | ||
244 | media_table = inspect_table(metadata, 'core__media_entries') | |
0b7cdb6f | 245 | |
e66431f4 CAW |
246 | for row in db.execute(media_table.select()): |
247 | # no slug, try setting to an id | |
248 | if not row.slug: | |
249 | append_garbage_till_unique(row, unicode(row.id)) | |
250 | # has "=" or ":" in it... we're getting rid of those | |
251 | elif u"=" in row.slug or u":" in row.slug: | |
252 | append_garbage_till_unique( | |
253 | row, row.slug.replace(u"=", u"-").replace(u":", u"-")) | |
0b7cdb6f CAW |
254 | |
255 | db.commit() | |
34d8bc98 RE |
256 | |
257 | ||
258 | @RegisterMigration(10, MIGRATIONS) | |
259 | def unique_collections_slug(db): | |
260 | """Add unique constraint to collection slug""" | |
261 | metadata = MetaData(bind=db.bind) | |
262 | collection_table = inspect_table(metadata, "core__collections") | |
263 | existing_slugs = {} | |
264 | slugs_to_change = [] | |
265 | ||
266 | for row in db.execute(collection_table.select()): | |
267 | # if duplicate slug, generate a unique slug | |
268 | if row.creator in existing_slugs and row.slug in \ | |
269 | existing_slugs[row.creator]: | |
270 | slugs_to_change.append(row.id) | |
271 | else: | |
272 | if not row.creator in existing_slugs: | |
273 | existing_slugs[row.creator] = [row.slug] | |
274 | else: | |
275 | existing_slugs[row.creator].append(row.slug) | |
276 | ||
277 | for row_id in slugs_to_change: | |
f96c284e | 278 | new_slug = unicode(uuid.uuid4()) |
34d8bc98 RE |
279 | db.execute(collection_table.update(). |
280 | where(collection_table.c.id == row_id). | |
281 | values(slug=new_slug)) | |
282 | # sqlite does not like to change the schema when a transaction(update) is | |
283 | # not yet completed | |
284 | db.commit() | |
285 | ||
286 | constraint = UniqueConstraint('creator', 'slug', | |
287 | name='core__collection_creator_slug_key', | |
288 | table=collection_table) | |
289 | constraint.create() | |
290 | ||
291 | db.commit() | |
8ad734af | 292 | |
8ad734af | 293 | @RegisterMigration(11, MIGRATIONS) |
342f06f7 RE |
294 | def drop_token_related_User_columns(db): |
295 | """ | |
296 | Drop unneeded columns from the User table after switching to using | |
297 | itsdangerous tokens for email and forgot password verification. | |
298 | """ | |
299 | metadata = MetaData(bind=db.bind) | |
300 | user_table = inspect_table(metadata, 'core__users') | |
301 | ||
302 | verification_key = user_table.columns['verification_key'] | |
303 | fp_verification_key = user_table.columns['fp_verification_key'] | |
304 | fp_token_expire = user_table.columns['fp_token_expire'] | |
305 | ||
306 | verification_key.drop() | |
307 | fp_verification_key.drop() | |
308 | fp_token_expire.drop() | |
309 | ||
310 | db.commit() | |
257b8ab6 | 311 | |
5adb906a | 312 | |
2d7b6bde JW |
313 | class CommentSubscription_v0(declarative_base()): |
314 | __tablename__ = 'core__comment_subscriptions' | |
315 | id = Column(Integer, primary_key=True) | |
316 | ||
317 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
318 | ||
319 | media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False) | |
320 | ||
321 | user_id = Column(Integer, ForeignKey(User.id), nullable=False) | |
322 | ||
323 | notify = Column(Boolean, nullable=False, default=True) | |
324 | send_email = Column(Boolean, nullable=False, default=True) | |
325 | ||
326 | ||
327 | class Notification_v0(declarative_base()): | |
328 | __tablename__ = 'core__notifications' | |
329 | id = Column(Integer, primary_key=True) | |
330 | type = Column(Unicode) | |
331 | ||
332 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
333 | ||
334 | user_id = Column(Integer, ForeignKey(User.id), nullable=False, | |
335 | index=True) | |
336 | seen = Column(Boolean, default=lambda: False, index=True) | |
337 | ||
338 | ||
339 | class CommentNotification_v0(Notification_v0): | |
340 | __tablename__ = 'core__comment_notifications' | |
341 | id = Column(Integer, ForeignKey(Notification_v0.id), primary_key=True) | |
342 | ||
343 | subject_id = Column(Integer, ForeignKey(MediaComment.id)) | |
344 | ||
345 | ||
346 | class ProcessingNotification_v0(Notification_v0): | |
347 | __tablename__ = 'core__processing_notifications' | |
348 | ||
349 | id = Column(Integer, ForeignKey(Notification_v0.id), primary_key=True) | |
350 | ||
351 | subject_id = Column(Integer, ForeignKey(MediaEntry.id)) | |
352 | ||
353 | ||
257b8ab6 | 354 | @RegisterMigration(12, MIGRATIONS) |
2d7b6bde JW |
355 | def add_new_notification_tables(db): |
356 | metadata = MetaData(bind=db.bind) | |
357 | ||
358 | user_table = inspect_table(metadata, 'core__users') | |
359 | mediaentry_table = inspect_table(metadata, 'core__media_entries') | |
360 | mediacomment_table = inspect_table(metadata, 'core__media_comments') | |
361 | ||
362 | CommentSubscription_v0.__table__.create(db.bind) | |
363 | ||
364 | Notification_v0.__table__.create(db.bind) | |
365 | CommentNotification_v0.__table__.create(db.bind) | |
366 | ProcessingNotification_v0.__table__.create(db.bind) | |
af4414a8 | 367 | |
e8eec575 CAW |
368 | db.commit() |
369 | ||
af4414a8 RE |
370 | |
371 | @RegisterMigration(13, MIGRATIONS) | |
8ad734af RE |
372 | def pw_hash_nullable(db): |
373 | """Make pw_hash column nullable""" | |
374 | metadata = MetaData(bind=db.bind) | |
375 | user_table = inspect_table(metadata, "core__users") | |
376 | ||
377 | user_table.c.pw_hash.alter(nullable=True) | |
378 | ||
15db1831 CAW |
379 | # sqlite+sqlalchemy seems to drop this constraint during the |
380 | # migration, so we add it back here for now a bit manually. | |
5a1be074 | 381 | if db.bind.url.drivername == 'sqlite': |
e4deacd9 RE |
382 | constraint = UniqueConstraint('username', table=user_table) |
383 | constraint.create() | |
384 | ||
8ad734af | 385 | db.commit() |
8ddd7769 | 386 | |
387 | ||
7271b062 | 388 | # oauth1 migrations |
8e3bf978 | 389 | class Client_v0(declarative_base()): |
7271b062 | 390 | """ |
391 | Model representing a client - Used for API Auth | |
392 | """ | |
393 | __tablename__ = "core__clients" | |
394 | ||
395 | id = Column(Unicode, nullable=True, primary_key=True) | |
396 | secret = Column(Unicode, nullable=False) | |
397 | expirey = Column(DateTime, nullable=True) | |
398 | application_type = Column(Unicode, nullable=False) | |
399 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
400 | updated = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
401 | ||
402 | # optional stuff | |
403 | redirect_uri = Column(JSONEncoded, nullable=True) | |
404 | logo_url = Column(Unicode, nullable=True) | |
405 | application_name = Column(Unicode, nullable=True) | |
406 | contacts = Column(JSONEncoded, nullable=True) | |
407 | ||
408 | def __repr__(self): | |
409 | if self.application_name: | |
410 | return "<Client {0} - {1}>".format(self.application_name, self.id) | |
411 | else: | |
412 | return "<Client {0}>".format(self.id) | |
413 | ||
8e3bf978 | 414 | class RequestToken_v0(declarative_base()): |
7271b062 | 415 | """ |
416 | Model for representing the request tokens | |
417 | """ | |
418 | __tablename__ = "core__request_tokens" | |
419 | ||
420 | token = Column(Unicode, primary_key=True) | |
421 | secret = Column(Unicode, nullable=False) | |
8e3bf978 | 422 | client = Column(Unicode, ForeignKey(Client_v0.id)) |
7271b062 | 423 | user = Column(Integer, ForeignKey(User.id), nullable=True) |
424 | used = Column(Boolean, default=False) | |
425 | authenticated = Column(Boolean, default=False) | |
426 | verifier = Column(Unicode, nullable=True) | |
427 | callback = Column(Unicode, nullable=False, default=u"oob") | |
428 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
429 | updated = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
93d805ad | 430 | |
8e3bf978 | 431 | class AccessToken_v0(declarative_base()): |
7271b062 | 432 | """ |
433 | Model for representing the access tokens | |
434 | """ | |
435 | __tablename__ = "core__access_tokens" | |
436 | ||
437 | token = Column(Unicode, nullable=False, primary_key=True) | |
438 | secret = Column(Unicode, nullable=False) | |
439 | user = Column(Integer, ForeignKey(User.id)) | |
8e3bf978 | 440 | request_token = Column(Unicode, ForeignKey(RequestToken_v0.token)) |
7271b062 | 441 | created = Column(DateTime, nullable=False, default=datetime.datetime.now) |
442 | updated = Column(DateTime, nullable=False, default=datetime.datetime.now) | |
93d805ad | 443 | |
7271b062 | 444 | |
8e3bf978 | 445 | class NonceTimestamp_v0(declarative_base()): |
7271b062 | 446 | """ |
447 | A place the timestamp and nonce can be stored - this is for OAuth1 | |
448 | """ | |
449 | __tablename__ = "core__nonce_timestamps" | |
450 | ||
451 | nonce = Column(Unicode, nullable=False, primary_key=True) | |
452 | timestamp = Column(DateTime, nullable=False, primary_key=True) | |
453 | ||
454 | ||
8ddd7769 | 455 | @RegisterMigration(14, MIGRATIONS) |
456 | def create_oauth1_tables(db): | |
457 | """ Creates the OAuth1 tables """ | |
458 | ||
7271b062 | 459 | Client_v0.__table__.create(db.bind) |
460 | RequestToken_v0.__table__.create(db.bind) | |
461 | AccessToken_v0.__table__.create(db.bind) | |
462 | NonceTimestamp_v0.__table__.create(db.bind) | |
8ddd7769 | 463 | |
464 | db.commit() | |
93d805ad RE |
465 | |
466 | ||
467 | @RegisterMigration(15, MIGRATIONS) | |
468 | def wants_notifications(db): | |
469 | """Add a wants_notifications field to User model""" | |
470 | metadata = MetaData(bind=db.bind) | |
471 | user_table = inspect_table(metadata, "core__users") | |
472 | ||
473 | col = Column('wants_notifications', Boolean, default=True) | |
474 | col.create(user_table) | |
475 | ||
476 | db.commit() | |
28eab59a CAW |
477 | |
478 | ||
479 | @RegisterMigration(16, MIGRATIONS) | |
bdd22421 RE |
480 | def upload_limits(db): |
481 | """Add user upload limit columns""" | |
482 | metadata = MetaData(bind=db.bind) | |
483 | ||
484 | user_table = inspect_table(metadata, 'core__users') | |
485 | media_entry_table = inspect_table(metadata, 'core__media_entries') | |
486 | ||
487 | col = Column('uploaded', Integer, default=0) | |
488 | col.create(user_table) | |
489 | ||
490 | col = Column('upload_limit', Integer) | |
491 | col.create(user_table) | |
492 | ||
493 | col = Column('file_size', Integer, default=0) | |
494 | col.create(media_entry_table) | |
495 | ||
496 | db.commit() | |
529eb17b CAW |
497 | |
498 | ||
499 | @RegisterMigration(17, MIGRATIONS) | |
e002452f RE |
500 | def add_file_metadata(db): |
501 | """Add file_metadata to MediaFile""" | |
502 | metadata = MetaData(bind=db.bind) | |
503 | media_file_table = inspect_table(metadata, "core__mediafiles") | |
504 | ||
505 | col = Column('file_metadata', JSONEncoded) | |
506 | col.create(media_file_table) | |
529eb17b | 507 | |
e002452f | 508 | db.commit() |