env.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. from alembic import context
  2. from sqlalchemy import engine_from_config, pool
  3. from logging.config import fileConfig
  4. # this is the Alembic Config object, which provides
  5. # access to the values within the .ini file in use.
  6. config = context.config
  7. # Interpret the config file for Python logging.
  8. # This line sets up loggers basically.
  9. fileConfig(config.config_file_name)
  10. # add your model's MetaData object here
  11. # for 'autogenerate' support
  12. import os
  13. import sys
  14. sys.path.append(os.getcwd())
  15. from daklib.dbconn import DBConn
  16. target_metadata = DBConn().db_meta
  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 include_object(object, name, type_, reflected, compare_to):
  22. if type_ == "table" and name in DBConn.views:
  23. return False
  24. return True
  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(
  36. url=url,
  37. target_metadata=target_metadata,
  38. literal_binds=True,
  39. include_object=include_object,
  40. )
  41. with context.begin_transaction():
  42. context.run_migrations()
  43. def run_migrations_online():
  44. """Run migrations in 'online' mode.
  45. In this scenario we need to create an Engine
  46. and associate a connection with the context.
  47. """
  48. connectable = engine_from_config(
  49. config.get_section(config.config_ini_section),
  50. prefix='sqlalchemy.',
  51. poolclass=pool.NullPool)
  52. with connectable.connect() as connection:
  53. context.configure(
  54. connection=connection,
  55. target_metadata=target_metadata,
  56. include_object=include_object,
  57. )
  58. with context.begin_transaction():
  59. context.run_migrations()
  60. if context.is_offline_mode():
  61. run_migrations_offline()
  62. else:
  63. run_migrations_online()