-- Test 5: PostGIS
\set ON_ERROR_STOP on

CREATE TABLE spatial_data (
    id serial PRIMARY KEY,
    geom geometry(Point, 4326)
);

INSERT INTO spatial_data (geom) VALUES (ST_GeomFromText('POINT(-71.060316 48.432044)', 4326));
INSERT INTO spatial_data (geom) VALUES (ST_GeomFromText('POINT(-71.059773 48.432867)', 4326));

DO $$
DECLARE
    dist float;
BEGIN
    SELECT ST_Distance(
        (SELECT geom FROM spatial_data WHERE id = 1),
        (SELECT geom FROM spatial_data WHERE id = 2)
    ) INTO dist;

    IF dist IS NULL THEN
        RAISE EXCEPTION 'PostGIS distance calculation failed';
    END IF;
    RAISE NOTICE 'PostGIS Check: OK, distance is %', dist;
END
$$;

DROP TABLE spatial_data;
