pico
created pr with
97.1
added 97.2
1: fdc255e = 1: fdc255e refactor: remove unused db methods
2: 3f855f6 = 2: 3f855f6 chore: add tests for postgres db impl
3: 7cd29a6 = 3: 7cd29a6 refactor: use sqlx interface
4: acd449e = 4: acd449e chore: add db tags
5: f6ed0d7 = 5: f6ed0d7 refactor: use sqlx
6: 29035c6 = 6: 29035c6 refactor: replace custom sql functions with sqlx
7: d4ea6d9 = 7: d4ea6d9 refactor: inline all sql queries
-: ------- > 8: 4bd37ed refactor: use `select * from` where possible
cmds
checkout latest patchset:
ssh pr.pico.sh print 97 | git am -3checkout any patchset in a patch request:
ssh pr.pico.sh print 97.[rev] | git am -3add changes to patch request:
git format-patch main --stdout | ssh pr.pico.sh pr add 97
Patchset
97.2
refactor: remove unused db methods
Eric Bower
chore: add tests for postgres db impl
2025-12-18T03:03:10ZEric Bower
refactor: use sqlx interface
2025-12-18T03:17:57ZEric Bower
chore: add db tags
2025-12-18T03:27:57ZEric Bower
refactor: use sqlx
2025-12-18T03:31:25ZEric Bower
refactor: replace custom sql functions with sqlx
2025-12-18T03:43:01ZEric Bower
refactor: inline all sql queries
2025-12-18T03:57:00ZEric Bower
→ refactor: use `select * from` where possible
2025-12-18T04:02:50ZEric Bower
2025-12-18T04:33:58Z
refactor: use `select * from` where possible
Eric Bower
2025-12-18T04:33:58ZSemantic diff summary
2 added,
14 modified,
0 signature changed,
0 removed
across 2 analyzed files
pkg/db/postgres/storage.go
-
method_declarationfindPublicKeymodified -
method_declarationFindKeysForUsermodified -
method_declarationFindUserByNamemodified -
method_declarationFindFeaturemodified -
method_declarationFindFeaturesForUsermodified -
method_declarationFindFeedItemsByPostIDmodified -
method_declarationFindProjectByNamemodified -
method_declarationFindTokensForUsermodified -
method_declarationFindTunsEventLogsByAddrmodified -
method_declarationFindTunsEventLogsmodified -
method_declarationFindAccessLogsmodified -
method_declarationFindAccessLogsByPubkeymodified
+19
-6
pkg/db/db.go
#
| ... | ... | @@ -205,13 +205,25 @@ type AnalyticsVisits struct { | |
| 205 | 205 | ContentType string `json:"content_type" db:"content_type"` | |
| 206 | 206 | } | |
| 207 | 207 | ||
| 208 | + | type AccessLogData struct{} | |
| 209 | + | ||
| 210 | + | func (p *AccessLogData) Scan(value any) error { | |
| 211 | + | b, err := tcast(value) | |
| 212 | + | if err != nil { | |
| 213 | + | return err | |
| 214 | + | } | |
| 215 | + | ||
| 216 | + | return json.Unmarshal(b, &p) | |
| 217 | + | } | |
| 218 | + | ||
| 208 | 219 | type AccessLog struct { | |
| 209 | - | ID string `json:"id" db:"id"` | |
| 210 | - | UserID string `json:"user_id" db:"user_id"` | |
| 211 | - | Service string `json:"service" db:"service"` | |
| 212 | - | Pubkey string `json:"pubkey" db:"pubkey"` | |
| 213 | - | Identity string `json:"identity" db:"identity"` | |
| 214 | - | CreatedAt *time.Time `json:"created_at" db:"created_at"` | |
| 220 | + | ID string `json:"id" db:"id"` | |
| 221 | + | UserID string `json:"user_id" db:"user_id"` | |
| 222 | + | Service string `json:"service" db:"service"` | |
| 223 | + | Pubkey string `json:"pubkey" db:"pubkey"` | |
| 224 | + | Identity string `json:"identity" db:"identity"` | |
| 225 | + | Data AccessLogData `json:"data" db:"data"` | |
| 226 | + | CreatedAt *time.Time `json:"created_at" db:"created_at"` | |
| 215 | 227 | } | |
| 216 | 228 | ||
| 217 | 229 | type Pager struct { |
| ... | ... | @@ -231,6 +243,7 @@ type Token struct { | |
| 231 | 243 | ID string `json:"id" db:"id"` | |
| 232 | 244 | UserID string `json:"user_id" db:"user_id"` | |
| 233 | 245 | Name string `json:"name" db:"name"` | |
| 246 | + | Token string `json:"token" db:"token"` | |
| 234 | 247 | CreatedAt *time.Time `json:"created_at" db:"created_at"` | |
| 235 | 248 | ExpiresAt *time.Time `json:"expires_at" db:"expires_at"` | |
| 236 | 249 | } |
+12
-15
pkg/db/postgres/storage.go
#
| ... | ... | @@ -199,7 +199,7 @@ func (me *PsqlDB) findPublicKeyForKey(key string) (*db.PublicKey, error) { | |
| 199 | 199 | ||
| 200 | 200 | func (me *PsqlDB) findPublicKey(pubkeyID string) (*db.PublicKey, error) { | |
| 201 | 201 | pk := &db.PublicKey{} | |
| 202 | - | err := me.Db.Get(pk, `SELECT id, user_id, name, public_key, created_at FROM public_keys WHERE id = $1`, pubkeyID) | |
| 202 | + | err := me.Db.Get(pk, `SELECT * FROM public_keys WHERE id = $1`, pubkeyID) | |
| 203 | 203 | if err != nil { | |
| 204 | 204 | return nil, err | |
| 205 | 205 | } |
| ... | ... | @@ -208,7 +208,7 @@ func (me *PsqlDB) findPublicKey(pubkeyID string) (*db.PublicKey, error) { | |
| 208 | 208 | ||
| 209 | 209 | func (me *PsqlDB) FindKeysForUser(user *db.User) ([]*db.PublicKey, error) { | |
| 210 | 210 | var keys []*db.PublicKey | |
| 211 | - | err := me.Db.Select(&keys, `SELECT id, user_id, name, public_key, created_at FROM public_keys WHERE user_id = $1 ORDER BY created_at ASC`, user.ID) | |
| 211 | + | err := me.Db.Select(&keys, `SELECT * FROM public_keys WHERE user_id = $1 ORDER BY created_at ASC`, user.ID) | |
| 212 | 212 | if err != nil { | |
| 213 | 213 | return nil, err | |
| 214 | 214 | } |
| ... | ... | @@ -328,7 +328,7 @@ func (me *PsqlDB) validateName(name string) (bool, error) { | |
| 328 | 328 | ||
| 329 | 329 | func (me *PsqlDB) FindUserByName(name string) (*db.User, error) { | |
| 330 | 330 | user := &db.User{} | |
| 331 | - | err := me.Db.Get(user, `SELECT id, name, created_at FROM app_users WHERE name = $1`, strings.ToLower(name)) | |
| 331 | + | err := me.Db.Get(user, `SELECT * FROM app_users WHERE name = $1`, strings.ToLower(name)) | |
| 332 | 332 | if err != nil { | |
| 333 | 333 | return nil, err | |
| 334 | 334 | } |
| ... | ... | @@ -1160,7 +1160,7 @@ func (me *PsqlDB) FindPopularTags(space string) ([]string, error) { | |
| 1160 | 1160 | ||
| 1161 | 1161 | func (me *PsqlDB) FindFeature(userID string, feature string) (*db.FeatureFlag, error) { | |
| 1162 | 1162 | ff := &db.FeatureFlag{} | |
| 1163 | - | err := me.Db.Get(ff, `SELECT id, user_id, payment_history_id, name, data, created_at, expires_at FROM feature_flags WHERE user_id = $1 AND name = $2 ORDER BY expires_at DESC LIMIT 1`, userID, feature) | |
| 1163 | + | err := me.Db.Get(ff, `SELECT * FROM feature_flags WHERE user_id = $1 AND name = $2 ORDER BY expires_at DESC LIMIT 1`, userID, feature) | |
| 1164 | 1164 | if err != nil { | |
| 1165 | 1165 | return nil, err | |
| 1166 | 1166 | } |
| ... | ... | @@ -1170,8 +1170,7 @@ func (me *PsqlDB) FindFeature(userID string, feature string) (*db.FeatureFlag, e | |
| 1170 | 1170 | func (me *PsqlDB) FindFeaturesForUser(userID string) ([]*db.FeatureFlag, error) { | |
| 1171 | 1171 | var features []*db.FeatureFlag | |
| 1172 | 1172 | // https://stackoverflow.com/a/16920077 | |
| 1173 | - | query := `SELECT DISTINCT ON (name) | |
| 1174 | - | id, user_id, payment_history_id, name, data, created_at, expires_at | |
| 1173 | + | query := `SELECT DISTINCT ON (name) * | |
| 1175 | 1174 | FROM feature_flags | |
| 1176 | 1175 | WHERE user_id=$1 | |
| 1177 | 1176 | ORDER BY name, expires_at DESC;` |
| ... | ... | @@ -1220,7 +1219,7 @@ func (me *PsqlDB) InsertFeedItems(postID string, items []*db.FeedItem) error { | |
| 1220 | 1219 | ||
| 1221 | 1220 | func (me *PsqlDB) FindFeedItemsByPostID(postID string) ([]*db.FeedItem, error) { | |
| 1222 | 1221 | var items []*db.FeedItem | |
| 1223 | - | err := me.Db.Select(&items, `SELECT id, post_id, guid, data, created_at FROM feed_items WHERE post_id=$1`, postID) | |
| 1222 | + | err := me.Db.Select(&items, `SELECT * FROM feed_items WHERE post_id=$1`, postID) | |
| 1224 | 1223 | if err != nil { | |
| 1225 | 1224 | return nil, err | |
| 1226 | 1225 | } |
| ... | ... | @@ -1247,7 +1246,7 @@ func (me *PsqlDB) UpdateProject(userID, name string) error { | |
| 1247 | 1246 | ||
| 1248 | 1247 | func (me *PsqlDB) FindProjectByName(userID, name string) (*db.Project, error) { | |
| 1249 | 1248 | project := &db.Project{} | |
| 1250 | - | err := me.Db.Get(project, `SELECT id, user_id, name, project_dir, acl, blocked, created_at, updated_at FROM projects WHERE user_id = $1 AND name = $2;`, userID, name) | |
| 1249 | + | err := me.Db.Get(project, `SELECT * FROM projects WHERE user_id = $1 AND name = $2;`, userID, name) | |
| 1251 | 1250 | if err != nil { | |
| 1252 | 1251 | return nil, err | |
| 1253 | 1252 | } |
| ... | ... | @@ -1289,7 +1288,7 @@ func (me *PsqlDB) RemoveToken(tokenID string) error { | |
| 1289 | 1288 | ||
| 1290 | 1289 | func (me *PsqlDB) FindTokensForUser(userID string) ([]*db.Token, error) { | |
| 1291 | 1290 | var tokens []*db.Token | |
| 1292 | - | err := me.Db.Select(&tokens, `SELECT id, user_id, name, created_at, expires_at FROM tokens WHERE user_id = $1`, userID) | |
| 1291 | + | err := me.Db.Select(&tokens, `SELECT * FROM tokens WHERE user_id = $1`, userID) | |
| 1293 | 1292 | if err != nil { | |
| 1294 | 1293 | return nil, err | |
| 1295 | 1294 | } |
| ... | ... | @@ -1425,8 +1424,7 @@ func (me *PsqlDB) InsertTunsEventLog(log *db.TunsEventLog) error { | |
| 1425 | 1424 | func (me *PsqlDB) FindTunsEventLogsByAddr(userID, addr string) ([]*db.TunsEventLog, error) { | |
| 1426 | 1425 | var logs []*db.TunsEventLog | |
| 1427 | 1426 | err := me.Db.Select(&logs, | |
| 1428 | - | `SELECT id, user_id, server_id, remote_addr, event_type, tunnel_type, connection_type, tunnel_id, created_at | |
| 1429 | - | FROM tuns_event_logs WHERE user_id=$1 AND tunnel_id=$2 ORDER BY created_at DESC`, userID, addr) | |
| 1427 | + | `SELECT * FROM tuns_event_logs WHERE user_id=$1 AND tunnel_id=$2 ORDER BY created_at DESC`, userID, addr) | |
| 1430 | 1428 | if err != nil { | |
| 1431 | 1429 | return nil, err | |
| 1432 | 1430 | } |
| ... | ... | @@ -1436,8 +1434,7 @@ func (me *PsqlDB) FindTunsEventLogsByAddr(userID, addr string) ([]*db.TunsEventL | |
| 1436 | 1434 | func (me *PsqlDB) FindTunsEventLogs(userID string) ([]*db.TunsEventLog, error) { | |
| 1437 | 1435 | var logs []*db.TunsEventLog | |
| 1438 | 1436 | err := me.Db.Select(&logs, | |
| 1439 | - | `SELECT id, user_id, server_id, remote_addr, event_type, tunnel_type, connection_type, tunnel_id, created_at | |
| 1440 | - | FROM tuns_event_logs WHERE user_id=$1 ORDER BY created_at DESC`, userID) | |
| 1437 | + | `SELECT * FROM tuns_event_logs WHERE user_id=$1 ORDER BY created_at DESC`, userID) | |
| 1441 | 1438 | if err != nil { | |
| 1442 | 1439 | return nil, err | |
| 1443 | 1440 | } |
| ... | ... | @@ -1482,7 +1479,7 @@ func (me *PsqlDB) FindUserStats(userID string) (*db.UserStats, error) { | |
| 1482 | 1479 | ||
| 1483 | 1480 | func (me *PsqlDB) FindAccessLogs(userID string, fromDate *time.Time) ([]*db.AccessLog, error) { | |
| 1484 | 1481 | var logs []*db.AccessLog | |
| 1485 | - | err := me.Db.Select(&logs, `SELECT id, user_id, service, pubkey, identity, created_at FROM access_logs WHERE user_id=$1 AND created_at >= $2 ORDER BY created_at DESC`, userID, fromDate) | |
| 1482 | + | err := me.Db.Select(&logs, `SELECT * FROM access_logs WHERE user_id=$1 AND created_at >= $2 ORDER BY created_at DESC`, userID, fromDate) | |
| 1486 | 1483 | if err != nil { | |
| 1487 | 1484 | return nil, err | |
| 1488 | 1485 | } |
| ... | ... | @@ -1491,7 +1488,7 @@ func (me *PsqlDB) FindAccessLogs(userID string, fromDate *time.Time) ([]*db.Acce | |
| 1491 | 1488 | ||
| 1492 | 1489 | func (me *PsqlDB) FindAccessLogsByPubkey(pubkey string, fromDate *time.Time) ([]*db.AccessLog, error) { | |
| 1493 | 1490 | var logs []*db.AccessLog | |
| 1494 | - | err := me.Db.Select(&logs, `SELECT id, user_id, service, pubkey, identity, created_at FROM access_logs WHERE pubkey=$1 AND created_at >= $2 ORDER BY created_at DESC`, pubkey, fromDate) | |
| 1491 | + | err := me.Db.Select(&logs, `SELECT * FROM access_logs WHERE pubkey=$1 AND created_at >= $2 ORDER BY created_at DESC`, pubkey, fromDate) | |
| 1495 | 1492 | if err != nil { | |
| 1496 | 1493 | return nil, err | |
| 1497 | 1494 | } |