env.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from logging.config import fileConfig
  2. import os
  3. import warnings
  4. from sqlalchemy import engine_from_config
  5. from sqlalchemy import pool
  6. from sqlalchemy.exc import SAWarning
  7. from alembic import context
  8. from gpustack import schemas
  9. from gpustack.utils.db import patch_pg_version_info
  10. from sqlmodel import SQLModel
  11. patch_pg_version_info()
  12. # OceanBase DDL contains syntax (e.g. BLOCK_SIZE, non-standard FK format) that
  13. # SQLAlchemy's MySQL dialect doesn't recognise during reflection. These warnings
  14. # are harmless — suppress them so they don't clutter migration output.
  15. warnings.filterwarnings("ignore", message=r"Unknown schema content", category=SAWarning)
  16. # this is the Alembic Config object, which provides
  17. # access to the values within the .ini file in use.
  18. config = context.config
  19. # Interpret the config file for Python logging.
  20. # This line sets up loggers basically.
  21. if config.config_file_name is not None:
  22. fileConfig(config.config_file_name)
  23. # add your model's MetaData object here
  24. # for 'autogenerate' support
  25. # from myapp import mymodel
  26. # target_metadata = mymodel.Base.metadata
  27. target_metadata = SQLModel.metadata
  28. # other values from the config, defined by the needs of env.py,
  29. # can be acquired:
  30. # my_important_option = config.get_main_option("my_important_option")
  31. # ... etc.
  32. def run_migrations_offline() -> None:
  33. """Run migrations in 'offline' mode.
  34. This configures the context with just a URL
  35. and not an Engine, though an Engine is acceptable
  36. here as well. By skipping the Engine creation
  37. we don't even need a DBAPI to be available.
  38. Calls to context.execute() here emit the given string to the
  39. script output.
  40. """
  41. url = config.get_main_option("sqlalchemy.url", os.getenv('DATABASE_URL'))
  42. context.configure(
  43. url=url,
  44. target_metadata=target_metadata,
  45. literal_binds=True,
  46. dialect_opts={"paramstyle": "named"},
  47. )
  48. with context.begin_transaction():
  49. context.run_migrations()
  50. def run_migrations_online() -> None:
  51. """Run migrations in 'online' mode.
  52. In this scenario we need to create an Engine
  53. and associate a connection with the context.
  54. """
  55. url = config.get_main_option("sqlalchemy.url", os.getenv('DATABASE_URL'))
  56. connectable = engine_from_config(
  57. config.get_section(config.config_ini_section, {}),
  58. prefix="sqlalchemy.",
  59. poolclass=pool.NullPool,
  60. url=url,
  61. )
  62. with connectable.connect() as connection:
  63. context.configure(
  64. connection=connection,
  65. target_metadata=target_metadata,
  66. render_as_batch=True,
  67. transactional_ddl=True,
  68. )
  69. with context.begin_transaction():
  70. context.run_migrations()
  71. if context.is_offline_mode():
  72. run_migrations_offline()
  73. else:
  74. run_migrations_online()