diff --git a/migrations/generic/timetables/1000000000000_R_before_migrate/down.sql b/migrations/generic/timetables/1000000000000_R_before_migrate/down.sql index 9f2cdf9e..2affc14e 100644 --- a/migrations/generic/timetables/1000000000000_R_before_migrate/down.sql +++ b/migrations/generic/timetables/1000000000000_R_before_migrate/down.sql @@ -7,3 +7,4 @@ DROP SCHEMA vehicle_service; DROP SCHEMA vehicle_journey; DROP SCHEMA passing_times; DROP SCHEMA return_value; +DROP SCHEMA internal_service_calendar; diff --git a/migrations/generic/timetables/1000000000000_R_before_migrate/up.sql b/migrations/generic/timetables/1000000000000_R_before_migrate/up.sql index c0a10f5b..ae3c111d 100644 --- a/migrations/generic/timetables/1000000000000_R_before_migrate/up.sql +++ b/migrations/generic/timetables/1000000000000_R_before_migrate/up.sql @@ -1,44 +1,49 @@ -- create schemas if they don't yet exist, because the drop function below reference them CREATE SCHEMA IF NOT EXISTS route; -COMMENT ON SCHEMA route +COMMENT ON SCHEMA route IS 'The route model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:1:3:416'; CREATE SCHEMA IF NOT EXISTS journey_pattern; -COMMENT ON SCHEMA journey_pattern +COMMENT ON SCHEMA journey_pattern IS 'The journey pattern model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:3:1:683 '; CREATE SCHEMA IF NOT EXISTS service_pattern; -COMMENT ON SCHEMA service_pattern +COMMENT ON SCHEMA service_pattern IS 'The service pattern model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=2:3:4:723 '; CREATE SCHEMA IF NOT EXISTS service_calendar; -COMMENT ON SCHEMA service_calendar +COMMENT ON SCHEMA service_calendar IS 'The service calendar model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=1:6:3:294 '; CREATE SCHEMA IF NOT EXISTS vehicle_schedule; -COMMENT ON SCHEMA vehicle_schedule +COMMENT ON SCHEMA vehicle_schedule IS 'The vehicle schedule frame adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:7:2:993 '; CREATE SCHEMA IF NOT EXISTS vehicle_service; -COMMENT ON SCHEMA vehicle_service +COMMENT ON SCHEMA vehicle_service IS 'The vehicle service model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:5:947 '; CREATE SCHEMA IF NOT EXISTS vehicle_journey; -COMMENT ON SCHEMA vehicle_journey +COMMENT ON SCHEMA vehicle_journey IS 'The vehicle journey model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:1:1:824 '; CREATE SCHEMA IF NOT EXISTS passing_times; -COMMENT ON SCHEMA passing_times +COMMENT ON SCHEMA passing_times IS 'The passing times model adapted from Transmodel: https://www.transmodel-cen.eu/model/index.htm?goto=3:4:939 '; CREATE SCHEMA IF NOT EXISTS internal_utils; -COMMENT ON SCHEMA internal_utils -IS 'General utilities'; +COMMENT ON SCHEMA internal_utils +IS 'Internal general utilities. Functions in this schema are not exposed to GraphQL API, hence the prefix.'; CREATE SCHEMA IF NOT EXISTS return_value; -COMMENT ON SCHEMA return_value +COMMENT ON SCHEMA return_value IS 'This schema is used for all the SQL functions that need to have a table as return value. Nothing is stored in the tables in this schema.'; +CREATE SCHEMA IF NOT EXISTS internal_service_calendar; +COMMENT ON SCHEMA internal_service_calendar +IS 'This schema is used for the internal SQL functions that are related to service_calendar schema. These internal functions are +not exposed to GraphQL API, hence the prefix.'; + -- drop all triggers in jore4 schemas -- note: information_schema.triggers is missing TRUNCATE triggers -- note2: pg_catalog.pg_triggers contains the TRUNCATE triggers but many other things too diff --git a/migrations/hsl/timetables/2000000000104_R_after_migrate_create_get_vehicle_schedules/up.sql b/migrations/hsl/timetables/2000000000104_R_after_migrate_create_get_vehicle_schedules/up.sql index f1b5f578..7896d915 100644 --- a/migrations/hsl/timetables/2000000000104_R_after_migrate_create_get_vehicle_schedules/up.sql +++ b/migrations/hsl/timetables/2000000000104_R_after_migrate_create_get_vehicle_schedules/up.sql @@ -1,3 +1,35 @@ +CREATE OR REPLACE FUNCTION internal_service_calendar.get_substitute_operating_period_begin_date(substitute_operating_period_uuid uuid) +RETURNS date +LANGUAGE SQL + STABLE +AS $$ + SELECT MIN(superseded_date) + FROM service_calendar.substitute_operating_day_by_line_type + WHERE substitute_operating_period_id = substitute_operating_period_uuid; +$$ +; + +COMMENT ON FUNCTION internal_service_calendar.get_substitute_operating_period_begin_date(substitute_operating_period_uuid uuid) +IS 'Returns the begin date of a substitute operating period that consists of substitute_operating_day_by_line_types +(superseded_date). +'; + +CREATE OR REPLACE FUNCTION internal_service_calendar.get_substitute_operating_period_end_date(substitute_operating_period_uuid uuid) +RETURNS date +LANGUAGE SQL + STABLE +AS $$ + SELECT MAX(superseded_date) + FROM service_calendar.substitute_operating_day_by_line_type + WHERE substitute_operating_period_id = substitute_operating_period_uuid; +$$ +; + +COMMENT ON FUNCTION internal_service_calendar.get_substitute_operating_period_end_date(substitute_operating_period_uuid uuid) +IS 'Returns the last date of a substitute operating period that consists of substitute_operating_day_by_line_types +(superseded_date). +'; + CREATE OR REPLACE FUNCTION vehicle_journey.get_vehicle_schedules_on_date(journey_pattern_uuid uuid, observation_date date) RETURNS SETOF return_value.vehicle_schedule LANGUAGE SQL @@ -6,8 +38,8 @@ AS $$ WITH substitute_operating_day_by_line_type_vehicle_schedules AS ( SELECT DISTINCT vj.vehicle_journey_id, - vsf.validity_start, - vsf.validity_end, + internal_service_calendar.get_substitute_operating_period_begin_date(sodblt.substitute_operating_period_id), + internal_service_calendar.get_substitute_operating_period_end_date(sodblt.substitute_operating_period_id), internal_utils.const_timetables_priority_substitute_by_line_type(), -- priority ( -- calculate what is the correct day type id for the superseded date SELECT day_type_id @@ -113,3 +145,4 @@ the time range does not return any vehicle journeys (or if the substitute_operat a row which does not have vehicle_journey set. This is an indicator that the day type does not have operation on the given day. This is of course overruled by special priority schedules, it being a higher priority. '; + diff --git a/migrations/timetablesdb-dump.sql b/migrations/timetablesdb-dump.sql index 9fbcefb9..43ec858e 100644 --- a/migrations/timetablesdb-dump.sql +++ b/migrations/timetablesdb-dump.sql @@ -25,11 +25,18 @@ COMMENT ON EXTENSION btree_gist IS 'support for indexing common datatypes in GiS COMMENT ON EXTENSION pgcrypto IS 'cryptographic functions'; +-- +-- Name: SCHEMA internal_service_calendar; Type: COMMENT; Schema: -; Owner: dbhasura +-- + +COMMENT ON SCHEMA internal_service_calendar IS 'This schema is used for the internal SQL functions that are related to service_calendar schema. These internal functions are +not exposed to GraphQL API, hence the prefix.'; + -- -- Name: SCHEMA internal_utils; Type: COMMENT; Schema: -; Owner: dbhasura -- -COMMENT ON SCHEMA internal_utils IS 'General utilities'; +COMMENT ON SCHEMA internal_utils IS 'Internal general utilities. Functions in this schema are not exposed to GraphQL API, hence the prefix.'; -- -- Name: SCHEMA journey_pattern; Type: COMMENT; Schema: -; Owner: dbhasura @@ -1663,6 +1670,15 @@ CREATE INDEX idx_vehicle_service_vehicle_schedule_frame ON vehicle_service.vehic CREATE UNIQUE INDEX vehicle_type_label_idx ON vehicle_type.vehicle_type USING btree (label); +-- +-- Name: internal_service_calendar; Type: SCHEMA; Schema: -; Owner: dbhasura +-- + +CREATE SCHEMA internal_service_calendar; + + +ALTER SCHEMA internal_service_calendar OWNER TO dbhasura; + -- -- Name: internal_utils; Type: SCHEMA; Schema: -; Owner: dbhasura --