Skip to content

Commit 0db7993

Browse files
committed
added get_table_from_snaphot to MigrationManager, along with offset
1 parent 946e153 commit 0db7993

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

piccolo/apps/migrations/auto/migration_manager.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,40 @@ def deserialise_params(
322322

323323
###########################################################################
324324

325+
def get_table_from_snaphot(
326+
self,
327+
app_name: str,
328+
table_class_name: str,
329+
offset: int = 0,
330+
migration_id: t.Optional[str] = None,
331+
) -> t.Type[Table]:
332+
"""
333+
Returns a Table subclass which can be used for modifying data within
334+
a migration.
335+
336+
:param offset:
337+
Lets you get a table as it appeared in an older migration. If the
338+
offset is -1, the table will come from the previous migration.
339+
340+
"""
341+
from piccolo.apps.migrations.commands.base import BaseMigrationManager
342+
343+
if migration_id is None:
344+
migration_id = self.migration_id
345+
346+
return (
347+
BaseMigrationManager()
348+
.get_table_from_snaphot(
349+
app_name=app_name,
350+
table_class_name=table_class_name,
351+
max_migration_id=migration_id,
352+
offset=offset,
353+
)
354+
.to_table_class()
355+
)
356+
357+
###########################################################################
358+
325359
async def _run_alter_columns(self, backwards=False):
326360
for table_class_name in self.alter_columns.table_class_names:
327361
alter_columns = self.alter_columns.for_table_class_name(

piccolo/apps/migrations/commands/base.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ def get_migration_ids(
126126
return sorted(list(migration_module_dict.keys()))
127127

128128
def get_migration_managers(
129-
self, app_name: str, max_migration_id: t.Optional[str] = None
129+
self,
130+
app_name: str,
131+
max_migration_id: t.Optional[str] = None,
132+
offset: int = 0,
130133
) -> t.List[MigrationManager]:
131134
"""
132135
:param max_migration_id:
@@ -154,20 +157,28 @@ def get_migration_managers(
154157
else:
155158
migration_managers.append(response)
156159

157-
return migration_managers
160+
if offset > 0:
161+
raise Exception(
162+
"Positive offset values aren't currently supported"
163+
)
164+
elif offset < 0:
165+
return migration_managers[0:offset]
166+
else:
167+
return migration_managers
158168

159169
def get_table_from_snaphot(
160170
self,
161171
app_name: str,
162172
table_class_name: str,
163173
max_migration_id: t.Optional[str] = None,
174+
offset: int = 0,
164175
) -> DiffableTable:
165176
"""
166177
This will generate a SchemaSnapshot up to the given migration ID, and
167178
will return a DiffableTable class from that snapshot.
168179
"""
169180
migration_managers = self.get_migration_managers(
170-
app_name=app_name, max_migration_id=max_migration_id
181+
app_name=app_name, max_migration_id=max_migration_id, offset=offset
171182
)
172183
schema_snapshot = SchemaSnapshot(managers=migration_managers)
173184

0 commit comments

Comments
 (0)