Coverage for mcpgateway/alembic/env.py: 75%

24 statements  

« prev     ^ index     » next       coverage.py v7.9.2, created at 2025-07-09 11:03 +0100

1# -*- coding: utf-8 -*- 

2# Standard 

3from logging.config import fileConfig 

4 

5# Third-Party 

6from sqlalchemy import engine_from_config, pool 

7 

8# First-Party 

9from alembic import context 

10from mcpgateway.config import settings 

11from mcpgateway.db import Base 

12 

13# from mcpgateway.db import get_metadata 

14# target_metadata = get_metadata() 

15 

16# this is the Alembic Config object, which provides 

17# access to the values within the .ini file in use. 

18config = context.config 

19 

20# Interpret the config file for Python logging. 

21# This line sets up loggers basically. 

22if config.config_file_name is not None: 22 ↛ 33line 22 didn't jump to line 33 because the condition on line 22 was always true

23 fileConfig( 

24 config.config_file_name, 

25 disable_existing_loggers=False, 

26 ) 

27 

28# First-Party 

29# add your model's MetaData object here 

30# for 'autogenerate' support 

31# from myapp import mymodel 

32 

33target_metadata = Base.metadata 

34 

35# other values from the config, defined by the needs of env.py, 

36# can be acquired: 

37# my_important_option = config.get_main_option("my_important_option") 

38# ... etc. 

39 

40config.set_main_option( 

41 "sqlalchemy.url", 

42 settings.database_url, 

43) 

44 

45 

46def run_migrations_offline() -> None: 

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

48 

49 This configures the context with just a URL 

50 and not an Engine, though an Engine is acceptable 

51 here as well. By skipping the Engine creation 

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

53 

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

55 script output. 

56 

57 """ 

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

59 context.configure( 

60 url=url, 

61 target_metadata=target_metadata, 

62 literal_binds=True, 

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

64 ) 

65 

66 with context.begin_transaction(): 

67 context.run_migrations() 

68 

69 

70def run_migrations_online() -> None: 

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

72 

73 In this scenario we need to create an Engine 

74 and associate a connection with the context. 

75 

76 """ 

77 connectable = engine_from_config( 

78 config.get_section(config.config_ini_section, {}), 

79 prefix="sqlalchemy.", 

80 poolclass=pool.NullPool, 

81 ) 

82 

83 with connectable.connect() as connection: 

84 context.configure(connection=connection, target_metadata=target_metadata) 

85 

86 with context.begin_transaction(): 

87 context.run_migrations() 

88 

89 

90if context.is_offline_mode(): 90 ↛ 91line 90 didn't jump to line 91 because the condition on line 90 was never true

91 run_migrations_offline() 

92else: 

93 run_migrations_online()