Add API to access individual notes
[cds-indico.git] / migrations / env.py
blob430dd1745a55673c11ab8d7e6a34eed305b68c4c
1 import logging.config
3 from alembic import context
4 from flask import current_app
5 from sqlalchemy import engine_from_config, pool
7 from indico.core.db import db
8 from indico.core.db.sqlalchemy.util.session import update_session_options
9 from indico.core.db.sqlalchemy.util.models import import_all_models
12 # Ensure all our models are imported
13 import_all_models()
15 # this is the Alembic Config object, which provides
16 # access to the values within the .ini file in use.
17 config = context.config
18 logging.config.fileConfig(config.config_file_name)
20 config.set_main_option('sqlalchemy.url', current_app.config.get('SQLALCHEMY_DATABASE_URI'))
21 target_metadata = current_app.extensions['migrate'].db.metadata
23 # Get rid of the ZODB transaction manager
24 update_session_options(db)
27 def _include_symbol(tablename, schema):
28 # We ignore plugin tables in migrations
29 if schema and schema.startswith('plugin_'):
30 return False
31 return not tablename.startswith('alembic_version_')
34 def _render_item(type_, obj, autogen_context):
35 if hasattr(obj, 'info') and obj.info.get('alembic_dont_render'):
36 return None
37 func = getattr(obj, 'alembic_render_' + type_, None)
38 if func is None:
39 return False
40 return func(autogen_context)
43 def run_migrations_offline():
44 """Run migrations in 'offline' mode.
46 This configures the context with just a URL
47 and not an Engine, though an Engine is acceptable
48 here as well. By skipping the Engine creation
49 we don't even need a DBAPI to be available.
51 Calls to context.execute() here emit the given string to the
52 script output.
54 """
55 url = config.get_main_option('sqlalchemy.url')
56 context.configure(url=url, target_metadata=target_metadata, include_schemas=True,
57 version_table_schema='public', include_symbol=_include_symbol, render_item=_render_item)
59 with context.begin_transaction():
60 context.run_migrations()
63 def run_migrations_online():
64 """Run migrations in 'online' mode.
66 In this scenario we need to create an Engine
67 and associate a connection with the context.
69 """
70 engine = engine_from_config(config.get_section(config.config_ini_section),
71 prefix='sqlalchemy.',
72 poolclass=pool.NullPool)
74 connection = engine.connect()
75 context.configure(connection=connection, target_metadata=target_metadata, include_schemas=True,
76 version_table_schema='public', include_symbol=_include_symbol, render_item=_render_item)
78 try:
79 with context.begin_transaction():
80 context.run_migrations()
81 finally:
82 connection.close()
85 if context.is_offline_mode():
86 run_migrations_offline()
87 else:
88 run_migrations_online()