Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[cleanup] Remove rid database v3 backward compatibility (RID schema version 4 is required after this commit) #1142

Merged
merged 3 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 16 additions & 39 deletions pkg/rid/store/cockroach/identification_service_area.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,20 @@ import (
"github.com/interuss/dss/pkg/geo"
dssmodels "github.com/interuss/dss/pkg/models"
ridmodels "github.com/interuss/dss/pkg/rid/models"
repos "github.com/interuss/dss/pkg/rid/repos"
dssql "github.com/interuss/dss/pkg/sql"
"github.com/interuss/stacktrace"

"github.com/coreos/go-semver/semver"
"github.com/golang/geo/s2"
"github.com/jackc/pgx/v5/pgtype"
"go.uber.org/zap"
)

const (
isaFields = "id, owner, url, cells, starts_at, ends_at, writer, updated_at"
updateISAFields = "id, url, cells, starts_at, ends_at, writer, updated_at"
)

func NewISARepo(ctx context.Context, db dssql.Queryable, dbVersion semver.Version, logger *zap.Logger) repos.ISA {
if dbVersion.Compare(v400) >= 0 {
return &isaRepo{
Queryable: db,
logger: logger,
}
}
return &isaRepoV3{
Queryable: db,
logger: logger,
}
}

// isaRepo is an implementation of the ISARepo for CRDB.
type isaRepo struct {
dssql.Queryable

logger *zap.Logger
}

