Might as well call it "set2 migrations"
[mediagoblin.git] / mediagoblin / tests / test_sql_migrations.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 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 copy
18
19 from sqlalchemy import (
20 Table, Column, MetaData,
21 Integer, Unicode, UnicodeText, DateTime, Boolean, ForeignKey,
22 UniqueConstraint, PickleType)
23 from sqlalchemy.ext.declarative import declarative_base
24 from migrate import changeset
25
26 from mediagoblin.db.sql.base import GMGTableBase
27
28
29 # This one will get filled with local migrations
30 FULL_MIGRATIONS = {}
31
32
33 #######################################################
34 # Migration set 1: Define initial models, no migrations
35 #######################################################
36
37 Base1 = declarative_base(cls=GMGTableBase)
38
39 class Creature1(Base1):
40 __tablename__ = "creature"
41
42 id = Column(Integer, primary_key=True)
43 name = Column(Unicode, unique=True, nullable=False, index=True)
44 num_legs = Column(Integer, nullable=False)
45 is_demon = Column(Boolean)
46
47 class Level1(Base1):
48 __tablename__ = "level"
49
50 id = Column(Integer, primary_key=True)
51 name = Column(Unicode, unique=True, nullable=False, index=True)
52 description = Column(UnicodeText)
53 exits = Column(PickleType)
54
55 SET1_MODELS = [Creature1, Level1]
56
57 SET1_MIGRATIONS = []
58
59 #######################################################
60 # Migration set 2: A few migrations and new model
61 #######################################################
62
63 Base2 = declarative_base(cls=GMGTableBase)
64
65 class Creature2(Base2):
66 __tablename__ = "creature"
67
68 id = Column(Integer, primary_key=True)
69 name = Column(Unicode, unique=True, nullable=False, index=True)
70 num_legs = Column(Integer, nullable=False)
71
72 class CreaturePower2(Base2):
73 __tablename__ = "creature_power"
74
75 id = Column(Integer, primary_key=True)
76 creature = Column(
77 Integer, ForeignKey('creature.id'), nullable=False)
78 name = Column(Unicode)
79 description = Column(Unicode)
80
81 class Level2(Base2):
82 __tablename__ = "level"
83
84 id = Column(Integer, primary_key=True)
85 name = Column(Unicode)
86 description = Column(UnicodeText)
87
88 class LevelExit2(Base2):
89 __tablename__ = "level_exit"
90
91 id = Column(Integer, primary_key=True)
92 name = Column(Unicode)
93 from_level = Column(
94 Integer, ForeignKey('level.id'), nullable=False)
95 to_level = Column(
96 Integer, ForeignKey('level.id'), nullable=False)
97
98 SET2_MODELS = [Creature2, CreaturePower2, Level2, LevelExit2]
99
100
101 @RegisterMigration(1, FULL_MIGRATIONS)
102 def creature_remove_is_demon(db_conn):
103 creature_table = Table(
104 'creature', MetaData(),
105 autoload=True, autoload_with=db_conn.engine)
106 db_conn.execute(
107 creature_table.drop_column('is_demon'))
108
109
110 @RegisterMigration(2, FULL_MIGRATIONS)
111 def creature_powers_new_table(db_conn):
112 pass
113
114 @RegisterMigration(3, FULL_MIGRATIONS)
115 def level_exits_new_table(db_conn):
116 pass
117
118
119 # A hack! At this point we freeze-fame and get just a partial list of
120 # migrations
121
122 SET2_MIGRATIONS = copy.copy(FULL_MIGRATIONS)
123
124 #######################################################
125 # Migration set 3: Final migrations
126 #######################################################
127
128 Base3 = declarative_base(cls=GMGTableBase)
129
130 class Creature3(Base3):
131 __tablename__ = "creature"
132
133 id = Column(Integer, primary_key=True)
134 name = Column(Unicode, unique=True, nullable=False, index=True)
135 num_limbs= Column(Integer, nullable=False)
136
137 class CreaturePower3(Base3):
138 __tablename__ = "creature_power"
139
140 id = Column(Integer, primary_key=True)
141 creature = Column(
142 Integer, ForeignKey('creature.id'), nullable=False, index=True)
143 name = Column(Unicode)
144 description = Column(Unicode)
145
146 class Level3(Base3):
147 __tablename__ = "level"
148
149 id = Column(Integer, primary_key=True)
150 name = Column(Unicode)
151 description = Column(UnicodeText)
152
153 class LevelExit3(Base3):
154 __tablename__ = "level_exit"
155
156 id = Column(Integer, primary_key=True)
157 name = Column(Unicode)
158 from_level = Column(
159 Integer, ForeignKey('level.id'), nullable=False, index=True)
160 to_level = Column(
161 Integer, ForeignKey('level.id'), nullable=False, index=True)
162
163
164 SET3_MODELS = [Creature3, CreaturePower3, Level3, LevelExit3]
165
166
167 @RegisterMigration(4, FULL_MIGRATIONS)
168 def creature_num_legs_to_num_limbs(db_conn):
169 pass
170
171 @RegisterMigration(5, FULL_MIGRATIONS)
172 def level_exit_index_from_and_to_level(db_conn):
173 pass
174
175 @RegisterMigration(6, FULL_MIGRATIONS)
176 def creature_power_index_creature(db_conn):
177 pass