Merge remote branch 'remotes/elrond/misc/add_db_field'

This commit is contained in:
Christopher Allan Webber 2011-10-23 15:38:38 -05:00
commit 839aeaa944
2 changed files with 18 additions and 27 deletions

View File

@ -18,6 +18,17 @@ from mediagoblin.db.util import RegisterMigration
from mediagoblin.tools.text import cleaned_markdown_conversion from mediagoblin.tools.text import cleaned_markdown_conversion
def add_table_field(db, table_name, field_name, default_value):
"""
Add a new field to the table/collection named table_name.
The field will have the name field_name and the value default_value
"""
db[table_name].update(
{field_name: {'$exists': False}},
{'$set': {field_name: default_value}},
multi=True)
# Please see mediagoblin/tests/test_migrations.py for some examples of # Please see mediagoblin/tests/test_migrations.py for some examples of
# basic migrations. # basic migrations.
@ -70,11 +81,7 @@ def mediaentry_add_queued_task_id(database):
""" """
Add the 'queued_task_id' field for entries that don't have it. Add the 'queued_task_id' field for entries that don't have it.
""" """
collection = database['media_entries'] add_table_field(database, 'media_entries', 'queued_task_id', None)
collection.update(
{'queued_task_id': {'$exists': False}},
{'$set': {'queued_task_id': None}},
multi=True)
@RegisterMigration(5) @RegisterMigration(5)
@ -82,16 +89,8 @@ def mediaentry_add_fail_error_and_metadata(database):
""" """
Add 'fail_error' and 'fail_metadata' fields to media entries Add 'fail_error' and 'fail_metadata' fields to media entries
""" """
collection = database['media_entries'] add_table_field(database, 'media_entries', 'fail_error', None)
collection.update( add_table_field(database, 'media_entries', 'fail_metadata', {})
{'fail_error': {'$exists': False}},
{'$set': {'fail_error': None}},
multi=True)
collection.update(
{'fail_metadata': {'$exists': False}},
{'$set': {'fail_metadata': {}}},
multi=True)
@RegisterMigration(6) @RegisterMigration(6)
@ -99,11 +98,5 @@ def user_add_forgot_password_token_and_expires(database):
""" """
Add token and expiration fields to help recover forgotten passwords Add token and expiration fields to help recover forgotten passwords
""" """
database['users'].update( add_table_field(database, 'users', 'fp_verification_key', None)
{'fp_verification_key': {'$exists': False}}, add_table_field(database, 'users', 'fp_token_expire', None)
{'$set': {'fp_verification_key': None}},
multi=True)
database['users'].update(
{'fp_token_expire': {'$exists': False}},
{'$set': {'fp_token_expire': None}},
multi=True)

View File

@ -23,6 +23,7 @@ from mediagoblin.tests.tools import (
from mediagoblin.db.util import ( from mediagoblin.db.util import (
RegisterMigration, MigrationManager, ObjectId, RegisterMigration, MigrationManager, ObjectId,
MissingCurrentMigration) MissingCurrentMigration)
from mediagoblin.db.migrations import add_table_field
# This one will get filled with local migrations # This one will get filled with local migrations
TEST_MIGRATION_REGISTRY = {} TEST_MIGRATION_REGISTRY = {}
@ -45,10 +46,7 @@ def creature_add_magical_powers(database):
magical powers, all existing monsters, setting to an empty list is magical powers, all existing monsters, setting to an empty list is
fine. fine.
""" """
database['creatures'].update( add_table_field(database, 'creatures', 'magical_powers', [])
{'magical_powers': {'$exists': False}},
{'$set': {'magical_powers': []}},
multi=True)
@RegisterMigration(2, TEST_MIGRATION_REGISTRY) @RegisterMigration(2, TEST_MIGRATION_REGISTRY)