env.py 2.0 KB

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