env.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. # 导入数据库连接信息
  14. from app.database import DB_USER, DB_PASSWORD, DB_HOST, DB_PORT, DB_NAME, Base
  15. # 导入所有模型
  16. import app.models
  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. target_metadata = Base.metadata
  34. # 定义要忽略的索引名称模式
  35. def include_object(object, name, type_, reflected, compare_to):
  36. """过滤掉不需要的索引变化和表删除操作"""
  37. # 如果是表操作,且是删除操作,则忽略
  38. if type_ == "table" and reflected and not compare_to:
  39. return False
  40. if type_ == "index":
  41. # 如果是新表或新列的索引,保留
  42. if not reflected:
  43. return True
  44. # 如果是已存在表的索引,且名称以 'ix_' 开头,忽略
  45. if name.startswith('ix_'):
  46. return False
  47. # 如果是已存在表的索引,且名称以 '_idx' 结尾,忽略
  48. if name.endswith('_idx'):
  49. return False
  50. # 如果是已存在表的索引,且名称以 'idx_' 开头,保留
  51. if name.startswith('idx_'):
  52. return True
  53. return True
  54. # other values from the config, defined by the needs of env.py,
  55. # can be acquired:
  56. # my_important_option = config.get_main_option("my_important_option")
  57. # ... etc.
  58. def run_migrations_offline():
  59. """Run migrations in 'offline' mode.
  60. This configures the context with just a URL
  61. and not an Engine, though an Engine is acceptable
  62. here as well. By skipping the Engine creation
  63. we don't even need a DBAPI to be available.
  64. Calls to context.execute() here emit the given string to the
  65. script output.
  66. """
  67. url = config.get_main_option("sqlalchemy.url")
  68. context.configure(
  69. url=url,
  70. target_metadata=target_metadata,
  71. literal_binds=True,
  72. dialect_opts={"paramstyle": "named"},
  73. include_object=include_object, # 添加过滤规则
  74. )
  75. with context.begin_transaction():
  76. context.run_migrations()
  77. def run_migrations_online():
  78. """Run migrations in 'online' mode.
  79. In this scenario we need to create an Engine
  80. and associate a connection with the context.
  81. """
  82. connectable = engine_from_config(
  83. config.get_section(config.config_ini_section),
  84. prefix="sqlalchemy.",
  85. poolclass=pool.NullPool,
  86. )
  87. with connectable.connect() as connection:
  88. context.configure(
  89. connection=connection,
  90. target_metadata=target_metadata,
  91. include_object=include_object, # 添加过滤规则
  92. )
  93. with context.begin_transaction():
  94. context.run_migrations()
  95. if context.is_offline_mode():
  96. run_migrations_offline()
  97. else:
  98. run_migrations_online()