env.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. # 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. try:
  11. fileConfig(config.config_file_name)
  12. except:
  13. pass
  14. # add your model's MetaData object here
  15. # for 'autogenerate' support
  16. # from myapp import mymodel
  17. # target_metadata = mymodel.Base.metadata
  18. target_metadata = None
  19. # other values from the config, defined by the needs of env.py,
  20. # can be acquired:
  21. # my_important_option = config.get_main_option("my_important_option")
  22. # ... etc.
  23. def run_migrations_offline():
  24. """Run migrations in 'offline' mode.
  25. This configures the context with just a URL
  26. and not an Engine, though an Engine is acceptable
  27. here as well. By skipping the Engine creation
  28. we don't even need a DBAPI to be available.
  29. Calls to context.execute() here emit the given string to the
  30. script output.
  31. """
  32. url = config.get_main_option("sqlalchemy.url")
  33. context.configure(url=url)
  34. with context.begin_transaction():
  35. context.run_migrations()
  36. def run_migrations_online():
  37. """Run migrations in 'online' mode.
  38. In this scenario we need to create an Engine
  39. and associate a connection with the context.
  40. """
  41. engine = engine_from_config(
  42. config.get_section(config.config_ini_section),
  43. prefix='sqlalchemy.',
  44. poolclass=pool.NullPool)
  45. connection = engine.connect()
  46. context.configure(
  47. connection=connection,
  48. target_metadata=target_metadata
  49. )
  50. try:
  51. with context.begin_transaction():
  52. context.run_migrations()
  53. finally:
  54. connection.close()
  55. if context.is_offline_mode():
  56. run_migrations_offline()
  57. else:
  58. run_migrations_online()