Coverage for slidge/db/alembic/env.py: 0%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-11-07 05:11 +0000

1from alembic import context 

2 

3from slidge import global_config 

4from slidge.db.meta import Base, get_engine 

5 

6config = context.config 

7 

8target_metadata = Base.metadata 

9 

10 

11def run_migrations_offline() -> None: 

12 """Run migrations in 'offline' mode. 

13 

14 This configures the context with just a URL 

15 and not an Engine, though an Engine is acceptable 

16 here as well. By skipping the Engine creation 

17 we don't even need a DBAPI to be available. 

18 

19 Calls to context.execute() here emit the given string to the 

20 script output. 

21 

22 """ 

23 url = config.get_main_option("sqlalchemy.url") 

24 context.configure( 

25 url=url, 

26 target_metadata=target_metadata, 

27 literal_binds=True, 

28 dialect_opts={"paramstyle": "named"}, 

29 render_as_batch=True, 

30 ) 

31 

32 with context.begin_transaction(): 

33 context.run_migrations() 

34 

35 

36def run_migrations_online() -> None: 

37 """Run migrations in 'online' mode. 

38 

39 In this scenario we need to create an Engine 

40 and associate a connection with the context. 

41 

42 """ 

43 try: 

44 # in prod 

45 connectable = get_engine(global_config.DB_URL) 

46 except AttributeError: 

47 # during dev, to generate migrations 

48 connectable = get_engine("sqlite+pysqlite:///dev/slidge.sqlite") 

49 

50 with connectable.connect() as connection: 

51 context.configure( 

52 connection=connection, 

53 target_metadata=target_metadata, 

54 render_as_batch=True, 

55 ) 

56 

57 with context.begin_transaction(): 

58 context.run_migrations() 

59 

60 

61if context.is_offline_mode(): 

62 run_migrations_offline() 

63else: 

64 run_migrations_online()