appview/db: return ids in a few places #592

merged
opened by anirudh.fi targeting master from push-xwotmtuuvokm
Changed files
+76 -41
appview
db
notify
db
state
-7
appview/db/db.go
··· 818 818 _, err := tx.Exec(` 819 819 alter table spindles add column needs_upgrade integer not null default 0; 820 820 `) 821 - if err != nil { 822 - return err 823 - } 824 - 825 - _, err = tx.Exec(` 826 - update spindles set needs_upgrade = 1; 827 - `) 828 821 return err 829 822 }) 830 823
+12 -1
appview/db/pulls.go
··· 55 55 parentChangeId = &pull.ParentChangeId 56 56 } 57 57 58 - _, err = tx.Exec( 58 + result, err := tx.Exec( 59 59 ` 60 60 insert into pulls ( 61 61 repo_at, owner_did, pull_id, title, target_branch, body, rkey, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id ··· 79 79 return err 80 80 } 81 81 82 + // Set the database primary key ID 83 + id, err := result.LastInsertId() 84 + if err != nil { 85 + return err 86 + } 87 + pull.ID = int(id) 88 + 82 89 _, err = tx.Exec(` 83 90 insert into pull_submissions (pull_id, repo_at, round_number, patch, source_rev) 84 91 values (?, ?, ?, ?, ?) ··· 121 128 122 129 query := fmt.Sprintf(` 123 130 select 131 + id, 124 132 owner_did, 125 133 repo_at, 126 134 pull_id, ··· 154 162 var createdAt string 155 163 var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString 156 164 err := rows.Scan( 165 + &pull.ID, 157 166 &pull.OwnerDid, 158 167 &pull.RepoAt, 159 168 &pull.PullId, ··· 325 334 func GetPull(e Execer, repoAt syntax.ATURI, pullId int) (*models.Pull, error) { 326 335 query := ` 327 336 select 337 + id, 328 338 owner_did, 329 339 pull_id, 330 340 created, ··· 350 360 var createdAt string 351 361 var sourceBranch, sourceRepoAt, stackId, changeId, parentChangeId sql.NullString 352 362 err := row.Scan( 363 + &pull.ID, 353 364 &pull.OwnerDid, 354 365 &pull.PullId, 355 366 &createdAt,
+36 -6
appview/db/repos.go
··· 10 10 "time" 11 11 12 12 "github.com/bluesky-social/indigo/atproto/syntax" 13 + securejoin "github.com/cyphar/filepath-securejoin" 14 + "tangled.org/core/api/tangled" 13 15 "tangled.org/core/appview/models" 14 16 ) 15 17 18 + type Repo struct { 19 + Id int64 20 + Did string 21 + Name string 22 + Knot string 23 + Rkey string 24 + Created time.Time 25 + Description string 26 + Spindle string 27 + 28 + // optionally, populate this when querying for reverse mappings 29 + RepoStats *models.RepoStats 30 + 31 + // optional 32 + Source string 33 + } 34 + 35 + func (r Repo) RepoAt() syntax.ATURI { 36 + return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey)) 37 + } 38 + 39 + func (r Repo) DidSlashRepo() string { 40 + p, _ := securejoin.SecureJoin(r.Did, r.Name) 41 + return p 42 + } 43 + 16 44 func GetRepos(e Execer, limit int, filters ...filter) ([]models.Repo, error) { 17 45 repoMap := make(map[syntax.ATURI]*models.Repo) 18 46 ··· 35 63 36 64 repoQuery := fmt.Sprintf( 37 65 `select 66 + id, 38 67 did, 39 68 name, 40 69 knot, ··· 63 92 var description, source, spindle sql.NullString 64 93 65 94 err := rows.Scan( 95 + &repo.Id, 66 96 &repo.Did, 67 97 &repo.Name, 68 98 &repo.Knot, ··· 327 357 var repo models.Repo 328 358 var nullableDescription sql.NullString 329 359 330 - row := e.QueryRow(`select did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri) 360 + row := e.QueryRow(`select id, did, name, knot, created, rkey, description from repos where at_uri = ?`, atUri) 331 361 332 362 var createdAt string 333 - if err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil { 363 + if err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &createdAt, &repo.Rkey, &nullableDescription); err != nil { 334 364 return nil, err 335 365 } 336 366 createdAtTime, _ := time.Parse(time.RFC3339, createdAt) ··· 373 403 var repos []models.Repo 374 404 375 405 rows, err := e.Query( 376 - `select distinct r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source 406 + `select distinct r.id, r.did, r.name, r.knot, r.rkey, r.description, r.created, r.source 377 407 from repos r 378 408 left join collaborators c on r.at_uri = c.repo_at 379 409 where (r.did = ? or c.subject_did = ?) ··· 393 423 var nullableDescription sql.NullString 394 424 var nullableSource sql.NullString 395 425 396 - err := rows.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource) 426 + err := rows.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource) 397 427 if err != nil { 398 428 return nil, err 399 429 } ··· 430 460 var nullableSource sql.NullString 431 461 432 462 row := e.QueryRow( 433 - `select did, name, knot, rkey, description, created, source 463 + `select id, did, name, knot, rkey, description, created, source 434 464 from repos 435 465 where did = ? and name = ? and source is not null and source != ''`, 436 466 did, name, 437 467 ) 438 468 439 - err := row.Scan(&repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource) 469 + err := row.Scan(&repo.Id, &repo.Did, &repo.Name, &repo.Knot, &repo.Rkey, &nullableDescription, &createdAt, &nullableSource) 440 470 if err != nil { 441 471 return nil, err 442 472 }
+27 -27
appview/notify/db/db.go
··· 4 4 "context" 5 5 "log" 6 6 7 - "tangled.sh/tangled.sh/core/appview/db" 8 - "tangled.sh/tangled.sh/core/appview/notify" 9 - "tangled.sh/tangled.sh/core/idresolver" 7 + "tangled.org/core/appview/db" 8 + "tangled.org/core/appview/models" 9 + "tangled.org/core/appview/notify" 10 + "tangled.org/core/idresolver" 10 11 ) 11 12 12 13 type databaseNotifier struct { ··· 23 24 24 25 var _ notify.Notifier = &databaseNotifier{} 25 26 26 - func (n *databaseNotifier) NewRepo(ctx context.Context, repo *db.Repo) { 27 + func (n *databaseNotifier) NewRepo(ctx context.Context, repo *models.Repo) { 27 28 // no-op for now 28 29 } 29 30 30 - func (n *databaseNotifier) NewStar(ctx context.Context, star *db.Star) { 31 + func (n *databaseNotifier) NewStar(ctx context.Context, star *models.Star) { 31 32 var err error 32 33 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(star.RepoAt))) 33 34 if err != nil { ··· 61 62 Type: models.NotificationTypeRepoStarred, 62 63 EntityType: "repo", 63 64 EntityId: string(star.RepoAt), 64 - RepoId: &repo.ID, 65 + RepoId: &repo.Id, 65 66 } 66 - 67 67 err = n.db.CreateNotification(ctx, notification) 68 68 if err != nil { 69 69 log.Printf("NewStar: failed to create notification: %v", err) ··· 71 71 } 72 72 } 73 73 74 - func (n *databaseNotifier) DeleteStar(ctx context.Context, star *db.Star) { 74 + func (n *databaseNotifier) DeleteStar(ctx context.Context, star *models.Star) { 75 75 // no-op 76 76 } 77 77 78 - func (n *databaseNotifier) NewIssue(ctx context.Context, issue *db.Issue) { 78 + func (n *databaseNotifier) NewIssue(ctx context.Context, issue *models.Issue) { 79 79 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt))) 80 80 if err != nil { 81 81 log.Printf("NewIssue: failed to get repos: %v", err) ··· 106 106 Type: models.NotificationTypeIssueCreated, 107 107 EntityType: "issue", 108 108 EntityId: string(issue.AtUri()), 109 - RepoId: &repo.ID, 109 + RepoId: &repo.Id, 110 110 IssueId: &issue.Id, 111 111 } 112 112 ··· 117 117 } 118 118 } 119 119 120 - func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *db.IssueComment) { 120 + func (n *databaseNotifier) NewIssueComment(ctx context.Context, comment *models.IssueComment) { 121 121 issues, err := db.GetIssues(n.db, db.FilterEq("at_uri", comment.IssueAt)) 122 122 if err != nil { 123 123 log.Printf("NewIssueComment: failed to get issues: %v", err) ··· 170 170 Type: models.NotificationTypeIssueCommented, 171 171 EntityType: "issue", 172 172 EntityId: string(issue.AtUri()), 173 - RepoId: &repo.ID, 173 + RepoId: &repo.Id, 174 174 IssueId: &issue.Id, 175 175 } 176 176 ··· 181 181 } 182 182 } 183 183 184 - func (n *databaseNotifier) NewFollow(ctx context.Context, follow *db.Follow) { 184 + func (n *databaseNotifier) NewFollow(ctx context.Context, follow *models.Follow) { 185 185 prefs, err := n.db.GetNotificationPreferences(ctx, follow.SubjectDid) 186 186 if err != nil { 187 187 log.Printf("NewFollow: failed to get notification preferences for %s: %v", follow.SubjectDid, err) ··· 206 206 } 207 207 } 208 208 209 - func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *db.Follow) { 209 + func (n *databaseNotifier) DeleteFollow(ctx context.Context, follow *models.Follow) { 210 210 // no-op 211 211 } 212 212 213 - func (n *databaseNotifier) NewPull(ctx context.Context, pull *db.Pull) { 213 + func (n *databaseNotifier) NewPull(ctx context.Context, pull *models.Pull) { 214 214 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) 215 215 if err != nil { 216 216 log.Printf("NewPull: failed to get repos: %v", err) ··· 241 241 Type: models.NotificationTypePullCreated, 242 242 EntityType: "pull", 243 243 EntityId: string(pull.RepoAt), 244 - RepoId: &repo.ID, 244 + RepoId: &repo.Id, 245 245 PullId: func() *int64 { id := int64(pull.ID); return &id }(), 246 246 } 247 247 ··· 252 252 } 253 253 } 254 254 255 - func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *db.PullComment) { 255 + func (n *databaseNotifier) NewPullComment(ctx context.Context, comment *models.PullComment) { 256 256 pulls, err := db.GetPulls(n.db, 257 257 db.FilterEq("repo_at", comment.RepoAt), 258 258 db.FilterEq("pull_id", comment.PullId)) ··· 306 306 Type: models.NotificationTypePullCommented, 307 307 EntityType: "pull", 308 308 EntityId: comment.RepoAt, 309 - RepoId: &repo.ID, 309 + RepoId: &repo.Id, 310 310 PullId: func() *int64 { id := int64(pull.ID); return &id }(), 311 311 } 312 312 ··· 317 317 } 318 318 } 319 319 320 - func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *db.Profile) { 320 + func (n *databaseNotifier) UpdateProfile(ctx context.Context, profile *models.Profile) { 321 321 // no-op 322 322 } 323 323 ··· 325 325 // no-op 326 326 } 327 327 328 - func (n *databaseNotifier) EditString(ctx context.Context, string *db.String) { 328 + func (n *databaseNotifier) EditString(ctx context.Context, string *models.String) { 329 329 // no-op 330 330 } 331 331 332 - func (n *databaseNotifier) NewString(ctx context.Context, string *db.String) { 332 + func (n *databaseNotifier) NewString(ctx context.Context, string *models.String) { 333 333 // no-op 334 334 } 335 335 336 - func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *db.Issue) { 336 + func (n *databaseNotifier) NewIssueClosed(ctx context.Context, issue *models.Issue) { 337 337 // Get repo details 338 338 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(issue.RepoAt))) 339 339 if err != nil { ··· 367 367 Type: models.NotificationTypeIssueClosed, 368 368 EntityType: "issue", 369 369 EntityId: string(issue.AtUri()), 370 - RepoId: &repo.ID, 370 + RepoId: &repo.Id, 371 371 IssueId: &issue.Id, 372 372 } 373 373 ··· 378 378 } 379 379 } 380 380 381 - func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *db.Pull) { 381 + func (n *databaseNotifier) NewPullMerged(ctx context.Context, pull *models.Pull) { 382 382 // Get repo details 383 383 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) 384 384 if err != nil { ··· 412 412 Type: models.NotificationTypePullMerged, 413 413 EntityType: "pull", 414 414 EntityId: string(pull.RepoAt), 415 - RepoId: &repo.ID, 415 + RepoId: &repo.Id, 416 416 PullId: func() *int64 { id := int64(pull.ID); return &id }(), 417 417 } 418 418 ··· 423 423 } 424 424 } 425 425 426 - func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *db.Pull) { 426 + func (n *databaseNotifier) NewPullClosed(ctx context.Context, pull *models.Pull) { 427 427 // Get repo details 428 428 repos, err := db.GetRepos(n.db, 1, db.FilterEq("at_uri", string(pull.RepoAt))) 429 429 if err != nil { ··· 457 457 Type: models.NotificationTypePullClosed, 458 458 EntityType: "pull", 459 459 EntityId: string(pull.RepoAt), 460 - RepoId: &repo.ID, 460 + RepoId: &repo.Id, 461 461 PullId: func() *int64 { id := int64(pull.ID); return &id }(), 462 462 } 463 463
+1
appview/state/state.go
··· 25 25 "tangled.org/core/appview/db" 26 26 "tangled.org/core/appview/models" 27 27 "tangled.org/core/appview/notify" 28 + dbnotify "tangled.org/core/appview/notify/db" 28 29 phnotify "tangled.org/core/appview/notify/posthog" 29 30 "tangled.org/core/appview/oauth" 30 31 "tangled.org/core/appview/pages"