env.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. """Pylons bootstrap environment.
  2. Place 'pylons_config_file' into alembic.ini, and the application will
  3. be loaded from there.
  4. """
  5. from alembic import context
  6. from paste.deploy import loadapp
  7. from logging.config import fileConfig
  8. from sqlalchemy.engine.base import Engine
  9. try:
  10. # if pylons app already in, don't create a new app
  11. from pylons import config as pylons_config
  12. pylons_config['__file__']
  13. except:
  14. config = context.config
  15. # can use config['__file__'] here, i.e. the Pylons
  16. # ini file, instead of alembic.ini
  17. config_file = config.get_main_option('pylons_config_file')
  18. fileConfig(config_file)
  19. wsgi_app = loadapp('config:%s' % config_file, relative_to='.')
  20. # customize this section for non-standard engine configurations.
  21. meta = __import__("%s.model.meta" % wsgi_app.config['pylons.package']).model.meta
  22. # add your model's MetaData object here
  23. # for 'autogenerate' support
  24. # from myapp import mymodel
  25. # target_metadata = mymodel.Base.metadata
  26. target_metadata = None
  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. context.configure(
  37. url=meta.engine.url, target_metadata=target_metadata,
  38. literal_binds=True)
  39. with context.begin_transaction():
  40. context.run_migrations()
  41. def run_migrations_online():
  42. """Run migrations in 'online' mode.
  43. In this scenario we need to create an Engine
  44. and associate a connection with the context.
  45. """
  46. # specify here how the engine is acquired
  47. # engine = meta.engine
  48. raise NotImplementedError("Please specify engine connectivity here")
  49. with engine.connect() as connection:
  50. context.configure(
  51. connection=connection,
  52. target_metadata=target_metadata
  53. )
  54. with context.begin_transaction():
  55. context.run_migrations()
  56. if context.is_offline_mode():
  57. run_migrations_offline()
  58. else:
  59. run_migrations_online()