4
4
from flux .backend .applied_migration import AppliedMigration
5
5
from flux .backend .base import MigrationBackend
6
6
from flux .config import FluxConfig
7
- from flux .exceptions import MigrationDirectoryCorruptedError
7
+ from flux .exceptions import MigrationApplyError , MigrationDirectoryCorruptedError
8
8
from flux .migration .migration import Migration
9
9
from flux .migration .read_migration import (
10
10
read_migrations ,
@@ -37,12 +37,12 @@ class FluxRunner:
37
37
38
38
async def __aenter__ (self ):
39
39
self ._conn_ctx = self .backend .connection ()
40
- self ._tx_ctx = self .backend .transaction ()
41
40
self ._lock_ctx = self .backend .migration_lock ()
41
+ self ._tx_ctx = self .backend .transaction ()
42
42
43
43
await self ._conn_ctx .__aenter__ ()
44
- await self ._tx_ctx .__aenter__ ()
45
44
await self ._lock_ctx .__aenter__ ()
45
+ await self ._tx_ctx .__aenter__ ()
46
46
47
47
if not await self .backend .is_initialized ():
48
48
await self .backend .initialize ()
@@ -66,14 +66,17 @@ async def validate_applied_migrations(self):
66
66
- There is no discontinuity in the applied migrations
67
67
- The migration hashes of all applied migrations haven't changed
68
68
"""
69
+ applied_migrations = sorted (self .applied_migrations , key = lambda m : m .id )
70
+ if not applied_migrations :
71
+ return
72
+
73
+ last_applied_migration = applied_migrations [- 1 ]
69
74
applied_migration_files = [
70
- m
71
- for m in self .migrations
72
- if m .id in {_m .id for _m in self .applied_migrations }
75
+ m for m in self .migrations if m .id <= last_applied_migration .id
73
76
]
74
77
75
- if not [m .id for m in applied_migration_files ] = = [
76
- m .id for m in self . migrations [: len ( applied_migration_files )]
78
+ if [m .id for m in applied_migration_files ] ! = [
79
+ m .id for m in applied_migrations
77
80
]:
78
81
raise MigrationDirectoryCorruptedError (
79
82
"There is a discontinuity in the applied migrations"
@@ -100,17 +103,32 @@ async def apply_migrations(self, n: int | None = None):
100
103
migrations_to_apply = unapplied_migrations [:n ]
101
104
102
105
for migration in self .pre_apply_migrations :
103
- await self .backend .apply_migration (migration .up )
106
+ try :
107
+ await self .backend .apply_migration (migration .up )
108
+ except Exception as e :
109
+ raise MigrationApplyError (
110
+ f"Failed to apply pre-apply migration { migration .id } "
111
+ ) from e
104
112
105
113
for migration in migrations_to_apply :
106
114
if migration .id in {m .id for m in self .applied_migrations }:
107
115
continue
108
116
109
- await self .backend .apply_migration (migration .up )
110
- await self .backend .register_migration (migration )
117
+ try :
118
+ await self .backend .apply_migration (migration .up )
119
+ await self .backend .register_migration (migration )
120
+ except Exception as e :
121
+ raise MigrationApplyError (
122
+ f"Failed to apply migration { migration .id } "
123
+ ) from e
111
124
112
125
for migration in self .post_apply_migrations :
113
- await self .backend .apply_migration (migration .up )
126
+ try :
127
+ await self .backend .apply_migration (migration .up )
128
+ except Exception as e :
129
+ raise MigrationApplyError (
130
+ f"Failed to apply post-apply migration { migration .id } "
131
+ ) from e
114
132
115
133
self .applied_migrations = await self .backend .get_applied_migrations ()
116
134
@@ -129,8 +147,13 @@ async def rollback_migrations(self, n: int | None = None):
129
147
migrations_to_rollback = migrations_to_rollback [::- 1 ]
130
148
131
149
for migration in migrations_to_rollback :
132
- if migration .down is not None :
133
- await self .backend .apply_migration (migration .down )
134
- await self .backend .unregister_migration (migration )
150
+ try :
151
+ if migration .down is not None :
152
+ await self .backend .apply_migration (migration .down )
153
+ await self .backend .unregister_migration (migration )
154
+ except Exception as e :
155
+ raise MigrationApplyError (
156
+ f"Failed to rollback migration { migration .id } "
157
+ ) from e
135
158
136
159
self .applied_migrations = await self .backend .get_applied_migrations ()
0 commit comments