Merge remote-tracking branch 'spaetz/formerge/507_remove_routes'
[mediagoblin.git] / mediagoblin / db / sql / migrations.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
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
17 import datetime
18
19 from sqlalchemy import (MetaData, Table, Column, Boolean, SmallInteger,
20 Integer, Unicode, UnicodeText, DateTime,
21 ForeignKey, UniqueConstraint)
22 from sqlalchemy.ext.declarative import declarative_base
23
24 from mediagoblin.db.sql.util import RegisterMigration
25 from mediagoblin.db.sql.models import MediaEntry, Collection, User
26
27 MIGRATIONS = {}
28
29
30 @RegisterMigration(1, MIGRATIONS)
31 def ogg_to_webm_audio(db_conn):
32 metadata = MetaData(bind=db_conn.bind)
33
34 file_keynames = Table('core__file_keynames', metadata, autoload=True,
35 autoload_with=db_conn.bind)
36
37 db_conn.execute(
38 file_keynames.update().where(file_keynames.c.name == 'ogg').
39 values(name='webm_audio')
40 )
41 db_conn.commit()
42
43
44 @RegisterMigration(2, MIGRATIONS)
45 def add_wants_notification_column(db_conn):
46 metadata = MetaData(bind=db_conn.bind)
47
48 users = Table('core__users', metadata, autoload=True,
49 autoload_with=db_conn.bind)
50
51 col = Column('wants_comment_notification', Boolean,
52 default=True, nullable=True)
53 col.create(users, populate_defaults=True)
54 db_conn.commit()
55
56
57 @RegisterMigration(3, MIGRATIONS)
58 def add_transcoding_progress(db_conn):
59 metadata = MetaData(bind=db_conn.bind)
60
61 media_entry = Table('core__media_entries', metadata, autoload=True,
62 autoload_with=db_conn.bind)
63
64 col = Column('transcoding_progress', SmallInteger)
65 col.create(media_entry)
66 db_conn.commit()
67
68
69 class Collection_v0(declarative_base()):
70 __tablename__ = "core__collections"
71
72 id = Column(Integer, primary_key=True)
73 title = Column(Unicode, nullable=False)
74 slug = Column(Unicode)
75 created = Column(DateTime, nullable=False, default=datetime.datetime.now,
76 index=True)
77 description = Column(UnicodeText)
78 creator = Column(Integer, ForeignKey(User.id), nullable=False)
79 items = Column(Integer, default=0)
80
81 class CollectionItem_v0(declarative_base()):
82 __tablename__ = "core__collection_items"
83
84 id = Column(Integer, primary_key=True)
85 media_entry = Column(
86 Integer, ForeignKey(MediaEntry.id), nullable=False, index=True)
87 collection = Column(Integer, ForeignKey(Collection.id), nullable=False)
88 note = Column(UnicodeText, nullable=True)
89 added = Column(DateTime, nullable=False, default=datetime.datetime.now)
90 position = Column(Integer)
91
92 ## This should be activated, normally.
93 ## But this would change the way the next migration used to work.
94 ## So it's commented for now.
95 # __table_args__ = (
96 # UniqueConstraint('collection', 'media_entry'),
97 # {})
98
99 @RegisterMigration(4, MIGRATIONS)
100 def add_collection_tables(db_conn):
101 Collection_v0.__table__.create(db_conn.bind)
102 CollectionItem_v0.__table__.create(db_conn.bind)
103
104 db_conn.commit()
105
106
107 @RegisterMigration(5, MIGRATIONS)
108 def add_mediaentry_collected(db_conn):
109 metadata = MetaData(bind=db_conn.bind)
110
111 media_entry = Table('core__media_entries', metadata, autoload=True,
112 autoload_with=db_conn.bind)
113
114 col = Column('collected', Integer, default=0)
115 col.create(media_entry)
116 db_conn.commit()
117
118
119 class ProcessingMetaData_v0(declarative_base()):
120 __tablename__ = 'core__processing_metadata'
121
122 id = Column(Integer, primary_key=True)
123 media_entry_id = Column(Integer, ForeignKey(MediaEntry.id), nullable=False,
124 index=True)
125 callback_url = Column(Unicode)
126
127 @RegisterMigration(6, MIGRATIONS)
128 def create_processing_metadata_table(db):
129 ProcessingMetaData_v0.__table__.create(db.bind)
130 db.commit()