1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011, 2012 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/>.
17 from mediagoblin
.tools
.common
import simple_printer
18 from sqlalchemy
import Table
20 class TableAlreadyExists(Exception):
24 class MigrationManager(object):
26 Migration handling tool.
28 Takes information about a database, lets you update the database
29 to the latest migrations, etc.
32 def __init__(self
, name
, models
, migration_registry
, session
,
33 printer
=simple_printer
):
36 - name: identifier of this section of the database
37 - session: session we're going to migrate
38 - migration_registry: where we should find all migrations to
41 self
.name
= unicode(name
)
43 self
.session
= session
44 self
.migration_registry
= migration_registry
45 self
._sorted
_migrations
= None
46 self
.printer
= printer
49 from mediagoblin
.db
.models
import MigrationData
51 self
.migration_model
= MigrationData
52 self
.migration_table
= MigrationData
.__table
__
55 def sorted_migrations(self
):
57 Sort migrations if necessary and store in self._sorted_migrations
59 if not self
._sorted
_migrations
:
60 self
._sorted
_migrations
= sorted(
61 self
.migration_registry
.items(),
62 # sort on the key... the migration number
63 key
=lambda migration_tuple
: migration_tuple
[0])
65 return self
._sorted
_migrations
68 def migration_data(self
):
70 Get the migration row associated with this object, if any.
72 return self
.session
.query(
73 self
.migration_model
).filter_by(name
=self
.name
).first()
76 def latest_migration(self
):
78 Return a migration number for the latest migration, or 0 if
79 there are no migrations.
81 if self
.sorted_migrations
:
82 return self
.sorted_migrations
[-1][0]
84 # If no migrations have been set, we start at 0.
88 def database_current_migration(self
):
90 Return the current migration in the database.
92 # If the table doesn't even exist, return None.
93 if not self
.migration_table
.exists(self
.session
.bind
):
96 # Also return None if self.migration_data is None.
97 if self
.migration_data
is None:
100 return self
.migration_data
.version
102 def set_current_migration(self
, migration_number
=None):
104 Set the migration in the database to migration_number
105 (or, the latest available)
107 self
.migration_data
.version
= migration_number
or self
.latest_migration
108 self
.session
.commit()
110 def migrations_to_run(self
):
112 Get a list of migrations to run still, if any.
114 Note that this will fail if there's no migration record for
117 assert self
.database_current_migration
is not None
119 db_current_migration
= self
.database_current_migration
122 (migration_number
, migration_func
)
123 for migration_number
, migration_func
in self
.sorted_migrations
124 if migration_number
> db_current_migration
]
127 def init_tables(self
):
129 Create all tables relative to this package
131 # sanity check before we proceed, none of these should be created
132 for model
in self
.models
:
133 # Maybe in the future just print out a "Yikes!" or something?
134 if model
.__table
__.exists(self
.session
.bind
):
135 raise TableAlreadyExists(
136 u
"Intended to create table '%s' but it already exists" %
137 model
.__table
__.name
)
139 self
.migration_model
.metadata
.create_all(
141 tables
=[model
.__table
__ for model
in self
.models
])
143 def populate_table_foundations(self
):
145 Create the table foundations (default rows) as layed out in FOUNDATIONS
146 in mediagoblin.db.models
148 from mediagoblin
.db
.models
import FOUNDATIONS
as MAIN_FOUNDATIONS
149 for Model
, rows
in MAIN_FOUNDATIONS
.items():
150 print u
'\n--> Laying foundations for %s table' % Model
.__name
__
151 for parameters
in rows
:
152 row
= Model(**parameters
)
155 def create_new_migration_record(self
):
157 Create a new migration record for this migration set
159 migration_record
= self
.migration_model(
161 version
=self
.latest_migration
)
162 self
.session
.add(migration_record
)
163 self
.session
.commit()
167 Print out a dry run of what we would have upgraded.
169 if self
.database_current_migration
is None:
171 u
'~> Woulda initialized: %s\n' % self
.name_for_printing())
174 migrations_to_run
= self
.migrations_to_run()
175 if migrations_to_run
:
177 u
'~> Woulda updated %s:\n' % self
.name_for_printing())
179 for migration_number
, migration_func
in migrations_to_run():
181 u
' + Would update %s, "%s"\n' % (
182 migration_number
, migration_func
.func_name
))
186 def name_for_printing(self
):
187 if self
.name
== u
'__main__':
188 return u
"main mediagoblin tables"
190 return u
'plugin "%s"' % self
.name
192 def init_or_migrate(self
):
194 Initialize the database or migrate if appropriate.
196 Returns information about whether or not we initialized
197 ('inited'), migrated ('migrated'), or did nothing (None)
199 assure_migrations_table_setup(self
.session
)
201 # Find out what migration number, if any, this database data is at,
202 # and what the latest is.
203 migration_number
= self
.database_current_migration
205 # Is this our first time? Is there even a table entry for
208 # - create all tables
209 # - create record in migrations registry
210 # - print / inform the user
212 if migration_number
is None:
213 self
.printer(u
"-> Initializing %s... " % self
.name_for_printing())
216 # auto-set at latest migration number
217 self
.create_new_migration_record()
218 if self
.name
==u
'__main__':
219 self
.populate_table_foundations()
221 self
.printer(u
"done.\n")
222 self
.set_current_migration()
225 # Run migrations, if appropriate.
226 migrations_to_run
= self
.migrations_to_run()
227 if migrations_to_run
:
229 u
'-> Updating %s:\n' % self
.name_for_printing())
230 for migration_number
, migration_func
in migrations_to_run
:
232 u
' + Running migration %s, "%s"... ' % (
233 migration_number
, migration_func
.func_name
))
234 migration_func(self
.session
)
235 self
.set_current_migration(migration_number
)
236 self
.printer('done.\n')
240 # Otherwise return None. Well it would do this anyway, but
245 class RegisterMigration(object):
247 Tool for registering migrations
251 @RegisterMigration(33)
252 def update_dwarves(database):
255 This will register your migration with the default migration
256 registry. Alternately, to specify a very specific
257 migration_registry, you can pass in that as the second argument.
259 Note, the number of your migration should NEVER be 0 or less than
260 0. 0 is the default "no migrations" state!
262 def __init__(self
, migration_number
, migration_registry
):
263 assert migration_number
> 0, "Migration number must be > 0!"
264 assert migration_number
not in migration_registry
, \
265 "Duplicate migration numbers detected! That's not allowed!"
267 self
.migration_number
= migration_number
268 self
.migration_registry
= migration_registry
270 def __call__(self
, migration
):
271 self
.migration_registry
[self
.migration_number
] = migration
275 def assure_migrations_table_setup(db
):
277 Make sure the migrations table is set up in the database.
279 from mediagoblin
.db
.models
import MigrationData
281 if not MigrationData
.__table
__.exists(db
.bind
):
282 MigrationData
.metadata
.create_all(
283 db
.bind
, tables
=[MigrationData
.__table
__])
286 def inspect_table(metadata
, table_name
):
287 """Simple helper to get a ref to an already existing table"""
288 return Table(table_name
, metadata
, autoload
=True,
289 autoload_with
=metadata
.bind
)