feat(db): add script db/gtfs-join-trats.sh

This commit is contained in:
dancingCycle 2023-08-29 13:06:36 +02:00
parent fa08b30b70
commit 079407dbf8
2 changed files with 45 additions and 0 deletions

24
db/gtfs-join-trats.sh Normal file
View File

@ -0,0 +1,24 @@
#!/bin/bash
#
echo "Started..."
#Started...
#
# special variable $# is the number of arguments
if [ $# -lt 4 ] ; then
echo 'Call ./<script> <db name> <db user> <db schema> <db gtfs schema>'
exit 1
fi
#
DB_NAME="$1"
echo "DB_NAME: $DB_NAME"
DB_USER="$2"
echo "DB_USER: $DB_USER"
DB_SCHEMA="$3"
echo "DB_SCHEMA: ${DB_SCHEMA}"
DB_SCHEMA_GTFS="$4"
echo "DB_SCHEMA_GTFS: ${DB_SCHEMA_GTFS}"
#
psql -h localhost -p 5432 -U $DB_USER -f ./sql/gtfs-join-trats.sql -d $DB_NAME -v schema_gtfs=$DB_SCHEMA_GTFS -v schema=$DB_SCHEMA
#
echo "Done."
#done.

View File

@ -0,0 +1,21 @@
CREATE SCHEMA IF NOT EXISTS :schema;
CREATE SCHEMA IF NOT EXISTS :schema_gtfs;
SET search_path to :schema, :schema_gtfs, public;
DROP TABLE IF EXISTS :schema.gtfs_join_trats;
CREATE INDEX IF NOT EXISTS agency_id_idx ON :schema_gtfs.agency(agency_id);
CREATE INDEX IF NOT EXISTS routes_id_idx ON :schema_gtfs.routes(route_id);
CREATE INDEX IF NOT EXISTS stop_times_stop_id_idx ON :schema_gtfs.stop_times(stop_id);
CREATE INDEX IF NOT EXISTS stop_times_trip_id_idx ON :schema_gtfs.stop_times(trip_id);
CREATE INDEX IF NOT EXISTS stops_id_idx ON :schema_gtfs.stops(stop_id);
CREATE INDEX IF NOT EXISTS trips_id_idx ON :schema_gtfs.trips(trip_id);
VACUUM ANALYZE;
CREATE TABLE IF NOT EXISTS :schema_gtfs.gtfs_join_trats AS SELECT trips.trip_id, trips.trip_headsign, routes.route_id, routes.route_short_name, agency.agency_id, agency.agency_name, stops.stop_id, stops.stop_name FROM :schema_gtfs.trips AS trips JOIN :schema_gtfs.routes AS routes ON trips.route_id = routes.route_id JOIN :schema_gtfs.agency AS agency ON routes.agency_id = agency.agency_id JOIN :schema_gtfs.stop_times AS times ON trips.trip_id = times.trip_id JOIN :schema_gtfs.stops AS stops ON times.stop_id = stops.stop_id ORDER BY stops.stop_id, routes.route_id, trips.trip_id;
CREATE INDEX IF NOT EXISTS gtfs_join_trats_stop_id_idx ON :schema_gtfs.gtfs_join_trats(stop_id);
VACUUM ANALYZE;