----------------------------------------------------------------------------
-- xplordb
-- 
-- Copyright (C) 2022  Oslandia / OpenLog
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU Affero General Public License as published
-- by the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-- GNU Affero General Public License for more details.
-- 
-- You should have received a copy of the GNU Affero General Public License
-- along with this program.  If not, see <https://www.gnu.org/licenses/>.
-- 
-- __authors__ = ["davidms"]
-- __contact__ = "geology@oslandia.com"
-- __date__ = "2022/02/02"
-- __license__ = "AGPLv3"
----------------------------------------------------------------------------

--
-- PostgreSQL database dump
--
-- Dumped from database version 13.5 (Ubuntu 13.5-0ubuntu0.21.10.1)
-- Dumped by pg_dump version 13.5 (Ubuntu 13.5-0ubuntu0.21.10.1)
SET statement_timeout = 0;

SET lock_timeout = 0;

SET idle_in_transaction_session_timeout = 0;

SET client_encoding = 'UTF8';

SET standard_conforming_strings = ON;

SELECT
    pg_catalog.set_config('search_path', '', FALSE);

SET check_function_bodies = FALSE;

SET xmloption = content;

SET client_min_messages = warning;

SET row_security = OFF;

SET default_tablespace = '';

SET default_table_access_method = heap;

--
-- Name: surv; Type: TABLE; Schema: dh; Owner: postgres
--
CREATE TABLE dh.surv (
    data_set integer NOT NULL,
    hole_id character varying(30) NOT NULL,
    depth_m numeric NOT NULL,
    dip real NOT NULL,
    azimuth_grid real NOT NULL,
    load_date timestamp with time zone DEFAULT ('now'::text) ::timestamp with time zone NOT NULL,
    loaded_by integer NOT NULL,
    CONSTRAINT depth_check CHECK (((depth_m >= (0)::double precision))),
    CONSTRAINT dh_surv_check_degrees_grid CHECK (((azimuth_grid >= (0)::double precision) AND (azimuth_grid <= (360)::double precision))),
    CONSTRAINT dh_surv_dip_check CHECK (((dip >= (- (90)::double precision)) AND (dip <= (90)::double precision)))
);

ALTER TABLE dh.surv OWNER TO postgres;

--
-- Name: TABLE surv; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON TABLE dh.surv IS 'Down hole drill hole survey table';

--
-- Name: COLUMN surv.data_set; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.data_set IS 'Data set for the Down hole survey, should be the same data_set as the hole_id data_set';

--
-- Name: COLUMN surv.hole_id; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.hole_id IS 'Drill hole Identification(id) number/ code, needs to have a match in dh.dh_collars.hole_id';

--
-- Name: COLUMN surv.depth_m; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.depth_m IS 'Depth the down hole survey was taken in metres';

--
-- Name: COLUMN surv.dip; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.dip IS 'Dip of the down hole survey, negative (-) values are down, positive (+) are up';

--
-- Name: COLUMN surv.azimuth_grid; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.azimuth_grid IS 'Azumith based on a Cartesian (grid) co-ordinate system such as UTM';

--
-- Name: COLUMN surv.load_date; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.load_date IS 'The date the data was loaded into xplordb';

--
-- Name: COLUMN surv.loaded_by; Type: COMMENT; Schema: dh; Owner: postgres
--
COMMENT ON COLUMN dh.surv.loaded_by IS 'The person who loaded the data into xplordb, see ref.person';


--
-- Name: surv surv_pkey; Type: CONSTRAINT; Schema: dh; Owner: postgres
--
ALTER TABLE ONLY dh.surv
    ADD CONSTRAINT surv_pkey PRIMARY KEY (hole_id, data_set, depth_m);

--
-- Name: surv check_depth_m_dh_surv; Type: TRIGGER; Schema: dh; Owner: postgres
--
CREATE TRIGGER check_depth_m_dh_surv
    BEFORE INSERT OR UPDATE OF depth_m ON dh.surv
    FOR EACH ROW
    EXECUTE FUNCTION dh.check_depth_m ();

--
-- Name: surv trace_insert_surv; Type: TRIGGER; Schema: dh; Owner: postgres
--
CREATE OR REPLACE TRIGGER trace_insert_surv
AFTER INSERT ON dh.surv
REFERENCING NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION dh.trace_update_surv ();

--
-- Name: surv trace_update_surv; Type: TRIGGER; Schema: dh; Owner: postgres
--

CREATE OR REPLACE TRIGGER trace_update_surv
AFTER UPDATE ON dh.surv
REFERENCING NEW TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION dh.trace_update_surv ();

--
-- Name: surv trace_delete_surv; Type: TRIGGER; Schema: dh; Owner: postgres
--

CREATE OR REPLACE TRIGGER trace_delete_surv
AFTER DELETE ON dh.surv
REFERENCING OLD TABLE AS new_table
FOR EACH STATEMENT
EXECUTE FUNCTION dh.trace_update_surv ();


--
-- Name: surv dh_surv_data_set_fkey; Type: FK CONSTRAINT; Schema: dh; Owner: postgres
--
ALTER TABLE ONLY dh.surv
    ADD CONSTRAINT dh_surv_data_set_fkey FOREIGN KEY (data_set) REFERENCES ref.data_sets (id) ON DELETE CASCADE;


--
-- Name: surv dh_surv_hole_id_fkey; Type: FK CONSTRAINT; Schema: dh; Owner: postgres
--
ALTER TABLE ONLY dh.surv
    ADD CONSTRAINT dh_surv_hole_id_dataset_fkey FOREIGN KEY (hole_id, data_set) REFERENCES dh.collar (hole_id, data_set) ON DELETE CASCADE;

--
-- Name: surv dh_surv_loaded_by_fkey; Type: FK CONSTRAINT; Schema: dh; Owner: postgres
--
ALTER TABLE ONLY dh.surv
    ADD CONSTRAINT dh_surv_loaded_by_fkey FOREIGN KEY (loaded_by) REFERENCES ref.person (id) ON DELETE CASCADE;

