Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

86 řádky
2.3 KiB

  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. print("Alembic env.py target_metadata: ", target_metadata)
  21. print("Alembic env.py settings: ", settings.DATABASE_URL)
  22. print("Alembic env.py config: ", Base)
  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 = config.get_main_option("sqlalchemy.url")
  37. context.configure(
  38. url=url,
  39. target_metadata=target_metadata,
  40. literal_binds=True,
  41. dialect_opts={"paramstyle": "named"},
  42. )
  43. with context.begin_transaction():
  44. context.run_migrations()
  45. def run_migrations_online() -> None:
  46. """Run migrations in 'online' mode.
  47. In this scenario we need to create an Engine
  48. and associate a connection with the context.
  49. """
  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(connection=connection, target_metadata=target_metadata)
  57. with context.begin_transaction():
  58. context.run_migrations()
  59. if context.is_offline_mode():
  60. run_migrations_offline()
  61. else:
  62. run_migrations_online()