env.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from logging.config import fileConfig
  2. from sqlalchemy import engine_from_config
  3. from sqlalchemy import pool
  4. # 添加类型导入以解决兼容性问题
  5. from sqlalchemy.types import NullType
  6. # 添加GeoAlchemy2导入
  7. import geoalchemy2
  8. from alembic import context
  9. import os
  10. import sys
  11. # 添加项目根目录到 Python 路径
  12. sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))
  13. # 从orm_models导入Base和模型
  14. from app.models.orm_models import Base
  15. # 导入数据库连接信息
  16. from app.database import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME
  17. # this is the Alembic Config object, which provides
  18. # access to the values within the .ini file in use.
  19. config = context.config
  20. # 设置数据库连接 URL
  21. section = config.config_ini_section
  22. config.set_section_option(section, 'DB_USER', DB_USER)
  23. config.set_section_option(section, 'DB_PASSWORD', DB_PASSWORD)
  24. config.set_section_option(section, 'DB_HOST', DB_HOST)
  25. config.set_section_option(section, 'DB_PORT', DB_PORT)
  26. config.set_section_option(section, 'DB_NAME', DB_NAME)
  27. # Interpret the config file for Python logging.
  28. # This line sets up loggers basically.
  29. if config.config_file_name is not None:
  30. fileConfig(config.config_file_name)
  31. # add your model's MetaData object here
  32. # for 'autogenerate' support
  33. # from myapp import mymodel
  34. # target_metadata = mymodel.Base.metadata
  35. target_metadata = Base.metadata
  36. # other values from the config, defined by the needs of env.py,
  37. # can be acquired:
  38. # my_important_option = config.get_main_option("my_important_option")
  39. # ... etc.
  40. def run_migrations_offline():
  41. """Run migrations in 'offline' mode.
  42. This configures the context with just a URL
  43. and not an Engine, though an Engine is acceptable
  44. here as well. By skipping the Engine creation
  45. we don't even need a DBAPI to be available.
  46. Calls to context.execute() here emit the given string to the
  47. script output.
  48. """
  49. url = config.get_main_option("sqlalchemy.url")
  50. context.configure(
  51. url=url,
  52. target_metadata=target_metadata,
  53. literal_binds=True,
  54. dialect_opts={"paramstyle": "named"},
  55. )
  56. with context.begin_transaction():
  57. context.run_migrations()
  58. def run_migrations_online():
  59. """Run migrations in 'online' mode.
  60. In this scenario we need to create an Engine
  61. and associate a connection with the context.
  62. """
  63. connectable = engine_from_config(
  64. config.get_section(config.config_ini_section),
  65. prefix="sqlalchemy.",
  66. poolclass=pool.NullPool,
  67. )
  68. with connectable.connect() as connection:
  69. context.configure(
  70. connection=connection, target_metadata=target_metadata
  71. )
  72. with context.begin_transaction():
  73. context.run_migrations()
  74. if context.is_offline_mode():
  75. run_migrations_offline()
  76. else:
  77. run_migrations_online()