You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
geodata-catalog/backend/src/models.py

84 lines
2.4 KiB

from sqlalchemy import Column, Computed, DateTime, Float, Index, Integer, String
# we might need vector concat later, then we'll have to bring in sqlalchemy_utils
# https://sqlalchemy-utils.readthedocs.io/en/latest/_modules/sqlalchemy_utils/types/ts_vector.html
from sqlalchemy.dialects.postgresql import TSVECTOR
# to correctly work with both alembic and fastapi we have to check
# if we are run as a module or as a script
# https://stackoverflow.com/questions/14132789/relative-imports-for-the-billionth-time/14132912
import sys
parent_module = sys.modules['.'.join(__name__.split('.')[:-1]) or '__main__']
if __name__ == '__main__' or parent_module.__name__ == '__main__':
from database import Base
else:
from .database import Base
class Header(Base):
__tablename__ = "headers"
database = Column(String, primary_key=True)
spreadsheet = Column(String)
class ItemBase(Base):
__tablename__ = "geodata"
id = Column(Integer, primary_key=True, index=True)
fadr = Column(String)
internal_id = Column(String)
x_coord = Column(String)
y_coord = Column(String)
gis_category = Column(String)
category = Column(String)
basin = Column(String)
deposit = Column(String)
well = Column(String)
depth = Column(String)
stratum = Column(String)
owner = Column(String)
org = Column(String)
ownercontacts = Column(String)
samplelist = Column(String)
description = Column(String)
form_dimentions = Column(String)
datalist = Column(String)
resolution = Column(String)
date = Column(String)
additional_info = Column(String)
scanner = Column(String)
comment = Column(String)
class ItemCreate(ItemBase):
pass
class Item(ItemBase):
# split on '-' replace ',' with '.' and cast as numeric
depth_min = Column(
Float,
Computed(
"CAST(REGEXP_REPLACE(SPLIT_PART(depth,'-',1), ',', '.') AS DOUBLE PRECISION)"
),
)
depth_max = Column(
Float,
Computed(
"CAST(REGEXP_REPLACE(SPLIT_PART(depth,'-',2), ',', '.') AS DOUBLE PRECISION)"
),
)
geodata_search_ts = Column(
TSVECTOR,
Computed(
"to_tsvector('russian', coalesce(geodata.description, '') || ' ' || coalesce(geodata.additional_info, '') || ' ' || coalesce(geodata.comment, ''))",
persisted=True,
),
)
Index("geodata_search_idx", Item.geodata_search_ts, postgresql_using="gin")