Files
test987/新建 文本文档 (2).txt
2026-04-09 11:17:25 +00:00

53 lines
2.9 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

"""为地块表添加PostGIS空间字段支持
Revision ID: d95875797265
Revises: 55c85ceea855
Create Date: 2025-11-21 11:03:22.171351
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
import geoalchemy2.types
# revision identifiers, used by Alembic.
revision: str = 'd95875797265'
down_revision: Union[str, Sequence[str], None] = '55c85ceea855'
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
"""Upgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index(op.f('ix_patrol_records_field_date'), table_name='farm_work_patrol_records')
op.create_index('ix_patrol_records_field_date', 'farm_work_patrol_records', ['field_id', 'inspection_date'], unique=False, postgresql_ops={'inspection_date': 'DESC'})
op.drop_index(op.f('ix_patrol_records_tenant_date'), table_name='farm_work_patrol_records')
op.create_index('ix_patrol_records_tenant_date', 'farm_work_patrol_records', ['tenant_id', 'inspection_date'], unique=False, postgresql_ops={'inspection_date': 'DESC'})
# 使用原生SQL添加空间字段和索引避免重复创建错误
op.execute('ALTER TABLE fields ADD COLUMN IF NOT EXISTS geometry GEOMETRY(POLYGON, 4326);')
op.execute('ALTER TABLE fields ADD COLUMN IF NOT EXISTS centroid GEOMETRY(POINT, 4326);')
op.execute('COMMENT ON COLUMN fields.geometry IS \'地块边界多边形PostGIS几何格式支持空间查询\';')
op.execute('COMMENT ON COLUMN fields.centroid IS \'地块中心点PostGIS几何格式用于距离计算\';')
# 创建空间索引使用IF NOT EXISTS避免重复
op.execute('CREATE INDEX IF NOT EXISTS idx_fields_geometry ON fields USING GIST (geometry);')
op.execute('CREATE INDEX IF NOT EXISTS idx_fields_centroid ON fields USING GIST (centroid);')
# ### end Alembic commands ###
def downgrade() -> None:
"""Downgrade schema."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_index('idx_fields_geometry', table_name='fields', postgresql_using='gist')
op.drop_index('idx_fields_centroid', table_name='fields', postgresql_using='gist')
op.drop_column('fields', 'centroid')
op.drop_column('fields', 'geometry')
op.drop_index('ix_patrol_records_tenant_date', table_name='farm_work_patrol_records', postgresql_ops={'inspection_date': 'DESC'})
op.create_index(op.f('ix_patrol_records_tenant_date'), 'farm_work_patrol_records', ['tenant_id', sa.literal_column('inspection_date DESC')], unique=False)
op.drop_index('ix_patrol_records_field_date', table_name='farm_work_patrol_records', postgresql_ops={'inspection_date': 'DESC'})
op.create_index(op.f('ix_patrol_records_field_date'), 'farm_work_patrol_records', ['field_id', sa.literal_column('inspection_date DESC')], unique=False)
# ### end Alembic commands ###