Coverage for slidge/migration.py: 41%

27 statements  

« prev     ^ index     » next       coverage.py v7.11.3, created at 2025-11-26 19:34 +0000

1import logging 

2import sys 

3import traceback 

4from pathlib import Path 

5 

6from alembic import command 

7from alembic.config import Config 

8 

9 

10def get_alembic_cfg() -> Config: 

11 alembic_cfg = Config() 

12 alembic_cfg.set_section_option( 

13 "alembic", 

14 "script_location", 

15 str(Path(__file__).parent / "db" / "alembic"), 

16 ) 

17 return alembic_cfg 

18 

19 

20def migrate() -> None: 

21 try: 

22 command.upgrade(get_alembic_cfg(), "head") 

23 except Exception as e: 

24 traceback.print_exception(e) 

25 print( 

26 "Something went wrong during the migration. " 

27 "This is expected if you upgrade from slidge 0.2, in this case you need to start from a fresh database." 

28 ) 

29 exit(1) 

30 

31 

32def main() -> None: 

33 """ 

34 Updates the (dev) database in ./dev/slidge.sqlite and generates a revision 

35 

36 Usage: python -m slidge.migration "Revision message blah blah blah" 

37 """ 

38 dev_db = Path(".") / "dev" / "slidge.sqlite" 

39 if dev_db.exists(): 

40 # always start from a clean state 

41 dev_db.unlink() 

42 alembic_cfg = get_alembic_cfg() 

43 command.upgrade(alembic_cfg, "head") 

44 command.revision(alembic_cfg, sys.argv[1], autogenerate=True) 

45 

46 

47log = logging.getLogger(__name__) 

48 

49if __name__ == "__main__": 

50 main()