env.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from __future__ import with_statement
  2. from alembic import context
  3. from sqlalchemy import engine_from_config, pool
  4. from logging.config import fileConfig
  5. import logging
  6. # this is the Alembic Config object, which provides
  7. # access to the values within the .ini file in use.
  8. config = context.config
  9. # Interpret the config file for Python logging.
  10. # This line sets up loggers basically.
  11. fileConfig(config.config_file_name)
  12. logger = logging.getLogger('alembic.env')
  13. # add your model's MetaData object here
  14. # for 'autogenerate' support
  15. # from myapp import mymodel
  16. # target_metadata = mymodel.Base.metadata
  17. from flask import current_app
  18. config.set_main_option('sqlalchemy.url',
  19. current_app.config.get('SQLALCHEMY_DATABASE_URI'))
  20. target_metadata = current_app.extensions['migrate'].db.metadata
  21. # other values from the config, defined by the needs of env.py,
  22. # can be acquired:
  23. # my_important_option = config.get_main_option("my_important_option")
  24. # ... etc.
  25. def include_symbol(tablename, schema):
  26. return tablename not in ("apscheduler_jobs")
  27. def run_migrations_offline():
  28. """Run migrations in 'offline' mode.
  29. This configures the context with just a URL
  30. and not an Engine, though an Engine is acceptable
  31. here as well. By skipping the Engine creation
  32. we don't even need a DBAPI to be available.
  33. Calls to context.execute() here emit the given string to the
  34. script output.
  35. """
  36. url = config.get_main_option("sqlalchemy.url")
  37. context.configure(url=url)
  38. with context.begin_transaction():
  39. context.run_migrations()
  40. def run_migrations_online():
  41. """Run migrations in 'online' mode.
  42. In this scenario we need to create an Engine
  43. and associate a connection with the context.
  44. """
  45. # this callback is used to prevent an auto-migration from being generated
  46. # when there are no changes to the schema
  47. # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
  48. def process_revision_directives(context, revision, directives):
  49. if getattr(config.cmd_opts, 'autogenerate', False):
  50. script = directives[0]
  51. if script.upgrade_ops.is_empty():
  52. directives[:] = []
  53. logger.info('No changes in schema detected.')
  54. engine = engine_from_config(config.get_section(config.config_ini_section),
  55. prefix='sqlalchemy.',
  56. poolclass=pool.NullPool)
  57. connection = engine.connect()
  58. context.configure(connection=connection,
  59. target_metadata=target_metadata,
  60. process_revision_directives=process_revision_directives,
  61. compare_type=True,
  62. include_symbol=include_symbol,
  63. **current_app.extensions['migrate'].configure_args)
  64. try:
  65. with context.begin_transaction():
  66. context.run_migrations()
  67. except Exception as exception:
  68. logger.error(exception)
  69. raise exception
  70. finally:
  71. connection.close()
  72. if context.is_offline_mode():
  73. run_migrations_offline()
  74. else:
  75. run_migrations_online()