From def13c549a99c36da06f80191fed3bb607ff0cce Mon Sep 17 00:00:00 2001 From: Christopher Allan Webber Date: Thu, 29 Dec 2011 14:44:21 -0600 Subject: [PATCH] Beginnings of the SQL migration manager --- mediagoblin/db/sql/util.py | 101 ++++++++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 1 deletion(-) diff --git a/mediagoblin/db/sql/util.py b/mediagoblin/db/sql/util.py index a0cea111..ec3d6dcf 100644 --- a/mediagoblin/db/sql/util.py +++ b/mediagoblin/db/sql/util.py @@ -14,8 +14,107 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . + class MigrationManager(object): - pass + """ + Migration handling tool. + + Takes information about a database, lets you update the database + to the latest migrations, etc. + """ + + def __init__(self, name, models, migration_registry, database): + """ + Args: + - name: identifier of this section of the database + - database: database we're going to migrate + - migration_registry: where we should find all migrations to + run + """ + self.name = name + self.models = models + self.database = database + self.migration_registry = migration_registry + self._sorted_migrations = None + + # For convenience + from mediagoblin.db.sql.models import MigrationData + + self.migration_model = MigrationData + self.migration_table = MigrationData.__table__ + + @property + def sorted_migrations(self): + """ + Sort migrations if necessary and store in self._sorted_migrations + """ + if not self._sorted_migrations: + self._sorted_migrations = sorted( + self.migration_registry.items(), + # sort on the key... the migration number + key=lambda migration_tuple: migration_tuple[0]) + + return self._sorted_migrations + + def latest_migration(self): + """ + Return a migration number for the latest migration, or 0 if + there are no migrations. + """ + if self.sorted_migrations: + return self.sorted_migrations[-1][0] + else: + # If no migrations have been set, we start at 0. + return 0 + + def database_current_migration(self): + """ + Return the current migration in the database. + """ + # TODO + + def set_current_migration(self, migration_number): + """ + Set the migration in the database to migration_number + """ + # TODO + pass + + def migrations_to_run(self): + """ + Get a list of migrations to run still, if any. + + Note that calling this will set your migration version to the + latest version if it isn't installed to anything yet! + """ + ## TODO + # self._ensure_current_migration_record() + # + # db_current_migration = self.database_current_migration() + # + # return [ + # (migration_number, migration_func) + # for migration_number, migration_func in self.sorted_migrations + # if migration_number > db_current_migration] + pass + + def init_or_migrate(self): + # Find out what migration number, if any, this database data is at, + # and what the latest is. + + # Is this our first time? Is there even a table entry for + # this identifier? + # + # If so: + # - create all tables + # - create record in migrations registry + # - print / inform the user + # - return 'inited' + + # Run migrations, if appropriate. + + # If ran migrations, return 'migrated'. Otherwise, return None. + pass class RegisterMigration(object): -- 2.25.1