Set up virtualenv to use py2
[mediagoblin.git] / mediagoblin / db / util.py
index 8431361a89fc6dc6d22c962e238816fb9e3e0e60..aba9c59c79f06ef873f569d4e7aaf74cb6d1c790 100644 (file)
 # 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/>.
 
+import sys
+
+from mediagoblin import mg_globals as mgg
 from mediagoblin.db.base import Session
 from mediagoblin.db.models import MediaEntry, Tag, MediaTag, Collection
-
+from mediagoblin.gmg_commands.dbupdate import gather_database_data
 
 ##########################
 # Random utility functions
@@ -68,6 +71,18 @@ def check_collection_slug_used(creator_id, slug, ignore_c_id):
     return does_exist
 
 
+def check_db_up_to_date():
+    """Check if the database is up to date and quit if not"""
+    dbdatas = gather_database_data(mgg.global_config.get('plugins', {}).keys())
+
+    for dbdata in dbdatas:
+        migration_manager = dbdata.make_migration_manager(Session())
+        if migration_manager.database_current_migration is None or \
+                migration_manager.migrations_to_run():
+            sys.exit("Your database is not up to date. Please run "
+                     "'gmg dbupdate' before starting MediaGoblin.")
+
+
 if __name__ == '__main__':
     from mediagoblin.db.open import setup_connection_and_db_from_config