25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

env.py 2.2 KiB

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