1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import copy
from sqlalchemy import (
Table, Column, MetaData,
Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
UniqueConstraint, PickleType)
from sqlalchemy.ext.declarative import declarative_base
from migrate import changeset
from mediagoblin.db.sql.base import GMGTableBase
# This one will get filled with local migrations
FULL_MIGRATIONS = {}
#######################################################
# Migration set 1: Define initial models, no migrations
#######################################################
Base1 = declarative_base(cls=GMGTableBase)
class Creature1(Base1):
__tablename__ = "creature"
id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True, nullable=False, index=True)
num_legs = Column(Integer, nullable=False)
is_demon = Column(Boolean)
class Level1(Base1):
__tablename__ = "level"
id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True, nullable=False, index=True)
description = Column(UnicodeText)
exits = Column(PickleType)
SET1_MODELS = [Creature1, Level1]
SET1_MIGRATIONS = []
#######################################################
# Migration set 2: A few migrations and new model
#######################################################
Base2 = declarative_base(cls=GMGTableBase)
class Creature2(Base2):
__tablename__ = "creature"
id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True, nullable=False, index=True)
num_legs = Column(Integer, nullable=False)
class CreaturePower2(Base2):
__tablename__ = "creature_power"
id = Column(Integer, primary_key=True)
creature = Column(
Integer, ForeignKey('creature.id'), nullable=False)
name = Column(Unicode)
description = Column(Unicode)
class Level2(Base2):
__tablename__ = "level"
id = Column(Integer, primary_key=True)
name = Column(Unicode)
description = Column(UnicodeText)
class LevelExit2(Base2):
__tablename__ = "level_exit"
id = Column(Integer, primary_key=True)
name = Column(Unicode)
from_level = Column(
Integer, ForeignKey('level.id'), nullable=False)
to_level = Column(
Integer, ForeignKey('level.id'), nullable=False)
SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2]
@RegisterMigration(1, FULL_MIGRATIONS)
def creature_remove_is_demon(db_conn):
creature_table = Table(
'creature', MetaData(),
autoload=True, autoload_with=db_conn.engine)
db_conn.execute(
creature_table.drop_column('is_demon'))
@RegisterMigration(2, FULL_MIGRATIONS)
def creature_powers_new_table(db_conn):
pass
@RegisterMigration(3, FULL_MIGRATIONS)
def level_exits_new_table(db_conn):
pass
# A hack! At this point we freeze-fame and get just a partial list of
# migrations
PARTIAL_MIGRATIONS = copy.copy(FULL_MIGRATIONS)
#######################################################
# Migration set 3: Final migrations
#######################################################
Base3 = declarative_base(cls=GMGTableBase)
class Creature3(Base3):
__tablename__ = "creature"
id = Column(Integer, primary_key=True)
name = Column(Unicode, unique=True, nullable=False, index=True)
num_limbs= Column(Integer, nullable=False)
class CreaturePower3(Base3):
__tablename__ = "creature_power"
id = Column(Integer, primary_key=True)
creature = Column(
Integer, ForeignKey('creature.id'), nullable=False, index=True)
name = Column(Unicode)
description = Column(Unicode)
class Level3(Base3):
__tablename__ = "level"
id = Column(Integer, primary_key=True)
name = Column(Unicode)
description = Column(UnicodeText)
class LevelExit3(Base3):
__tablename__ = "level_exit"
id = Column(Integer, primary_key=True)
name = Column(Unicode)
from_level = Column(
Integer, ForeignKey('level.id'), nullable=False, index=True)
to_level = Column(
Integer, ForeignKey('level.id'), nullable=False, index=True)
SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3]
@RegisterMigration(4, FULL_MIGRATIONS)
def creature_num_legs_to_num_limbs(db_conn):
pass
@RegisterMigration(5, FULL_MIGRATIONS)
def level_exit_index_from_and_to_level(db_conn):
pass
@RegisterMigration(6, FULL_MIGRATIONS)
def creature_power_index_creature(db_conn):
pass
|