env.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. import os
  14. import sys
  15. sys.path.append(os.getcwd())
  16. from daklib.dbconn import DBConn
  17. target_metadata = DBConn().db_meta
  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 include_object(object, name, type_, reflected, compare_to):
  23. if type_ == "table" and name in DBConn.views:
  24. return False
  25. return True
  26. def run_migrations_offline():
  27. """Run migrations in 'offline' mode.
  28. This configures the context with just a URL
  29. and not an Engine, though an Engine is acceptable
  30. here as well. By skipping the Engine creation
  31. we don't even need a DBAPI to be available.
  32. Calls to context.execute() here emit the given string to the
  33. script output.
  34. """
  35. url = config.get_main_option("sqlalchemy.url")
  36. context.configure(
  37. url=url,
  38. target_metadata=target_metadata,
  39. literal_binds=True,
  40. include_object=include_object,
  41. )
  42. with context.begin_transaction():
  43. context.run_migrations()
  44. def run_migrations_online():
  45. """Run migrations in 'online' mode.
  46. In this scenario we need to create an Engine
  47. and associate a connection with the context.
  48. """
  49. connectable = engine_from_config(
  50. config.get_section(config.config_ini_section),
  51. prefix='sqlalchemy.',
  52. poolclass=pool.NullPool)
  53. with connectable.connect() as connection:
  54. context.configure(
  55. connection=connection,
  56. target_metadata=target_metadata,
  57. include_object=include_object,
  58. )
  59. with context.begin_transaction():
  60. context.run_migrations()
  61. if context.is_offline_mode():
  62. run_migrations_offline()
  63. else:
  64. run_migrations_online()