env.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import with_statement
  2. from logging.config import fileConfig
  3. from sqlalchemy import engine_from_config
  4. from sqlalchemy import pool
  5. from alembic import context
  6. # this is the Alembic Config object, which provides
  7. # access to the values within the .ini file in use.
  8. config = context.config
  9. # Interpret the config file for Python logging.
  10. # This line sets up loggers basically.
  11. fileConfig(config.config_file_name)
  12. # add your model's MetaData object here
  13. # for 'autogenerate' support
  14. # from myapp import mymodel
  15. # target_metadata = mymodel.Base.metadata
  16. import os
  17. import sys
  18. sys.path.append(os.getcwd())
  19. from app import models
  20. target_metadata = models.Base.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 exclude_tables_from_config(config_):
  26. """Return list of tables to exclude"""
  27. tables_ = config_.get("tables", None)
  28. if tables_ is not None:
  29. tables = tables_.split(",")
  30. return tables
  31. exclude_tables = exclude_tables_from_config(config.get_section('alembic:exclude'))
  32. def include_object(object, name, type_, reflected, compare_to):
  33. """Compare table names to excluded tables"""
  34. if type_ == "table" and name in exclude_tables:
  35. return False
  36. return True
  37. def run_migrations_offline():
  38. """Run migrations in 'offline' mode.
  39. This configures the context with just a URL
  40. and not an Engine, though an Engine is acceptable
  41. here as well. By skipping the Engine creation
  42. we don't even need a DBAPI to be available.
  43. Calls to context.execute() here emit the given string to the
  44. script output.
  45. """
  46. url = config.get_main_option("sqlalchemy.url")
  47. context.configure(
  48. url=url,
  49. target_metadata=target_metadata,
  50. literal_binds=True,
  51. include_object=include_object,
  52. compare_type=True,
  53. )
  54. with context.begin_transaction():
  55. context.run_migrations()
  56. def run_migrations_online():
  57. """Run migrations in 'online' mode.
  58. In this scenario we need to create an Engine
  59. and associate a connection with the context.
  60. """
  61. connectable = engine_from_config(
  62. config.get_section(config.config_ini_section),
  63. prefix="sqlalchemy.",
  64. poolclass=pool.NullPool,
  65. )
  66. with connectable.connect() as connection:
  67. context.configure(
  68. connection=connection,
  69. target_metadata=target_metadata,
  70. include_object=include_object,
  71. compare_type=True,
  72. )
  73. with context.begin_transaction():
  74. context.run_migrations()
  75. if context.is_offline_mode():
  76. run_migrations_offline()
  77. else:
  78. run_migrations_online()