Skip to content

Commit

Permalink
Improve handling of multi-column unique checks
Browse files Browse the repository at this point in the history
  • Loading branch information
irees committed Dec 18, 2024
1 parent 45cd2bc commit 28bd174
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 232 deletions.
16 changes: 16 additions & 0 deletions causes/causes.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,22 @@ func (e *DuplicateIDError) Error() string {

////////////////////////////

// DuplicateIDError reports when a unique ID is used more than once in a file.
type DuplicateKeyError struct {
bc
}

// NewDuplicateIDError returns a new DuplicateIDErrror
func NewDuplicateKeyError(eid string) *DuplicateKeyError {
return &DuplicateKeyError{bc: bc{Value: eid}}
}

func (e *DuplicateKeyError) Error() string {
return fmt.Sprintf("entity with fields '%s' is present more than once", e.Value)
}

////////////////////////////

// DuplicateServiceExceptionError reports when a (service_id,date) value is present more than once.
type DuplicateServiceExceptionError struct {
ServiceID string
Expand Down
6 changes: 2 additions & 4 deletions copier/copier.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,16 +243,14 @@ func NewCopier(reader adapters.Reader, writer adapters.Writer, opts Options) (*C

// Default set of validators
if !opts.NoValidators {
copier.AddValidator(&rules.EntityDuplicateCheck{}, 0)
copier.AddValidator(&rules.EntityDuplicateIDCheck{}, 0)
copier.AddValidator(&rules.EntityDuplicateKeyCheck{}, 0)
copier.AddValidator(&rules.ValidFarezoneCheck{}, 0)
copier.AddValidator(&rules.AgencyIDConditionallyRequiredCheck{}, 0)
copier.AddValidator(&rules.StopTimeSequenceCheck{}, 0)
copier.AddValidator(&rules.InconsistentTimezoneCheck{}, 0)
copier.AddValidator(&rules.ParentStationLocationTypeCheck{}, 0)
copier.AddValidator(&rules.CalendarDuplicateDates{}, 0)
copier.AddValidator(&rules.DuplicateFareLegRuleCheck{}, 0)
copier.AddValidator(&rules.DuplicateFareTransferRuleCheck{}, 0)
copier.AddValidator(&rules.DuplicateFareProductCheck{}, 0)
}

// Default extensions
Expand Down
15 changes: 15 additions & 0 deletions gtfs/fare_leg_rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gtfs

import (
"fmt"

"github.com/interline-io/transitland-lib/tt"
)

Expand Down Expand Up @@ -33,3 +35,16 @@ func (ent *FareLegRule) TableName() string {
func (ent *FareLegRule) GroupKey() (string, string) {
return "leg_group_id", ent.LegGroupID.Val
}

func (ent *FareLegRule) DuplicateKey() string {
key := fmt.Sprintf(
"fare_product_id:'%s' network_id:'%s' from_area_id:'%s' to_area_id:'%s' from_timeframe_group_id:'%s' to_timeframe_group_id:'%s'",
ent.FareProductID.Val,
ent.NetworkID.Val,
ent.FromAreaID.Val,
ent.ToAreaID.Val,
ent.FromTimeframeGroupID.Val,
ent.ToTimeframeGroupID.Val,
)
return key
}
11 changes: 11 additions & 0 deletions gtfs/fare_product.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gtfs

import (
"fmt"

"github.com/interline-io/transitland-lib/causes"
"github.com/interline-io/transitland-lib/tt"
)
Expand Down Expand Up @@ -54,3 +56,12 @@ func (ent *FareProduct) ConditionalErrors() (errs []error) {
}
return errs
}

func (ent *FareProduct) DuplicateKey() string {
return fmt.Sprintf(
"fare_product_id:'%s' rider_category_id:'%s' fare_media_id:'%s'",
ent.FareProductID,
ent.RiderCategoryID,
ent.FareMediaID,
)
}
12 changes: 12 additions & 0 deletions gtfs/fare_rule.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package gtfs

import (
"fmt"

"github.com/interline-io/transitland-lib/tt"
)

Expand All @@ -23,3 +25,13 @@ func (ent *FareRule) Filename() string {
func (ent *FareRule) TableName() string {
return "gtfs_fare_rules"
}

func (ent *FareRule) DuplicateKey() string {
return fmt.Sprintf(
"route_id:'%s' origin_id:'%s' destination_id:'%s' contains_id:'%s'",
ent.RouteID.Val,
ent.OriginID.Val,
ent.DestinationID.Val,
ent.ContainsID.Val,
)
}
12 changes: 12 additions & 0 deletions gtfs/fare_transfer_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,15 @@ func (ent *FareTransferRule) ConditionalErrors() (errs []error) {
}
return errs
}

func (ent *FareTransferRule) DuplicateKey() string {
return fmt.Sprintf(
"fare_product_id:'%s' from_leg_group_id:'%s' to_leg_group_id:'%s' filter_fare_product_id:'%s' transfer_count:%d duration_limit:%d",
ent.FareProductID.Val,
ent.FromLegGroupID.Val,
ent.ToLegGroupID.Val,
ent.FilterFareProductID.Val,
ent.TransferCount.Val,
ent.DurationLimit.Val,
)
}
8 changes: 6 additions & 2 deletions gtfs/route_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func (ent *RouteNetwork) TableName() string {
return "gtfs_route_networks"
}

func (ent *RouteNetwork) EntityKey() string {
return fmt.Sprintf("%s:%s", ent.NetworkID.Val, ent.RouteID.Val)
func (ent *RouteNetwork) DuplicateKey() string {
return fmt.Sprintf(
"network_id:'%s' route_id:'%s'",
ent.NetworkID.Val,
ent.RouteID.Val,
)
}
54 changes: 0 additions & 54 deletions rules/duplicate_fare_leg_rule.go

This file was deleted.

51 changes: 0 additions & 51 deletions rules/duplicate_fare_product.go

This file was deleted.

53 changes: 0 additions & 53 deletions rules/duplicate_fare_rule.go

This file was deleted.

60 changes: 0 additions & 60 deletions rules/duplicate_fare_transfer_rule.go

This file was deleted.

Loading

0 comments on commit 28bd174

Please sign in to comment.