env.py 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. from __future__ import with_statement
  3. from alembic import context
  4. from sqlalchemy import engine_from_config, pool
  5. from tracim import models
  6. # from logging.config import fileConfig
  7. # this is the Alembic Config object, which provides
  8. # access to the values within the .ini file in use.
  9. config = context.config
  10. # Interpret the config file for Python logging.
  11. # This line sets up loggers basically.
  12. # fileConfig(config.config_file_name)
  13. # add your model's MetaData object here
  14. # for 'autogenerate' support
  15. # from myapp import mymodel
  16. # target_metadata = mymodel.Base.metadata
  17. target_metadata = models.meta.metadata
  18. # other values from the config, defined by the needs of env.py,
  19. # can be acquired:
  20. # my_important_option = config.get_main_option("my_important_option")
  21. # ... etc.
  22. def run_migrations_offline():
  23. """Run migrations in 'offline' mode.
  24. This configures the context with just a URL
  25. and not an Engine, though an Engine is acceptable
  26. here as well. By skipping the Engine creation
  27. we don't even need a DBAPI to be available.
  28. Calls to context.execute() here emit the given string to the
  29. script output.
  30. """
  31. url = config.get_main_option("sqlalchemy.url")
  32. context.configure(url=url, version_table='migrate_version')
  33. with context.begin_transaction():
  34. context.run_migrations()
  35. def run_migrations_online():
  36. """Run migrations in 'online' mode.
  37. In this scenario we need to create an Engine
  38. and associate a connection with the context.
  39. """
  40. engine = engine_from_config(
  41. config.get_section(config.config_ini_section),
  42. prefix='sqlalchemy.',
  43. poolclass=pool.NullPool)
  44. connection = engine.connect()
  45. context.configure(
  46. connection=connection,
  47. target_metadata=target_metadata,
  48. version_table='migrate_version'
  49. )
  50. try:
  51. with context.begin_transaction():
  52. context.run_migrations()
  53. finally:
  54. connection.close()
  55. engine.dispose()
  56. if context.is_offline_mode():
  57. run_migrations_offline()
  58. else:
  59. run_migrations_online()