Beginnings of the SQL migration manager
[mediagoblin.git] / mediagoblin / db / sql / util.py
1 # GNU MediaGoblin -- federated, autonomous media hosting
2 # Copyright (C) 2011 MediaGoblin contributors. See AUTHORS.
3 #
4 # This program is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU Affero General Public License as published by
6 # the Free Software Foundation, either version 3 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU Affero General Public License for more details.
13 #
14 # You should have received a copy of the GNU Affero General Public License
15 # along with this program. If not, see <http://www.gnu.org/licenses/>.
16
17
18 class MigrationManager(object):
19 """
20 Migration handling tool.
21
22 Takes information about a database, lets you update the database
23 to the latest migrations, etc.
24 """
25
26 def __init__(self, name, models, migration_registry, database):
27 """
28 Args:
29 - name: identifier of this section of the database
30 - database: database we're going to migrate
31 - migration_registry: where we should find all migrations to
32 run
33 """
34 self.name = name
35 self.models = models
36 self.database = database
37 self.migration_registry = migration_registry
38 self._sorted_migrations = None
39
40 # For convenience
41 from mediagoblin.db.sql.models import MigrationData
42
43 self.migration_model = MigrationData
44 self.migration_table = MigrationData.__table__
45
46 @property
47 def sorted_migrations(self):
48 """
49 Sort migrations if necessary and store in self._sorted_migrations
50 """
51 if not self._sorted_migrations:
52 self._sorted_migrations = sorted(
53 self.migration_registry.items(),
54 # sort on the key... the migration number
55 key=lambda migration_tuple: migration_tuple[0])
56
57 return self._sorted_migrations
58
59 def latest_migration(self):
60 """
61 Return a migration number for the latest migration, or 0 if
62 there are no migrations.
63 """
64 if self.sorted_migrations:
65 return self.sorted_migrations[-1][0]
66 else:
67 # If no migrations have been set, we start at 0.
68 return 0
69
70 def database_current_migration(self):
71 """
72 Return the current migration in the database.
73 """
74 # TODO
75
76 def set_current_migration(self, migration_number):
77 """
78 Set the migration in the database to migration_number
79 """
80 # TODO
81 pass
82
83 def migrations_to_run(self):
84 """
85 Get a list of migrations to run still, if any.
86
87 Note that calling this will set your migration version to the
88 latest version if it isn't installed to anything yet!
89 """
90 ## TODO
91 # self._ensure_current_migration_record()
92 #
93 # db_current_migration = self.database_current_migration()
94 #
95 # return [
96 # (migration_number, migration_func)
97 # for migration_number, migration_func in self.sorted_migrations
98 # if migration_number > db_current_migration]
99 pass
100
101 def init_or_migrate(self):
102 # Find out what migration number, if any, this database data is at,
103 # and what the latest is.
104
105 # Is this our first time? Is there even a table entry for
106 # this identifier?
107 #
108 # If so:
109 # - create all tables
110 # - create record in migrations registry
111 # - print / inform the user
112 # - return 'inited'
113
114 # Run migrations, if appropriate.
115
116 # If ran migrations, return 'migrated'. Otherwise, return None.
117 pass
118
119
120 class RegisterMigration(object):
121 """
122 Tool for registering migrations
123
124 Call like:
125
126 @RegisterMigration(33)
127 def update_dwarves(database):
128 [...]
129
130 This will register your migration with the default migration
131 registry. Alternately, to specify a very specific
132 migration_registry, you can pass in that as the second argument.
133
134 Note, the number of your migration should NEVER be 0 or less than
135 0. 0 is the default "no migrations" state!
136 """
137 def __init__(self, migration_number, migration_registry):
138 assert migration_number > 0, "Migration number must be > 0!"
139 assert migration_number not in migration_registry, \
140 "Duplicate migration numbers detected! That's not allowed!"
141
142 self.migration_number = migration_number
143 self.migration_registry = migration_registry
144
145 def __call__(self, migration):
146 self.migration_registry[self.migration_number] = migration
147 return migration
148
149
150 def assure_migrations_table_setup(db):
151 """
152 Make sure the migrations table is set up in the database.
153 """
154 from mediagoblin.db.sql.models import MigrationData
155
156 if not MigrationData.__table__.exists(db):
157 MigrationData.metadata.create_all(
158 db, tables=[MigrationData.__table__])