diff options
Diffstat (limited to 'mediagoblin/gmg_commands')
-rw-r--r-- | mediagoblin/gmg_commands/__init__.py | 8 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/dbupdate.py | 89 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/migrate.py | 9 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/mongosql.py | 28 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/shell.py | 44 | ||||
-rw-r--r-- | mediagoblin/gmg_commands/wipealldata.py | 8 |
6 files changed, 174 insertions, 12 deletions
diff --git a/mediagoblin/gmg_commands/__init__.py b/mediagoblin/gmg_commands/__init__.py index db944b3c..054e2616 100644 --- a/mediagoblin/gmg_commands/__init__.py +++ b/mediagoblin/gmg_commands/__init__.py @@ -53,6 +53,14 @@ SUBCOMMAND_MAP = { 'setup': 'mediagoblin.gmg_commands.import_export:import_export_parse_setup', 'func': 'mediagoblin.gmg_commands.import_export:env_import', 'help': 'Exports the data for this MediaGoblin instance'}, + 'dbupdate': { + 'setup': 'mediagoblin.gmg_commands.dbupdate:dbupdate_parse_setup', + 'func': 'mediagoblin.gmg_commands.dbupdate:dbupdate', + 'help': 'Set up or update the SQL database'}, + 'convert_mongo_to_sql': { + 'setup': 'mediagoblin.gmg_commands.mongosql:mongosql_parser_setup', + 'func': 'mediagoblin.gmg_commands.mongosql:mongosql', + 'help': 'Convert Mongo DB data to SQL DB data'}, } diff --git a/mediagoblin/gmg_commands/dbupdate.py b/mediagoblin/gmg_commands/dbupdate.py new file mode 100644 index 00000000..27698170 --- /dev/null +++ b/mediagoblin/gmg_commands/dbupdate.py @@ -0,0 +1,89 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. +# +# 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 sqlalchemy.orm import sessionmaker + +from mediagoblin.db.sql.open import setup_connection_and_db_from_config +from mediagoblin.db.sql.util import ( + MigrationManager, assure_migrations_table_setup) +from mediagoblin.init import setup_global_and_app_config +from mediagoblin.tools.common import import_component + + +def dbupdate_parse_setup(subparser): + pass + + +class DatabaseData(object): + def __init__(self, name, models, migrations): + self.name = name + self.models = models + self.migrations = migrations + + def make_migration_manager(self, session): + return MigrationManager( + self.name, self.models, self.migrations, session) + + +def gather_database_data(media_types): + """ + Gather all database data relevant to the extensions we have + installed so we can do migrations and table initialization. + + Returns a list of DatabaseData objects. + """ + managed_dbdata = [] + + # Add main first + from mediagoblin.db.sql.models import MODELS as MAIN_MODELS + from mediagoblin.db.sql.migrations import MIGRATIONS as MAIN_MIGRATIONS + + managed_dbdata.append( + DatabaseData( + '__main__', MAIN_MODELS, MAIN_MIGRATIONS)) + + # Then get all registered media managers (eventually, plugins) + for media_type in media_types: + models = import_component('%s.models:MODELS' % media_type) + migrations = import_component('%s.migrations:MIGRATIONS' % media_type) + managed_dbdata.append( + DatabaseData(media_type, models, migrations)) + + return managed_dbdata + + +def dbupdate(args): + """ + Initialize or migrate the database as specified by the config file. + + Will also initialize or migrate all extensions (media types, and + in the future, plugins) + """ + globa_config, app_config = setup_global_and_app_config(args.conf_file) + + # Gather information from all media managers / projects + dbdatas = gather_database_data(app_config['media_types']) + + # Set up the database + connection, db = setup_connection_and_db_from_config(app_config) + + Session = sessionmaker(bind=db.engine) + + # Setup media managers for all dbdata, run init/migrate and print info + # For each component, create/migrate tables + for dbdata in dbdatas: + migration_manager = dbdata.make_migration_manager(Session()) + migration_manager.init_or_migrate() diff --git a/mediagoblin/gmg_commands/migrate.py b/mediagoblin/gmg_commands/migrate.py index cacf5d19..af541786 100644 --- a/mediagoblin/gmg_commands/migrate.py +++ b/mediagoblin/gmg_commands/migrate.py @@ -17,7 +17,7 @@ import sys from mediagoblin.db.mongo import util as db_util -from mediagoblin.db.open import setup_connection_and_db_from_config +from mediagoblin.db.mongo.open import setup_connection_and_db_from_config from mediagoblin.init import setup_global_and_app_config # This MUST be imported so as to set up the appropriate migrations! @@ -41,7 +41,12 @@ def _print_finished_migration(migration_number, migration_func): def migrate(args): - global_config, app_config = setup_global_and_app_config(args.conf_file) + run_migrate(args.conf_file) + + +def run_migrate(conf_file): + global_config, app_config = setup_global_and_app_config(conf_file) + connection, db = setup_connection_and_db_from_config( app_config, use_pymongo=True) migration_manager = db_util.MigrationManager(db) diff --git a/mediagoblin/gmg_commands/mongosql.py b/mediagoblin/gmg_commands/mongosql.py new file mode 100644 index 00000000..dd53f575 --- /dev/null +++ b/mediagoblin/gmg_commands/mongosql.py @@ -0,0 +1,28 @@ +# GNU MediaGoblin -- federated, autonomous media hosting +# Copyright (C) 2012 MediaGoblin contributors. See AUTHORS. +# +# 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/>. + + +def mongosql_parser_setup(subparser): + pass + + +def mongosql(args): + # First, make sure our mongo migrations are up to date... + from mediagoblin.gmg_commands.migrate import run_migrate + run_migrate(args.conf_file) + + from mediagoblin.db.sql.convert import run_conversion + run_conversion(args.conf_file) diff --git a/mediagoblin/gmg_commands/shell.py b/mediagoblin/gmg_commands/shell.py index fe15e9f7..ec1ab535 100644 --- a/mediagoblin/gmg_commands/shell.py +++ b/mediagoblin/gmg_commands/shell.py @@ -22,7 +22,9 @@ from mediagoblin.gmg_commands import util as commands_util def shell_parser_setup(subparser): - pass + subparser.add_argument( + '--ipython', help='Use ipython', + action="store_true") SHELL_BANNER = """\ @@ -34,16 +36,42 @@ Available vars: - db: database instance """ +def py_shell(**user_namespace): + """ + Run a shell using normal python shell. + """ + code.interact( + banner=SHELL_BANNER, + local=user_namespace) + + +def ipython_shell(**user_namespace): + """ + Run a shell for the user using ipython. + """ + try: + from IPython import embed + except: + print "IPython not available... exiting!" + return + + embed( + banner1=SHELL_BANNER, + user_ns=user_namespace) + def shell(args): """ Setup a shell for the user + either a normal Python shell + or an IPython one """ - mgoblin_app = commands_util.setup_app(args) + user_namespace = { + 'mg_globals': mg_globals, + 'mgoblin_app': commands_util.setup_app(args), + 'db': mg_globals.database} - code.interact( - banner=SHELL_BANNER, - local={ - 'mgoblin_app': mgoblin_app, - 'mg_globals': mg_globals, - 'db': mg_globals.database}) + if args.ipython: + ipython_shell(**user_namespace) + else: + py_shell(**user_namespace) diff --git a/mediagoblin/gmg_commands/wipealldata.py b/mediagoblin/gmg_commands/wipealldata.py index 3081bbc0..37217fd1 100644 --- a/mediagoblin/gmg_commands/wipealldata.py +++ b/mediagoblin/gmg_commands/wipealldata.py @@ -20,12 +20,16 @@ import sys import os import shutil +from mediagoblin.init import setup_global_and_app_config + def wipe_parser_setup(subparser): pass def wipe(args): + global_config, app_config = setup_global_and_app_config(args.conf_file) + print "*** WARNING! ***" print "" print "Running this will destroy your mediagoblin database," @@ -39,12 +43,12 @@ def wipe(args): 'Are you **SURE** you want to destroy your environment? ' '(if so, type "yes")> ') - if not drop_it == 'yes': + if drop_it != 'yes': return print "nixing data in mongodb...." conn = pymongo.Connection() - conn.drop_database('mediagoblin') + conn.drop_database(app_config["db_name"]) for directory in [os.path.join(os.getcwd(), "user_dev", "media"), os.path.join(os.getcwd(), "user_dev", "beaker")]: |