I actually had to do a bit more work than I thought, because I needed to account

for plugins. In this commit I changed the MigrationManager and DatabaseData ob-
jects to account for FOUNDATIONS in any plugin's (or main program's) models.py
file.
This commit is contained in:
tilly-Q 2013-07-29 17:15:29 -04:00
parent f2b2008da5
commit 08cd10d84f
2 changed files with 27 additions and 13 deletions

View File

@ -29,7 +29,7 @@ class MigrationManager(object):
to the latest migrations, etc. to the latest migrations, etc.
""" """
def __init__(self, name, models, migration_registry, session, def __init__(self, name, models, foundations, migration_registry, session,
printer=simple_printer): printer=simple_printer):
""" """
Args: Args:
@ -40,6 +40,7 @@ class MigrationManager(object):
""" """
self.name = unicode(name) self.name = unicode(name)
self.models = models self.models = models
self.foundations = foundations
self.session = session self.session = session
self.migration_registry = migration_registry self.migration_registry = migration_registry
self._sorted_migrations = None self._sorted_migrations = None
@ -145,12 +146,11 @@ class MigrationManager(object):
Create the table foundations (default rows) as layed out in FOUNDATIONS Create the table foundations (default rows) as layed out in FOUNDATIONS
in mediagoblin.db.models in mediagoblin.db.models
""" """
from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS for Model, rows in self.foundations.items():
for Model, rows in MAIN_FOUNDATIONS.items(): print u'\n + Laying foundations for %s table' % (Model.__name__)
print u'\n--> Laying foundations for %s table' % Model.__name__
for parameters in rows: for parameters in rows:
row = Model(**parameters) new_row = Model(**parameters)
row.save() new_row.save()
def create_new_migration_record(self): def create_new_migration_record(self):
""" """
@ -215,8 +215,7 @@ class MigrationManager(object):
self.init_tables() self.init_tables()
# auto-set at latest migration number # auto-set at latest migration number
self.create_new_migration_record() self.create_new_migration_record()
if self.name==u'__main__': self.populate_table_foundations()
self.populate_table_foundations()
self.printer(u"done.\n") self.printer(u"done.\n")
self.set_current_migration() self.set_current_migration()

View File

@ -32,14 +32,15 @@ def dbupdate_parse_setup(subparser):
class DatabaseData(object): class DatabaseData(object):
def __init__(self, name, models, migrations): def __init__(self, name, models, foundations, migrations):
self.name = name self.name = name
self.models = models self.models = models
self.foundations = foundations
self.migrations = migrations self.migrations = migrations
def make_migration_manager(self, session): def make_migration_manager(self, session):
return MigrationManager( return MigrationManager(
self.name, self.models, self.migrations, session) self.name, self.models, self.foundations, self.migrations, session)
def gather_database_data(plugins): def gather_database_data(plugins):
@ -54,10 +55,11 @@ def gather_database_data(plugins):
# Add main first # Add main first
from mediagoblin.db.models import MODELS as MAIN_MODELS from mediagoblin.db.models import MODELS as MAIN_MODELS
from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS
from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS
managed_dbdata.append( managed_dbdata.append(
DatabaseData( DatabaseData(
u'__main__', MAIN_MODELS, MAIN_MIGRATIONS)) u'__main__', MAIN_MODELS, MAIN_FOUNDATIONS, MAIN_MIGRATIONS))
for plugin in plugins: for plugin in plugins:
try: try:
@ -83,13 +85,26 @@ forgotten to add it? ({1})'.format(plugin, exc))
migrations = {} migrations = {}
except AttributeError as exc: except AttributeError as exc:
_log.debug('Cloud not find MIGRATIONS in {0}.migrations, have you \ _log.debug('Could not find MIGRATIONS in {0}.migrations, have you \
forgotten to add it? ({1})'.format(plugin, exc)) forgotten to add it? ({1})'.format(plugin, exc))
migrations = {} migrations = {}
try:
foundations = import_component('{0}.models:FOUNDATIONS'.format(plugin))
except ImportError as exc:
_log.debug('No foundations found for {0}: {1}'.format(
plugin,
exc))
foundations = []
except AttributeError as exc:
_log.debug('Could not find FOUNDATIONS in {0}.models, have you \
forgotten to add it? ({1})'.format(plugin, exc))
foundations = {}
if models: if models:
managed_dbdata.append( managed_dbdata.append(
DatabaseData(plugin, models, migrations)) DatabaseData(plugin, models, foundations, migrations))
return managed_dbdata return managed_dbdata