1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
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.
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.
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/>.
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
26 from mediagoblin
.db
.sql
.base
import GMGTableBase
29 # This one will get filled with local migrations
33 #######################################################
34 # Migration set 1: Define initial models, no migrations
35 #######################################################
37 Base1
= declarative_base(cls
=GMGTableBase
)
39 class Creature1(Base1
):
40 __tablename__
= "creature"
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
)
48 __tablename__
= "level"
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
)
55 SET1_MODELS
= [Creature1
, Level1
]
59 #######################################################
60 # Migration set 2: A few migrations and new model
61 #######################################################
63 Base2
= declarative_base(cls
=GMGTableBase
)
65 class Creature2(Base2
):
66 __tablename__
= "creature"
68 id = Column(Integer
, primary_key
=True)
69 name
= Column(Unicode
, unique
=True, nullable
=False, index
=True)
70 num_legs
= Column(Integer
, nullable
=False)
72 class CreaturePower2(Base2
):
73 __tablename__
= "creature_power"
75 id = Column(Integer
, primary_key
=True)
77 Integer
, ForeignKey('creature.id'), nullable
=False)
78 name
= Column(Unicode
)
79 description
= Column(Unicode
)
82 __tablename__
= "level"
84 id = Column(Integer
, primary_key
=True)
85 name
= Column(Unicode
)
86 description
= Column(UnicodeText
)
88 class LevelExit2(Base2
):
89 __tablename__
= "level_exit"
91 id = Column(Integer
, primary_key
=True)
92 name
= Column(Unicode
)
94 Integer
, ForeignKey('level.id'), nullable
=False)
96 Integer
, ForeignKey('level.id'), nullable
=False)
98 SET2_MODELS
= [Creature2
, CreaturePower2
, Level2
, LevelExit2
]
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
)
107 creature_table
.drop_column('is_demon'))
110 @RegisterMigration(2, FULL_MIGRATIONS
)
111 def creature_powers_new_table(db_conn
):
114 @RegisterMigration(3, FULL_MIGRATIONS
)
115 def level_exits_new_table(db_conn
):
119 # A hack! At this point we freeze-fame and get just a partial list of
122 SET2_MIGRATIONS
= copy
.copy(FULL_MIGRATIONS
)
124 #######################################################
125 # Migration set 3: Final migrations
126 #######################################################
128 Base3
= declarative_base(cls
=GMGTableBase
)
130 class Creature3(Base3
):
131 __tablename__
= "creature"
133 id = Column(Integer
, primary_key
=True)
134 name
= Column(Unicode
, unique
=True, nullable
=False, index
=True)
135 num_limbs
= Column(Integer
, nullable
=False)
137 class CreaturePower3(Base3
):
138 __tablename__
= "creature_power"
140 id = Column(Integer
, primary_key
=True)
142 Integer
, ForeignKey('creature.id'), nullable
=False, index
=True)
143 name
= Column(Unicode
)
144 description
= Column(Unicode
)
147 __tablename__
= "level"
149 id = Column(Integer
, primary_key
=True)
150 name
= Column(Unicode
)
151 description
= Column(UnicodeText
)
153 class LevelExit3(Base3
):
154 __tablename__
= "level_exit"
156 id = Column(Integer
, primary_key
=True)
157 name
= Column(Unicode
)
159 Integer
, ForeignKey('level.id'), nullable
=False, index
=True)
161 Integer
, ForeignKey('level.id'), nullable
=False, index
=True)
164 SET3_MODELS
= [Creature3
, CreaturePower3
, Level3
, LevelExit3
]
167 @RegisterMigration(4, FULL_MIGRATIONS
)
168 def creature_num_legs_to_num_limbs(db_conn
):
171 @RegisterMigration(5, FULL_MIGRATIONS
)
172 def level_exit_index_from_and_to_level(db_conn
):
175 @RegisterMigration(6, FULL_MIGRATIONS
)
176 def creature_power_index_creature(db_conn
):