No Description

env.py 2.9KB

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