Coverage for mindsdb / migrations / env.py: 75%

22 statements  

« prev     ^ index     » next       coverage.py v7.13.1, created at 2026-01-21 00:36 +0000

1from alembic import context 

2from sqlalchemy import engine_from_config, pool 

3 

4from mindsdb.interfaces.storage import db 

5from mindsdb.utilities.config import config as app_config 

6 

7# this is the Alembic Config object, which provides 

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

9config = context.config 

10 

11# add your model's MetaData object here 

12# for 'autogenerate' support 

13# from myapp import mymodel 

14# target_metadata = mymodel.Base.metadata 

15 

16# initialize 

17 

18db.init() 

19 

20target_metadata = db.Base.metadata 

21 

22config.set_main_option("sqlalchemy.url", app_config["storage_db"]) 

23 

24 

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

26# can be acquired: 

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

28# ... etc. 

29 

30 

31def run_migrations_offline(): 

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

33 

34 This configures the context with just a URL 

35 and not an Engine, though an Engine is acceptable 

36 here as well. By skipping the Engine creation 

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

38 

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

40 script output. 

41 

42 """ 

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

44 context.configure( 

45 url=url, 

46 target_metadata=target_metadata, 

47 literal_binds=True, 

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

49 render_as_batch=True, 

50 ) 

51 

52 with context.begin_transaction(): 

53 context.run_migrations() 

54 

55 

56def run_migrations_online(): 

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

58 

59 In this scenario we need to create an Engine 

60 and associate a connection with the context. 

61 

62 """ 

63 connectable = engine_from_config( 

64 config.get_section(config.config_ini_section), 

65 prefix="sqlalchemy.", 

66 poolclass=pool.NullPool, 

67 ) 

68 

69 with connectable.connect() as connection: 

70 context.configure(connection=connection, target_metadata=target_metadata, render_as_batch=True) 

71 

72 with context.begin_transaction(): 

73 context.run_migrations() 

74 

75 

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

77 run_migrations_offline() 

78else: 

79 run_migrations_online()