env.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. from __future__ import with_statement
  2. import logging
  3. from logging.config import fileConfig
  4. from sqlalchemy import engine_from_config
  5. from sqlalchemy import pool
  6. from alembic import context
  7. # this is the Alembic Config object, which provides
  8. # access to the values within the .ini file in use.
  9. config = context.config
  10. # Interpret the config file for Python logging.
  11. # This line sets up loggers basically.
  12. fileConfig(config.config_file_name)
  13. logger = logging.getLogger('alembic.env')
  14. # add your model's MetaData object here
  15. # for 'autogenerate' support
  16. # from myapp import mymodel
  17. # target_metadata = mymodel.Base.metadata
  18. from flask import current_app
  19. config.set_main_option(
  20. 'sqlalchemy.url', current_app.config.get(
  21. 'SQLALCHEMY_DATABASE_URI').replace('%', '%%'))
  22. target_metadata = current_app.extensions['migrate'].db.metadata
  23. # other values from the config, defined by the needs of env.py,
  24. # can be acquired:
  25. # my_important_option = config.get_main_option("my_important_option")
  26. # ... etc.
  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(
  38. url=url, target_metadata=target_metadata, literal_binds=True
  39. )
  40. with context.begin_transaction():
  41. context.run_migrations()
  42. def run_migrations_online():
  43. """Run migrations in 'online' mode.
  44. In this scenario we need to create an Engine
  45. and associate a connection with the context.
  46. """
  47. # this callback is used to prevent an auto-migration from being generated
  48. # when there are no changes to the schema
  49. # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html
  50. def process_revision_directives(context, revision, directives):
  51. if getattr(config.cmd_opts, 'autogenerate', False):
  52. script = directives[0]
  53. if script.upgrade_ops.is_empty():
  54. directives[:] = []
  55. logger.info('No changes in schema detected.')
  56. connectable = engine_from_config(
  57. config.get_section(config.config_ini_section),
  58. prefix='sqlalchemy.',
  59. poolclass=pool.NullPool,
  60. )
  61. with connectable.connect() as connection:
  62. context.configure(
  63. connection=connection,
  64. target_metadata=target_metadata,
  65. process_revision_directives=process_revision_directives,
  66. **current_app.extensions['migrate'].configure_args
  67. )
  68. with context.begin_transaction():
  69. context.run_migrations()
  70. if context.is_offline_mode():
  71. run_migrations_offline()
  72. else:
  73. run_migrations_online()