Merge remote-tracking branch 'tilly-q/ticket-679'

This commit is contained in:
Rodney Ewing 2013-08-02 07:28:35 -07:00
commit bf5a8e5452
4 changed files with 86 additions and 15 deletions

View File

@ -29,7 +29,7 @@ class MigrationManager(object):
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):
"""
Args:
@ -40,6 +40,7 @@ class MigrationManager(object):
"""
self.name = unicode(name)
self.models = models
self.foundations = foundations
self.session = session
self.migration_registry = migration_registry
self._sorted_migrations = None
@ -140,6 +141,18 @@ class MigrationManager(object):
self.session.bind,
tables=[model.__table__ for model in self.models])
def populate_table_foundations(self):
"""
Create the table foundations (default rows) as layed out in FOUNDATIONS
in mediagoblin.db.models
"""
for Model, rows in self.foundations.items():
self.printer(u' + Laying foundations for %s table\n' %
(Model.__name__))
for parameters in rows:
new_row = Model(**parameters)
self.session.add(new_row)
def create_new_migration_record(self):
"""
Create a new migration record for this migration set
@ -202,9 +215,9 @@ class MigrationManager(object):
self.init_tables()
# auto-set at latest migration number
self.create_new_migration_record()
self.create_new_migration_record()
self.printer(u"done.\n")
self.populate_table_foundations()
self.set_current_migration()
return u'inited'

View File

@ -585,6 +585,21 @@ MODELS = [
Notification, CommentNotification, ProcessingNotification,
CommentSubscription]
"""
Foundations are the default rows that are created immediately after the tables
are initialized. Each entry to this dictionary should be in the format of:
ModelConstructorObject:List of Dictionaries
(Each Dictionary represents a row on the Table to be created, containing each
of the columns' names as a key string, and each of the columns' values as a
value)
ex. [NOTE THIS IS NOT BASED OFF OF OUR USER TABLE]
user_foundations = [{'name':u'Joanna', 'age':24},
{'name':u'Andrea', 'age':41}]
FOUNDATIONS = {User:user_foundations}
"""
FOUNDATIONS = {}
######################################################
# Special, migrations-tracking table

View File

@ -32,14 +32,15 @@ def dbupdate_parse_setup(subparser):
class DatabaseData(object):
def __init__(self, name, models, migrations):
def __init__(self, name, models, foundations, migrations):
self.name = name
self.models = models
self.foundations = foundations
self.migrations = migrations
def make_migration_manager(self, session):
return MigrationManager(
self.name, self.models, self.migrations, session)
self.name, self.models, self.foundations, self.migrations, session)
def gather_database_data(plugins):
@ -54,10 +55,11 @@ def gather_database_data(plugins):
# Add main first
from mediagoblin.db.models import MODELS as MAIN_MODELS
from mediagoblin.db.migrations import MIGRATIONS as MAIN_MIGRATIONS
from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS
managed_dbdata.append(
DatabaseData(
u'__main__', MAIN_MODELS, MAIN_MIGRATIONS))
u'__main__', MAIN_MODELS, MAIN_FOUNDATIONS, MAIN_MIGRATIONS))
for plugin in plugins:
try:
@ -83,13 +85,26 @@ forgotten to add it? ({1})'.format(plugin, exc))
migrations = {}
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))
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:
managed_dbdata.append(
DatabaseData(plugin, models, migrations))
DatabaseData(plugin, models, foundations, migrations))
return managed_dbdata

View File

@ -58,6 +58,10 @@ class Level1(Base1):
SET1_MODELS = [Creature1, Level1]
FOUNDATIONS = {Creature1:[{'name':u'goblin','num_legs':2,'is_demon':False},
{'name':u'cerberus','num_legs':4,'is_demon':True}]
}
SET1_MIGRATIONS = {}
#######################################################
@ -542,7 +546,6 @@ def _insert_migration3_objects(session):
session.commit()
def create_test_engine():
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:', echo=False)
@ -572,7 +575,7 @@ def test_set1_to_set3():
printer = CollectingPrinter()
migration_manager = MigrationManager(
u'__main__', SET1_MODELS, SET1_MIGRATIONS, Session(),
u'__main__', SET1_MODELS, FOUNDATIONS, SET1_MIGRATIONS, Session(),
printer)
# Check latest migration and database current migration
@ -585,11 +588,13 @@ def test_set1_to_set3():
assert result == u'inited'
# Check output
assert printer.combined_string == (
"-> Initializing main mediagoblin tables... done.\n")
"-> Initializing main mediagoblin tables... done.\n" + \
" + Laying foundations for Creature1 table\n" )
# Check version in database
assert migration_manager.latest_migration == 0
assert migration_manager.database_current_migration == 0
# Install the initial set
# -----------------------
@ -597,8 +602,8 @@ def test_set1_to_set3():
# Try to "re-migrate" with same manager settings... nothing should happen
migration_manager = MigrationManager(
u'__main__', SET1_MODELS, SET1_MIGRATIONS, Session(),
printer)
u'__main__', SET1_MODELS, FOUNDATIONS, SET1_MIGRATIONS,
Session(), printer)
assert migration_manager.init_or_migrate() == None
# Check version in database
@ -639,6 +644,20 @@ def test_set1_to_set3():
# Now check to see if stuff seems to be in there.
session = Session()
# Check the creation of the foundation rows on the creature table
creature = session.query(Creature1).filter_by(
name=u'goblin').one()
assert creature.num_legs == 2
assert creature.is_demon == False
creature = session.query(Creature1).filter_by(
name=u'cerberus').one()
assert creature.num_legs == 4
assert creature.is_demon == True
# Check the creation of the inserted rows on the creature and levels tables
creature = session.query(Creature1).filter_by(
name=u'centipede').one()
assert creature.num_legs == 100
@ -679,7 +698,7 @@ def test_set1_to_set3():
# isn't said to be updated yet
printer = CollectingPrinter()
migration_manager = MigrationManager(
u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(),
u'__main__', SET3_MODELS, FOUNDATIONS, SET3_MIGRATIONS, Session(),
printer)
assert migration_manager.latest_migration == 8
@ -706,7 +725,7 @@ def test_set1_to_set3():
# Make sure version matches expected
migration_manager = MigrationManager(
u'__main__', SET3_MODELS, SET3_MIGRATIONS, Session(),
u'__main__', SET3_MODELS, FOUNDATIONS, SET3_MIGRATIONS, Session(),
printer)
assert migration_manager.latest_migration == 8
assert migration_manager.database_current_migration == 8
@ -772,6 +791,15 @@ def test_set1_to_set3():
# Now check to see if stuff seems to be in there.
session = Session()
# Start with making sure that the foundations did not run again
assert session.query(Creature3).filter_by(
name=u'goblin').count() == 1
assert session.query(Creature3).filter_by(
name=u'cerberus').count() == 1
# Then make sure the models have been migrated correctly
creature = session.query(Creature3).filter_by(
name=u'centipede').one()
assert creature.num_limbs == 100.0