env.py 2.1KB

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. #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. from tracim import model
  16. target_metadata = model.metadata
  17. # other values from the config, defined by the needs of env.py,
  18. # can be acquired:
  19. # my_important_option = config.get_main_option("my_important_option")
  20. # ... etc.
  21. def run_migrations_offline():
  22. """Run migrations in 'offline' mode.
  23. This configures the context with just a URL
  24. and not an Engine, though an Engine is acceptable
  25. here as well. By skipping the Engine creation
  26. we don't even need a DBAPI to be available.
  27. Calls to context.execute() here emit the given string to the
  28. script output.
  29. """
  30. url = config.get_main_option("sqlalchemy.url")
  31. context.configure(url=url, version_table='migrate_version')
  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. engine = engine_from_config(
  40. config.get_section(config.config_ini_section),
  41. prefix='sqlalchemy.',
  42. poolclass=pool.NullPool)
  43. connection = engine.connect()
  44. context.configure(
  45. connection=connection,
  46. target_metadata=target_metadata,
  47. version_table='migrate_version'
  48. )
  49. try:
  50. with context.begin_transaction():
  51. context.run_migrations()
  52. finally:
  53. connection.close()
  54. engine.dispose()
  55. if context.is_offline_mode():
  56. run_migrations_offline()
  57. else:
  58. run_migrations_online()