env.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import logging
  2. from logging.config import fileConfig
  3. from alembic import context
  4. from flask import current_app
  5. # this is the Alembic Config object, which provides
  6. # access to the values within the .ini file in use.
  7. config = context.config
  8. # Interpret the config file for Python logging.
  9. # This line sets up loggers basically.
  10. fileConfig(config.config_file_name)
  11. logger = logging.getLogger('alembic.env')
  12. def get_engine():
  13. return current_app.extensions['migrate'].db.engine
  14. def get_engine_url():
  15. try:
  16. return get_engine().url.render_as_string(hide_password=False).replace(
  17. '%', '%%')
  18. except AttributeError:
  19. return str(get_engine().url).replace('%', '%%')
  20. # add your model's MetaData object here
  21. # for 'autogenerate' support
  22. # from myapp import mymodel
  23. # target_metadata = mymodel.Base.metadata
  24. config.set_main_option('sqlalchemy.url', get_engine_url())
  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. from models.base import Base
  30. def get_metadata():
  31. return Base.metadata
  32. def include_object(object, name, type_, reflected, compare_to):
  33. if type_ == "foreign_key_constraint":
  34. return False
  35. else:
  36. return True
  37. def run_migrations_offline():
  38. """Run migrations in 'offline' mode.
  39. This configures the context with just a URL
  40. and not an Engine, though an Engine is acceptable
  41. here as well. By skipping the Engine creation
  42. we don't even need a DBAPI to be available.
  43. Calls to context.execute() here emit the given string to the
  44. script output.
  45. """
  46. url = config.get_main_option("sqlalchemy.url")
  47. context.configure(
  48. url=url, target_metadata=get_metadata(), literal_binds=True
  49. )
  50. with context.begin_transaction():
  51. context.run_migrations()
  52. def run_migrations_online():
  53. """Run migrations in 'online' mode.
  54. In this scenario we need to create an Engine
  55. and associate a connection with the context.
  56. """
  57. # this callback is used to prevent an auto-migration from being generated
  58. # when there are no changes to the schema
  59. # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
  60. def process_revision_directives(context, revision, directives):
  61. if getattr(config.cmd_opts, 'autogenerate', False):
  62. script = directives[0]
  63. if script.upgrade_ops.is_empty():
  64. directives[:] = []
  65. logger.info('No changes in schema detected.')
  66. connectable = get_engine()
  67. with connectable.connect() as connection:
  68. context.configure(
  69. connection=connection,
  70. target_metadata=get_metadata(),
  71. process_revision_directives=process_revision_directives,
  72. include_object=include_object,
  73. **current_app.extensions['migrate'].configure_args
  74. )
  75. with context.begin_transaction():
  76. context.run_migrations()
  77. if context.is_offline_mode():
  78. run_migrations_offline()
  79. else:
  80. run_migrations_online()