Skip to content

Commit

Permalink
Improve support for soft-deleted feed versions
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Oct 9, 2024
1 parent 6749202 commit c589c58
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 36 deletions.
3 changes: 1 addition & 2 deletions finders/dbfinder/agency_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ func AgencySelect(limit *int, after *model.Cursor, ids []int, active bool, permF
Join("current_feeds ON current_feeds.id = feed_versions.feed_id").
JoinClause("left join tl_agency_geometries ON tl_agency_geometries.agency_id = gtfs_agencies.id").
JoinClause("left join current_operators_in_feed coif ON coif.feed_id = current_feeds.id AND coif.resolved_gtfs_agency_id = gtfs_agencies.agency_id").
Where(sq.Eq{"current_feeds.deleted_at": nil}).
OrderBy("gtfs_agencies.feed_version_id,gtfs_agencies.id").
Limit(checkLimit(limit))

Expand Down Expand Up @@ -118,6 +117,6 @@ func AgencySelect(limit *int, after *model.Cursor, ids []int, active bool, permF
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}
5 changes: 2 additions & 3 deletions finders/dbfinder/feed_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func FeedSelect(limit *int, after *model.Cursor, ids []int, permFilter *model.Pe
).
From("current_feeds").
OrderBy("current_feeds.id asc").
Limit(checkRange(limit, 0, 10_000)).
Where(sq.Eq{"deleted_at": nil})
Limit(checkRange(limit, 0, 10_000))

if where != nil {
if where.OnestopID != nil {
Expand Down Expand Up @@ -140,6 +139,6 @@ func FeedSelect(limit *int, after *model.Cursor, ids []int, permFilter *model.Pe
}

// Handle permissions
q = pfJoinCheck(q, "current_feeds.id", "", permFilter)
q = pfJoinCheck(q, permFilter)
return q
}
3 changes: 1 addition & 2 deletions finders/dbfinder/feed_version_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ func FeedVersionSelect(limit *int, after *model.Cursor, ids []int, permFilter *m
).
From("feed_versions").
Join("current_feeds on current_feeds.id = feed_versions.feed_id").
Where(sq.Eq{"current_feeds.deleted_at": nil}).
Limit(checkLimit(limit)).
OrderBy("feed_versions.fetched_at desc, feed_versions.id desc")

Expand Down Expand Up @@ -146,7 +145,7 @@ func FeedVersionSelect(limit *int, after *model.Cursor, ids []int, permFilter *m
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}

Expand Down
2 changes: 1 addition & 1 deletion finders/dbfinder/operator_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func OperatorSelect(limit *int, after *model.Cursor, ids []int, permFilter *mode
}

// Handle permissions
q = pfJoinCheck(q, "coif.feed_id", "", permFilter)
q = pfJoinCheck(q, permFilter)

// Outer query - support pagination
qView := sq.StatementBuilder.Select("t.*").FromSelect(q, "t").OrderBy("id").Limit(checkLimit(limit))
Expand Down
2 changes: 1 addition & 1 deletion finders/dbfinder/pathway_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,6 @@ func PathwaySelect(limit *int, after *model.Cursor, ids []int, permFilter *model
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}
36 changes: 19 additions & 17 deletions finders/dbfinder/place_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,51 @@ func PlaceSelect(limit *int, after *model.Cursor, ids []int, level *model.PlaceA
var groupKeys []string
var selKeys []string
// Yucky mapping
selKeys = []string{"adm0name as adm0_name"}
groupKeys = []string{"adm0name"}
selKeys = []string{"tlap.adm0name as adm0_name"}
groupKeys = []string{"tlap.adm0name"}
if level != nil {
switch *level {
case model.PlaceAggregationLevelAdm0:
groupKeys = []string{"adm0name"}
groupKeys = []string{"tlap.adm0name"}
case model.PlaceAggregationLevelAdm0Adm1:
selKeys = []string{"adm0name as adm0_name", "adm1name as adm1_name"}
groupKeys = []string{"adm0name", "adm1name"}
selKeys = []string{"tlap.adm0name as adm0_name", "tlap.adm1name as adm1_name"}
groupKeys = []string{"tlap.adm0name", "tlap.adm1name"}
case model.PlaceAggregationLevelAdm0Adm1City:
selKeys = []string{"adm0name as adm0_name", "adm1name as adm1_name", "name as city_name"}
groupKeys = []string{"adm0name", "adm1name", "name"}
selKeys = []string{"tlap.adm0name as adm0_name", "tlap.adm1name as adm1_name", "tlap.name as city_name"}
groupKeys = []string{"tlap.adm0name", "tlap.adm1name", "tlap.name"}
case model.PlaceAggregationLevelAdm0City:
selKeys = []string{"adm0name as adm0_name", "name as city_name"}
groupKeys = []string{"adm0name", "name"}
selKeys = []string{"tlap.adm0name as adm0_name", "tlap.name as city_name"}
groupKeys = []string{"tlap.adm0name", "tlap.name"}
case model.PlaceAggregationLevelAdm1City:
selKeys = []string{"adm1name as adm1_name"}
groupKeys = []string{"adm1name", "name"}
selKeys = []string{"tlap.adm1name as adm1_name"}
groupKeys = []string{"tlap.adm1name", "tlap.name"}
case model.PlaceAggregationLevelCity:
selKeys = []string{"name as city_name"}
groupKeys = []string{"name"}
selKeys = []string{"tlap.name as city_name"}
groupKeys = []string{"tlap.name"}
}
}
q := sq.StatementBuilder.
Select(selKeys...).
Columns("json_agg(distinct tlap.agency_id) as agency_ids").
From("feed_states").
Join("tl_agency_places tlap on tlap.feed_version_id = feed_states.feed_version_id").
Join("feed_versions on feed_versions.id = feed_states.feed_version_id").
Join("current_feeds on current_feeds.id = feed_states.feed_id").
GroupBy(groupKeys...)

if where != nil {
if where.Adm0Name != nil {
q = q.Where(sq.Eq{"adm0name": where.Adm0Name})
q = q.Where(sq.Eq{"tlap.adm0name": where.Adm0Name})
}
if where.Adm1Name != nil {
q = q.Where(sq.Eq{"adm1name": where.Adm1Name})
q = q.Where(sq.Eq{"tlap.adm1name": where.Adm1Name})
}
if where.CityName != nil {
q = q.Where(sq.Eq{"name": where.CityName})
q = q.Where(sq.Eq{"tlap.name": where.CityName})
}
}

// Handle permissions
q = pfJoinCheck(q, "feed_states.feed_id", "feed_states.feed_version_id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}
3 changes: 1 addition & 2 deletions finders/dbfinder/route_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ func RouteSelect(limit *int, after *model.Cursor, ids []int, active bool, permFi
From("gtfs_routes").
Join("feed_versions ON feed_versions.id = gtfs_routes.feed_version_id").
Join("current_feeds ON current_feeds.id = feed_versions.feed_id").
Where(sq.Eq{"current_feeds.deleted_at": nil}).
OrderBy("gtfs_routes.feed_version_id,gtfs_routes.id").
Limit(checkLimit(limit))

Expand Down Expand Up @@ -148,7 +147,7 @@ func RouteSelect(limit *int, after *model.Cursor, ids []int, active bool, permFi
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}

Expand Down
3 changes: 1 addition & 2 deletions finders/dbfinder/stop_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func StopSelect(limit *int, after *model.Cursor, ids []int, active bool, permFil
From("gtfs_stops").
Join("feed_versions ON feed_versions.id = gtfs_stops.feed_version_id").
Join("current_feeds ON current_feeds.id = feed_versions.feed_id").
Where(sq.Eq{"current_feeds.deleted_at": nil}).
OrderBy("gtfs_stops.feed_version_id,gtfs_stops.id").
Limit(checkLimit(limit))
distinct := false
Expand Down Expand Up @@ -189,6 +188,6 @@ func StopSelect(limit *int, after *model.Cursor, ids []int, active bool, permFil
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}
2 changes: 1 addition & 1 deletion finders/dbfinder/trip_select.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,6 @@ func TripSelect(limit *int, after *model.Cursor, ids []int, active bool, permFil
}

// Handle permissions
q = pfJoinCheck(q, "feed_versions.feed_id", "feed_versions.id", permFilter)
q = pfJoinCheckFv(q, permFilter)
return q
}
18 changes: 13 additions & 5 deletions finders/dbfinder/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,22 @@ func escapeWordsWithSuffix(v string, sfx string) []string {
return ret
}

func pfJoinCheck(q sq.SelectBuilder, feedCol string, fvCol string, permFilter *model.PermFilter) sq.SelectBuilder {
func pfJoinCheck(q sq.SelectBuilder, permFilter *model.PermFilter) sq.SelectBuilder {
q = q.Join("feed_states fsp on fsp.feed_id = current_feeds.id")
sqOr := sq.Or{}
q = q.Join(fmt.Sprintf("feed_states fsp on fsp.feed_id = %s", az09(feedCol)))
sqOr = append(sqOr, sq.Expr("fsp.public = true"))
sqOr = append(sqOr, In("fsp.feed_id", permFilter.GetAllowedFeeds()))
if fvCol != "" {
sqOr = append(sqOr, In(az09(fvCol), permFilter.GetAllowedFeedVersions()))
}
return q.Where(sqOr)
}

func pfJoinCheckFv(q sq.SelectBuilder, permFilter *model.PermFilter) sq.SelectBuilder {
q = q.Join("feed_states fsp on fsp.feed_id = feed_versions.feed_id").
Where(sq.Eq{"current_feeds.deleted_at": nil}).
Where(sq.Eq{"feed_versions.deleted_at": nil})
sqOr := sq.Or{}
sqOr = append(sqOr, sq.Expr("fsp.public = true"))
sqOr = append(sqOr, In("feed_versions.feed_id", permFilter.GetAllowedFeeds()))
sqOr = append(sqOr, In("feed_versions.id", permFilter.GetAllowedFeedVersions()))
return q.Where(sqOr)
}

Expand Down

0 comments on commit c589c58

Please sign in to comment.