From f2b2008da51ff554df1af0e5a14a73aff4d89c33 Mon Sep 17 00:00:00 2001 From: tilly-Q Date: Mon, 29 Jul 2013 16:36:06 -0400 Subject: This was a very simple ticket actually. I created a list called FOUNDATIONS in mediagoblin/db/models.py. This list holds all of the information about rows that should be created at database initialization. Read the documentation near the FOUNDATIONS list to understand the proper format for this list. All of the work is done through a new method on MigrationManager in mediagoblin/db/migrations_tools.py. This method, `populate_table_foundations` parses the FOUNDATIONS list and creates the foundations based on the data incl- uded. This only ever happens when the database is initialized. Migrations to releases with new Foundations should be very easy just using the basic database functionality. --- mediagoblin/db/migration_tools.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'mediagoblin/db/migration_tools.py') diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py index aa22ef94..1192836d 100644 --- a/mediagoblin/db/migration_tools.py +++ b/mediagoblin/db/migration_tools.py @@ -140,6 +140,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 + """ + from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS + for Model, rows in MAIN_FOUNDATIONS.items(): + print u'\n--> Laying foundations for %s table' % Model.__name__ + for parameters in rows: + row = Model(**parameters) + row.save() + def create_new_migration_record(self): """ Create a new migration record for this migration set @@ -202,7 +214,9 @@ class MigrationManager(object): self.init_tables() # 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.printer(u"done.\n") self.set_current_migration() -- cgit v1.2.3 From 08cd10d84fd89df3f0cad3835ba8ab8b8000d4b2 Mon Sep 17 00:00:00 2001 From: tilly-Q Date: Mon, 29 Jul 2013 17:15:29 -0400 Subject: 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. --- mediagoblin/db/migration_tools.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'mediagoblin/db/migration_tools.py') diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py index 1192836d..ad137683 100644 --- a/mediagoblin/db/migration_tools.py +++ b/mediagoblin/db/migration_tools.py @@ -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 @@ -145,12 +146,11 @@ class MigrationManager(object): Create the table foundations (default rows) as layed out in FOUNDATIONS in mediagoblin.db.models """ - from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS - for Model, rows in MAIN_FOUNDATIONS.items(): - print u'\n--> Laying foundations for %s table' % Model.__name__ + for Model, rows in self.foundations.items(): + print u'\n + Laying foundations for %s table' % (Model.__name__) for parameters in rows: - row = Model(**parameters) - row.save() + new_row = Model(**parameters) + new_row.save() def create_new_migration_record(self): """ @@ -215,8 +215,7 @@ class MigrationManager(object): self.init_tables() # auto-set at latest migration number self.create_new_migration_record() - if self.name==u'__main__': - self.populate_table_foundations() + self.populate_table_foundations() self.printer(u"done.\n") self.set_current_migration() -- cgit v1.2.3 From 63c3ca28abab7f12592bc3e8bcd0b05749cd0053 Mon Sep 17 00:00:00 2001 From: tilly-Q Date: Tue, 30 Jul 2013 19:06:26 -0400 Subject: Starting to write unit tests... --- mediagoblin/db/migration_tools.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'mediagoblin/db/migration_tools.py') diff --git a/mediagoblin/db/migration_tools.py b/mediagoblin/db/migration_tools.py index ad137683..e75f3757 100644 --- a/mediagoblin/db/migration_tools.py +++ b/mediagoblin/db/migration_tools.py @@ -147,10 +147,11 @@ class MigrationManager(object): in mediagoblin.db.models """ for Model, rows in self.foundations.items(): - print u'\n + Laying foundations for %s table' % (Model.__name__) + self.printer(u' + Laying foundations for %s table\n' % + (Model.__name__)) for parameters in rows: new_row = Model(**parameters) - new_row.save() + self.session.add(new_row) def create_new_migration_record(self): """ @@ -215,9 +216,8 @@ class MigrationManager(object): self.init_tables() # auto-set at latest migration number self.create_new_migration_record() - self.populate_table_foundations() - self.printer(u"done.\n") + self.populate_table_foundations() self.set_current_migration() return u'inited' -- cgit v1.2.3