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 DESCENDING
34 from pymongo
.errors
import InvalidId
35 from mongokit
import ObjectId
37 from mediagoblin
.db
.indexes
import ACTIVE_INDEXES
, DEPRECATED_INDEXES
40 def add_new_indexes(database
, active_indexes
=ACTIVE_INDEXES
):
42 Add any new indexes to the database.
45 - database: pymongo or mongokit database instance.
46 - active_indexes: indexes to possibly add in the pattern of:
49 'index': [index_foo_goes_here],
51 where 'index' is the index to add and all other options are
52 arguments for collection.create_index.
55 A list of indexes added in form ('collection', 'index_name')
59 for collection_name
, indexes
in active_indexes
.iteritems():
60 collection
= database
[collection_name
]
61 collection_indexes
= collection
.index_information().keys()
63 for index_name
, index_data
in indexes
.iteritems():
64 if not index_name
in collection_indexes
:
65 # Get a copy actually so we don't modify the actual
67 index_data
= copy
.copy(index_data
)
68 index
= index_data
.pop('index')
69 collection
.create_index(
70 index
, name
=index_name
, **index_data
)
72 indexes_added
.append((collection_name
, index_name
))
77 def remove_deprecated_indexes(database
, deprecated_indexes
=DEPRECATED_INDEXES
):
79 Remove any deprecated indexes from the database.
82 - database: pymongo or mongokit database instance.
83 - deprecated_indexes: the indexes to deprecate in the pattern of:
84 {'collection': ['index_identifier1', 'index_identifier2']}
87 A list of indexes removed in form ('collection', 'index_name')
91 for collection_name
, index_names
in deprecated_indexes
.iteritems():
92 collection
= database
[collection_name
]
93 collection_indexes
= collection
.index_information().keys()
95 for index_name
in index_names
:
96 if index_name
in collection_indexes
:
97 collection
.drop_index(index_name
)
99 indexes_removed
.append((collection_name
, index_name
))
101 return indexes_removed