1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 Free Software Foundation, Inc
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 Utilities for database operations.
20 Some note on migration and indexing tools:
22 We store information about what the state of the database is in the
23 'mediagoblin' document of the 'app_metadata' collection. Keys in that
24 document relevant to here:
26 - 'migration_number': The integer representing the current state of
32 # Imports that other modules might use
33 from pymongo
import ASCENDING
, DESCENDING
34 from pymongo
.errors
import InvalidId
35 from mongokit
import ObjectId
37 from mediagoblin
.db
.indexes
import ACTIVE_INDEXES
, DEPRECATED_INDEXES
45 def add_new_indexes(database
, active_indexes
=ACTIVE_INDEXES
):
47 Add any new indexes to the database.
50 - database: pymongo or mongokit database instance.
51 - active_indexes: indexes to possibly add in the pattern of:
54 'index': [index_foo_goes_here],
56 where 'index' is the index to add and all other options are
57 arguments for collection.create_index.
60 A list of indexes added in form ('collection', 'index_name')
64 for collection_name
, indexes
in active_indexes
.iteritems():
65 collection
= database
[collection_name
]
66 collection_indexes
= collection
.index_information().keys()
68 for index_name
, index_data
in indexes
.iteritems():
69 if not index_name
in collection_indexes
:
70 # Get a copy actually so we don't modify the actual
72 index_data
= copy
.copy(index_data
)
73 index
= index_data
.pop('index')
74 collection
.create_index(
75 index
, name
=index_name
, **index_data
)
77 indexes_added
.append((collection_name
, index_name
))
82 def remove_deprecated_indexes(database
, deprecated_indexes
=DEPRECATED_INDEXES
):
84 Remove any deprecated indexes from the database.
87 - database: pymongo or mongokit database instance.
88 - deprecated_indexes: the indexes to deprecate in the pattern of:
89 {'collection': ['index_identifier1', 'index_identifier2']}
92 A list of indexes removed in form ('collection', 'index_name')
96 for collection_name
, index_names
in deprecated_indexes
.iteritems():
97 collection
= database
[collection_name
]
98 collection_indexes
= collection
.index_information().keys()
100 for index_name
in index_names
:
101 if index_name
in collection_indexes
:
102 collection
.drop_index(index_name
)
104 indexes_removed
.append((collection_name
, index_name
))
106 return indexes_removed
113 # The default migration registry...
115 # Don't set this yourself! RegisterMigration will automatically fill
116 # this with stuff via decorating methods in migrations.py
121 class RegisterMigration(object):
123 Tool for registering migrations
127 @RegisterMigration(33)
128 def update_dwarves(database):
131 This will register your migration with the default migration
132 registry. Alternately, to specify a very specific
133 migration_registry, you can pass in that as the second argument.
135 Note, the number of your migration should NEVER be 0 or less than
136 0. 0 is the default "no migrations" state!
138 def __init__(self
, migration_number
, migration_registry
=MIGRATIONS
):
139 assert migration_number
> 0, "Migration number must be > 0!"
141 self
.migration_number
= migration_number
142 self
.migration_registry
= migration_registry
144 def __call__(self
, migration
):
145 self
.migration_registry
[self
.migration_number
] = migration
149 class MigrationManager(object):
151 Migration handling tool.
153 Takes information about a database, lets you update the database
154 to the latest migrations, etc.
156 def __init__(self
, database
, migration_registry
=MIGRATIONS
):
159 - database: database we're going to migrate
160 - migration_registry: where we should find all migrations to
163 self
.database
= database
164 self
.migration_registry
= migration_registry
165 self
._sorted
_migrations
= None
168 def sorted_migrations(self
):
170 Sort migrations if necessary and store in self._sorted_migrations
172 if not self
._sorted
_migrations
:
173 self
._sorted
_migrations
= sorted(
174 self
.migration_registry
.items(),
175 # sort on the key... the migration number
176 key
=lambda migration_tuple
: migration_tuple
[0])
178 return self
._sorted
_migrations
180 def latest_migration(self
):
182 Return a migration number for the latest migration, or 0 if
183 there are no migrations.
185 if self
.sorted_migrations
:
186 return self
.sorted_migrations
[-1][0]
188 # If no migrations have been set, we start at 0.
191 def set_current_migration(self
, migration_number
):
193 Set the migration in the database to migration_number
195 # Add the mediagoblin migration if necessary
196 self
.database
[u
'app_metadata'].update(
197 {u
'_id': u
'mediagoblin'},
198 {u
'$set': {u
'current_migration': migration_number
}},
201 def install_migration_version_if_missing(self
):
203 Sets the migration to the latest version if no migration
204 version at all is set.
206 mgoblin_metadata
= self
.database
[u
'app_metadata'].find_one(
207 {u
'_id': u
'mediagoblin'})
208 if not mgoblin_metadata
:
209 latest_migration
= self
.latest_migration()
210 self
.set_current_migration(latest_migration
)
212 def database_current_migration(self
):
214 Return the current migration in the database.
216 mgoblin_metadata
= self
.database
[u
'app_metadata'].find_one(
217 {u
'_id': u
'mediagoblin'})
218 if not mgoblin_metadata
:
221 return mgoblin_metadata
[u
'current_migration']
223 def database_at_latest_migration(self
):
225 See if the database is at the latest migration.
228 current_migration
= self
.database_current_migration()
229 return current_migration
== self
.latest_migration()
231 def migrations_to_run(self
):
233 Get a list of migrations to run still, if any.
235 Note that calling this will set your migration version to the
236 latest version if it isn't installed to anything yet!
238 # If we aren't set to any version number, presume we're at the
239 # latest (which means we'll do nothing here...)
240 self
.install_migration_version_if_missing()
242 db_current_migration
= self
.database_current_migration()
245 (migration_number
, migration_func
)
246 for migration_number
, migration_func
in self
.sorted_migrations
247 if migration_number
> db_current_migration
]
249 def migrate_new(self
, pre_callback
=None, post_callback
=None):
253 Includes two optional args:
254 - pre_callback: if called, this is a callback on something to
255 run pre-migration. Takes (migration_number, migration_func)
257 - pre_callback: if called, this is a callback on something to
258 run post-migration. Takes (migration_number, migration_func)
261 for migration_number
, migration_func
in self
.migrations_to_run():
263 pre_callback(migration_number
, migration_func
)
264 migration_func(self
.database
)
265 self
.set_current_migration(migration_number
)
267 post_callback(migration_number
, migration_func
)