func (c *isaRepo) process(ctx context.Context, query string, args ...interface{}) ([]*ridmodels.IdentificationServiceArea, error) {
rows, err := c.Query(ctx, query, args...)
func (r *repo) fetchISAs(ctx context.Context, query string, args ...interface{}) ([]*ridmodels.IdentificationServiceArea, error) {
rows, err := r.Query(ctx, query, args...)
if err != nil {
return nil, stacktrace.Propagate(err, fmt.Sprintf("Error in query: %s", query))
}
Expand Down Expand Up @@ -85,8 +62,8 @@ func (c *isaRepo) process(ctx context.Context, query string, args ...interface{}
return payload, nil
}

func (c *isaRepo) processOne(ctx context.Context, query string, args ...interface{}) (*ridmodels.IdentificationServiceArea, error) {
isas, err := c.process(ctx, query, args...)
func (r *repo) fetchISA(ctx context.Context, query string, args ...interface{}) (*ridmodels.IdentificationServiceArea, error) {
isas, err := r.fetchISAs(ctx, query, args...)
if err != nil {
return nil, err // No need to Propagate this error as this stack layer does not add useful information
}
Expand All @@ -101,7 +78,7 @@ func (c *isaRepo) processOne(ctx context.Context, query string, args ...interfac

// GetISA returns the isa identified by "id".
// Returns nil, nil if not found
func (c *isaRepo) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
func (r *repo) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (*ridmodels.IdentificationServiceArea, error) {
var query = fmt.Sprintf(`
SELECT %s FROM
identification_service_areas
Expand All @@ -112,7 +89,7 @@ func (c *isaRepo) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
}
return c.processOne(ctx, query, uid)
return r.fetchISA(ctx, query, uid)
}

// InsertISA inserts the IdentificationServiceArea identified by "id" and owned
Expand All @@ -122,7 +99,7 @@ func (c *isaRepo) GetISA(ctx context.Context, id dssmodels.ID, forUpdate bool) (
// by it.
// TODO: Simplify the logic to insert without a query, such that the insert fails
// if there's an existing entity.
func (c *isaRepo) InsertISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
func (r *repo) InsertISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
var (
insertAreasQuery = fmt.Sprintf(`
INSERT INTO
Expand All @@ -147,7 +124,7 @@ func (c *isaRepo) InsertISA(ctx context.Context, isa *ridmodels.IdentificationSe
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
}
return c.processOne(ctx, insertAreasQuery, id, isa.Owner, isa.URL, cids, isa.StartTime, isa.EndTime, isa.Writer)
return r.fetchISA(ctx, insertAreasQuery, id, isa.Owner, isa.URL, cids, isa.StartTime, isa.EndTime, isa.Writer)

}

Expand All @@ -158,7 +135,7 @@ func (c *isaRepo) InsertISA(ctx context.Context, isa *ridmodels.IdentificationSe
// by it.
// TODO: simplify the logic to just update, without the primary query.
// Returns nil, nil if ID, version not found
func (c *isaRepo) UpdateISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
func (r *repo) UpdateISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
var (
updateAreasQuery = fmt.Sprintf(`
UPDATE
Expand All @@ -177,13 +154,13 @@ func (c *isaRepo) UpdateISA(ctx context.Context, isa *ridmodels.IdentificationSe
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
}
return c.processOne(ctx, updateAreasQuery, id, isa.URL, cids, isa.StartTime, isa.EndTime, isa.Version.ToTimestamp(), isa.Writer)
return r.fetchISA(ctx, updateAreasQuery, id, isa.URL, cids, isa.StartTime, isa.EndTime, isa.Version.ToTimestamp(), isa.Writer)
}

// DeleteISA deletes the IdentificationServiceArea identified by "id" and owned by "owner".
// Returns the delete IdentificationServiceArea and all Subscriptions affected by the delete.
// Returns nil, nil if ID, version not found
func (c *isaRepo) DeleteISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
func (r *repo) DeleteISA(ctx context.Context, isa *ridmodels.IdentificationServiceArea) (*ridmodels.IdentificationServiceArea, error) {
var (
deleteQuery = fmt.Sprintf(`
DELETE FROM
Expand All @@ -198,13 +175,13 @@ func (c *isaRepo) DeleteISA(ctx context.Context, isa *ridmodels.IdentificationSe
if err != nil {
return nil, stacktrace.Propagate(err, "Failed to convert id to PgUUID")
}
return c.processOne(ctx, deleteQuery, id, isa.Version.ToTimestamp())
return r.fetchISA(ctx, deleteQuery, id, isa.Version.ToTimestamp())
}

// SearchISAs searches IdentificationServiceArea
// instances that intersect with "cells" and, if set, the temporal volume
// defined by "earliest" and "latest".
func (c *isaRepo) SearchISAs(ctx context.Context, cells s2.CellUnion, earliest *time.Time, latest *time.Time) ([]*ridmodels.IdentificationServiceArea, error) {
func (r *repo) SearchISAs(ctx context.Context, cells s2.CellUnion, earliest *time.Time, latest *time.Time) ([]*ridmodels.IdentificationServiceArea, error) {
var (
// TODO: make earliest and latest required (NOT NULL) and remove coalesce.
// Make them real values (not pointers), on the model layer.
Expand All @@ -230,13 +207,13 @@ func (c *isaRepo) SearchISAs(ctx context.Context, cells s2.CellUnion, earliest *
return nil, stacktrace.NewError("Earliest start time is missing")
}

return c.process(ctx, isasInCellsQuery, earliest, latest, dssql.CellUnionToCellIds(cells), dssmodels.MaxResultLimit)
return r.fetchISAs(ctx, isasInCellsQuery, earliest, latest, dssql.CellUnionToCellIds(cells), dssmodels.MaxResultLimit)
}

// ListExpiredISAs lists all expired ISAs based on writer.
// Records expire if current time is <expiredDurationInMin> minutes more than records' endTime.
// The function queries both empty writer and null writer when passing empty string as a writer.
func (c *isaRepo) ListExpiredISAs(ctx context.Context, writer string) ([]*ridmodels.IdentificationServiceArea, error) {
func (r *repo) ListExpiredISAs(ctx context.Context, writer string) ([]*ridmodels.IdentificationServiceArea, error) {
writerQuery := "'" + writer + "'"
if len(writer) == 0 {
writerQuery = "'' OR writer = NULL"
Expand All @@ -255,5 +232,5 @@ func (c *isaRepo) ListExpiredISAs(ctx context.Context, writer string) ([]*ridmod
LIMIT $1`, isaFields, expiredDurationInMin, writerQuery)
)

return c.process(ctx, isasInCellsQuery, dssmodels.MaxResultLimit)
return r.fetchISAs(ctx, isasInCellsQuery, dssmodels.MaxResultLimit)
}
215 changes: 0 additions & 215 deletions pkg/rid/store/cockroach/identification_service_area_v3.go

This file was deleted.

Loading
Loading