aboutsummaryrefslogtreecommitdiffstats
path: root/mediagoblin/gmg_commands/dbupdate.py
blob: 1fc5121a17a8a47bcb1d1a104f8ecdd1c447645a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011 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 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


class DatabaseData(object):
    def __init__(self, name, models, migrations):
        self.name = name
        self.models = models
        self.migrations = migrations

    def make_migration_manager(self, db):
        return MigrationManager(
            self.name, self.models, self.migrations, db)


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)

    # If migrations table not made, make it!
    assure_migrations_table_setup(db)

    # 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(db)
        migration_manager.init_or_migrate()