@@ -14,8 +14,8 @@ def read_migrations(*, config: FluxConfig) -> list[Migration]:
14
14
"""
15
15
migrations = []
16
16
for migration_file in os .listdir (config .migration_directory ):
17
- if migration_file .endswith ("_up .sql" ):
18
- migration_id = migration_file [:- 7 ]
17
+ if migration_file .endswith (".sql" ) and not migration_file . endswith ( ".undo .sql" ):
18
+ migration_id = migration_file [:- 4 ]
19
19
migrations .append (
20
20
read_sql_migration (config = config , migration_id = migration_id )
21
21
)
@@ -88,8 +88,8 @@ def read_sql_migration(*, config: FluxConfig, migration_id: str) -> Migration:
88
88
"""
89
89
Read a pair of SQL migration files and return a Migration object
90
90
"""
91
- up_file = os .path .join (config .migration_directory , f"{ migration_id } _up .sql" )
92
- down_file = os .path .join (config .migration_directory , f"{ migration_id } _down .sql" )
91
+ up_file = os .path .join (config .migration_directory , f"{ migration_id } .sql" )
92
+ down_file = os .path .join (config .migration_directory , f"{ migration_id } .undo .sql" )
93
93
94
94
try :
95
95
with open (up_file ) as f :
@@ -128,6 +128,13 @@ def read_repeatable_sql_migration(
128
128
except Exception as e :
129
129
raise MigrationLoadingError ("Error reading up migration" ) from e
130
130
131
+ if os .path .exists (
132
+ os .path .join (
133
+ config .migration_directory , migration_subdir , f"{ migration_id } .undo.sql"
134
+ )
135
+ ):
136
+ raise MigrationLoadingError ("Repeatable migrations cannot have a down" )
137
+
131
138
return Migration (id = migration_id , up = up , down = None )
132
139
133
140
@@ -139,19 +146,19 @@ def read_python_migration(*, config: FluxConfig, migration_id: str) -> Migration
139
146
140
147
with temporary_module (migration_file ) as module :
141
148
try :
142
- up_migration = module .up ()
149
+ up_migration = module .apply ()
143
150
except Exception as e :
144
151
raise MigrationLoadingError ("Error reading up migration" ) from e
145
152
if not isinstance (up_migration , str ):
146
153
raise MigrationLoadingError ("Up migration must return a string" )
147
- if hasattr (module , "down " ):
154
+ if hasattr (module , "undo " ):
148
155
try :
149
- down_migration = module .down ()
156
+ down_migration = module .undo ()
150
157
except Exception as e :
151
158
raise MigrationLoadingError ("Error reading down migration" ) from e
152
- if not isinstance (down_migration , str ) and down_migration is not None :
159
+ if not isinstance (down_migration , str ):
153
160
raise MigrationLoadingError (
154
- "Down migration must return a string or None "
161
+ "Down migration must return a string"
155
162
)
156
163
else :
157
164
down_migration = None
@@ -174,12 +181,12 @@ def read_repeatable_python_migration(
174
181
175
182
with temporary_module (migration_file ) as module :
176
183
try :
177
- up_migration = module .up ()
184
+ up_migration = module .apply ()
178
185
except Exception as e :
179
186
raise MigrationLoadingError ("Error reading up migration" ) from e
180
187
if not isinstance (up_migration , str ):
181
188
raise MigrationLoadingError ("Up migration must return a string" )
182
- if hasattr (module , "down " ):
189
+ if hasattr (module , "undo " ):
183
190
raise MigrationLoadingError ("Repeatable migrations cannot have a down" )
184
191
185
192
return Migration (id = migration_id , up = up_migration , down = None )
0 commit comments