Add initial synchronization of Fieldseeker data by oauth

This involves a lot of copy-pasta of code from the previous version of
this logic in another code base. It'll need to be cleaned up, but for
now I need something sooner rather than later.
This commit is contained in:
Eli Ribble 2025-11-07 08:34:32 +00:00
parent 46ea655073
commit 9010726707
No known key found for this signature in database
235 changed files with 259263 additions and 153 deletions

View file

@ -0,0 +1,96 @@
// Code generated by BobGen psql v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package sql
import (
"context"
_ "embed"
"io"
"iter"
"github.com/aarondl/opt/null"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"
"github.com/stephenafamo/bob/dialect/psql/dialect"
"github.com/stephenafamo/bob/orm"
"github.com/stephenafamo/scan"
)
//go:embed org_by_oauth_id.bob.sql
var formattedQueries_org_by_oauth_id string
var orgByOauthIdSQL = formattedQueries_org_by_oauth_id[150:363]
type OrgByOauthIdQuery = orm.ModQuery[*dialect.SelectQuery, orgByOauthId, OrgByOauthIdRow, []OrgByOauthIdRow, orgByOauthIdTransformer]
func OrgByOauthId(ID int32) *OrgByOauthIdQuery {
var expressionTypArgs orgByOauthId
expressionTypArgs.ID = psql.Arg(ID)
return &OrgByOauthIdQuery{
Query: orm.Query[orgByOauthId, OrgByOauthIdRow, []OrgByOauthIdRow, orgByOauthIdTransformer]{
ExecQuery: orm.ExecQuery[orgByOauthId]{
BaseQuery: bob.BaseQuery[orgByOauthId]{
Expression: expressionTypArgs,
Dialect: dialect.Dialect,
QueryType: bob.QueryTypeSelect,
},
},
Scanner: func(context.Context, []string) (func(*scan.Row) (any, error), func(any) (OrgByOauthIdRow, error)) {
return func(row *scan.Row) (any, error) {
var t OrgByOauthIdRow
row.ScheduleScanByIndex(0, &t.OrganizationID)
row.ScheduleScanByIndex(1, &t.ArcgisID)
row.ScheduleScanByIndex(2, &t.FieldseekerURL)
return &t, nil
}, func(v any) (OrgByOauthIdRow, error) {
return *(v.(*OrgByOauthIdRow)), nil
}
},
},
Mod: bob.ModFunc[*dialect.SelectQuery](func(q *dialect.SelectQuery) {
q.AppendSelect(expressionTypArgs.subExpr(7, 94))
q.SetTable(expressionTypArgs.subExpr(100, 196))
q.AppendWhere(expressionTypArgs.subExpr(203, 213))
}),
}
}
type OrgByOauthIdRow = struct {
OrganizationID int32 `db:"organization_id"`
ArcgisID null.Val[string] `db:"arcgis_id"`
FieldseekerURL null.Val[string] `db:"fieldseeker_url"`
}
type orgByOauthIdTransformer = bob.SliceTransformer[OrgByOauthIdRow, []OrgByOauthIdRow]
type orgByOauthId struct {
ID bob.Expression
}
func (o orgByOauthId) args() iter.Seq[orm.ArgWithPosition] {
return func(yield func(arg orm.ArgWithPosition) bool) {
if !yield(orm.ArgWithPosition{
Name: "id",
Start: 211,
Stop: 213,
Expression: o.ID,
}) {
return
}
}
}
func (o orgByOauthId) raw(from, to int) string {
return orgByOauthIdSQL[from:to]
}
func (o orgByOauthId) subExpr(from, to int) bob.Expression {
return orm.ArgsToExpression(orgByOauthIdSQL, from, to, o.args())
}
func (o orgByOauthId) WriteSQL(ctx context.Context, w io.Writer, d bob.Dialect, start int) ([]any, error) {
return o.subExpr(0, len(orgByOauthIdSQL)).WriteSQL(ctx, w, d, start)
}

View file

@ -0,0 +1,9 @@
-- Code generated by BobGen psql v0.41.1. DO NOT EDIT.
-- This file is meant to be re-generated in place and/or deleted at any time.
-- OrgByOauthId
SELECT o.id AS organization_id, o.arcgis_id AS arcgis_id, o.fieldseeker_url AS fieldseeker_url
FROM oauth_token ot
JOIN user_ u ON ot.user_id = u.id
JOIN organization o ON u.organization_id = o.id
WHERE ot.id = $1;

View file

@ -0,0 +1,103 @@
// Code generated by BobGen psql v0.41.1. DO NOT EDIT.
// This file is meant to be re-generated in place and/or deleted at any time.
package sql
import (
"context"
"fmt"
"strings"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/stephenafamo/bob"
"github.com/stephenafamo/bob/dialect/psql"
testutils "github.com/stephenafamo/bob/test/utils"
)
func TestOrgByOauthId(t *testing.T) {
t.Run("Base", func(t *testing.T) {
var sb strings.Builder
query := OrgByOauthId(random_int32(nil))
if _, err := query.WriteQuery(t.Context(), &sb, 1); err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(orgByOauthIdSQL, sb.String()); diff != "" {
t.Fatalf("unexpected result (-got +want):\n%s", diff)
}
})
t.Run("Mod", func(t *testing.T) {
var sb strings.Builder
query := OrgByOauthId(random_int32(nil))
if _, err := psql.Select(query).WriteQuery(t.Context(), &sb, 1); err != nil {
t.Fatal(err)
}
queryDiff, err := testutils.QueryDiff(orgByOauthIdSQL, sb.String(), formatQuery)
if err != nil {
t.Fatal(err)
}
if queryDiff != "" {
fmt.Println(sb.String())
t.Fatalf("unexpected result (-got +want):\n%s", queryDiff)
}
})
t.Run("Scanning", func(t *testing.T) {
if testDB == nil {
t.Skip("skipping test, no DSN provided")
}
ctxTx, cancel := context.WithCancel(t.Context())
defer cancel()
tx, err := testDB.Begin(ctxTx)
if err != nil {
t.Fatalf("Error starting transaction: %v", err)
}
defer func() {
if err := tx.Rollback(ctxTx); err != nil {
t.Fatalf("Error rolling back transaction: %v", err)
}
}()
query, args, err := bob.Build(ctxTx, psql.Select(OrgByOauthId(random_int32(nil))))
if err != nil {
t.Fatal(err)
}
rows, err := tx.QueryContext(ctxTx, query, args...)
if err != nil {
t.Fatal(err)
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
t.Fatal(err)
}
if len(columns) != 3 {
t.Fatalf("expected %d columns, got %d", 3, len(columns))
}
if columns[0] != "organization_id" {
t.Fatalf("expected column %d to be %s, got %s", 0, "organization_id", columns[0])
}
if columns[1] != "arcgis_id" {
t.Fatalf("expected column %d to be %s, got %s", 1, "arcgis_id", columns[1])
}
if columns[2] != "fieldseeker_url" {
t.Fatalf("expected column %d to be %s, got %s", 2, "fieldseeker_url", columns[2])
}
})
}

6
sql/org_by_oauth_id.sql Normal file
View file

@ -0,0 +1,6 @@
-- OrgByOauthId
SELECT o.id AS organization_id, o.arcgis_id AS arcgis_id, o.fieldseeker_url AS fieldseeker_url
FROM oauth_token ot
JOIN user_ u ON ot.user_id = u.id
JOIN organization o ON u.organization_id = o.id
WHERE ot.id = $1