env.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. from __future__ import with_statement, print_function, absolute_import
  2. import os
  3. from alembic import context
  4. from sqlalchemy import create_engine, pool
  5. from logging.config import fileConfig
  6. # this is the Alembic Config object, which provides
  7. # access to the values within the .ini file in use.
  8. config = context.config
  9. # Interpret the config file for Python logging.
  10. # This line sets up loggers basically.
  11. fileConfig(config.config_file_name)
  12. if 'PAGURE_CONFIG' not in os.environ \
  13. and os.path.exists('/etc/pagure/pagure.cfg'):
  14. print('Using configuration file `/etc/pagure/pagure.cfg`')
  15. os.environ['PAGURE_CONFIG'] = '/etc/pagure/pagure.cfg'
  16. try:
  17. import pagure
  18. import pagure.lib.model
  19. except ImportError:
  20. import sys
  21. sys.path.insert(0, '.')
  22. import pagure
  23. import pagure.lib.model
  24. # add your model's MetaData object here
  25. # for 'autogenerate' support
  26. target_metadata = pagure.lib.model.BASE.metadata
  27. # other values from the config, defined by the needs of env.py,
  28. # can be acquired:
  29. # my_important_option = config.get_main_option("my_important_option")
  30. # ... etc.
  31. DBURL = config.get_main_option("sqlalchemy.url")
  32. if not DBURL:
  33. DBURL = pagure.config.config['DB_URL']
  34. def run_migrations_offline():
  35. """Run migrations in 'offline' mode.
  36. This configures the context with just a URL
  37. and not an Engine, though an Engine is acceptable
  38. here as well. By skipping the Engine creation
  39. we don't even need a DBAPI to be available.
  40. Calls to context.execute() here emit the given string to the
  41. script output.
  42. """
  43. context.configure(url=DBURL, target_metadata=target_metadata)
  44. with context.begin_transaction():
  45. context.run_migrations()
  46. def run_migrations_online():
  47. """Run migrations in 'online' mode.
  48. In this scenario we need to create an Engine
  49. and associate a connection with the context.
  50. """
  51. connectable = create_engine(DBURL, poolclass=pool.NullPool)
  52. with connectable.connect() as connection:
  53. context.configure(
  54. connection=connection,
  55. target_metadata=target_metadata
  56. )
  57. with context.begin_transaction():
  58. context.run_migrations()
  59. if context.is_offline_mode():
  60. run_migrations_offline()
  61. else:
  62. run_migrations_online()