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/>.
18 class MigrationManager(object):
20 Migration handling tool.
22 Takes information about a database, lets you update the database
23 to the latest migrations, etc.
26 def __init__(self
, name
, models
, migration_registry
, database
):
29 - name: identifier of this section of the database
30 - database: database we're going to migrate
31 - migration_registry: where we should find all migrations to
36 self
.database
= database
37 self
.migration_registry
= migration_registry
38 self
._sorted
_migrations
= None
41 from mediagoblin
.db
.sql
.models
import MigrationData
43 self
.migration_model
= MigrationData
44 self
.migration_table
= MigrationData
.__table
__
47 def sorted_migrations(self
):
49 Sort migrations if necessary and store in self._sorted_migrations
51 if not self
._sorted
_migrations
:
52 self
._sorted
_migrations
= sorted(
53 self
.migration_registry
.items(),
54 # sort on the key... the migration number
55 key
=lambda migration_tuple
: migration_tuple
[0])
57 return self
._sorted
_migrations
59 def latest_migration(self
):
61 Return a migration number for the latest migration, or 0 if
62 there are no migrations.
64 if self
.sorted_migrations
:
65 return self
.sorted_migrations
[-1][0]
67 # If no migrations have been set, we start at 0.
70 def database_current_migration(self
):
72 Return the current migration in the database.
76 def set_current_migration(self
, migration_number
):
78 Set the migration in the database to migration_number
83 def migrations_to_run(self
):
85 Get a list of migrations to run still, if any.
87 Note that calling this will set your migration version to the
88 latest version if it isn't installed to anything yet!
91 # self._ensure_current_migration_record()
93 # db_current_migration = self.database_current_migration()
96 # (migration_number, migration_func)
97 # for migration_number, migration_func in self.sorted_migrations
98 # if migration_number > db_current_migration]
101 def init_or_migrate(self
):
102 # Find out what migration number, if any, this database data is at,
103 # and what the latest is.
105 # Is this our first time? Is there even a table entry for
109 # - create all tables
110 # - create record in migrations registry
111 # - print / inform the user
114 # Run migrations, if appropriate.
116 # If ran migrations, return 'migrated'. Otherwise, return None.
120 class RegisterMigration(object):
122 Tool for registering migrations
126 @RegisterMigration(33)
127 def update_dwarves(database):
130 This will register your migration with the default migration
131 registry. Alternately, to specify a very specific
132 migration_registry, you can pass in that as the second argument.
134 Note, the number of your migration should NEVER be 0 or less than
135 0. 0 is the default "no migrations" state!
137 def __init__(self
, migration_number
, migration_registry
):
138 assert migration_number
> 0, "Migration number must be > 0!"
139 assert migration_number
not in migration_registry
, \
140 "Duplicate migration numbers detected! That's not allowed!"
142 self
.migration_number
= migration_number
143 self
.migration_registry
= migration_registry
145 def __call__(self
, migration
):
146 self
.migration_registry
[self
.migration_number
] = migration
150 def assure_migrations_table_setup(db
):
152 Make sure the migrations table is set up in the database.
154 from mediagoblin
.db
.sql
.models
import MigrationData
156 if not MigrationData
.__table
__.exists(db
):
157 MigrationData
.metadata
.create_all(
158 db
, tables
=[MigrationData
.__table
__])