diff --git a/CLAUDE.md b/CLAUDE.md index 0745e99..c069281 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -16,7 +16,14 @@ datum-cloud/enhancements#943. ## Rules that shape the code - `FeedbackSource` is stored in etcd through the generic registry. Every other - resource uses custom `rest.Storage` backed by Postgres. + resource uses `internal/storage/pg`: a generic Postgres `rest.Storage` + configured per kind with a `pg.Kind` (table, strategies, scope, extra + columns for field selectors). Tables are in `migrations/00002_objects.sql`. +- Scope: posts, releases and changelog entries live in the platform scope; + user-owned kinds use `pg.UserOwned` (the caller in the user context, every + user in the platform context). +- Side effects share the request's transaction: wrap them in `db.InTx(ctx, ...)` + and call other stores with that ctx. - The apiserver is the only component that connects to Postgres. Side effects of a request (e.g. the `Subscription` created with a `Vote`) are written in the same transaction. diff --git a/go.mod b/go.mod index e5c842c..2f6a2e3 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,8 @@ require ( github.com/pressly/goose/v3 v3.30.0 github.com/spf13/cobra v1.10.2 github.com/spf13/pflag v1.0.10 + github.com/testcontainers/testcontainers-go v0.45.0 + github.com/testcontainers/testcontainers-go/modules/postgres v0.45.0 k8s.io/apimachinery v0.37.2 k8s.io/apiserver v0.37.2 k8s.io/client-go v0.37.2 @@ -20,15 +22,28 @@ require ( require ( cel.dev/expr v0.25.3 // indirect + dario.cat/mergo v1.0.2 // indirect + github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4 // indirect + github.com/Microsoft/go-winio v0.6.4 // indirect github.com/NYTimes/gziphandler v1.1.1 // indirect github.com/antlr4-go/antlr/v4 v4.13.1 // indirect github.com/beorn7/perks v1.0.1 // indirect github.com/blang/semver/v4 v4.0.0 // indirect + github.com/cenkalti/backoff/v4 v4.3.0 // indirect github.com/cenkalti/backoff/v5 v5.0.3 // indirect github.com/cespare/xxhash/v2 v2.3.0 // indirect + github.com/containerd/errdefs v1.0.0 // indirect + github.com/containerd/errdefs/pkg v0.3.0 // indirect + github.com/containerd/log v0.1.0 // indirect + github.com/containerd/platforms v0.2.1 // indirect github.com/coreos/go-semver v0.3.1 // indirect github.com/coreos/go-systemd/v22 v22.7.0 // indirect + github.com/cpuguy83/dockercfg v0.3.2 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect + github.com/distribution/reference v0.6.0 // indirect + github.com/docker/go-connections v0.8.2 // indirect + github.com/docker/go-units v0.5.0 // indirect + github.com/ebitengine/purego v0.10.1 // indirect github.com/emicklei/go-restful/v3 v3.13.0 // indirect github.com/felixge/httpsnoop v1.1.0 // indirect github.com/fsnotify/fsnotify v1.9.0 // indirect @@ -36,6 +51,7 @@ require ( github.com/go-logr/logr v1.4.4 // indirect github.com/go-logr/stdr v1.2.2 // indirect github.com/go-logr/zapr v1.3.0 // indirect + github.com/go-ole/go-ole v1.3.0 // indirect github.com/go-openapi/jsonpointer v1.0.0 // indirect github.com/go-openapi/jsonreference v1.0.0 // indirect github.com/go-openapi/swag v0.27.1 // indirect @@ -62,18 +78,39 @@ require ( github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/puddle/v2 v2.2.3 // indirect github.com/json-iterator/go v1.1.12 // indirect + github.com/klauspost/compress v1.20.4 // indirect github.com/kylelemons/godebug v1.1.0 // indirect + github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e // indirect + github.com/magiconair/properties v1.18.12 // indirect github.com/mfridman/interpolate v0.0.2 // indirect + github.com/moby/docker-image-spec v1.3.1 // indirect + github.com/moby/go-archive v0.3.3 // indirect + github.com/moby/moby/api v1.56.1 // indirect + github.com/moby/moby/client v0.6.2 // indirect + github.com/moby/patternmatcher v0.6.1 // indirect + github.com/moby/sys/sequential v0.7.0 // indirect + github.com/moby/sys/user v0.4.1 // indirect + github.com/moby/sys/userns v0.1.0 // indirect + github.com/moby/term v0.5.2 // indirect github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect github.com/modern-go/reflect2 v1.0.3-0.20250322232337-35a7c28c31ee // indirect github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect + github.com/opencontainers/go-digest v1.0.0 // indirect + github.com/opencontainers/image-spec v1.1.1 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect + github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 // indirect github.com/prometheus/client_golang v1.24.0 // indirect github.com/prometheus/client_model v0.6.2 // indirect github.com/prometheus/common v0.70.0 // indirect github.com/prometheus/procfs v0.23.0 // indirect github.com/sethvargo/go-retry v0.5.0 // indirect + github.com/shirou/gopsutil/v4 v4.26.6 // indirect + github.com/sirupsen/logrus v1.10.2 // indirect + github.com/stretchr/testify v1.12.1 // indirect + github.com/tklauser/go-sysconf v0.4.0 // indirect + github.com/tklauser/numcpus v0.12.0 // indirect github.com/x448/float16 v0.8.4 // indirect + github.com/yusufpapurcu/wmi v1.2.4 // indirect go.etcd.io/etcd/api/v3 v3.7.0 // indirect go.etcd.io/etcd/client/pkg/v3 v3.7.0 // indirect go.etcd.io/etcd/client/v3 v3.7.0 // indirect diff --git a/go.sum b/go.sum index 96bae1f..7d3c598 100644 --- a/go.sum +++ b/go.sum @@ -1,5 +1,13 @@ cel.dev/expr v0.25.3 h1:A2jO8jwOugrrovveCWfj0KEZOfqiLgAcwjpHPhzIGw0= cel.dev/expr v0.25.3/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4= +dario.cat/mergo v1.0.2 h1:85+piFYR1tMbRrLcDwR18y4UKJ3aH1Tbzi24VRW1TK8= +dario.cat/mergo v1.0.2/go.mod h1:E/hbnu0NxMFBjpMIE34DRGLWqDy0g5FuKDhCb31ngxA= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6 h1:He8afgbRMd7mFxO99hRNu+6tazq8nFF9lIwo9JFroBk= +github.com/AdaLogics/go-fuzz-headers v0.0.0-20240806141605-e8a1dd7889d6/go.mod h1:8o94RPi1/7XTJvwPpRSzSUedZrtlirdB3r9Z20bi2f8= +github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4 h1:m2xcjRABtG288vnlV3U/PKh/HnPURxZp50RKNUwBxM8= +github.com/Azure/go-ansiterm v0.0.0-20261006220739-8c912ac31dd4/go.mod h1:MTUE7ZuZO89FAtAwozGg6AWD83pcdefnEq5gF82MB8s= +github.com/Microsoft/go-winio v0.6.4 h1:kJF2pamtYCWXf0xAT8sAkwKC0pMl2HYBrRPHAqyUc+g= +github.com/Microsoft/go-winio v0.6.4/go.mod h1:1xl5JXG4ze/m74GH6Dc4Mwxfc3VL5siRVQjIAlHH07U= github.com/NYTimes/gziphandler v1.1.1 h1:ZUDjpQae29j0ryrS0u/B8HZfJBtBQHjqw2rQ2cqUQ3I= github.com/NYTimes/gziphandler v1.1.1/go.mod h1:n/CVRwUEOgIxrgPvAQhUUr9oeUtvrhMomdKFjzJNB0c= github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ= @@ -8,21 +16,43 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM= github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw= github.com/blang/semver/v4 v4.0.0 h1:1PFHFE6yCCTv8C1TeyNNarDzntLi7wMI5i/pzqYIsAM= github.com/blang/semver/v4 v4.0.0/go.mod h1:IbckMUScFkM3pff0VJDNKRiT6TG/YpiHIM2yvyW5YoQ= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= github.com/cenkalti/backoff/v5 v5.0.3 h1:ZN+IMa753KfX5hd8vVaMixjnqRZ3y8CuJKRKj1xcsSM= github.com/cenkalti/backoff/v5 v5.0.3/go.mod h1:rkhZdG3JZukswDf7f0cwqPNk4K0sa+F97BxZthm/crw= github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs= github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs= +github.com/containerd/errdefs v1.0.0 h1:tg5yIfIlQIrxYtu9ajqY42W3lpS19XqdxRQeEwYG8PI= +github.com/containerd/errdefs v1.0.0/go.mod h1:+YBYIdtsnF4Iw6nWZhJcqGSg/dwvV7tyJ/kCkyJ2k+M= +github.com/containerd/errdefs/pkg v0.3.0 h1:9IKJ06FvyNlexW690DXuQNx2KA2cUJXx151Xdx3ZPPE= +github.com/containerd/errdefs/pkg v0.3.0/go.mod h1:NJw6s9HwNuRhnjJhM7pylWwMyAkmCQvQ4GpJHEqRLVk= +github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I= +github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo= +github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A= +github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw= github.com/coreos/go-semver v0.3.1 h1:yi21YpKnrx1gt5R+la8n5WgS0kCrsPp33dmEyHReZr4= github.com/coreos/go-semver v0.3.1/go.mod h1:irMmmIw/7yzSRPWryHsK7EYSg09caPQL03VsM8rvUec= github.com/coreos/go-systemd/v22 v22.7.0 h1:LAEzFkke61DFROc7zNLX/WA2i5J8gYqe0rSj9KI28KA= github.com/coreos/go-systemd/v22 v22.7.0/go.mod h1:xNUYtjHu2EDXbsxz1i41wouACIwT7Ybq9o0BQhMwD0w= +github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA= +github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc= github.com/cpuguy83/go-md2man/v2 v2.0.6/go.mod h1:oOW0eioCTA6cOiMLiUPZOpcVxMig6NIQQ7OS05n1F4g= +github.com/creack/pty v1.1.24 h1:bJrF4RRfyJnbTJqzRLHzcGaZK1NeM5kTC9jGgovnR1s= +github.com/creack/pty v1.1.24/go.mod h1:08sCNb52WyoAwi2QDyzUCTgcvVFhUzewun7wtTfvcwE= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM= github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk= +github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E= +github.com/docker/go-connections v0.8.2 h1:9d9DEokx5W7qlr0shkFXIxEA7+TTD+llwzcykSlxMNc= +github.com/docker/go-connections v0.8.2/go.mod h1:no1qkHdjq7kLMGUXYAduOhYPSJxxvgWBh7ogVvptn3Q= +github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4= +github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk= github.com/dustin/go-humanize v1.1.0 h1:dbKTrvD0klcbBV/h4AWJdMuZogJACoMlvWIWZ5b2xWg= github.com/dustin/go-humanize v1.1.0/go.mod h1:hc1CvRkJMsgxqjmjMQF3QNRAZBwY8AXBAzKYoSX9sFI= +github.com/ebitengine/purego v0.10.1 h1:dewVBCBT2GaMu1SrNTYxQhgQBethzfhiwvZiLGP/qyY= +github.com/ebitengine/purego v0.10.1/go.mod h1:iIjxzd6CiRiOG0UyXP+V1+jWqUXVjPKLAI0mRfJZTmQ= github.com/emicklei/go-restful/v3 v3.13.0 h1:C4Bl2xDndpU6nJ4bc1jXd+uTmYPVUwkD6bFY/oTyCes= github.com/emicklei/go-restful/v3 v3.13.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc= github.com/felixge/httpsnoop v1.1.0 h1:3YtUj32ZZkqZtt3sZZsClsymw/QDuVfpNhoA31zeORc= @@ -38,6 +68,9 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= github.com/go-logr/zapr v1.3.0 h1:XGdV8XW8zdwFiwOA2Dryh1gj2KRQyOOoNmBy4EplIcQ= github.com/go-logr/zapr v1.3.0/go.mod h1:YKepepNBd1u/oyhd/yQmtjVXmm9uML4IXUgMOwR8/Gg= +github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0= +github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE= +github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78= github.com/go-openapi/jsonpointer v1.0.0 h1:kR9tHqY0CtZaOPVFm622dPVNhrvYpwr4uCxgL3h1H8s= github.com/go-openapi/jsonpointer v1.0.0/go.mod h1:Z3rw7dWu1p9IgitXCFamSlA5lmDiklEB6vkaxcNZW5Y= github.com/go-openapi/jsonreference v1.0.0 h1:jlmTr6torcd1YgDQvSfNmRtKzYDO4FGBkrAdlAVWnpY= @@ -111,10 +144,38 @@ github.com/klauspost/compress v1.20.4 h1:dD0v/bvxtjCBqQu6sx1TNWkalEY5lNTIe+HY9w8 github.com/klauspost/compress v1.20.4/go.mod h1:LUdAzn7YLVvxLpc7y3V1m40wESHTgc1422pwwBSKYuI= github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0SNc= github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e h1:Q6MvJtQK/iRcRtzAscm/zF23XxJlbECiGPyRicsX+Ak= +github.com/lufia/plan9stats v0.0.0-20260330125221-c963978e514e/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg= +github.com/magiconair/properties v1.18.12 h1:sT9zQpvTB3B4gzrX0tmZNTEaGyg8Zw55MFYRE32Mr9I= +github.com/magiconair/properties v1.18.12/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0= github.com/mattn/go-isatty v0.0.24 h1:tGZZoVgT/KiqK1c8ocVLeDS8BSWMRd47J3Lbz7vsReI= github.com/mattn/go-isatty v0.0.24/go.mod h1:nMCL3Zebbrt45jsMDgnfIwz6ydEQApk5oEI3HqDio6A= +github.com/mdelapenya/tlscert v0.2.0 h1:7H81W6Z/4weDvZBNOfQte5GpIMo0lGYEeWbkGp5LJHI= +github.com/mdelapenya/tlscert v0.2.0/go.mod h1:O4njj3ELLnJjGdkN7M/vIVCpZ+Cf0L6muqOG4tLSl8o= github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6BbAxPY= github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg= +github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0= +github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo= +github.com/moby/go-archive v0.3.3 h1:OxxR9paxsluYi+zDUEXTTaIxtkK3viymW+Ka7vRhhME= +github.com/moby/go-archive v0.3.3/go.mod h1:Npdv43fFqlhZW7Xo8fbm3ZMYFvAGNviUPqX21VERbcE= +github.com/moby/moby/api v1.56.1 h1:PpWkvVPB7Fr/No8w+TfyJ/I6rWZ2YPZhT3JorIg+06c= +github.com/moby/moby/api v1.56.1/go.mod h1:sZ+THbVWkjOmBPPfbnzdD/G1LuIexWhqlSHHPTDQ1Uk= +github.com/moby/moby/client v0.6.2 h1:ekSPlMccrZGcw6Scb8T9D39qE0mB+XupU/yS0zNw9N4= +github.com/moby/moby/client v0.6.2/go.mod h1:DGcnO7qmPJrqobx/ggQgQ/AUk+2wgVdQv7uxB0/mEsc= +github.com/moby/patternmatcher v0.6.1 h1:qlhtafmr6kgMIJjKJMDmMWq7WLkKIo23hsrpR3x084U= +github.com/moby/patternmatcher v0.6.1/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc= +github.com/moby/sys/mount v0.3.5 h1:eS3fsZTjHaBihwjp4/+5Z3jxqLXYsbwxqpVSfFv3M00= +github.com/moby/sys/mount v0.3.5/go.mod h1:WUQDO+/uCiCIkIztx8SrwIDVn2dtMFRBebRhpDFT71M= +github.com/moby/sys/mountinfo v0.7.2 h1:1shs6aH5s4o5H2zQLn796ADW1wMrIwHsyJ2v9KouLrg= +github.com/moby/sys/mountinfo v0.7.2/go.mod h1:1YOa8w8Ih7uW0wALDUgT1dTTSBrZ+HiBLGws92L2RU4= +github.com/moby/sys/sequential v0.7.0 h1:ASQNGNROJSuOO6LL6bPHbKvuZu6NU8P4ldPWk31zj/8= +github.com/moby/sys/sequential v0.7.0/go.mod h1:NfSTAp6V3fw4tmkD62PEcOKeZKquXT8VKCkf7aVR79o= +github.com/moby/sys/user v0.4.1 h1:RgjRlaDKi/Xmyrz4t8lyzXT6v2ooFeO/7xtchmhVWE0= +github.com/moby/sys/user v0.4.1/go.mod h1:E9QsW5WRe1kUAf7kW8hXKwu1uhsZEAdPLYHYSDudF4Y= +github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g= +github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28= +github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ= +github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc= github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg= github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= @@ -125,9 +186,15 @@ github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ= github.com/ncruces/go-strftime v1.1.0 h1:vtPmPWMkb539RGV9WpWT9EwJGvrOCKvMNI4obWL47Hk= github.com/ncruces/go-strftime v1.1.0/go.mod h1:yVaq7iwCN09vHRrKBrrflXT0M9tTMG2KmZ9Fj7xeG/g= +github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U= +github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM= +github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJwooC2xJA040= +github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU= +github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE= github.com/pressly/goose/v3 v3.30.0 h1:hmsrzlpLLKiIxukTV1YKCfSoM/F0QZGGmJLsbXcYD/I= github.com/pressly/goose/v3 v3.30.0/go.mod h1:aw7C4/P6xjcOoa3Nsj9H2xGmLyIs+JgSLTUImg2KaWE= github.com/prometheus/client_golang v1.24.0 h1:5XStIklKuAtJSNpdD3s8XJj/Yv78IQmE1kbNk87JrAI= @@ -143,8 +210,10 @@ github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qq github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/sethvargo/go-retry v0.5.0 h1:JYfFKmVJ8ER06GReN1efzc5cm52BrVsFVzmcHiKJwfY= github.com/sethvargo/go-retry v0.5.0/go.mod h1:tvsjdKG6xfiCx4LSiUZ06kcv38xvdVQwv8R6/VnnVWg= -github.com/sirupsen/logrus v1.9.4 h1:TsZE7l11zFCLZnZ+teH4Umoq5BhEIfIzfRDZ1Uzql2w= -github.com/sirupsen/logrus v1.9.4/go.mod h1:ftWc9WdOfJ0a92nsE2jF5u5ZwH8Bv2zdeOC42RjbV2g= +github.com/shirou/gopsutil/v4 v4.26.6 h1:Mzr/npDtQC/xpeEuQKHZt8Zo9CmPvhTj8nkR8w5TLDs= +github.com/shirou/gopsutil/v4 v4.26.6/go.mod h1:LZ6ewCSkBqUpvSOf+LsTGnRinC6iaNUNMGBtDkJBaLQ= +github.com/sirupsen/logrus v1.10.2 h1:G2SED73/qrAu6YwbdxOD6peLkCBI3z7L+ykJFTXJBBo= +github.com/sirupsen/logrus v1.10.2/go.mod h1:SLEg8TqYulVKKfIGHldVp2K2aYz2DKSVBq4g/H5bR7Q= github.com/soheilhy/cmux v0.1.5 h1:jjzc5WVemNEDTLwv9tlmemhC73tI08BNOIGwBOo10Js= github.com/soheilhy/cmux v0.1.5/go.mod h1:T7TcVDs9LWfQgPlPsdngu6I6QIoyIFZDDC6sNE1GqG0= github.com/spf13/cobra v1.10.2 h1:DMTTonx5m65Ic0GOoRY2c16WCbHxOOw6xxezuLaBpcU= @@ -159,12 +228,22 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.12.1 h1:EuwCh5fleGS7H32xRwO3wRGT7DxrDhLAT6FF8MpWDWE= github.com/stretchr/testify v1.12.1/go.mod h1:MDEgiDPPsNp5cuIrHPPCyornHKgEVbtFUmoNlxoYthg= +github.com/testcontainers/testcontainers-go v0.45.0 h1:ss9Stm9KDX6JLgGL4jr2fC5dwpabTbCBsRcGadBPR9k= +github.com/testcontainers/testcontainers-go v0.45.0/go.mod h1:VMJZ7M+qhgfEHWNbtjm596OyEOraULSTDaus/h0hmJI= +github.com/testcontainers/testcontainers-go/modules/postgres v0.45.0 h1:XsZa4fjEGqWVM3psvyJx04+dG3ZOPV0KkwFhJRqzcJM= +github.com/testcontainers/testcontainers-go/modules/postgres v0.45.0/go.mod h1:v59yuxG94bpGrtq6MTMmrH1It+196bg7mtGwtDx1QEY= +github.com/tklauser/go-sysconf v0.4.0 h1:7H0uAN+7RkwWRaxhYXDLqa5V3LPrJeV8wmD9dRUgPQU= +github.com/tklauser/go-sysconf v0.4.0/go.mod h1:8mTNWyog7H+MpKijp4VmKJAd2bbYQ2zuUwkYRbUArPI= +github.com/tklauser/numcpus v0.12.0 h1:NR85qdvHA9pFse3x3weVZ0r0ST8R6l5RHbZrlRaqob4= +github.com/tklauser/numcpus v0.12.0/go.mod h1:ABHeXzJnr/qqwguhClkZKT1/8VABcYrsyUiUGobwWJg= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75 h1:6fotK7otjonDflCTK0BCfls4SPy3NcCVb5dqqmbRknE= github.com/tmc/grpc-websocket-proxy v0.0.0-20220101234140-673ab2c3ae75/go.mod h1:KO6IkyS8Y3j8OdNO85qEYBsRPuteD+YciPomcXdrMnk= github.com/x448/float16 v0.8.4 h1:qLwI1I70+NjRFUR3zs1JPUCgaCXSh3SW62uAKT1mSBM= github.com/x448/float16 v0.8.4/go.mod h1:14CWIYCyZA/cWjXOioeEpHeN/83MdbZDRQHoFcYsOfg= github.com/xiang90/probing v0.0.0-20221125231312-a49e3df8f510 h1:S2dVYn90KE98chqDkyE9Z4N61UnQd+KOfgp5Iu53llk= github.com/xiang90/probing v0.0.0-20221125231312-a49e3df8f510/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU= +github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0= +github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0= go.etcd.io/bbolt v1.5.0 h1:S7GAl7Fxv12yohbwFfIbQCGDWbQbtDGPET4P/bD4lxU= go.etcd.io/bbolt v1.5.0/go.mod h1:mkltfYE5aUHQxUct9N9V+Kp7aSjFqjgrhcXIS70Lrdk= go.etcd.io/etcd/api/v3 v3.7.0 h1:WZlGK7pRtYGDB8ti8wkrQ5D2oWGMbtNL9VA5e+vF2Fg= @@ -226,6 +305,9 @@ golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs= golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q= golang.org/x/sync v0.25.0 h1:dJnifr0nMV8/SBnBvdo4eSGGnrc3O4FFx6IqvtLcmRs= golang.org/x/sync v0.25.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0= +golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20201204225414-ed752295db88/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.50.0 h1:dkKZEFTEKaLr1sjhs0WiKqoi8Jj5nHbS3Njmx1UoMJs= golang.org/x/sys v0.50.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og= golang.org/x/term v0.47.0 h1:cpyOwQkr2Nzxe2p6KnrTG7Ei2wtzjsuSjhpXXepZ/BM= @@ -260,6 +342,8 @@ gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYs 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= +gotest.tools/v3 v3.5.2 h1:7koQfIKdy+I8UTetycgUqXWSDwpgv193Ka+qRsmBY8Q= +gotest.tools/v3 v3.5.2/go.mod h1:LtdLGcnqToBH83WByAAi/wiwSFCArdFIUV/xxN4pcjA= k8s.io/api v0.37.2 h1:PnxqnUFy93jr2GcpWDkKOm8wfy25kT9wa572MuQS8oc= k8s.io/api v0.37.2/go.mod h1:+NVRLft99kx+Jg9v34GmHw867Dapim89TSyCsn2hK+M= k8s.io/apimachinery v0.37.2 h1:G4t9RGoPDbQML38hz/kQQiltttgd3egg2ODCR8aIc2o= @@ -292,6 +376,8 @@ modernc.org/memory v1.12.1 h1:nFMiWrpStgZczNl6XI9GnIk/rWhYIyHGUaR04pGbp9g= modernc.org/memory v1.12.1/go.mod h1:/JP4VbVC+K5sU2wZi9bHoq2MAkCnrt2r98UGeSK7Mjw= modernc.org/sqlite v1.62.0 h1:gMaCjSM1qpl+iOjiWhR1LYP3zqrYDI+5GYLGt6veP2g= modernc.org/sqlite v1.62.0/go.mod h1:Jzjh1mt4s0HuQ0KvtB1LIuHiGRmU2D5pgj8dUhW7/SE= +pgregory.net/rapid v1.2.0 h1:keKAYRcjm+e1F0oAuU5F5+YPAWcyxNNRK2wud503Gnk= +pgregory.net/rapid v1.2.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.36.0 h1:/YpDJ4vReG7ZmzSpBGxduXgywWkJU9zHubgJG03MT+Y= sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.36.0/go.mod h1:tJo1aepTXyR+8Xs3sUsGBDk4Ub2AM5dPAPKJx0mpm5c= sigs.k8s.io/json v0.0.0-20250730193827-2d320260d730 h1:IpInykpT6ceI+QxKBbEflcR5EXP7sU1kvOlxwZh5txg= diff --git a/internal/storage/pg/db.go b/internal/storage/pg/db.go new file mode 100644 index 0000000..97f6d25 --- /dev/null +++ b/internal/storage/pg/db.go @@ -0,0 +1,66 @@ +// Package pg is the generic Postgres-backed rest.Storage used by every +// feedback resource except FeedbackSource. +// +// Each kind has its own table (see migrations/00002_objects.sql) with the +// same leading columns: scope_type, scope_name, name, uid, resource_version, +// created_at, labels and object. A Kind adds extra columns copied from the +// object for field selectors and sorting. +// +// Writes run in a transaction carried on the context. A storage method that +// is called while another write's transaction is in ctx joins it (as a +// savepoint), so side effects such as the Subscription created with a Vote +// commit or roll back with the request. +package pg + +import ( + "context" + "errors" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + "github.com/jackc/pgx/v5/pgxpool" +) + +// Querier is satisfied by both the pool and a transaction. +type Querier interface { + Exec(ctx context.Context, sql string, args ...any) (pgconn.CommandTag, error) + Query(ctx context.Context, sql string, args ...any) (pgx.Rows, error) + QueryRow(ctx context.Context, sql string, args ...any) pgx.Row +} + +// DB wraps the pool and the transaction-on-context helpers. +type DB struct { + Pool *pgxpool.Pool +} + +type txKey struct{} + +// errRollback makes InTx roll back without reporting an error, for dry runs. +var errRollback = errors.New("rollback requested") + +// InTx runs fn in a transaction. If ctx already carries one, fn runs in a +// savepoint inside it, so it commits only when the outer transaction does. +// fn must use Q(ctx) for every statement. +func (d *DB) InTx(ctx context.Context, fn func(ctx context.Context) error) error { + var beginner interface { + Begin(context.Context) (pgx.Tx, error) + } = d.Pool + if tx, ok := ctx.Value(txKey{}).(pgx.Tx); ok { + beginner = tx + } + err := pgx.BeginFunc(ctx, beginner, func(tx pgx.Tx) error { + return fn(context.WithValue(ctx, txKey{}, tx)) + }) + if errors.Is(err, errRollback) { + return nil + } + return err +} + +// Q returns the transaction in ctx, or the pool when there is none. +func (d *DB) Q(ctx context.Context) Querier { + if tx, ok := ctx.Value(txKey{}).(pgx.Tx); ok { + return tx + } + return d.Pool +} diff --git a/internal/storage/pg/kind.go b/internal/storage/pg/kind.go new file mode 100644 index 0000000..d0f22a8 --- /dev/null +++ b/internal/storage/pg/kind.go @@ -0,0 +1,72 @@ +package pg + +import ( + "errors" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/runtime/schema" + "k8s.io/apiserver/pkg/registry/rest" +) + +// Column is an extra column value copied from the object on write. +type Column struct { + Name string + Value any +} + +// Kind describes how one resource maps onto its table. +type Kind struct { + // Table is the table name. It comes from code, never from a request. + Table string + + NewFunc func() runtime.Object + NewListFunc func() runtime.Object + + DefaultQualifiedResource schema.GroupResource + SingularQualifiedResource schema.GroupResource + + CreateStrategy rest.RESTCreateStrategy + UpdateStrategy rest.RESTUpdateStrategy + DeleteStrategy rest.RESTDeleteStrategy + + // Scope resolves which owner a request reads and writes. + Scope ScopeFunc + + // Columns returns the extra column values for obj. Every call must return + // the same column names in the same order. + Columns func(obj runtime.Object) []Column + + // FieldColumns maps field selector labels to columns. metadata.name is + // always supported. + FieldColumns map[string]string + + // TableConvertor renders kubectl tables. Defaults to name and age. + TableConvertor rest.TableConvertor +} + +func (k Kind) validate() error { + var errs []error + if k.Table == "" { + errs = append(errs, errors.New("Table is required")) + } + if k.NewFunc == nil || k.NewListFunc == nil { + errs = append(errs, errors.New("NewFunc and NewListFunc are required")) + } + if k.DefaultQualifiedResource.Empty() { + errs = append(errs, errors.New("DefaultQualifiedResource is required")) + } + if k.CreateStrategy == nil || k.UpdateStrategy == nil || k.DeleteStrategy == nil { + errs = append(errs, errors.New("create, update and delete strategies are required")) + } + if k.Scope == nil { + errs = append(errs, errors.New("Scope is required")) + } + return errors.Join(errs...) +} + +func (k Kind) columns(obj runtime.Object) []Column { + if k.Columns == nil { + return nil + } + return k.Columns(obj) +} diff --git a/internal/storage/pg/query.go b/internal/storage/pg/query.go new file mode 100644 index 0000000..2d65707 --- /dev/null +++ b/internal/storage/pg/query.go @@ -0,0 +1,130 @@ +package pg + +import ( + "encoding/base64" + "encoding/json" + "fmt" + "strings" + + "github.com/jackc/pgx/v5" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/selection" +) + +// where accumulates SQL conditions and their positional arguments. +type where struct { + conds []string + args []any +} + +func (w *where) arg(v any) string { + w.args = append(w.args, v) + return fmt.Sprintf("$%d", len(w.args)) +} + +func (w *where) add(format string, vals ...any) { + ph := make([]any, len(vals)) + for i, v := range vals { + ph[i] = w.arg(v) + } + w.conds = append(w.conds, fmt.Sprintf(format, ph...)) +} + +func (w *where) sql() string { + if len(w.conds) == 0 { + return "TRUE" + } + return strings.Join(w.conds, " AND ") +} + +func (w *where) scope(s Scope) { + if s.All() { + return + } + w.add("scope_type = %s AND scope_name = %s", s.Type, s.Name) +} + +// labelSelector translates what it can of sel into SQL. The caller still +// checks every row with sel.Matches, so operators it skips (Gt, Lt) are +// applied in Go. +func (w *where) labelSelector(sel labels.Selector) { + if sel == nil || sel.Empty() { + return + } + reqs, _ := sel.Requirements() + for _, r := range reqs { + key, vals := r.Key(), r.Values().List() + switch r.Operator() { + case selection.Equals, selection.DoubleEquals: + w.add("labels->>%s = %s", key, vals[0]) + case selection.In: + w.add("labels->>%s = ANY(%s)", key, vals) + case selection.NotEquals: + w.add("labels->>%s IS DISTINCT FROM %s", key, vals[0]) + case selection.NotIn: + w.add("COALESCE(labels->>%s = ANY(%s), false) = false", key, vals) + case selection.Exists: + w.add("labels ? %s", key) + case selection.DoesNotExist: + w.add("NOT (labels ? %s)", key) + } + } +} + +// fieldSelector translates sel using the kind's column map. Unknown fields +// are an error; field selectors only support = and !=. +func (w *where) fieldSelector(sel fields.Selector, columns map[string]string) error { + if sel == nil || sel.Empty() { + return nil + } + for _, r := range sel.Requirements() { + col := "name" + if r.Field != "metadata.name" { + c, ok := columns[r.Field] + if !ok { + return fmt.Errorf("field selector %q is not supported", r.Field) + } + col = c + } + col = pgx.Identifier{col}.Sanitize() + switch r.Operator { + case selection.Equals, selection.DoubleEquals: + w.add(col+"::text = %s", r.Value) + case selection.NotEquals: + w.add(col+"::text <> %s", r.Value) + default: + return fmt.Errorf("field selector operator %q is not supported", r.Operator) + } + } + return nil +} + +// continueToken is the position after the last row of a page. Rows are +// ordered by (name, scope_type, scope_name), which is unique. +type continueToken struct { + Name string `json:"n"` + ScopeType string `json:"t"` + ScopeName string `json:"s"` +} + +func (c continueToken) encode() string { + b, _ := json.Marshal(c) + return base64.RawURLEncoding.EncodeToString(b) +} + +func decodeContinue(s string) (continueToken, error) { + var c continueToken + b, err := base64.RawURLEncoding.DecodeString(s) + if err != nil { + return c, fmt.Errorf("invalid continue token") + } + if err := json.Unmarshal(b, &c); err != nil { + return c, fmt.Errorf("invalid continue token") + } + return c, nil +} + +func (w *where) after(c continueToken) { + w.add("(name, scope_type, scope_name) > (%s, %s, %s)", c.Name, c.ScopeType, c.ScopeName) +} diff --git a/internal/storage/pg/scope.go b/internal/storage/pg/scope.go new file mode 100644 index 0000000..bbf739b --- /dev/null +++ b/internal/storage/pg/scope.go @@ -0,0 +1,79 @@ +package pg + +import ( + "context" + "fmt" + + "k8s.io/apiserver/pkg/authentication/user" + "k8s.io/apiserver/pkg/endpoints/request" +) + +// Scope types. A Scope with an empty Type means "every scope", used when +// staff list user-owned resources from the platform context. +const ( + ScopePlatform = "Platform" + ScopeUser = "User" +) + +// User extra keys Milo sets on requests it proxies to aggregated APIs. +const ( + ParentKindExtraKey = "iam.miloapis.com/parent-type" + ParentNameExtraKey = "iam.miloapis.com/parent-name" +) + +// Scope is who owns an object: the platform, or one user (by UID). +type Scope struct { + Type string + Name string +} + +// All reports whether the scope spans every owner. +func (s Scope) All() bool { return s.Type == "" } + +// AllScopes is the scope that matches every owner. +var AllScopes = Scope{} + +// ScopeFunc resolves the scope for a request. +type ScopeFunc func(ctx context.Context) (Scope, error) + +// RequestContext returns the context Milo resolved for the request: the +// parent kind ("User", "Organization", "Project") and name from the user +// extras, or ("Platform", "") when there are none. +func RequestContext(ctx context.Context) (kind, name string) { + u, ok := request.UserFrom(ctx) + if !ok { + return ScopePlatform, "" + } + return contextFromUser(u) +} + +func contextFromUser(u user.Info) (kind, name string) { + extra := u.GetExtra() + kinds, names := extra[ParentKindExtraKey], extra[ParentNameExtraKey] + if len(kinds) == 0 || len(names) == 0 || kinds[0] == "" { + return ScopePlatform, "" + } + return kinds[0], names[0] +} + +// PlatformScoped stores every object in the platform scope, whatever context +// the request came from. Used for posts, releases and changelog entries; +// authorization decides who may write them. +func PlatformScoped(context.Context) (Scope, error) { + return Scope{Type: ScopePlatform}, nil +} + +// UserOwned scopes objects to the calling user in the user context, and to +// every user in the platform context (staff views). Other contexts are +// rejected; the store reports that as Forbidden. +func UserOwned(ctx context.Context) (Scope, error) { + kind, name := RequestContext(ctx) + switch kind { + case ScopeUser: + return Scope{Type: ScopeUser, Name: name}, nil + case ScopePlatform: + return AllScopes, nil + default: + return Scope{}, fmt.Errorf("only available in the user or platform context, not %s", kind) + } +} diff --git a/internal/storage/pg/store.go b/internal/storage/pg/store.go new file mode 100644 index 0000000..6c81e3e --- /dev/null +++ b/internal/storage/pg/store.go @@ -0,0 +1,584 @@ +package pg + +import ( + "bytes" + "context" + "encoding/json" + "errors" + "fmt" + "strconv" + "strings" + "time" + + "github.com/jackc/pgx/v5" + "github.com/jackc/pgx/v5/pgconn" + apierrors "k8s.io/apimachinery/pkg/api/errors" + "k8s.io/apimachinery/pkg/api/meta" + metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apiserver/pkg/registry/rest" + "sigs.k8s.io/structured-merge-diff/v6/fieldpath" +) + +// Store is rest.Storage for one kind, backed by its Postgres table. It +// supports get, list, create, update (and so patch and apply) and delete. +// Watch and deletecollection are not supported. +type Store struct { + db *DB + kind Kind +} + +var ( + _ rest.Storage = (*Store)(nil) + _ rest.Scoper = (*Store)(nil) + _ rest.SingularNameProvider = (*Store)(nil) + _ rest.Getter = (*Store)(nil) + _ rest.Lister = (*Store)(nil) + _ rest.Creater = (*Store)(nil) + _ rest.Updater = (*Store)(nil) + _ rest.GracefulDeleter = (*Store)(nil) + _ rest.ResetFieldsStrategy = (*Store)(nil) +) + +// NewStore returns storage for kind. +func NewStore(db *DB, kind Kind) (*Store, error) { + if err := kind.validate(); err != nil { + return nil, fmt.Errorf("invalid kind %s: %w", kind.DefaultQualifiedResource, err) + } + if kind.TableConvertor == nil { + kind.TableConvertor = rest.NewDefaultTableConvertor(kind.DefaultQualifiedResource) + } + return &Store{db: db, kind: kind}, nil +} + +// DB returns the database, for side effects that must share a request's +// transaction. +func (s *Store) DB() *DB { return s.db } + +func (s *Store) New() runtime.Object { return s.kind.NewFunc() } +func (s *Store) NewList() runtime.Object { return s.kind.NewListFunc() } +func (s *Store) Destroy() {} +func (s *Store) NamespaceScoped() bool { return false } + +func (s *Store) GetSingularName() string { + return s.kind.SingularQualifiedResource.Resource +} + +func (s *Store) ConvertToTable(ctx context.Context, obj runtime.Object, opts runtime.Object) (*metav1.Table, error) { + return s.kind.TableConvertor.ConvertToTable(ctx, obj, opts) +} + +// GetResetFields delegates to the update strategy, so server-side apply on +// the main resource ignores status, and on status ignores spec. +func (s *Store) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { + if r, ok := s.kind.UpdateStrategy.(rest.ResetFieldsStrategy); ok { + return r.GetResetFields() + } + return nil +} + +func (s *Store) scope(ctx context.Context) (Scope, error) { + sc, err := s.kind.Scope(ctx) + if err == nil { + return sc, nil + } + var status apierrors.APIStatus + if errors.As(err, &status) { + return Scope{}, err + } + return Scope{}, apierrors.NewForbidden(s.kind.DefaultQualifiedResource, "", err) +} + +func (s *Store) table() string { return pgx.Identifier{s.kind.Table}.Sanitize() } + +func (s *Store) decode(rv int64, data []byte) (runtime.Object, error) { + obj := s.kind.NewFunc() + if err := json.Unmarshal(data, obj); err != nil { + return nil, fmt.Errorf("decode %s: %w", s.kind.DefaultQualifiedResource, err) + } + m, err := meta.Accessor(obj) + if err != nil { + return nil, err + } + m.SetResourceVersion(strconv.FormatInt(rv, 10)) + return obj, nil +} + +// encode returns the stored form: the object without its resourceVersion, +// which lives in its own column. +func encode(obj runtime.Object) ([]byte, error) { + c := obj.DeepCopyObject() + m, err := meta.Accessor(c) + if err != nil { + return nil, err + } + m.SetResourceVersion("") + return json.Marshal(c) +} + +// current is a loaded row. +type current struct { + scope Scope + rv int64 + obj runtime.Object +} + +// load reads one object. In AllScopes, a name that exists for more than one +// owner is ambiguous and rejected. +func (s *Store) load(ctx context.Context, sc Scope, name string, forUpdate bool) (*current, error) { + w := &where{} + w.scope(sc) + w.add("name = %s", name) + q := fmt.Sprintf("SELECT scope_type, scope_name, resource_version, object FROM %s WHERE %s LIMIT 2", s.table(), w.sql()) + if forUpdate { + q += " FOR UPDATE" + } + rows, err := s.db.Q(ctx).Query(ctx, q, w.args...) + if err != nil { + return nil, s.dbErr(err, name) + } + defer rows.Close() + + var found []*current + for rows.Next() { + var c current + var data []byte + if err := rows.Scan(&c.scope.Type, &c.scope.Name, &c.rv, &data); err != nil { + return nil, err + } + if c.obj, err = s.decode(c.rv, data); err != nil { + return nil, err + } + found = append(found, &c) + } + if err := rows.Err(); err != nil { + return nil, s.dbErr(err, name) + } + switch len(found) { + case 0: + return nil, apierrors.NewNotFound(s.kind.DefaultQualifiedResource, name) + case 1: + return found[0], nil + default: + return nil, apierrors.NewBadRequest(fmt.Sprintf( + "%s %q exists for more than one owner; request it from the owner's context", s.kind.DefaultQualifiedResource, name)) + } +} + +// Get implements rest.Getter. +func (s *Store) Get(ctx context.Context, name string, _ *metav1.GetOptions) (runtime.Object, error) { + sc, err := s.scope(ctx) + if err != nil { + return nil, err + } + c, err := s.load(ctx, sc, name, false) + if err != nil { + return nil, err + } + return c.obj, nil +} + +// List implements rest.Lister. Results are ordered by name and paged with +// limit and continue. Pages are not a consistent snapshot: rows written +// between pages can appear or be skipped. +func (s *Store) List(ctx context.Context, opts *metainternalversion.ListOptions) (runtime.Object, error) { + sc, err := s.scope(ctx) + if err != nil { + return nil, err + } + if opts == nil { + opts = &metainternalversion.ListOptions{} + } + + w := &where{} + w.scope(sc) + w.labelSelector(opts.LabelSelector) + if err := w.fieldSelector(opts.FieldSelector, s.kind.FieldColumns); err != nil { + return nil, apierrors.NewBadRequest(err.Error()) + } + if opts.Continue != "" { + c, err := decodeContinue(opts.Continue) + if err != nil { + return nil, apierrors.NewBadRequest(err.Error()) + } + w.after(c) + } + + q := fmt.Sprintf("SELECT scope_type, scope_name, name, resource_version, object FROM %s WHERE %s ORDER BY name, scope_type, scope_name", + s.table(), w.sql()) + if opts.Limit > 0 { + q += fmt.Sprintf(" LIMIT %d", opts.Limit+1) + } + + // Read the list resourceVersion first so it is never newer than a row + // the page leaves out. + var listRV int64 + if err := s.db.Q(ctx).QueryRow(ctx, "SELECT last_value FROM resource_version_seq").Scan(&listRV); err != nil { + return nil, s.dbErr(err, "") + } + + rows, err := s.db.Q(ctx).Query(ctx, q, w.args...) + if err != nil { + return nil, s.dbErr(err, "") + } + defer rows.Close() + + var ( + items []runtime.Object + scanned int64 + last continueToken + more bool + ) + for rows.Next() { + if opts.Limit > 0 && scanned == opts.Limit { + more = true + break + } + scanned++ + var ( + tok continueToken + rv int64 + data []byte + ) + if err := rows.Scan(&tok.ScopeType, &tok.ScopeName, &tok.Name, &rv, &data); err != nil { + return nil, err + } + last = tok + obj, err := s.decode(rv, data) + if err != nil { + return nil, err + } + // Apply the full label selector, for operators SQL skipped. + if opts.LabelSelector != nil && !opts.LabelSelector.Empty() { + m, _ := meta.Accessor(obj) + if !opts.LabelSelector.Matches(labels.Set(m.GetLabels())) { + continue + } + } + items = append(items, obj) + } + if err := rows.Err(); err != nil { + return nil, s.dbErr(err, "") + } + + list := s.kind.NewListFunc() + if err := meta.SetList(list, items); err != nil { + return nil, err + } + lm, err := meta.ListAccessor(list) + if err != nil { + return nil, err + } + lm.SetResourceVersion(strconv.FormatInt(listRV, 10)) + if more { + lm.SetContinue(last.encode()) + } + return list, nil +} + +// Create implements rest.Creater. +func (s *Store) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, opts *metav1.CreateOptions) (runtime.Object, error) { + sc, err := s.scope(ctx) + if err != nil { + return nil, err + } + var out runtime.Object + err = s.db.InTx(ctx, func(ctx context.Context) error { + var err error + if out, err = s.create(ctx, sc, obj, createValidation, false); err != nil { + return err + } + if opts != nil && len(opts.DryRun) > 0 { + return errRollback + } + return nil + }) + return out, err +} + +func (s *Store) create(ctx context.Context, sc Scope, obj runtime.Object, createValidation rest.ValidateObjectFunc, viaUpdate bool) (runtime.Object, error) { + if sc.All() { + return nil, apierrors.NewForbidden(s.kind.DefaultQualifiedResource, "", + errors.New("create this resource from the owning user's context")) + } + m, err := meta.Accessor(obj) + if err != nil { + return nil, err + } + // Same order as the generic registry: system fields and the generated + // name come before the strategy runs. A create through update or apply + // may carry stale system fields, so they are wiped first. + if viaUpdate { + rest.WipeObjectMetaSystemFields(m) + } + rest.FillObjectMetaSystemFields(m) + if m.GetGenerateName() != "" && m.GetName() == "" { + m.SetName(s.kind.CreateStrategy.GenerateName(m.GetGenerateName())) + } + + if err := rest.BeforeCreate(s.kind.CreateStrategy, ctx, obj); err != nil { + return nil, err + } + if createValidation != nil { + if err := createValidation(ctx, obj.DeepCopyObject()); err != nil { + return nil, err + } + } + + data, err := encode(obj) + if err != nil { + return nil, err + } + + cols := []string{"scope_type", "scope_name", "name", "uid", "created_at", "labels", "object"} + vals := []any{sc.Type, sc.Name, m.GetName(), string(m.GetUID()), creationTime(m), labelsOf(m), json.RawMessage(data)} + for _, c := range s.kind.columns(obj) { + cols = append(cols, c.Name) + vals = append(vals, c.Value) + } + quoted := make([]string, len(cols)) + ph := make([]string, len(vals)) + for i := range cols { + quoted[i] = pgx.Identifier{cols[i]}.Sanitize() + ph[i] = fmt.Sprintf("$%d", i+1) + } + q := fmt.Sprintf("INSERT INTO %s (%s, resource_version) VALUES (%s, nextval('resource_version_seq')) RETURNING resource_version", + s.table(), strings.Join(quoted, ", "), strings.Join(ph, ", ")) + + var rv int64 + if err := s.db.Q(ctx).QueryRow(ctx, q, vals...).Scan(&rv); err != nil { + return nil, s.dbErr(err, m.GetName()) + } + m.SetResourceVersion(strconv.FormatInt(rv, 10)) + return obj, nil +} + +// Update implements rest.Updater. It locks the row, applies objInfo to the +// current object, checks the resourceVersion, and writes only if the stored +// form changed. +func (s *Store) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, + updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, opts *metav1.UpdateOptions) (runtime.Object, bool, error) { + sc, err := s.scope(ctx) + if err != nil { + return nil, false, err + } + + var ( + out runtime.Object + created bool + ) + err = s.db.InTx(ctx, func(ctx context.Context) error { + cur, err := s.load(ctx, sc, name, true) + switch { + case apierrors.IsNotFound(err): + if !forceAllowCreate && !s.kind.UpdateStrategy.AllowCreateOnUpdate(ctx) { + return err + } + obj, err := objInfo.UpdatedObject(ctx, nil) + if err != nil { + return err + } + if out, err = s.create(ctx, sc, obj, createValidation, true); err != nil { + return err + } + created = true + case err != nil: + return err + default: + if out, err = s.update(ctx, cur, objInfo, updateValidation); err != nil { + return err + } + } + if opts != nil && len(opts.DryRun) > 0 { + return errRollback + } + return nil + }) + if err != nil { + return nil, false, err + } + return out, created, nil +} + +func (s *Store) update(ctx context.Context, cur *current, objInfo rest.UpdatedObjectInfo, updateValidation rest.ValidateObjectUpdateFunc) (runtime.Object, error) { + old := cur.obj + obj, err := objInfo.UpdatedObject(ctx, old.DeepCopyObject()) + if err != nil { + return nil, err + } + m, err := meta.Accessor(obj) + if err != nil { + return nil, err + } + + curRV := strconv.FormatInt(cur.rv, 10) + switch rv := m.GetResourceVersion(); { + case rv == "" && !s.kind.UpdateStrategy.AllowUnconditionalUpdate(ctx): + return nil, apierrors.NewBadRequest("metadata.resourceVersion must be set for an update") + case rv == "": + m.SetResourceVersion(curRV) + case rv != curRV: + return nil, apierrors.NewConflict(s.kind.DefaultQualifiedResource, m.GetName(), + errors.New("the object has been modified; please apply your changes to the latest version and try again")) + } + + if err := rest.BeforeUpdate(s.kind.UpdateStrategy, ctx, obj, old); err != nil { + return nil, err + } + if updateValidation != nil { + if err := updateValidation(ctx, obj.DeepCopyObject(), old.DeepCopyObject()); err != nil { + return nil, err + } + } + + data, err := encode(obj) + if err != nil { + return nil, err + } + oldData, err := encode(old) + if err != nil { + return nil, err + } + if bytes.Equal(data, oldData) { + return old, nil + } + + w := &where{} + sets := []string{ + "resource_version = nextval('resource_version_seq')", + "labels = " + w.arg(labelsOf(m)), + "object = " + w.arg(json.RawMessage(data)), + } + for _, c := range s.kind.columns(obj) { + sets = append(sets, pgx.Identifier{c.Name}.Sanitize()+" = "+w.arg(c.Value)) + } + w.add("scope_type = %s AND scope_name = %s AND name = %s AND resource_version = %s", + cur.scope.Type, cur.scope.Name, m.GetName(), cur.rv) + q := fmt.Sprintf("UPDATE %s SET %s WHERE %s RETURNING resource_version", s.table(), strings.Join(sets, ", "), w.sql()) + + var rv int64 + if err := s.db.Q(ctx).QueryRow(ctx, q, w.args...).Scan(&rv); err != nil { + if errors.Is(err, pgx.ErrNoRows) { + return nil, apierrors.NewConflict(s.kind.DefaultQualifiedResource, m.GetName(), errors.New("the object has been modified")) + } + return nil, s.dbErr(err, m.GetName()) + } + m.SetResourceVersion(strconv.FormatInt(rv, 10)) + return obj, nil +} + +// Delete implements rest.GracefulDeleter. Deletion is immediate; finalizers +// and grace periods are not supported. +func (s *Store) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, opts *metav1.DeleteOptions) (runtime.Object, bool, error) { + sc, err := s.scope(ctx) + if err != nil { + return nil, false, err + } + var out runtime.Object + err = s.db.InTx(ctx, func(ctx context.Context) error { + cur, err := s.load(ctx, sc, name, true) + if err != nil { + return err + } + m, err := meta.Accessor(cur.obj) + if err != nil { + return err + } + if opts != nil && opts.Preconditions != nil { + p := opts.Preconditions + if p.UID != nil && *p.UID != m.GetUID() { + return apierrors.NewConflict(s.kind.DefaultQualifiedResource, name, + fmt.Errorf("precondition failed: UID in precondition: %v, UID in object meta: %v", *p.UID, m.GetUID())) + } + if p.ResourceVersion != nil && *p.ResourceVersion != m.GetResourceVersion() { + return apierrors.NewConflict(s.kind.DefaultQualifiedResource, name, + fmt.Errorf("precondition failed: ResourceVersion in precondition: %v, ResourceVersion in object meta: %v", *p.ResourceVersion, m.GetResourceVersion())) + } + } + if deleteValidation != nil { + if err := deleteValidation(ctx, cur.obj); err != nil { + return err + } + } + if _, err := s.db.Q(ctx).Exec(ctx, + fmt.Sprintf("DELETE FROM %s WHERE scope_type = $1 AND scope_name = $2 AND name = $3", s.table()), + cur.scope.Type, cur.scope.Name, name); err != nil { + return s.dbErr(err, name) + } + out = cur.obj + if opts != nil && len(opts.DryRun) > 0 { + return errRollback + } + return nil + }) + if err != nil { + return nil, false, err + } + return out, true, nil +} + +// dbErr maps Postgres errors onto API errors. +func (s *Store) dbErr(err error, name string) error { + var pgErr *pgconn.PgError + if errors.As(err, &pgErr) && pgErr.Code == "23505" { // unique_violation + return apierrors.NewAlreadyExists(s.kind.DefaultQualifiedResource, name) + } + var status apierrors.APIStatus + if errors.As(err, &status) { + return err + } + return apierrors.NewInternalError(fmt.Errorf("%s storage: %w", s.kind.DefaultQualifiedResource, err)) +} + +func creationTime(m metav1.Object) time.Time { + if t := m.GetCreationTimestamp(); !t.IsZero() { + return t.Time + } + return time.Now() +} + +func labelsOf(m metav1.Object) map[string]string { + if l := m.GetLabels(); l != nil { + return l + } + return map[string]string{} +} + +// StatusStore serves a status subresource: get, and update with the status +// strategy. +type StatusStore struct { + store *Store +} + +var ( + _ rest.Getter = (*StatusStore)(nil) + _ rest.Updater = (*StatusStore)(nil) + _ rest.ResetFieldsStrategy = (*StatusStore)(nil) +) + +// NewStatusStore returns status subresource storage that updates through +// strategy. +func NewStatusStore(main *Store, strategy rest.RESTUpdateStrategy) *StatusStore { + k := main.kind + k.UpdateStrategy = strategy + return &StatusStore{store: &Store{db: main.db, kind: k}} +} + +func (s *StatusStore) New() runtime.Object { return s.store.New() } +func (s *StatusStore) Destroy() {} + +func (s *StatusStore) Get(ctx context.Context, name string, opts *metav1.GetOptions) (runtime.Object, error) { + return s.store.Get(ctx, name, opts) +} + +func (s *StatusStore) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, + updateValidation rest.ValidateObjectUpdateFunc, _ bool, opts *metav1.UpdateOptions) (runtime.Object, bool, error) { + // A status update never creates the object. + return s.store.Update(ctx, name, objInfo, createValidation, updateValidation, false, opts) +} + +func (s *StatusStore) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set { + return s.store.GetResetFields() +} diff --git a/internal/storage/pg/store_test.go b/internal/storage/pg/store_test.go new file mode 100644 index 0000000..3872510 --- /dev/null +++ b/internal/storage/pg/store_test.go @@ -0,0 +1,594 @@ +package pg_test + +import ( + "context" + "errors" + "fmt" + "os" + "testing" + "time" + + "github.com/testcontainers/testcontainers-go" + tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres" + "github.com/testcontainers/testcontainers-go/wait" + apierrors "k8s.io/apimachinery/pkg/api/errors" + metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/fields" + "k8s.io/apimachinery/pkg/labels" + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/types" + "k8s.io/apimachinery/pkg/util/validation/field" + "k8s.io/apiserver/pkg/authentication/user" + "k8s.io/apiserver/pkg/endpoints/request" + "k8s.io/apiserver/pkg/registry/rest" + "k8s.io/apiserver/pkg/storage/names" + + "go.miloapis.com/feedback/internal/postgres" + "go.miloapis.com/feedback/internal/storage/pg" + "go.miloapis.com/feedback/pkg/apis/feedback/install" + v1 "go.miloapis.com/feedback/pkg/apis/feedback/v1alpha1" +) + +var db *pg.DB + +// TestMain starts one pgvector Postgres for the package and applies the real +// migrations. Without Docker the tests skip. +func TestMain(m *testing.M) { + os.Exit(func() int { + ctx := context.Background() + ctr, err := tcpostgres.Run(ctx, "pgvector/pgvector:pg17", + tcpostgres.WithDatabase("feedback"), + tcpostgres.WithUsername("feedback"), + tcpostgres.WithPassword("feedback"), + testcontainers.WithWaitStrategy(wait.ForLog("database system is ready to accept connections"). + WithOccurrence(2).WithStartupTimeout(90*time.Second)), + ) + if err != nil { + fmt.Fprintf(os.Stderr, "skipping Postgres tests: %v\n", err) + return m.Run() // tests call requireDB and skip + } + defer func() { _ = testcontainers.TerminateContainer(ctr) }() + + dsn, err := ctr.ConnectionString(ctx, "sslmode=disable") + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + cfg := postgres.Config{DSN: dsn} + if err := postgres.Migrate(ctx, cfg); err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + pool, err := postgres.NewPool(ctx, cfg) + if err != nil { + fmt.Fprintln(os.Stderr, err) + return 1 + } + defer pool.Close() + db = &pg.DB{Pool: pool} + return m.Run() + }()) +} + +func requireDB(t *testing.T) { + t.Helper() + if db == nil { + t.Skip("Postgres not available (needs Docker)") + } + // Each test starts from empty tables. + _, err := db.Pool.Exec(context.Background(), "TRUNCATE posts, votes, subscriptions") + if err != nil { + t.Fatal(err) + } +} + +var scheme = func() *runtime.Scheme { + s := runtime.NewScheme() + install.Install(s) + return s +}() + +// strategy is a minimal create/update/delete strategy for tests. +type strategy struct { + runtime.ObjectTyper + names.NameGenerator + unconditional bool +} + +func (strategy) NamespaceScoped() bool { return false } +func (strategy) PrepareForCreate(context.Context, runtime.Object) {} +func (strategy) PrepareForUpdate(context.Context, runtime.Object, runtime.Object) {} +func (strategy) Validate(context.Context, runtime.Object) field.ErrorList { return nil } +func (strategy) ValidateUpdate(context.Context, runtime.Object, runtime.Object) field.ErrorList { + return nil +} +func (strategy) WarningsOnCreate(context.Context, runtime.Object) []string { return nil } +func (strategy) WarningsOnUpdate(context.Context, runtime.Object, runtime.Object) []string { + return nil +} +func (strategy) Canonicalize(runtime.Object) {} +func (strategy) AllowCreateOnUpdate(context.Context) bool { return false } +func (s strategy) AllowUnconditionalUpdate(context.Context) bool { return s.unconditional } + +// postStatusStrategy only lets status change. +type postStatusStrategy struct{ strategy } + +func (postStatusStrategy) PrepareForUpdate(_ context.Context, obj, old runtime.Object) { + obj.(*v1.Post).Spec = old.(*v1.Post).Spec +} + +func newStrategy(unconditional bool) strategy { + return strategy{ObjectTyper: scheme, NameGenerator: names.SimpleNameGenerator, unconditional: unconditional} +} + +func postStore(t *testing.T) *pg.Store { + t.Helper() + st := newStrategy(true) + s, err := pg.NewStore(db, pg.Kind{ + Table: "posts", + NewFunc: func() runtime.Object { return &v1.Post{} }, + NewListFunc: func() runtime.Object { return &v1.PostList{} }, + DefaultQualifiedResource: v1.Resource("posts"), + SingularQualifiedResource: v1.Resource("post"), + CreateStrategy: st, UpdateStrategy: st, DeleteStrategy: st, + Scope: pg.PlatformScoped, + Columns: func(obj runtime.Object) []pg.Column { + p := obj.(*v1.Post) + return []pg.Column{ + {Name: "hidden", Value: p.Spec.Hidden}, + {Name: "state", Value: string(p.Status.State)}, + } + }, + FieldColumns: map[string]string{"spec.hidden": "hidden", "status.state": "state"}, + }) + if err != nil { + t.Fatal(err) + } + return s +} + +func voteStore(t *testing.T, unconditional bool) *pg.Store { + t.Helper() + st := newStrategy(unconditional) + s, err := pg.NewStore(db, pg.Kind{ + Table: "votes", + NewFunc: func() runtime.Object { return &v1.Vote{} }, + NewListFunc: func() runtime.Object { return &v1.VoteList{} }, + DefaultQualifiedResource: v1.Resource("votes"), + SingularQualifiedResource: v1.Resource("vote"), + CreateStrategy: st, UpdateStrategy: st, DeleteStrategy: st, + Scope: pg.UserOwned, + Columns: func(obj runtime.Object) []pg.Column { + return []pg.Column{{Name: "post_name", Value: obj.(*v1.Vote).Spec.PostRef.Name}} + }, + FieldColumns: map[string]string{"spec.postRef.name": "post_name"}, + }) + if err != nil { + t.Fatal(err) + } + return s +} + +func subscriptionStore(t *testing.T) *pg.Store { + t.Helper() + st := newStrategy(true) + s, err := pg.NewStore(db, pg.Kind{ + Table: "subscriptions", + NewFunc: func() runtime.Object { return &v1.Subscription{} }, + NewListFunc: func() runtime.Object { return &v1.SubscriptionList{} }, + DefaultQualifiedResource: v1.Resource("subscriptions"), + SingularQualifiedResource: v1.Resource("subscription"), + CreateStrategy: st, UpdateStrategy: st, DeleteStrategy: st, + Scope: pg.UserOwned, + Columns: func(obj runtime.Object) []pg.Column { + sub := obj.(*v1.Subscription) + return []pg.Column{{Name: "post_name", Value: sub.Spec.PostRef.Name}, {Name: "reason", Value: string(sub.Spec.Reason)}} + }, + }) + if err != nil { + t.Fatal(err) + } + return s +} + +// requestContext is what the apiserver passes a cluster-scoped request. +func requestContext() context.Context { + return request.WithNamespace(context.Background(), "") +} + +func asUser(uid string) context.Context { + return request.WithUser(requestContext(), &user.DefaultInfo{ + Name: uid + "@example.com", UID: uid, + Extra: map[string][]string{pg.ParentKindExtraKey: {"User"}, pg.ParentNameExtraKey: {uid}}, + }) +} + +func asPlatform() context.Context { + return request.WithUser(requestContext(), &user.DefaultInfo{Name: "staff@datum.net", UID: "staff"}) +} + +func vote(post string) *v1.Vote { + return &v1.Vote{ObjectMeta: metav1.ObjectMeta{Name: post}, Spec: v1.VoteSpec{PostRef: v1.PostReference{Name: post}}} +} + +func post(name string, state v1.PostState, lbls map[string]string) *v1.Post { + return &v1.Post{ObjectMeta: metav1.ObjectMeta{Name: name, Labels: lbls}, Status: v1.PostStatus{Title: name, State: state}} +} + +func mustCreate(t *testing.T, ctx context.Context, s *pg.Store, obj runtime.Object) runtime.Object { + t.Helper() + out, err := s.Create(ctx, obj, rest.ValidateAllObjectFunc, &metav1.CreateOptions{}) + if err != nil { + t.Fatalf("create: %v", err) + } + return out +} + +func names_(t *testing.T, list runtime.Object) []string { + t.Helper() + var out []string + switch l := list.(type) { + case *v1.PostList: + for _, i := range l.Items { + out = append(out, i.Name) + } + case *v1.VoteList: + for _, i := range l.Items { + out = append(out, i.Name) + } + default: + t.Fatalf("unexpected list %T", list) + } + return out +} + +func equal(a, b []string) bool { return fmt.Sprint(a) == fmt.Sprint(b) } + +func TestCreateGetDelete(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + + created := mustCreate(t, ctx, s, post("datum-cloud.enhancements.1", v1.PostStateBacklog, nil)).(*v1.Post) + if created.ResourceVersion == "" || created.UID == "" || created.CreationTimestamp.IsZero() { + t.Fatalf("system fields not set: %+v", created.ObjectMeta) + } + + got, err := s.Get(ctx, created.Name, &metav1.GetOptions{}) + if err != nil { + t.Fatal(err) + } + if g := got.(*v1.Post); g.ResourceVersion != created.ResourceVersion || g.Status.Title != created.Status.Title { + t.Fatalf("get = %+v, want %+v", g, created) + } + + if _, err := s.Create(ctx, post(created.Name, "", nil), nil, &metav1.CreateOptions{}); !apierrors.IsAlreadyExists(err) { + t.Fatalf("duplicate create: got %v, want AlreadyExists", err) + } + + wrongUID := types.UID("nope") + if _, _, err := s.Delete(ctx, created.Name, nil, &metav1.DeleteOptions{Preconditions: &metav1.Preconditions{UID: &wrongUID}}); !apierrors.IsConflict(err) { + t.Fatalf("delete with wrong UID: got %v, want Conflict", err) + } + if _, _, err := s.Delete(ctx, created.Name, nil, &metav1.DeleteOptions{}); err != nil { + t.Fatal(err) + } + if _, err := s.Get(ctx, created.Name, &metav1.GetOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("get after delete: got %v, want NotFound", err) + } +} + +func TestGenerateName(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + out := mustCreate(t, ctx, s, &v1.Post{ObjectMeta: metav1.ObjectMeta{GenerateName: "gen-"}}).(*v1.Post) + if len(out.Name) <= len("gen-") { + t.Fatalf("name not generated: %q", out.Name) + } +} + +func TestUpdate(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + p := mustCreate(t, ctx, s, post("p", v1.PostStateBacklog, nil)).(*v1.Post) + + // A no-op update keeps the resourceVersion. + same, _, err := s.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(p.DeepCopy()), nil, nil, false, &metav1.UpdateOptions{}) + if err != nil { + t.Fatal(err) + } + if same.(*v1.Post).ResourceVersion != p.ResourceVersion { + t.Fatalf("no-op update bumped resourceVersion %s -> %s", p.ResourceVersion, same.(*v1.Post).ResourceVersion) + } + + changed := p.DeepCopy() + changed.Status.State = v1.PostStateInProgress + out, created, err := s.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(changed), nil, nil, false, &metav1.UpdateOptions{}) + if err != nil || created { + t.Fatalf("update: created=%v err=%v", created, err) + } + if out.(*v1.Post).ResourceVersion == p.ResourceVersion { + t.Fatal("update did not bump resourceVersion") + } + + // The extra column follows the object. + var state string + if err := db.Pool.QueryRow(ctx, "SELECT state FROM posts WHERE name = 'p'").Scan(&state); err != nil || state != "in-progress" { + t.Fatalf("state column = %q, %v", state, err) + } + + // Writing with the old resourceVersion conflicts. + stale := p.DeepCopy() + stale.Status.State = v1.PostStateShipped + if _, _, err := s.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(stale), nil, nil, false, &metav1.UpdateOptions{}); !apierrors.IsConflict(err) { + t.Fatalf("stale update: got %v, want Conflict", err) + } + + // Without a resourceVersion it applies, because the strategy allows + // unconditional updates. + stale.ResourceVersion = "" + if _, _, err := s.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(stale), nil, nil, false, &metav1.UpdateOptions{}); err != nil { + t.Fatalf("unconditional update: %v", err) + } + + if _, _, err := s.Update(ctx, "missing", rest.DefaultUpdatedObjectInfo(post("missing", "", nil)), nil, nil, false, &metav1.UpdateOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("update missing: got %v, want NotFound", err) + } + out, created, err = s.Update(ctx, "upserted", rest.DefaultUpdatedObjectInfo(post("upserted", "", nil)), nil, nil, true, &metav1.UpdateOptions{}) + if err != nil || !created || out.(*v1.Post).UID == "" { + t.Fatalf("forced create on update: created=%v err=%v", created, err) + } +} + +func TestUnconditionalUpdateRejected(t *testing.T) { + requireDB(t) + s, ctx := voteStore(t, false), asUser("u1") + v := mustCreate(t, ctx, s, vote("p")).(*v1.Vote) + v.ResourceVersion = "" + v.Spec.Source = v1.ClientPortal + if _, _, err := s.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(v), nil, nil, false, &metav1.UpdateOptions{}); !apierrors.IsBadRequest(err) { + t.Fatalf("got %v, want BadRequest", err) + } +} + +func TestStatusStore(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + status := pg.NewStatusStore(s, postStatusStrategy{newStrategy(true)}) + p := mustCreate(t, ctx, s, post("p", v1.PostStateBacklog, nil)).(*v1.Post) + + edit := p.DeepCopy() + edit.Spec.Hidden = true + edit.Status.State = v1.PostStateShipped + out, _, err := status.Update(ctx, "p", rest.DefaultUpdatedObjectInfo(edit), nil, nil, false, &metav1.UpdateOptions{}) + if err != nil { + t.Fatal(err) + } + if got := out.(*v1.Post); got.Spec.Hidden || got.Status.State != v1.PostStateShipped { + t.Fatalf("status update: hidden=%v state=%s", got.Spec.Hidden, got.Status.State) + } +} + +func TestListSelectors(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + mustCreate(t, ctx, s, post("a", v1.PostStateBacklog, map[string]string{"team": "edge", "tier": "1"})) + mustCreate(t, ctx, s, post("b", v1.PostStateInProgress, map[string]string{"team": "core", "tier": "2"})) + mustCreate(t, ctx, s, post("c", v1.PostStateInProgress, map[string]string{"team": "edge"})) + mustCreate(t, ctx, s, post("d", v1.PostStateShipped, nil)) + + cases := []struct { + label, field string + want []string + }{ + {want: []string{"a", "b", "c", "d"}}, + {label: "team=edge", want: []string{"a", "c"}}, + {label: "team in (core,edge),tier", want: []string{"a", "b"}}, + {label: "team!=edge", want: []string{"b", "d"}}, + {label: "team notin (edge)", want: []string{"b", "d"}}, + {label: "!tier", want: []string{"c", "d"}}, + {label: "tier>1", want: []string{"b"}}, // applied in Go + {field: "status.state=in-progress", want: []string{"b", "c"}}, + {field: "status.state!=in-progress,metadata.name!=d", want: []string{"a"}}, + {label: "team=edge", field: "status.state=in-progress", want: []string{"c"}}, + {field: "spec.hidden=false", want: []string{"a", "b", "c", "d"}}, + } + for _, tc := range cases { + t.Run(tc.label+"|"+tc.field, func(t *testing.T) { + opts := &metainternalversion.ListOptions{} + if tc.label != "" { + sel, err := labels.Parse(tc.label) + if err != nil { + t.Fatal(err) + } + opts.LabelSelector = sel + } + if tc.field != "" { + sel, err := fields.ParseSelector(tc.field) + if err != nil { + t.Fatal(err) + } + opts.FieldSelector = sel + } + list, err := s.List(ctx, opts) + if err != nil { + t.Fatal(err) + } + if got := names_(t, list); !equal(got, tc.want) { + t.Fatalf("got %v, want %v", got, tc.want) + } + }) + } + + _, err := s.List(ctx, &metainternalversion.ListOptions{FieldSelector: fields.OneTermEqualSelector("status.title", "x")}) + if !apierrors.IsBadRequest(err) { + t.Fatalf("unsupported field: got %v, want BadRequest", err) + } +} + +func TestListPaging(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + for _, n := range []string{"e", "a", "d", "b", "c"} { + mustCreate(t, ctx, s, post(n, "", nil)) + } + var all []string + cont := "" + for pages := 0; ; pages++ { + if pages > 5 { + t.Fatal("paging did not end") + } + list, err := s.List(ctx, &metainternalversion.ListOptions{Limit: 2, Continue: cont}) + if err != nil { + t.Fatal(err) + } + l := list.(*v1.PostList) + if l.ResourceVersion == "" { + t.Fatal("list has no resourceVersion") + } + all = append(all, names_(t, l)...) + if cont = l.Continue; cont == "" { + break + } + } + if !equal(all, []string{"a", "b", "c", "d", "e"}) { + t.Fatalf("pages = %v", all) + } + if _, err := s.List(ctx, &metainternalversion.ListOptions{Continue: "!!"}); !apierrors.IsBadRequest(err) { + t.Fatalf("bad continue: got %v, want BadRequest", err) + } +} + +func TestUserScopes(t *testing.T) { + requireDB(t) + s := voteStore(t, true) + alice, bob := asUser("alice-uid"), asUser("bob-uid") + + mustCreate(t, alice, s, vote("p1")) + mustCreate(t, alice, s, vote("p2")) + mustCreate(t, bob, s, vote("p1")) // same name, different owner + + for ctx, want := range map[context.Context][]string{alice: {"p1", "p2"}, bob: {"p1"}} { + list, err := s.List(ctx, &metainternalversion.ListOptions{}) + if err != nil { + t.Fatal(err) + } + if got := names_(t, list); !equal(got, want) { + t.Fatalf("got %v, want %v", got, want) + } + } + if _, err := s.Get(bob, "p2", &metav1.GetOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("bob reading alice's vote: got %v, want NotFound", err) + } + if _, _, err := s.Delete(bob, "p2", nil, &metav1.DeleteOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("bob deleting alice's vote: got %v, want NotFound", err) + } + + // Staff in the platform context see every user's votes. + staff := asPlatform() + list, err := s.List(staff, &metainternalversion.ListOptions{FieldSelector: fields.OneTermEqualSelector("spec.postRef.name", "p1")}) + if err != nil { + t.Fatal(err) + } + if got := names_(t, list); !equal(got, []string{"p1", "p1"}) { + t.Fatalf("platform list = %v", got) + } + if _, err := s.Get(staff, "p2", &metav1.GetOptions{}); err != nil { + t.Fatalf("platform get of a unique name: %v", err) + } + if _, err := s.Get(staff, "p1", &metav1.GetOptions{}); !apierrors.IsBadRequest(err) { + t.Fatalf("platform get of an ambiguous name: got %v, want BadRequest", err) + } + if _, err := s.Create(staff, vote("p3"), nil, &metav1.CreateOptions{}); !apierrors.IsForbidden(err) { + t.Fatalf("platform create: got %v, want Forbidden", err) + } + + org := request.WithUser(requestContext(), &user.DefaultInfo{Name: "x", Extra: map[string][]string{ + pg.ParentKindExtraKey: {"Organization"}, pg.ParentNameExtraKey: {"acme"}}}) + if _, err := s.List(org, &metainternalversion.ListOptions{}); !apierrors.IsForbidden(err) { + t.Fatalf("organization context: got %v, want Forbidden", err) + } +} + +func TestTransactions(t *testing.T) { + requireDB(t) + votes, subs := voteStore(t, true), subscriptionStore(t) + ctx := asUser("u1") + + // A vote and its subscription commit together... + err := db.InTx(ctx, func(ctx context.Context) error { + mustCreate(t, ctx, votes, vote("p1")) + _, err := subs.Create(ctx, &v1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "p1"}, + Spec: v1.SubscriptionSpec{PostRef: v1.PostReference{Name: "p1"}, Reason: v1.SubscriptionReasonVoted}}, nil, &metav1.CreateOptions{}) + return err + }) + if err != nil { + t.Fatal(err) + } + + // ...and roll back together. + boom := errors.New("boom") + err = db.InTx(ctx, func(ctx context.Context) error { + mustCreate(t, ctx, votes, vote("p2")) + mustCreate(t, ctx, subs, &v1.Subscription{ObjectMeta: metav1.ObjectMeta{Name: "p2"}, + Spec: v1.SubscriptionSpec{PostRef: v1.PostReference{Name: "p2"}}}) + return boom + }) + if !errors.Is(err, boom) { + t.Fatalf("got %v, want boom", err) + } + for _, s := range []*pg.Store{votes, subs} { + if _, err := s.Get(ctx, "p1", &metav1.GetOptions{}); err != nil { + t.Fatalf("committed row missing: %v", err) + } + if _, err := s.Get(ctx, "p2", &metav1.GetOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("rolled-back row present: %v", err) + } + } + + // A failing nested write rolls back only its savepoint when the caller + // handles the error. + err = db.InTx(ctx, func(ctx context.Context) error { + mustCreate(t, ctx, votes, vote("p3")) + if _, err := votes.Create(ctx, vote("p3"), nil, &metav1.CreateOptions{}); !apierrors.IsAlreadyExists(err) { + return fmt.Errorf("want AlreadyExists, got %v", err) + } + return nil + }) + if err != nil { + t.Fatal(err) + } + if _, err := votes.Get(ctx, "p3", &metav1.GetOptions{}); err != nil { + t.Fatalf("outer write lost after nested failure: %v", err) + } +} + +func TestDryRun(t *testing.T) { + requireDB(t) + s, ctx := postStore(t), asPlatform() + dry := []string{metav1.DryRunAll} + + out, err := s.Create(ctx, post("dry", "", nil), nil, &metav1.CreateOptions{DryRun: dry}) + if err != nil || out.(*v1.Post).UID == "" { + t.Fatalf("dry-run create: %v", err) + } + if _, err := s.Get(ctx, "dry", &metav1.GetOptions{}); !apierrors.IsNotFound(err) { + t.Fatalf("dry-run create persisted: %v", err) + } + + p := mustCreate(t, ctx, s, post("real", v1.PostStateBacklog, nil)).(*v1.Post) + p.Status.State = v1.PostStateShipped + if _, _, err := s.Update(ctx, "real", rest.DefaultUpdatedObjectInfo(p), nil, nil, false, &metav1.UpdateOptions{DryRun: dry}); err != nil { + t.Fatal(err) + } + if _, _, err := s.Delete(ctx, "real", nil, &metav1.DeleteOptions{DryRun: dry}); err != nil { + t.Fatal(err) + } + got, err := s.Get(ctx, "real", &metav1.GetOptions{}) + if err != nil { + t.Fatalf("dry-run delete removed the object: %v", err) + } + if got.(*v1.Post).Status.State != v1.PostStateBacklog { + t.Fatal("dry-run update persisted") + } +} diff --git a/migrations/00002_objects.sql b/migrations/00002_objects.sql new file mode 100644 index 0000000..24df3e2 --- /dev/null +++ b/migrations/00002_objects.sql @@ -0,0 +1,168 @@ +-- +goose Up +-- One table per kind. Every table has the same leading columns, which the +-- generic store in internal/storage/pg reads and writes: +-- +-- scope_type, scope_name who owns the object: ('Platform', '') or ('User', ) +-- name metadata.name, unique within a scope +-- uid metadata.uid +-- resource_version from resource_version_seq, bumped on every write +-- labels metadata.labels, for label selectors +-- object the full object as JSON, without resourceVersion +-- +-- The remaining columns are copied out of the object on every write, for +-- field selectors, sorting and joins. The object column is the source of truth. + +CREATE SEQUENCE resource_version_seq; + +CREATE TABLE posts ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + hidden boolean NOT NULL DEFAULT false, + state text NOT NULL DEFAULT '', + github_org text NOT NULL DEFAULT '', + github_repository text NOT NULL DEFAULT '', + github_number integer NOT NULL DEFAULT 0, + github_created_at timestamptz, + release text NOT NULL DEFAULT '', + votes_total integer NOT NULL DEFAULT 0, + trending_score double precision NOT NULL DEFAULT 0, + delivery_stage text NOT NULL DEFAULT '', + delivery_changed_at timestamptz, + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX posts_state_idx ON posts (state) WHERE NOT hidden; +CREATE INDEX posts_github_org_idx ON posts (github_org); +CREATE INDEX posts_release_idx ON posts (release) WHERE release <> ''; +CREATE INDEX posts_votes_total_idx ON posts (votes_total DESC, name); +CREATE INDEX posts_trending_idx ON posts (trending_score DESC, name); +CREATE INDEX posts_github_created_idx ON posts (github_created_at DESC NULLS LAST, name); +CREATE INDEX posts_delivery_changed_idx ON posts (delivery_changed_at DESC NULLS LAST, name); + +-- Votes and subscriptions are in the user scope: scope_name is the user UID +-- and name is the post name. Rows outlive their post so votes survive a post +-- that is removed and later returns. +CREATE TABLE votes ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + post_name text NOT NULL, + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX votes_post_idx ON votes (post_name, created_at); + +CREATE TABLE subscriptions ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + post_name text NOT NULL, + reason text NOT NULL DEFAULT '', + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX subscriptions_post_idx ON subscriptions (post_name); + +-- Submissions and security reports are created in the user scope. Staff list +-- them across users, so phase indexes don't lead with the scope. +CREATE TABLE submissions ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + kind text NOT NULL DEFAULT '', + phase text NOT NULL DEFAULT '', + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX submissions_phase_idx ON submissions (phase, created_at); +-- Names are generated, and staff fetch them by name across users, so they +-- must be unique across scopes too. +CREATE UNIQUE INDEX submissions_name_key ON submissions (name); + +CREATE TABLE security_reports ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + phase text NOT NULL DEFAULT '', + severity text NOT NULL DEFAULT '', + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX security_reports_phase_idx ON security_reports (phase, created_at); +CREATE UNIQUE INDEX security_reports_name_key ON security_reports (name); + +CREATE TABLE releases ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + shipped boolean NOT NULL DEFAULT false, + PRIMARY KEY (scope_type, scope_name, name) +); + +CREATE TABLE changelog_entries ( + scope_type text NOT NULL, + scope_name text NOT NULL, + name text NOT NULL, + uid uuid NOT NULL UNIQUE, + resource_version bigint NOT NULL, + created_at timestamptz NOT NULL, + labels jsonb NOT NULL DEFAULT '{}', + object jsonb NOT NULL, + published_at timestamptz, + PRIMARY KEY (scope_type, scope_name, name) +); +CREATE INDEX changelog_entries_published_idx ON changelog_entries (published_at DESC NULLS LAST, name); + +-- Post embeddings for PostQuery.spec.similarTo. The dimension is left open +-- until the AI gateway's embedding model is chosen; the ANN index is added +-- with it. +CREATE TABLE post_embeddings ( + post_name text PRIMARY KEY, + model text NOT NULL, + content_hash text NOT NULL, + embedding vector NOT NULL, + updated_at timestamptz NOT NULL DEFAULT now() +); + +-- Pending digest events per follower. feedback-controller appends through +-- the API and marks rows sent when the daily digest goes out. +CREATE TABLE digest_events ( + id bigserial PRIMARY KEY, + user_uid text NOT NULL, + post_name text NOT NULL DEFAULT '', + event text NOT NULL, + detail jsonb NOT NULL DEFAULT '{}', + created_at timestamptz NOT NULL DEFAULT now(), + sent_at timestamptz +); +CREATE INDEX digest_events_pending_idx ON digest_events (user_uid, created_at) WHERE sent_at IS NULL; + +-- +goose Down +-- Migrations are forward-only; see README.md. +SELECT 1;