~bigbes/core-go

ref: 0c56cc143e7dc0ba583114c3f50bce35035007a9 core-go/database d---------
20be483e — Conrad Hoffmann 9 months ago
Don't uppercase error messages
39c3fd1e — Conrad Hoffmann 9 months ago
Run modernize

See https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/modernize

It's mostly interface{} -> any, but also two quite useful applications
of slices.Contains.
898f2e95 — Drew DeVault 10 months ago
database: add WithReadOnlyTx
3902e4bd — Simon Ser 2 years ago
database: don't panic on context.DeadlineExceeded

Checking context.Cancelled is not enough, I also see the same issue
with context.DeadlineExceeded:

    panic: Transaction error: driver: bad connection
    Closure error: context deadline exceeded

    goroutine 66664 [running]:
    git.sr.ht/~sircmpwn/core-go/database.WithTx({0x5578e1847a58, 0xc0002c44e0}, 0x50?, 0xc000285f38)
            e7b8e02696a1">git.sr.ht/~sircmpwn/core-go@v0.0.0-20231129165057-e7b8e02696a1/database/middleware.go:74 +0x2ce
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.fetchUsersByID.func1({0xc0002cf190, 0x1, 0x1})
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/middleware.go:34 +0x118
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).end(...)
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:222
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).startTimer(0xc00044a360, 0xc00042d380)
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:218 +0xe7
    created by git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).keyIndex
            git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:191 +0x119
e7b8e026 — Simon Ser 2 years ago
database: print both errors in WithTx

Make it easier to debug panics by printing both errors instead of
sending one of them to /dev/null.
7f5f7071 — Simon Ser 2 years ago
database/middleware: fix error value used in panic

We were checking txErr, but panic'ing with err.

Fixes: a06a6247898c ("database: don't panic with ErrBadConn")
72c2f060 — Simon Ser 3 years ago
database: specify field name for graphql.CollectedField

Newer versions of the graphql package add new fields to this struct.
This results in errors:

    database/ql.go:40:4: too few values in struct literal of type graphql.CollectedField
    database/ql.go:71:4: too few values in struct literal of type graphql.CollectedField
a06a6247 — Simon Ser 3 years ago
database: don't panic with ErrBadConn

Sometimes we're panic'ing like so:

    panic: driver: bad connection

    goroutine 9333 [running]:
    git.sr.ht/~sircmpwn/core-go/database.WithTx({0x561c2e623478, 0xc0002b6ed0}, 0x50?, 0xc00025ff38)
    	65b1657b30a1">git.sr.ht/~sircmpwn/core-go@v0.0.0-20230816134313-65b1657b30a1/database/middleware.go:57 +0x1fc
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.fetchUsersByID.func1({0xc0002d80f0, 0x1, 0x1})
    	git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/middleware.go:34 +0x118
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).end(...)
    	git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:222
    git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).startTimer(0xc0004e5380, 0xc0000e5140)
    	git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:218 +0xe7
    created by git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders.(*usersByIDLoaderBatch).keyIndex
    	git.sr.ht/~sircmpwn/builds.sr.ht/api/loaders/usersbyidloader_gen.go:191 +0x119

I believe this is a pq bug, see the linked bug report. Stop
panic'ing in that case.
43a2be15 — Simon Ser 3 years ago
database/middleware: don't use sql.Conn in WithTx

We can just use sql.DB instead of sql.Conn here. Simplifies the
code and reduces the risk of getting "bad connection" errors.
209e1a86 — Simon Ser 3 years ago
database/middleware: do not recover/panic in defer

defer always runs after a panic. There is no need to recover and
re-panic, we can just unconditionally tx.Rollback() (it will
silently error out if the transaction has already been committed or
rolled back).

This fixes the stack trace of the panic being incorrect. It points
to this function instead of the real culprit.
8110e635 — Conrad Hoffmann 3 years ago
database: add helpers to fetch all columns

The current solution of fetching columns based on the GraphQL context
has some limits. While probably not the solution for all use-cases, it
sometimes can be desirable to simply fetch all columns from the database
when retrieving objects.

This commit adds two simple functions doing just that. They can be used
when building SQL queries, like

    query := database.SelectAll(new(model.Email))

and

    rows.Scan(database.ScanAll(&email)...)
33bc768c — Adnan Maolood 4 years ago
database: Remove Apply
e7ae287d — Simon Ser 4 years ago
database: always include table name in SELECT

When JOINs are performed on the auto-generated SELECT query,
multiple tables may have the same field names. For instance, in
lists.sr.ht the "list" and "subscription" tables both have an "id"
column. When performing a "mailingLists" GraphQL query this results
in this error:

    pq: column reference "id" is ambiguous

The old query looks like this:

    SELECT "description", "name", "id", "owner_id", "updated", COALESCE(
    access.permissions,
    CASE WHEN list.owner_id = $1
    THEN $2
    ELSE CASE WHEN sub.id IS NOT NULL
    	THEN list.subscriber_permissions
    	ELSE null END
    END,
    list.nonsubscriber_permissions | list.account_permissions), access.id AS access_id, sub.id AS subscription_id FROM list LEFT JOIN access ON
    access.list_id = list.id AND
    access.user_id = $3 LEFT JOIN subscription sub ON
    sub.list_id = list.id AND
    sub.user_id = $4 WHERE list.owner_id = $5 ORDER BY "updated" DESC LIMIT 26

The new query looks like this:

    SELECT "description", "name", "list"."id", "list"."owner_id", "list"."updated", COALESCE(
    access.permissions,
    CASE WHEN list.owner_id = $1
    THEN $2
    ELSE CASE WHEN sub.id IS NOT NULL
    	THEN list.subscriber_permissions
    	ELSE null END
    END,
    list.nonsubscriber_permissions | list.account_permissions), access.id AS access_id, sub.id AS subscription_id FROM list LEFT JOIN access ON
    access.list_id = list.id AND
    access.user_id = $3 LEFT JOIN subscription sub ON
    sub.list_id = list.id AND
    sub.user_id = $4 WHERE list.owner_id = $5 ORDER BY "updated" DESC LIMIT 26
7ccaeb92 — Drew DeVault 5 years ago
database: panic on errors from Commit/Rollback

The previous change would eat the error from fn(tx); in truth we should
ensure that it is returned to the caller. This panics instead if an
error occurs during Commit or Rollback. I'm not sure of what scenarios
would cause this to occur, but they all probably deserve further
investigation - a panic will raise it to our attention.
a7d80e21 — Drew DeVault 5 years ago
database: handle Commit/Rollback errors
07056613 — Drew DeVault 5 years ago
database: expand field map for composite fields

This allows models to fetch multiple SQL columns to obtain the data
necessary to compute a single composite GraphQL field, while still
avoiding unnecessary fetches if those fields are not queried.
4cc9b34f — Drew DeVault 5 years ago
database.Apply: add support for non-pointer fields
33562018 — Drew DeVault 5 years ago
database.WithTx: rollback if func returns error

Previously, this only rolled back if the called function panicked. This
updates it to also roll back if the called function returns an error.
af2afebd — Drew DeVault 5 years ago
Add legacy webhooks worker implementation
14f4b921 — Drew DeVault 5 years ago
Expand context for work queues
Next