DotaNoobs main site.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
2.2 KiB

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