Skip to content

Commit

Permalink
Merge branch 'feature-optimize-database-inserts' into jona
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaraphael committed Nov 18, 2023
2 parents 15aabef + eb5e034 commit 3c8affe
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 112 deletions.
39 changes: 6 additions & 33 deletions alembic/versions/3c4693517ef6_add_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,39 +146,12 @@ def upgrade() -> None:
sa.Column("machine_confidence", sa.Float),
sa.Column("precursor_slicks", ARRAY(sa.BigInteger)),
sa.Column("notes", sa.Text),
# sa.Column(
# "length",
# sa.Float,
# sa.Computed(
# """
# GREATEST(
# ST_Distance(
# ST_PointN(ST_ExteriorRing(ST_OrientedEnvelope(geometry::geometry)), 1)::geography,
# ST_PointN(ST_ExteriorRing(ST_OrientedEnvelope(geometry::geometry)), 2)::geography
# ),
# ST_Distance(
# ST_PointN(ST_ExteriorRing(ST_OrientedEnvelope(geometry::geometry)), 2)::geography,
# ST_PointN(ST_ExteriorRing(ST_OrientedEnvelope(geometry::geometry)), 3)::geography
# )
# )
# """
# ),
# ),
# sa.Column("area", sa.Float, sa.Computed("ST_Area(geometry)")),
# sa.Column("perimeter", sa.Float, sa.Computed("ST_Perimeter(geometry)")),
# sa.Column("centroid", Geography("POINT"), sa.Computed("ST_Centroid(geometry)")),
# sa.Column(
# "polsby_popper",
# sa.Float,
# sa.Computed("4 * pi() * ST_Area(geometry) / ST_Perimeter(geometry)^2"),
# ),
# sa.Column(
# "fill_factor",
# sa.Float,
# sa.Computed(
# "ST_Area(geometry) / ST_Area(ST_OrientedEnvelope(geometry::geometry)::geography)"
# ),
# ),
sa.Column("length", sa.Float),
sa.Column("area", sa.Float),
sa.Column("perimeter", sa.Float),
sa.Column("centroid", Geography("POINT")),
sa.Column("polsby_popper", sa.Float),
sa.Column("fill_factor", sa.Float),
)

op.create_table(
Expand Down
1 change: 0 additions & 1 deletion alembic/versions/54c42e9e879f_add_postgis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

def upgrade() -> None:
"""create postgis extension"""
# op.execute("CREATE EXTENSION postgis;")
op.execute("CREATE EXTENSION IF NOT EXISTS postgis WITH SCHEMA public;")
op.execute(
"COMMENT ON EXTENSION postgis IS 'PostGIS geometry, geography, and raster spatial types and functions';"
Expand Down
4 changes: 4 additions & 0 deletions alembic/versions/c7c033c1cdb5_populate_aoi_chunks_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,16 @@ def upgrade() -> None:


def downgrade() -> None:
<<<<<<< HEAD
"""downgrade"""
=======
"""
Downgrade the database schema.
This function clears the 'aoi_chunks' table by truncating it, effectively
undoing the changes made by the upgrade function.
"""
>>>>>>> 5d078708752b74e3cb743f77ce85b658acb327b5
bind = op.get_bind()
session = orm.Session(bind=bind)
with session.begin():
Expand Down
78 changes: 0 additions & 78 deletions alembic/versions/cb7ceecc3f87_add_funcs_on_insert.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,63 +85,6 @@ def upgrade() -> None:
)
)

# # Create a trigger function that maps slick to aoi upon insert
# op.execute(
# """
# CREATE OR REPLACE FUNCTION map_slick_to_aoi()
# RETURNS TRIGGER AS $$
# BEGIN
# INSERT INTO slick_to_aoi (slick, aoi)
# SELECT DISTINCT NEW.id, aoi.id FROM aoi
# WHERE ST_Intersects(aoi.geometry, NEW.geometry);
# RETURN NEW;
# END;
# $$ LANGUAGE plpgsql;
# """
# )

# # Create a trigger that calls the trigger function after insert
# op.execute(
# """
# CREATE TRIGGER trigger_map_slick_to_aoi
# AFTER INSERT ON slick
# FOR EACH ROW
# EXECUTE FUNCTION map_slick_to_aoi();
# """
# )

# op.execute(
# """
# CREATE OR REPLACE FUNCTION populate_cls()
# RETURNS TRIGGER AS
# $$
# BEGIN
# NEW.cls := (
# SELECT cls.id
# FROM cls
# JOIN orchestrator_run ON NEW.orchestrator_run = orchestrator_run.id
# JOIN LATERAL json_each_text((SELECT cls_map FROM model WHERE id = orchestrator_run.model))
# m(key, value)
# ON key::integer = NEW.inference_idx
# WHERE cls.short_name = value
# LIMIT 1
# );
# RETURN NEW;
# END;
# $$
# LANGUAGE plpgsql;
# """
# )

# op.execute(
# """
# CREATE TRIGGER trigger_populate_cls
# BEFORE INSERT ON slick
# FOR EACH ROW
# EXECUTE FUNCTION populate_cls();
# """
# )


def downgrade() -> None:
"""Add funcs on insert"""
Expand All @@ -155,24 +98,3 @@ def downgrade() -> None:
DROP FUNCTION IF EXISTS slick_before_trigger_func();
"""
)

# op.execute(
# """
# DROP TRIGGER IF EXISTS trigger_map_slick_to_aoi ON slick;
# """
# )
# op.execute(
# """
# DROP FUNCTION IF EXISTS map_slick_to_aoi();
# """
# )
# op.execute(
# """
# DROP TRIGGER IF EXISTS trigger_populate_cls ON slick;
# """
# )
# op.execute(
# """
# DROP FUNCTION IF EXISTS populate_cls();
# """
# )

0 comments on commit 3c8affe

Please sign in to comment.