env.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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(
  31. url=url, target_metadata=target_metadata, literal_binds=True)
  32. with context.begin_transaction():
  33. context.run_migrations()
  34. def run_migrations_online():
  35. """Run migrations in 'online' mode.
  36. In this scenario we need to create an Engine
  37. and associate a connection with the context.
  38. """
  39. connectable = engine_from_config(
  40. config.get_section(config.config_ini_section),
  41. prefix='sqlalchemy.',
  42. poolclass=pool.NullPool)
  43. with connectable.connect() as connection:
  44. context.configure(
  45. connection=connection,
  46. target_metadata=target_metadata
  47. )
  48. with context.begin_transaction():
  49. context.run_migrations()
  50. if context.is_offline_mode():
  51. run_migrations_offline()
  52. else:
  53. run_migrations_online()