env.py 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. from logging.config import fileConfig
  2. from sqlalchemy import engine_from_config
  3. from sqlalchemy import pool
  4. from alembic import context
  5. from alien_gateway.config import settings
  6. from alien_database.base import Base
  7. from alien_store.db.models import contract_store
  8. from alien_lawyer.db.models import lawyer_contract
  9. from alien_contract.db.models import bundle, document, event
  10. # this is the Alembic Config object, which provides
  11. # access to the values within the .ini file in use.
  12. config = context.config
  13. # Interpret the config file for Python logging.
  14. # This line sets up loggers basically.
  15. if config.config_file_name is not None:
  16. fileConfig(config.config_file_name)
  17. # add your model's MetaData object here
  18. # for 'autogenerate' support
  19. # from myapp import mymodel
  20. # target_metadata = mymodel.Base.metadata
  21. target_metadata = Base.metadata
  22. _MODEL_IMPORTS = (contract_store, lawyer_contract, bundle, document, event)
  23. # other values from the config, defined by the needs of env.py,
  24. # can be acquired:
  25. # my_important_option = config.get_main_option("my_important_option")
  26. # ... etc.
  27. def run_migrations_offline() -> None:
  28. """Run migrations in 'offline' mode.
  29. This configures the context with just a URL
  30. and not an Engine, though an Engine is acceptable
  31. here as well. By skipping the Engine creation
  32. we don't even need a DBAPI to be available.
  33. Calls to context.execute() here emit the given string to the
  34. script output.
  35. """
  36. url = settings.SQLALCHEMY_DATABASE_URI
  37. context.configure(
  38. url=url,
  39. target_metadata=target_metadata,
  40. literal_binds=True,
  41. dialect_opts={"paramstyle": "named"},
  42. compare_type=True,
  43. )
  44. with context.begin_transaction():
  45. context.run_migrations()
  46. def run_migrations_online() -> None:
  47. """Run migrations in 'online' mode.
  48. In this scenario we need to create an Engine
  49. and associate a connection with the context.
  50. """
  51. config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URI)
  52. connectable = engine_from_config(
  53. config.get_section(config.config_ini_section, {}),
  54. prefix="sqlalchemy.",
  55. poolclass=pool.NullPool,
  56. )
  57. with connectable.connect() as connection:
  58. context.configure(
  59. connection=connection, target_metadata=target_metadata, compare_type=True
  60. )
  61. with context.begin_transaction():
  62. context.run_migrations()
  63. if context.is_offline_mode():
  64. run_migrations_offline()
  65. else:
  66. run_migrations_online()