User migration works (but the rest of the system isn't updated for new user setup yet)
This commit is contained in:
parent
8820121ad1
commit
757f37a52d
39
mediagoblin/db/migrations.py
Normal file
39
mediagoblin/db/migrations.py
Normal file
@ -0,0 +1,39 @@
|
||||
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||
# Copyright (C) 2011 Free Software Foundation, Inc
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
from mongokit import DocumentMigration
|
||||
|
||||
from mediagoblin import globals as mediagoblin_globals
|
||||
|
||||
|
||||
class MediaEntryMigration(DocumentMigration):
|
||||
def allmigration01_uploader_to_reference(self):
|
||||
"""
|
||||
Old MediaEntry['uploader'] accidentally embedded the User instead
|
||||
of referencing it. Fix that!
|
||||
"""
|
||||
# uploader is an associative array
|
||||
self.target = {'uploader': {'$type': 3}}
|
||||
if not self.status:
|
||||
for doc in self.collection.find(self.target):
|
||||
self.update = {
|
||||
'$set': {
|
||||
'uploader': doc['uploader']['_id']}}
|
||||
self.collection.update(
|
||||
self.target, self.update, multi=True, safe=True)
|
||||
|
||||
|
||||
MIGRATE_CLASSES = ['MediaEntry']
|
@ -21,6 +21,8 @@ from mongokit import Document, Set
|
||||
from mediagoblin import util
|
||||
from mediagoblin.auth import lib as auth_lib
|
||||
from mediagoblin import globals as mediagoblin_globals
|
||||
from mediagoblin.db import migrations
|
||||
from mediagoblin.db.util import ObjectId
|
||||
|
||||
###################
|
||||
# Custom validators
|
||||
@ -67,7 +69,7 @@ class MediaEntry(Document):
|
||||
__collection__ = 'media_entries'
|
||||
|
||||
structure = {
|
||||
'uploader': User,
|
||||
'uploader': ObjectId,
|
||||
'title': unicode,
|
||||
'slug': unicode,
|
||||
'created': datetime.datetime,
|
||||
@ -99,6 +101,8 @@ class MediaEntry(Document):
|
||||
'created': datetime.datetime.utcnow,
|
||||
'state': u'unprocessed'}
|
||||
|
||||
migration_handler = migrations.MediaEntryMigration
|
||||
|
||||
# Actually we should referene uniqueness by uploader, but we
|
||||
# should fix http://bugs.foocorp.net/issues/340 first.
|
||||
# indexes = [
|
||||
|
@ -24,6 +24,10 @@ SUBCOMMAND_MAP = {
|
||||
'setup': 'mediagoblin.gmg_commands.shell:shell_parser_setup',
|
||||
'func': 'mediagoblin.gmg_commands.shell:shell',
|
||||
'help': 'Run a shell with some tools pre-setup'},
|
||||
'migrate': {
|
||||
'setup': 'mediagoblin.gmg_commands.migrate:migrate_parser_setup',
|
||||
'func': 'mediagoblin.gmg_commands.migrate:migrate',
|
||||
'help': 'Apply all unapplied bulk migrations to the database'},
|
||||
}
|
||||
|
||||
|
||||
|
45
mediagoblin/gmg_commands/migrate.py
Normal file
45
mediagoblin/gmg_commands/migrate.py
Normal file
@ -0,0 +1,45 @@
|
||||
# GNU MediaGoblin -- federated, autonomous media hosting
|
||||
# Copyright (C) 2011 Free Software Foundation, Inc
|
||||
#
|
||||
# This program is free software: you can redistribute it and/or modify
|
||||
# it under the terms of the GNU Affero General Public License as published by
|
||||
# the Free Software Foundation, either version 3 of the License, or
|
||||
# (at your option) any later version.
|
||||
#
|
||||
# This program is distributed in the hope that it will be useful,
|
||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
# GNU Affero General Public License for more details.
|
||||
#
|
||||
# You should have received a copy of the GNU Affero General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
|
||||
from mediagoblin.db import migrations
|
||||
from mediagoblin.gmg_commands import util as commands_util
|
||||
from mediagoblin import globals as mgoblin_globals
|
||||
|
||||
|
||||
def migrate_parser_setup(subparser):
|
||||
subparser.add_argument(
|
||||
'-cf', '--conf_file', default='mediagoblin.ini',
|
||||
help="Config file used to set up environment")
|
||||
subparser.add_argument(
|
||||
'-cs', '--app_section', default='app:mediagoblin',
|
||||
help="Section of the config file where the app config is stored.")
|
||||
|
||||
|
||||
def migrate(args):
|
||||
mgoblin_app = commands_util.setup_app(args)
|
||||
print "Applying migrations..."
|
||||
|
||||
for model_name in migrations.MIGRATE_CLASSES:
|
||||
model = getattr(mgoblin_app.db, model_name)
|
||||
|
||||
if not hasattr(model, 'migration_handler') or not model.collection:
|
||||
continue
|
||||
|
||||
migration = model.migration_handler(model)
|
||||
migration.migrate_all(collection=model.collection)
|
||||
|
||||
print "... done."
|
Loading…
x
Reference in New Issue
Block a user