env.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. # this is the Alembic Config object, which provides
  10. # access to the values within the .ini file in use.
  11. config = context.config
  12. # Interpret the config file for Python logging.
  13. # This line sets up loggers basically.
  14. if config.config_file_name is not None:
  15. fileConfig(config.config_file_name)
  16. # add your model's MetaData object here
  17. # for 'autogenerate' support
  18. # from myapp import mymodel
  19. # target_metadata = mymodel.Base.metadata
  20. target_metadata = 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 run_migrations_offline() -> None:
  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 = settings.SQLALCHEMY_DATABASE_URI
  35. context.configure(
  36. url=url,
  37. target_metadata=target_metadata,
  38. literal_binds=True,
  39. dialect_opts={"paramstyle": "named"},
  40. compare_type=True,
  41. )
  42. with context.begin_transaction():
  43. context.run_migrations()
  44. def run_migrations_online() -> None:
  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. config.set_main_option("sqlalchemy.url", settings.SQLALCHEMY_DATABASE_URI)
  50. connectable = engine_from_config(
  51. config.get_section(config.config_ini_section, {}),
  52. prefix="sqlalchemy.",
  53. poolclass=pool.NullPool,
  54. )
  55. with connectable.connect() as connection:
  56. context.configure(
  57. connection=connection, target_metadata=target_metadata, compare_type=True
  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()