| 
									
										
										
										
											2023-02-11 21:26:10 -09:00
										 |  |  | import pathlib | 
					
						
							| 
									
										
										
										
											2022-02-21 21:44:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-11 21:26:10 -09:00
										 |  |  | from pydantic import BaseModel | 
					
						
							| 
									
										
										
										
											2022-02-21 21:44:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-11 21:26:10 -09:00
										 |  |  | from tests.utils.alembic_reader import ALEMBIC_MIGRATIONS, import_file | 
					
						
							| 
									
										
										
										
											2022-02-21 21:44:23 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-11 21:26:10 -09:00
										 |  |  | class AlembicMigration(BaseModel): | 
					
						
							|  |  |  |     path: pathlib.Path | 
					
						
							|  |  |  |     revision: str | None | 
					
						
							|  |  |  |     down_revision: str | None | 
					
						
							| 
									
										
										
										
											2022-02-21 21:44:23 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-11 21:26:10 -09:00
										 |  |  | 
 | 
					
						
							|  |  |  | def test_alembic_revisions_are_in_order() -> None: | 
					
						
							|  |  |  |     # read all files | 
					
						
							|  |  |  |     paths = sorted(ALEMBIC_MIGRATIONS.glob("*.py")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # convert to sorted list of AlembicMigration | 
					
						
							|  |  |  |     migrations: list[AlembicMigration] = [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     for path in paths: | 
					
						
							|  |  |  |         mod = import_file("alembic_version", path) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         revision = getattr(mod, "revision", None) | 
					
						
							|  |  |  |         down_revision = getattr(mod, "down_revision", None) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         migrations.append( | 
					
						
							|  |  |  |             AlembicMigration( | 
					
						
							|  |  |  |                 path=path, | 
					
						
							|  |  |  |                 revision=revision, | 
					
						
							|  |  |  |                 down_revision=down_revision, | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     # step through each migration and check | 
					
						
							|  |  |  |     #   - revision is in order | 
					
						
							|  |  |  |     #   - down_revision is in order | 
					
						
							|  |  |  |     #   - down_revision is the previous revision | 
					
						
							|  |  |  |     last = None | 
					
						
							|  |  |  |     for migration in migrations: | 
					
						
							|  |  |  |         if last is not None: | 
					
						
							|  |  |  |             assert ( | 
					
						
							|  |  |  |                 last.revision == migration.down_revision | 
					
						
							|  |  |  |             ), f"{last.revision} != {migration.down_revision} for {migration.path}" | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         last = migration | 
					
						
							|  |  |  |         last = migration |