Beginnings of the SQL migration manager
authorChristopher Allan Webber <cwebber@dustycloud.org>
Thu, 29 Dec 2011 20:44:21 +0000 (14:44 -0600)
committerChristopher Allan Webber <cwebber@dustycloud.org>
Sun, 29 Jan 2012 22:33:44 +0000 (16:33 -0600)
mediagoblin/db/sql/util.py

index a0cea111b444e4c647c5f872d68b1ec305e7767e..ec3d6dcf58728db4242e0b23388ce6c7d8867478 100644 (file)
 # 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/>.
 
+
 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):