feat: postgres data source
This commit is contained in:
+1
-1
@@ -1,6 +1,6 @@
|
||||
HTTP_ADDR=:8080
|
||||
APP_ENV=production
|
||||
DATABASE_PATH=./minecraft-auth.db
|
||||
DATABASE_URL=postgres://postgres:postgres@localhost:5432/authenticator?sslmode=disable
|
||||
MINECRAFT_SESSION_TTL=5m
|
||||
MINECRAFT_SERVER_TOKENS=survival=replace-with-a-long-random-token
|
||||
MINECRAFT_LOGIN_URL=https://minecraft.csui.dev/api/v1/minecraft/login
|
||||
|
||||
+1
-2
@@ -1,2 +1 @@
|
||||
*.db*
|
||||
.env
|
||||
.env
|
||||
|
||||
@@ -4,19 +4,15 @@ go 1.27
|
||||
|
||||
require (
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/jackc/pgx/v5 v5.7.6
|
||||
github.com/joho/godotenv v1.5.1
|
||||
modernc.org/sqlite v1.38.2
|
||||
)
|
||||
|
||||
require (
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/ncruces/go-strftime v0.1.9 // indirect
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b // indirect
|
||||
golang.org/x/sys v0.34.0 // indirect
|
||||
modernc.org/libc v1.66.3 // indirect
|
||||
modernc.org/mathutil v1.7.1 // indirect
|
||||
modernc.org/memory v1.11.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
github.com/jackc/puddle/v2 v2.2.2 // indirect
|
||||
golang.org/x/crypto v0.37.0 // indirect
|
||||
golang.org/x/sync v0.15.0 // indirect
|
||||
golang.org/x/text v0.24.0 // indirect
|
||||
)
|
||||
|
||||
@@ -1,53 +1,32 @@
|
||||
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
|
||||
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e h1:ijClszYn+mADRFY17kjQEVQ1XRhq2/JR1M3sGqeJoxs=
|
||||
github.com/google/pprof v0.0.0-20250317173921-a4b03ec1a45e/go.mod h1:boTsfXsheKC2y+lKOCMpSfarhxDeIzfZG1jqGcPl3cA=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
|
||||
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
|
||||
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
|
||||
github.com/jackc/pgx/v5 v5.7.6 h1:rWQc5FwZSPX58r1OQmkuaNicxdmExaEz5A2DO2hUuTk=
|
||||
github.com/jackc/pgx/v5 v5.7.6/go.mod h1:aruU7o91Tc2q2cFp5h4uP3f6ztExVpyVv88Xl/8Vl8M=
|
||||
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
|
||||
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
|
||||
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
|
||||
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
|
||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
|
||||
github.com/ncruces/go-strftime v0.1.9 h1:bY0MQC28UADQmHmaF5dgpLmImcShSi2kHU9XLdhx/f4=
|
||||
github.com/ncruces/go-strftime v0.1.9/go.mod h1:Fwc5htZGVVkseilnfgOVb9mKy6w1naJmn9CehxcKcls=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
|
||||
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b h1:M2rDM6z3Fhozi9O7NWsxAkg/yqS/lQJ6PmkyIV3YP+o=
|
||||
golang.org/x/exp v0.0.0-20250620022241-b7579e27df2b/go.mod h1:3//PLf8L/X+8b4vuAfHzxeRUl04Adcb341+IGKfnqS8=
|
||||
golang.org/x/mod v0.25.0 h1:n7a+ZbQKQA/Ysbyb0/6IbB1H/X41mKgbhfv7AfG/44w=
|
||||
golang.org/x/mod v0.25.0/go.mod h1:IXM97Txy2VM4PJ3gI61r1YEk/gAj6zAHN3AdZt6S9Ww=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
|
||||
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
|
||||
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
|
||||
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
|
||||
golang.org/x/crypto v0.37.0 h1:kJNSjF/Xp7kU0iB2Z+9viTPMW4EqqsrywMXLJOOsXSE=
|
||||
golang.org/x/crypto v0.37.0/go.mod h1:vg+k43peMZ0pUMhYmVAWysMK35e6ioLh3wB8ZCAfbVc=
|
||||
golang.org/x/sync v0.15.0 h1:KWH3jNZsfyT6xfAfKiz6MRNmd46ByHDYaZ7KSkCtdW8=
|
||||
golang.org/x/sync v0.15.0/go.mod h1:1dzgHSNfp02xaA81J2MS99Qcpr2w7fw1gpm99rleRqA=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.34.0 h1:H5Y5sJ2L2JRdyv7ROF1he/lPdvFsd0mJHFw2ThKHxLA=
|
||||
golang.org/x/sys v0.34.0/go.mod h1:BJP2sWEmIv4KK5OTEluFJCKSidICx8ciO85XgH3Ak8k=
|
||||
golang.org/x/tools v0.34.0 h1:qIpSLOxeCYGg9TrcJokLBG4KFA6d795g0xkBkiESGlo=
|
||||
golang.org/x/tools v0.34.0/go.mod h1:pAP9OwEaY1CAW3HOmg3hLZC5Z0CCmzjAF2UQMSqNARg=
|
||||
modernc.org/cc/v4 v4.26.2 h1:991HMkLjJzYBIfha6ECZdjrIYz2/1ayr+FL8GN+CNzM=
|
||||
modernc.org/cc/v4 v4.26.2/go.mod h1:uVtb5OGqUKpoLWhqwNQo/8LwvoiEBLvZXIQ/SmO6mL0=
|
||||
modernc.org/ccgo/v4 v4.28.0 h1:rjznn6WWehKq7dG4JtLRKxb52Ecv8OUGah8+Z/SfpNU=
|
||||
modernc.org/ccgo/v4 v4.28.0/go.mod h1:JygV3+9AV6SmPhDasu4JgquwU81XAKLd3OKTUDNOiKE=
|
||||
modernc.org/fileutil v1.3.8 h1:qtzNm7ED75pd1C7WgAGcK4edm4fvhtBsEiI/0NQ54YM=
|
||||
modernc.org/fileutil v1.3.8/go.mod h1:HxmghZSZVAz/LXcMNwZPA/DRrQZEVP9VX0V4LQGQFOc=
|
||||
modernc.org/gc/v2 v2.6.5 h1:nyqdV8q46KvTpZlsw66kWqwXRHdjIlJOhG6kxiV/9xI=
|
||||
modernc.org/gc/v2 v2.6.5/go.mod h1:YgIahr1ypgfe7chRuJi2gD7DBQiKSLMPgBQe9oIiito=
|
||||
modernc.org/goabi0 v0.2.0 h1:HvEowk7LxcPd0eq6mVOAEMai46V+i7Jrj13t4AzuNks=
|
||||
modernc.org/goabi0 v0.2.0/go.mod h1:CEFRnnJhKvWT1c1JTI3Avm+tgOWbkOu5oPA8eH8LnMI=
|
||||
modernc.org/libc v1.66.3 h1:cfCbjTUcdsKyyZZfEUKfoHcP3S0Wkvz3jgSzByEWVCQ=
|
||||
modernc.org/libc v1.66.3/go.mod h1:XD9zO8kt59cANKvHPXpx7yS2ELPheAey0vjIuZOhOU8=
|
||||
modernc.org/mathutil v1.7.1 h1:GCZVGXdaN8gTqB1Mf/usp1Y/hSqgI2vAGGP4jZMCxOU=
|
||||
modernc.org/mathutil v1.7.1/go.mod h1:4p5IwJITfppl0G4sUEDtCr4DthTaT47/N3aT6MhfgJg=
|
||||
modernc.org/memory v1.11.0 h1:o4QC8aMQzmcwCK3t3Ux/ZHmwFPzE6hf2Y5LbkRs+hbI=
|
||||
modernc.org/memory v1.11.0/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw=
|
||||
modernc.org/opt v0.1.4 h1:2kNGMRiUjrp4LcaPuLY2PzUfqM/w9N23quVwhKt5Qm8=
|
||||
modernc.org/opt v0.1.4/go.mod h1:03fq9lsNfvkYSfxrfUhZCWPk1lm4cq4N+Bh//bEtgns=
|
||||
modernc.org/sortutil v1.2.1 h1:+xyoGf15mM3NMlPDnFqrteY07klSFxLElE2PVuWIJ7w=
|
||||
modernc.org/sortutil v1.2.1/go.mod h1:7ZI3a3REbai7gzCLcotuw9AC4VZVpYMjDzETGsSMqJE=
|
||||
modernc.org/sqlite v1.38.2 h1:Aclu7+tgjgcQVShZqim41Bbw9Cho0y/7WzYptXqkEek=
|
||||
modernc.org/sqlite v1.38.2/go.mod h1:cPTJYSlgg3Sfg046yBShXENNtPrWrDX8bsbAQBzgQ5E=
|
||||
modernc.org/strutil v1.2.1 h1:UneZBkQA+DX2Rp35KcM69cSsNES9ly8mQWD71HKlOA0=
|
||||
modernc.org/strutil v1.2.1/go.mod h1:EHkiggD70koQxjVdSBM3JKM7k6L0FbGE5eymy9i3B9A=
|
||||
modernc.org/token v1.1.0 h1:Xl7Ap9dKaEs5kLoOQeQmPWevfnk/DM5qcLcYlA8ys6Y=
|
||||
modernc.org/token v1.1.0/go.mod h1:UGzOrNV1mAFSEB63lOFHIpNRUVMvYTc6yu1SMY/XTDM=
|
||||
golang.org/x/text v0.24.0 h1:dd5Bzh4yt5KYA8f9CJHCP4FB4D51c2c6JvN37xJJkJ0=
|
||||
golang.org/x/text v0.24.0/go.mod h1:L8rBsPeo2pSS+xqN0d5u2ikmjtmoJbDBT1b7nHvFCdU=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||
|
||||
@@ -12,7 +12,7 @@ import (
|
||||
|
||||
type Config struct {
|
||||
AppEnv string
|
||||
DatabasePath string
|
||||
DatabaseURL string
|
||||
HTTPAddr string
|
||||
MinecraftLoginURL string
|
||||
CASLoginURL string
|
||||
@@ -30,7 +30,7 @@ func Load() (Config, error) {
|
||||
|
||||
cfg := Config{
|
||||
AppEnv: strings.TrimSpace(os.Getenv("APP_ENV")),
|
||||
DatabasePath: os.Getenv("DATABASE_PATH"),
|
||||
DatabaseURL: os.Getenv("DATABASE_URL"),
|
||||
HTTPAddr: os.Getenv("HTTP_ADDR"),
|
||||
MinecraftLoginURL: strings.TrimSpace(os.Getenv("MINECRAFT_LOGIN_URL")),
|
||||
CASLoginURL: strings.TrimSpace(os.Getenv("CAS_LOGIN_URL")),
|
||||
|
||||
@@ -6,10 +6,11 @@ import (
|
||||
_ "embed"
|
||||
"errors"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"oauth-mc-web/internal/database/repository"
|
||||
|
||||
_ "modernc.org/sqlite"
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
@@ -24,20 +25,21 @@ type TransactionRunner interface {
|
||||
//go:embed schema/schema.sql
|
||||
var schema string
|
||||
|
||||
func Open(ctx context.Context, path string) (*DB, error) {
|
||||
if path == "" {
|
||||
path = "minecraft-auth.db"
|
||||
func Open(ctx context.Context, databaseURL string) (*DB, error) {
|
||||
if databaseURL == "" {
|
||||
return nil, errors.New("DATABASE_URL is required")
|
||||
}
|
||||
db, err := sql.Open("sqlite", path)
|
||||
db, err := sql.Open("pgx", databaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open SQLite database: %w", err)
|
||||
return nil, fmt.Errorf("open PostgreSQL database: %w", err)
|
||||
}
|
||||
db.SetMaxOpenConns(1)
|
||||
if _, err := db.ExecContext(ctx, `PRAGMA journal_mode=WAL`); err != nil {
|
||||
return nil, closeWithError(db, fmt.Errorf("configure SQLite database: %w", err))
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, schema); err != nil {
|
||||
return nil, closeWithError(db, fmt.Errorf("initialize SQLite schema: %w", err))
|
||||
for _, statement := range strings.Split(schema, ";") {
|
||||
if strings.TrimSpace(statement) == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := db.ExecContext(ctx, statement); err != nil {
|
||||
return nil, closeWithError(db, fmt.Errorf("initialize PostgreSQL schema: %w", err))
|
||||
}
|
||||
}
|
||||
queries := repository.New(db)
|
||||
if err := queries.RestoreAccountBindings(ctx); err != nil {
|
||||
|
||||
@@ -5,17 +5,18 @@ import (
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"errors"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"oauth-mc-web/internal/database/repository"
|
||||
"oauth-mc-web/internal/database/testutil"
|
||||
)
|
||||
|
||||
//go:embed migrations/0002_uuid_account_bindings.sql
|
||||
var uuidAccountBindingsMigration string
|
||||
|
||||
func TestWithinTransaction(t *testing.T) {
|
||||
db, err := Open(context.Background(), filepath.Join(t.TempDir(), "test.db"))
|
||||
db, err := Open(context.Background(), testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -61,8 +62,8 @@ func TestWithinTransaction(t *testing.T) {
|
||||
|
||||
func TestOpenDoesNotRestoreUnlinkedBindingFromOldSessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
path := filepath.Join(t.TempDir(), "test.db")
|
||||
db, err := Open(ctx, path)
|
||||
databaseURL := testutil.URL(t)
|
||||
db, err := Open(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -132,7 +133,7 @@ func TestOpenDoesNotRestoreUnlinkedBindingFromOldSessions(t *testing.T) {
|
||||
if err := db.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db, err = Open(ctx, path)
|
||||
db, err = Open(ctx, databaseURL)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -149,7 +150,7 @@ func TestOpenDoesNotRestoreUnlinkedBindingFromOldSessions(t *testing.T) {
|
||||
|
||||
func TestUUIDAccountBindingsMigrationBackfillsRetainedSessions(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := Open(ctx, filepath.Join(t.TempDir(), "test.db"))
|
||||
db, err := Open(ctx, testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -188,8 +189,13 @@ func TestUUIDAccountBindingsMigrationBackfillsRetainedSessions(t *testing.T) {
|
||||
if err != nil || updated != 1 {
|
||||
t.Fatalf("approve legacy session: updated=%d err=%v", updated, err)
|
||||
}
|
||||
if _, err := db.sql.ExecContext(ctx, uuidAccountBindingsMigration); err != nil {
|
||||
t.Fatalf("apply data migration: %v", err)
|
||||
for _, statement := range strings.Split(uuidAccountBindingsMigration, ";") {
|
||||
if strings.TrimSpace(statement) == "" {
|
||||
continue
|
||||
}
|
||||
if _, err := db.sql.ExecContext(ctx, statement); err != nil {
|
||||
t.Fatalf("apply data migration: %v", err)
|
||||
}
|
||||
}
|
||||
if _, err := db.Queries.GetBoundAccountForUUID(ctx, repository.GetBoundAccountForUUIDParams{
|
||||
ServerID: "server-a",
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
INSERT OR IGNORE INTO minecraft_uuid_account_bindings(
|
||||
INSERT INTO minecraft_uuid_account_bindings(
|
||||
server_id,
|
||||
minecraft_uuid,
|
||||
minecraft_name_key,
|
||||
@@ -19,9 +19,10 @@ JOIN minecraft_account_bindings AS bindings
|
||||
AND bindings.account_subject = lower(trim(sessions.account_subject))
|
||||
WHERE sessions.status = 'approved'
|
||||
AND sessions.account_subject <> ''
|
||||
ORDER BY sessions.expires_at DESC;
|
||||
ORDER BY sessions.expires_at DESC
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
INSERT OR IGNORE INTO minecraft_login_requests(
|
||||
INSERT INTO minecraft_login_requests(
|
||||
server_id,
|
||||
request_id,
|
||||
minecraft_uuid,
|
||||
@@ -37,4 +38,5 @@ SELECT
|
||||
login_url,
|
||||
expires_at
|
||||
FROM minecraft_sessions
|
||||
ORDER BY expires_at DESC;
|
||||
ORDER BY expires_at DESC
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
# Database data migrations
|
||||
|
||||
The application creates the current tables when it opens SQLite. Before upgrading an existing database to UUID-based Minecraft account links and durable login request deduplication, apply `0002_uuid_account_bindings.sql` once after the new tables have been created. Do not apply it automatically at startup.
|
||||
The application creates the current tables when it opens PostgreSQL. When upgrading an older database to UUID-based Minecraft account links and durable login request deduplication, apply `0002_uuid_account_bindings.sql` once after the new tables have been created. Do not apply it automatically at startup.
|
||||
|
||||
The old schema associated accounts with Minecraft names. This migration can recover UUID links only from approved sessions that are still present and match an existing name binding. Links whose session history has already been pruned cannot be mapped reliably; those players need to run `/login` once to establish a UUID link before `/unlink` can remove it.
|
||||
|
||||
@@ -1,99 +1,100 @@
|
||||
-- name: RestoreAccountBindings :exec
|
||||
INSERT OR IGNORE INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
SELECT minecraft_name_key, minecraft_name, account_subject, created_at FROM minecraft_uuid_account_bindings
|
||||
ORDER BY created_at DESC;
|
||||
ORDER BY created_at DESC
|
||||
ON CONFLICT DO NOTHING;
|
||||
|
||||
-- name: ExpirePendingSessions :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id
|
||||
WHERE status='pending' AND expires_at<=?;
|
||||
WHERE status='pending' AND expires_at<=$1;
|
||||
|
||||
-- name: ListExpiredSessions :many
|
||||
SELECT session_id, minecraft_uuid FROM minecraft_sessions WHERE status='expired';
|
||||
|
||||
-- name: DeleteClosedSessions :exec
|
||||
DELETE FROM minecraft_sessions WHERE status IN ('approved','denied','expired') AND expires_at<=?;
|
||||
DELETE FROM minecraft_sessions WHERE status IN ('approved','denied','expired') AND expires_at<=$1;
|
||||
|
||||
-- name: AcknowledgeSessionResult :execrows
|
||||
DELETE FROM minecraft_sessions WHERE session_id=? AND server_id=? AND status IN ('approved','denied','expired');
|
||||
DELETE FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2 AND status IN ('approved','denied','expired');
|
||||
|
||||
-- name: CountActivePendingSessions :one
|
||||
SELECT COUNT(*) FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid<>? AND status='pending' AND expires_at>?;
|
||||
SELECT COUNT(*) FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid<>$2 AND status='pending' AND expires_at>$3;
|
||||
|
||||
-- name: ListPendingSessionIDsForPlayer :many
|
||||
SELECT session_id FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid=? AND status='pending';
|
||||
SELECT session_id FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid=$2 AND status='pending';
|
||||
|
||||
-- name: DeletePendingSessionsForPlayer :exec
|
||||
DELETE FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid=? AND status='pending';
|
||||
DELETE FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid=$2 AND status='pending';
|
||||
|
||||
-- name: InsertSession :exec
|
||||
INSERT INTO minecraft_sessions(session_id, request_id, server_id, minecraft_uuid, minecraft_name, code, login_url, expires_at, status)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, 'pending');
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, 'pending');
|
||||
|
||||
-- name: GetLoginRequest :one
|
||||
SELECT minecraft_uuid, session_id, login_url, expires_at
|
||||
FROM minecraft_login_requests WHERE server_id=? AND request_id=?;
|
||||
FROM minecraft_login_requests WHERE server_id=$1 AND request_id=$2;
|
||||
|
||||
-- name: InsertLoginRequest :exec
|
||||
INSERT INTO minecraft_login_requests(server_id, request_id, minecraft_uuid, session_id, login_url, expires_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?);
|
||||
VALUES($1, $2, $3, $4, $5, $6);
|
||||
|
||||
-- name: CancelPendingSession :execrows
|
||||
DELETE FROM minecraft_sessions WHERE session_id=? AND server_id=? AND minecraft_uuid=? AND status='pending';
|
||||
DELETE FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2 AND minecraft_uuid=$3 AND status='pending';
|
||||
|
||||
-- name: GetSessionForReplay :one
|
||||
SELECT request_id, minecraft_uuid, minecraft_name, login_url, expires_at, status, account_subject, account_display_name
|
||||
FROM minecraft_sessions WHERE session_id=? AND server_id=?;
|
||||
FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2;
|
||||
|
||||
-- name: ExpirePendingSession :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE session_id=? AND status='pending';
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE session_id=$1 AND status='pending';
|
||||
|
||||
-- name: ExpirePendingSessionsForServer :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE server_id=? AND status='pending' AND expires_at<=?;
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE server_id=$1 AND status='pending' AND expires_at<=$2;
|
||||
|
||||
-- name: DeleteClosedSessionsForServer :exec
|
||||
DELETE FROM minecraft_sessions WHERE server_id=? AND status IN ('approved','denied','expired') AND expires_at<=?;
|
||||
DELETE FROM minecraft_sessions WHERE server_id=$1 AND status IN ('approved','denied','expired') AND expires_at<=$2;
|
||||
|
||||
-- name: GetSessionResult :one
|
||||
SELECT minecraft_uuid, status, account_subject, account_display_name FROM minecraft_sessions WHERE session_id=? AND server_id=?;
|
||||
SELECT minecraft_uuid, status, account_subject, account_display_name FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2;
|
||||
|
||||
-- name: GetPendingSessionExpiry :one
|
||||
SELECT expires_at FROM minecraft_sessions WHERE code=? AND status='pending';
|
||||
SELECT expires_at FROM minecraft_sessions WHERE code=$1 AND status='pending';
|
||||
|
||||
-- name: GetPendingSessionByCode :one
|
||||
SELECT session_id, server_id, minecraft_uuid, minecraft_name, expires_at FROM minecraft_sessions WHERE code=? AND status='pending';
|
||||
SELECT session_id, server_id, minecraft_uuid, minecraft_name, expires_at FROM minecraft_sessions WHERE code=$1 AND status='pending';
|
||||
|
||||
-- name: DenyPendingSession :execrows
|
||||
UPDATE minecraft_sessions SET status='denied', code='closed-'||session_id WHERE session_id=? AND status='pending';
|
||||
UPDATE minecraft_sessions SET status='denied', code='closed-'||session_id WHERE session_id=$1 AND status='pending';
|
||||
|
||||
-- name: GetBoundSubjectForName :one
|
||||
SELECT account_subject FROM minecraft_account_bindings WHERE minecraft_name_key=?;
|
||||
SELECT account_subject FROM minecraft_account_bindings WHERE minecraft_name_key=$1;
|
||||
|
||||
-- name: GetBoundNameForSubject :one
|
||||
SELECT minecraft_name_key FROM minecraft_account_bindings WHERE account_subject=?;
|
||||
SELECT minecraft_name_key FROM minecraft_account_bindings WHERE account_subject=$1;
|
||||
|
||||
-- name: GetBoundAccountForUUID :one
|
||||
SELECT minecraft_name_key, account_subject FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?;
|
||||
SELECT minecraft_name_key, account_subject FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2;
|
||||
|
||||
-- name: GetBoundSubjectForUUID :one
|
||||
SELECT account_subject FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?;
|
||||
SELECT account_subject FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2;
|
||||
|
||||
-- name: GetBoundUUIDForSubject :one
|
||||
SELECT minecraft_uuid FROM minecraft_uuid_account_bindings WHERE server_id=? AND account_subject=?;
|
||||
SELECT minecraft_uuid FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND account_subject=$2;
|
||||
|
||||
-- name: InsertAccountBinding :exec
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at) VALUES(?, ?, ?, ?);
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at) VALUES($1, $2, $3, $4);
|
||||
|
||||
-- name: UpsertUUIDAccountBinding :exec
|
||||
INSERT INTO minecraft_uuid_account_bindings(server_id, minecraft_uuid, minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
VALUES($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT(server_id, minecraft_uuid) DO UPDATE SET minecraft_name_key=excluded.minecraft_name_key, minecraft_name=excluded.minecraft_name, created_at=excluded.created_at;
|
||||
|
||||
-- name: DeleteUUIDAccountBinding :execrows
|
||||
DELETE FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?;
|
||||
DELETE FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2;
|
||||
|
||||
-- name: DeleteUnusedAccountBinding :execrows
|
||||
DELETE FROM minecraft_account_bindings
|
||||
WHERE minecraft_account_bindings.minecraft_name_key=? AND minecraft_account_bindings.account_subject=?
|
||||
WHERE minecraft_account_bindings.minecraft_name_key=$1 AND minecraft_account_bindings.account_subject=$2
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM minecraft_uuid_account_bindings
|
||||
WHERE minecraft_uuid_account_bindings.minecraft_name_key=minecraft_account_bindings.minecraft_name_key
|
||||
@@ -101,14 +102,14 @@ AND NOT EXISTS (
|
||||
);
|
||||
|
||||
-- name: GetUnlinkRequest :one
|
||||
SELECT minecraft_uuid, status FROM minecraft_unlink_requests WHERE server_id=? AND request_id=?;
|
||||
SELECT minecraft_uuid, status FROM minecraft_unlink_requests WHERE server_id=$1 AND request_id=$2;
|
||||
|
||||
-- name: InsertUnlinkRequest :exec
|
||||
INSERT INTO minecraft_unlink_requests(server_id, request_id, minecraft_uuid, status, created_at) VALUES(?, ?, ?, ?, ?);
|
||||
INSERT INTO minecraft_unlink_requests(server_id, request_id, minecraft_uuid, status, created_at) VALUES($1, $2, $3, $4, $5);
|
||||
|
||||
-- name: ApproveSession :execrows
|
||||
UPDATE minecraft_sessions SET status='approved', code='closed-'||session_id, account_subject=?, account_display_name=?
|
||||
WHERE session_id=? AND server_id=? AND status='pending' AND expires_at>?;
|
||||
UPDATE minecraft_sessions SET status='approved', code='closed-'||session_id, account_subject=$1, account_display_name=$2
|
||||
WHERE session_id=$3 AND server_id=$4 AND status='pending' AND expires_at>$5;
|
||||
|
||||
-- name: GetSessionServerID :one
|
||||
SELECT server_id FROM minecraft_sessions WHERE session_id=?;
|
||||
SELECT server_id FROM minecraft_sessions WHERE session_id=$1;
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
)
|
||||
|
||||
const acknowledgeSessionResult = `-- name: AcknowledgeSessionResult :execrows
|
||||
DELETE FROM minecraft_sessions WHERE session_id=? AND server_id=? AND status IN ('approved','denied','expired')
|
||||
DELETE FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2 AND status IN ('approved','denied','expired')
|
||||
`
|
||||
|
||||
type AcknowledgeSessionResultParams struct {
|
||||
@@ -27,8 +27,8 @@ func (q *Queries) AcknowledgeSessionResult(ctx context.Context, arg AcknowledgeS
|
||||
}
|
||||
|
||||
const approveSession = `-- name: ApproveSession :execrows
|
||||
UPDATE minecraft_sessions SET status='approved', code='closed-'||session_id, account_subject=?, account_display_name=?
|
||||
WHERE session_id=? AND server_id=? AND status='pending' AND expires_at>?
|
||||
UPDATE minecraft_sessions SET status='approved', code='closed-'||session_id, account_subject=$1, account_display_name=$2
|
||||
WHERE session_id=$3 AND server_id=$4 AND status='pending' AND expires_at>$5
|
||||
`
|
||||
|
||||
type ApproveSessionParams struct {
|
||||
@@ -54,7 +54,7 @@ func (q *Queries) ApproveSession(ctx context.Context, arg ApproveSessionParams)
|
||||
}
|
||||
|
||||
const cancelPendingSession = `-- name: CancelPendingSession :execrows
|
||||
DELETE FROM minecraft_sessions WHERE session_id=? AND server_id=? AND minecraft_uuid=? AND status='pending'
|
||||
DELETE FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2 AND minecraft_uuid=$3 AND status='pending'
|
||||
`
|
||||
|
||||
type CancelPendingSessionParams struct {
|
||||
@@ -72,7 +72,7 @@ func (q *Queries) CancelPendingSession(ctx context.Context, arg CancelPendingSes
|
||||
}
|
||||
|
||||
const countActivePendingSessions = `-- name: CountActivePendingSessions :one
|
||||
SELECT COUNT(*) FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid<>? AND status='pending' AND expires_at>?
|
||||
SELECT COUNT(*) FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid<>$2 AND status='pending' AND expires_at>$3
|
||||
`
|
||||
|
||||
type CountActivePendingSessionsParams struct {
|
||||
@@ -89,7 +89,7 @@ func (q *Queries) CountActivePendingSessions(ctx context.Context, arg CountActiv
|
||||
}
|
||||
|
||||
const deleteClosedSessions = `-- name: DeleteClosedSessions :exec
|
||||
DELETE FROM minecraft_sessions WHERE status IN ('approved','denied','expired') AND expires_at<=?
|
||||
DELETE FROM minecraft_sessions WHERE status IN ('approved','denied','expired') AND expires_at<=$1
|
||||
`
|
||||
|
||||
func (q *Queries) DeleteClosedSessions(ctx context.Context, expiresAt string) error {
|
||||
@@ -98,7 +98,7 @@ func (q *Queries) DeleteClosedSessions(ctx context.Context, expiresAt string) er
|
||||
}
|
||||
|
||||
const deleteClosedSessionsForServer = `-- name: DeleteClosedSessionsForServer :exec
|
||||
DELETE FROM minecraft_sessions WHERE server_id=? AND status IN ('approved','denied','expired') AND expires_at<=?
|
||||
DELETE FROM minecraft_sessions WHERE server_id=$1 AND status IN ('approved','denied','expired') AND expires_at<=$2
|
||||
`
|
||||
|
||||
type DeleteClosedSessionsForServerParams struct {
|
||||
@@ -112,7 +112,7 @@ func (q *Queries) DeleteClosedSessionsForServer(ctx context.Context, arg DeleteC
|
||||
}
|
||||
|
||||
const deletePendingSessionsForPlayer = `-- name: DeletePendingSessionsForPlayer :exec
|
||||
DELETE FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid=? AND status='pending'
|
||||
DELETE FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid=$2 AND status='pending'
|
||||
`
|
||||
|
||||
type DeletePendingSessionsForPlayerParams struct {
|
||||
@@ -126,7 +126,7 @@ func (q *Queries) DeletePendingSessionsForPlayer(ctx context.Context, arg Delete
|
||||
}
|
||||
|
||||
const deleteUUIDAccountBinding = `-- name: DeleteUUIDAccountBinding :execrows
|
||||
DELETE FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?
|
||||
DELETE FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2
|
||||
`
|
||||
|
||||
type DeleteUUIDAccountBindingParams struct {
|
||||
@@ -144,7 +144,7 @@ func (q *Queries) DeleteUUIDAccountBinding(ctx context.Context, arg DeleteUUIDAc
|
||||
|
||||
const deleteUnusedAccountBinding = `-- name: DeleteUnusedAccountBinding :execrows
|
||||
DELETE FROM minecraft_account_bindings
|
||||
WHERE minecraft_account_bindings.minecraft_name_key=? AND minecraft_account_bindings.account_subject=?
|
||||
WHERE minecraft_account_bindings.minecraft_name_key=$1 AND minecraft_account_bindings.account_subject=$2
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM minecraft_uuid_account_bindings
|
||||
WHERE minecraft_uuid_account_bindings.minecraft_name_key=minecraft_account_bindings.minecraft_name_key
|
||||
@@ -166,7 +166,7 @@ func (q *Queries) DeleteUnusedAccountBinding(ctx context.Context, arg DeleteUnus
|
||||
}
|
||||
|
||||
const denyPendingSession = `-- name: DenyPendingSession :execrows
|
||||
UPDATE minecraft_sessions SET status='denied', code='closed-'||session_id WHERE session_id=? AND status='pending'
|
||||
UPDATE minecraft_sessions SET status='denied', code='closed-'||session_id WHERE session_id=$1 AND status='pending'
|
||||
`
|
||||
|
||||
func (q *Queries) DenyPendingSession(ctx context.Context, sessionID string) (int64, error) {
|
||||
@@ -178,7 +178,7 @@ func (q *Queries) DenyPendingSession(ctx context.Context, sessionID string) (int
|
||||
}
|
||||
|
||||
const expirePendingSession = `-- name: ExpirePendingSession :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE session_id=? AND status='pending'
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE session_id=$1 AND status='pending'
|
||||
`
|
||||
|
||||
func (q *Queries) ExpirePendingSession(ctx context.Context, sessionID string) error {
|
||||
@@ -188,7 +188,7 @@ func (q *Queries) ExpirePendingSession(ctx context.Context, sessionID string) er
|
||||
|
||||
const expirePendingSessions = `-- name: ExpirePendingSessions :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id
|
||||
WHERE status='pending' AND expires_at<=?
|
||||
WHERE status='pending' AND expires_at<=$1
|
||||
`
|
||||
|
||||
func (q *Queries) ExpirePendingSessions(ctx context.Context, expiresAt string) error {
|
||||
@@ -197,7 +197,7 @@ func (q *Queries) ExpirePendingSessions(ctx context.Context, expiresAt string) e
|
||||
}
|
||||
|
||||
const expirePendingSessionsForServer = `-- name: ExpirePendingSessionsForServer :exec
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE server_id=? AND status='pending' AND expires_at<=?
|
||||
UPDATE minecraft_sessions SET status='expired', code='closed-'||session_id WHERE server_id=$1 AND status='pending' AND expires_at<=$2
|
||||
`
|
||||
|
||||
type ExpirePendingSessionsForServerParams struct {
|
||||
@@ -211,7 +211,7 @@ func (q *Queries) ExpirePendingSessionsForServer(ctx context.Context, arg Expire
|
||||
}
|
||||
|
||||
const getBoundAccountForUUID = `-- name: GetBoundAccountForUUID :one
|
||||
SELECT minecraft_name_key, account_subject FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?
|
||||
SELECT minecraft_name_key, account_subject FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2
|
||||
`
|
||||
|
||||
type GetBoundAccountForUUIDParams struct {
|
||||
@@ -232,7 +232,7 @@ func (q *Queries) GetBoundAccountForUUID(ctx context.Context, arg GetBoundAccoun
|
||||
}
|
||||
|
||||
const getBoundNameForSubject = `-- name: GetBoundNameForSubject :one
|
||||
SELECT minecraft_name_key FROM minecraft_account_bindings WHERE account_subject=?
|
||||
SELECT minecraft_name_key FROM minecraft_account_bindings WHERE account_subject=$1
|
||||
`
|
||||
|
||||
func (q *Queries) GetBoundNameForSubject(ctx context.Context, accountSubject string) (string, error) {
|
||||
@@ -243,7 +243,7 @@ func (q *Queries) GetBoundNameForSubject(ctx context.Context, accountSubject str
|
||||
}
|
||||
|
||||
const getBoundSubjectForName = `-- name: GetBoundSubjectForName :one
|
||||
SELECT account_subject FROM minecraft_account_bindings WHERE minecraft_name_key=?
|
||||
SELECT account_subject FROM minecraft_account_bindings WHERE minecraft_name_key=$1
|
||||
`
|
||||
|
||||
func (q *Queries) GetBoundSubjectForName(ctx context.Context, minecraftNameKey string) (string, error) {
|
||||
@@ -254,7 +254,7 @@ func (q *Queries) GetBoundSubjectForName(ctx context.Context, minecraftNameKey s
|
||||
}
|
||||
|
||||
const getBoundSubjectForUUID = `-- name: GetBoundSubjectForUUID :one
|
||||
SELECT account_subject FROM minecraft_uuid_account_bindings WHERE server_id=? AND minecraft_uuid=?
|
||||
SELECT account_subject FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND minecraft_uuid=$2
|
||||
`
|
||||
|
||||
type GetBoundSubjectForUUIDParams struct {
|
||||
@@ -270,7 +270,7 @@ func (q *Queries) GetBoundSubjectForUUID(ctx context.Context, arg GetBoundSubjec
|
||||
}
|
||||
|
||||
const getBoundUUIDForSubject = `-- name: GetBoundUUIDForSubject :one
|
||||
SELECT minecraft_uuid FROM minecraft_uuid_account_bindings WHERE server_id=? AND account_subject=?
|
||||
SELECT minecraft_uuid FROM minecraft_uuid_account_bindings WHERE server_id=$1 AND account_subject=$2
|
||||
`
|
||||
|
||||
type GetBoundUUIDForSubjectParams struct {
|
||||
@@ -287,7 +287,7 @@ func (q *Queries) GetBoundUUIDForSubject(ctx context.Context, arg GetBoundUUIDFo
|
||||
|
||||
const getLoginRequest = `-- name: GetLoginRequest :one
|
||||
SELECT minecraft_uuid, session_id, login_url, expires_at
|
||||
FROM minecraft_login_requests WHERE server_id=? AND request_id=?
|
||||
FROM minecraft_login_requests WHERE server_id=$1 AND request_id=$2
|
||||
`
|
||||
|
||||
type GetLoginRequestParams struct {
|
||||
@@ -315,7 +315,7 @@ func (q *Queries) GetLoginRequest(ctx context.Context, arg GetLoginRequestParams
|
||||
}
|
||||
|
||||
const getPendingSessionByCode = `-- name: GetPendingSessionByCode :one
|
||||
SELECT session_id, server_id, minecraft_uuid, minecraft_name, expires_at FROM minecraft_sessions WHERE code=? AND status='pending'
|
||||
SELECT session_id, server_id, minecraft_uuid, minecraft_name, expires_at FROM minecraft_sessions WHERE code=$1 AND status='pending'
|
||||
`
|
||||
|
||||
type GetPendingSessionByCodeRow struct {
|
||||
@@ -340,7 +340,7 @@ func (q *Queries) GetPendingSessionByCode(ctx context.Context, code string) (Get
|
||||
}
|
||||
|
||||
const getPendingSessionExpiry = `-- name: GetPendingSessionExpiry :one
|
||||
SELECT expires_at FROM minecraft_sessions WHERE code=? AND status='pending'
|
||||
SELECT expires_at FROM minecraft_sessions WHERE code=$1 AND status='pending'
|
||||
`
|
||||
|
||||
func (q *Queries) GetPendingSessionExpiry(ctx context.Context, code string) (string, error) {
|
||||
@@ -352,7 +352,7 @@ func (q *Queries) GetPendingSessionExpiry(ctx context.Context, code string) (str
|
||||
|
||||
const getSessionForReplay = `-- name: GetSessionForReplay :one
|
||||
SELECT request_id, minecraft_uuid, minecraft_name, login_url, expires_at, status, account_subject, account_display_name
|
||||
FROM minecraft_sessions WHERE session_id=? AND server_id=?
|
||||
FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2
|
||||
`
|
||||
|
||||
type GetSessionForReplayParams struct {
|
||||
@@ -388,7 +388,7 @@ func (q *Queries) GetSessionForReplay(ctx context.Context, arg GetSessionForRepl
|
||||
}
|
||||
|
||||
const getSessionResult = `-- name: GetSessionResult :one
|
||||
SELECT minecraft_uuid, status, account_subject, account_display_name FROM minecraft_sessions WHERE session_id=? AND server_id=?
|
||||
SELECT minecraft_uuid, status, account_subject, account_display_name FROM minecraft_sessions WHERE session_id=$1 AND server_id=$2
|
||||
`
|
||||
|
||||
type GetSessionResultParams struct {
|
||||
@@ -416,7 +416,7 @@ func (q *Queries) GetSessionResult(ctx context.Context, arg GetSessionResultPara
|
||||
}
|
||||
|
||||
const getSessionServerID = `-- name: GetSessionServerID :one
|
||||
SELECT server_id FROM minecraft_sessions WHERE session_id=?
|
||||
SELECT server_id FROM minecraft_sessions WHERE session_id=$1
|
||||
`
|
||||
|
||||
func (q *Queries) GetSessionServerID(ctx context.Context, sessionID string) (string, error) {
|
||||
@@ -427,7 +427,7 @@ func (q *Queries) GetSessionServerID(ctx context.Context, sessionID string) (str
|
||||
}
|
||||
|
||||
const getUnlinkRequest = `-- name: GetUnlinkRequest :one
|
||||
SELECT minecraft_uuid, status FROM minecraft_unlink_requests WHERE server_id=? AND request_id=?
|
||||
SELECT minecraft_uuid, status FROM minecraft_unlink_requests WHERE server_id=$1 AND request_id=$2
|
||||
`
|
||||
|
||||
type GetUnlinkRequestParams struct {
|
||||
@@ -448,7 +448,7 @@ func (q *Queries) GetUnlinkRequest(ctx context.Context, arg GetUnlinkRequestPara
|
||||
}
|
||||
|
||||
const insertAccountBinding = `-- name: InsertAccountBinding :exec
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at) VALUES(?, ?, ?, ?)
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at) VALUES($1, $2, $3, $4)
|
||||
`
|
||||
|
||||
type InsertAccountBindingParams struct {
|
||||
@@ -470,7 +470,7 @@ func (q *Queries) InsertAccountBinding(ctx context.Context, arg InsertAccountBin
|
||||
|
||||
const insertLoginRequest = `-- name: InsertLoginRequest :exec
|
||||
INSERT INTO minecraft_login_requests(server_id, request_id, minecraft_uuid, session_id, login_url, expires_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
VALUES($1, $2, $3, $4, $5, $6)
|
||||
`
|
||||
|
||||
type InsertLoginRequestParams struct {
|
||||
@@ -496,7 +496,7 @@ func (q *Queries) InsertLoginRequest(ctx context.Context, arg InsertLoginRequest
|
||||
|
||||
const insertSession = `-- name: InsertSession :exec
|
||||
INSERT INTO minecraft_sessions(session_id, request_id, server_id, minecraft_uuid, minecraft_name, code, login_url, expires_at, status)
|
||||
VALUES(?, ?, ?, ?, ?, ?, ?, ?, 'pending')
|
||||
VALUES($1, $2, $3, $4, $5, $6, $7, $8, 'pending')
|
||||
`
|
||||
|
||||
type InsertSessionParams struct {
|
||||
@@ -525,7 +525,7 @@ func (q *Queries) InsertSession(ctx context.Context, arg InsertSessionParams) er
|
||||
}
|
||||
|
||||
const insertUnlinkRequest = `-- name: InsertUnlinkRequest :exec
|
||||
INSERT INTO minecraft_unlink_requests(server_id, request_id, minecraft_uuid, status, created_at) VALUES(?, ?, ?, ?, ?)
|
||||
INSERT INTO minecraft_unlink_requests(server_id, request_id, minecraft_uuid, status, created_at) VALUES($1, $2, $3, $4, $5)
|
||||
`
|
||||
|
||||
type InsertUnlinkRequestParams struct {
|
||||
@@ -580,7 +580,7 @@ func (q *Queries) ListExpiredSessions(ctx context.Context) ([]ListExpiredSession
|
||||
}
|
||||
|
||||
const listPendingSessionIDsForPlayer = `-- name: ListPendingSessionIDsForPlayer :many
|
||||
SELECT session_id FROM minecraft_sessions WHERE server_id=? AND minecraft_uuid=? AND status='pending'
|
||||
SELECT session_id FROM minecraft_sessions WHERE server_id=$1 AND minecraft_uuid=$2 AND status='pending'
|
||||
`
|
||||
|
||||
type ListPendingSessionIDsForPlayerParams struct {
|
||||
@@ -612,9 +612,10 @@ func (q *Queries) ListPendingSessionIDsForPlayer(ctx context.Context, arg ListPe
|
||||
}
|
||||
|
||||
const restoreAccountBindings = `-- name: RestoreAccountBindings :exec
|
||||
INSERT OR IGNORE INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
INSERT INTO minecraft_account_bindings(minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
SELECT minecraft_name_key, minecraft_name, account_subject, created_at FROM minecraft_uuid_account_bindings
|
||||
ORDER BY created_at DESC
|
||||
ON CONFLICT DO NOTHING
|
||||
`
|
||||
|
||||
func (q *Queries) RestoreAccountBindings(ctx context.Context) error {
|
||||
@@ -624,7 +625,7 @@ func (q *Queries) RestoreAccountBindings(ctx context.Context) error {
|
||||
|
||||
const upsertUUIDAccountBinding = `-- name: UpsertUUIDAccountBinding :exec
|
||||
INSERT INTO minecraft_uuid_account_bindings(server_id, minecraft_uuid, minecraft_name_key, minecraft_name, account_subject, created_at)
|
||||
VALUES(?, ?, ?, ?, ?, ?)
|
||||
VALUES($1, $2, $3, $4, $5, $6)
|
||||
ON CONFLICT(server_id, minecraft_uuid) DO UPDATE SET minecraft_name_key=excluded.minecraft_name_key, minecraft_name=excluded.minecraft_name, created_at=excluded.created_at
|
||||
`
|
||||
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"net/url"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
_ "github.com/jackc/pgx/v5/stdlib"
|
||||
)
|
||||
|
||||
// URL creates an isolated PostgreSQL schema for one test. Set TEST_DATABASE_URL
|
||||
// to a disposable PostgreSQL database URL to enable integration tests.
|
||||
func URL(t *testing.T) string {
|
||||
t.Helper()
|
||||
|
||||
baseURL := os.Getenv("TEST_DATABASE_URL")
|
||||
if baseURL == "" {
|
||||
t.Skip("set TEST_DATABASE_URL to run PostgreSQL integration tests")
|
||||
}
|
||||
parsed, err := url.Parse(baseURL)
|
||||
if err != nil || (parsed.Scheme != "postgres" && parsed.Scheme != "postgresql") {
|
||||
t.Fatalf("TEST_DATABASE_URL must be a PostgreSQL URL")
|
||||
}
|
||||
|
||||
random := make([]byte, 8)
|
||||
if _, err := rand.Read(random); err != nil {
|
||||
t.Fatalf("create isolated test schema name: %v", err)
|
||||
}
|
||||
schema := "test_" + hex.EncodeToString(random)
|
||||
ctx := context.Background()
|
||||
admin, err := sql.Open("pgx", baseURL)
|
||||
if err != nil {
|
||||
t.Fatalf("open PostgreSQL test database: %v", err)
|
||||
}
|
||||
if err := admin.PingContext(ctx); err != nil {
|
||||
admin.Close()
|
||||
t.Fatalf("connect to PostgreSQL test database: %v", err)
|
||||
}
|
||||
if _, err := admin.ExecContext(ctx, `CREATE SCHEMA "`+schema+`"`); err != nil {
|
||||
admin.Close()
|
||||
t.Fatalf("create isolated PostgreSQL test schema: %v", err)
|
||||
}
|
||||
t.Cleanup(func() {
|
||||
if _, err := admin.ExecContext(ctx, `DROP SCHEMA "`+schema+`" CASCADE`); err != nil {
|
||||
t.Errorf("drop isolated PostgreSQL test schema: %v", err)
|
||||
}
|
||||
if err := admin.Close(); err != nil {
|
||||
t.Errorf("close PostgreSQL test database: %v", err)
|
||||
}
|
||||
})
|
||||
|
||||
query := parsed.Query()
|
||||
query.Set("options", "-csearch_path="+schema)
|
||||
parsed.RawQuery = query.Encode()
|
||||
return parsed.String()
|
||||
}
|
||||
@@ -10,18 +10,18 @@ import (
|
||||
"log/slog"
|
||||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/websocket"
|
||||
"oauth-mc-web/internal/database"
|
||||
"oauth-mc-web/internal/database/repository"
|
||||
"oauth-mc-web/internal/database/testutil"
|
||||
)
|
||||
|
||||
func TestUnlinkAccountIsServerScopedAndIdempotent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := database.Open(ctx, filepath.Join(t.TempDir(), "minecraft.db"))
|
||||
db, err := database.Open(ctx, testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -100,7 +100,7 @@ func TestUnlinkAccountIsServerScopedAndIdempotent(t *testing.T) {
|
||||
|
||||
func TestApproveCASLoginCreatesServerScopedUUIDBinding(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := database.Open(ctx, filepath.Join(t.TempDir(), "minecraft.db"))
|
||||
db, err := database.Open(ctx, testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -142,7 +142,7 @@ func TestApproveCASLoginCreatesServerScopedUUIDBinding(t *testing.T) {
|
||||
|
||||
func TestLoginStartRequestIsIdempotent(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
db, err := database.Open(ctx, filepath.Join(t.TempDir(), "minecraft.db"))
|
||||
db, err := database.Open(ctx, testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -189,7 +189,7 @@ func TestLoginStartRequestIsIdempotent(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestUnknownMessageTypeEchoesRequestID(t *testing.T) {
|
||||
db, err := database.Open(context.Background(), filepath.Join(t.TempDir(), "minecraft.db"))
|
||||
db, err := database.Open(context.Background(), testutil.URL(t))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
@@ -45,7 +45,7 @@ func New(ctx context.Context, cfg config.Config, logger *slog.Logger) (*Server,
|
||||
if logger == nil {
|
||||
logger = slog.Default()
|
||||
}
|
||||
db, err := database.Open(ctx, cfg.DatabasePath)
|
||||
db, err := database.Open(ctx, cfg.DatabaseURL)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("open database: %w", err)
|
||||
}
|
||||
|
||||
@@ -1,12 +1,13 @@
|
||||
version: "2"
|
||||
sql:
|
||||
- engine: "sqlite"
|
||||
- engine: "postgresql"
|
||||
schema: "internal/database/schema"
|
||||
queries: "internal/database/repository/query.sql"
|
||||
gen:
|
||||
go:
|
||||
package: "repository"
|
||||
out: "internal/database/repository"
|
||||
sql_package: "database/sql"
|
||||
emit_json_tags: false
|
||||
emit_interface: true
|
||||
emit_exact_table_names: false
|
||||
|
||||
Reference in New Issue
Block a user