Monorepo for Tangled

[WIP] appview: add repo_did columns, migrations, and model struct fields

+382 -12
+318
appview/db/db.go
··· 1255 1255 return err 1256 1256 }) 1257 1257 1258 + orm.RunMigration(conn, logger, "add-repo-did-column", func(tx *sql.Tx) error { 1259 + _, err := tx.Exec(` 1260 + alter table repos add column repo_did text; 1261 + create unique index if not exists idx_repos_repo_did on repos(repo_did); 1262 + 1263 + alter table issues add column repo_did text; 1264 + alter table pulls add column repo_did text; 1265 + alter table artifacts add column repo_did text; 1266 + alter table webhooks add column repo_did text; 1267 + alter table collaborators add column repo_did text; 1268 + alter table pull_comments add column repo_did text; 1269 + alter table profile_pinned_repositories add column repo_did text; 1270 + alter table repo_issue_seqs add column repo_did text; 1271 + alter table repo_pull_seqs add column repo_did text; 1272 + alter table repo_languages add column repo_did text; 1273 + alter table repo_labels add column repo_did text; 1274 + alter table stars add column subject_did text; 1275 + `) 1276 + return err 1277 + }) 1278 + 1279 + conn.ExecContext(ctx, "pragma foreign_keys = off;") 1280 + orm.RunMigration(conn, logger, "add-repo-did-fk-constraints", func(tx *sql.Tx) error { 1281 + _, err := tx.Exec(` 1282 + create table repo_issue_seqs_new ( 1283 + repo_at text primary key, 1284 + next_issue_id integer not null default 1, 1285 + repo_did text, 1286 + foreign key (repo_did) references repos(repo_did) on delete cascade 1287 + ); 1288 + insert into repo_issue_seqs_new select repo_at, next_issue_id, repo_did from repo_issue_seqs; 1289 + drop table repo_issue_seqs; 1290 + alter table repo_issue_seqs_new rename to repo_issue_seqs; 1291 + 1292 + create table repo_pull_seqs_new ( 1293 + repo_at text primary key, 1294 + next_pull_id integer not null default 1, 1295 + repo_did text, 1296 + foreign key (repo_did) references repos(repo_did) on delete cascade 1297 + ); 1298 + insert into repo_pull_seqs_new select repo_at, next_pull_id, repo_did from repo_pull_seqs; 1299 + drop table repo_pull_seqs; 1300 + alter table repo_pull_seqs_new rename to repo_pull_seqs; 1301 + 1302 + create table repo_languages_new ( 1303 + id integer primary key autoincrement, 1304 + repo_at text not null, 1305 + ref text not null, 1306 + is_default_ref integer not null default 0, 1307 + language text not null, 1308 + bytes integer not null check (bytes >= 0), 1309 + repo_did text, 1310 + unique(repo_at, ref, language), 1311 + foreign key (repo_did) references repos(repo_did) on delete cascade 1312 + ); 1313 + insert into repo_languages_new select id, repo_at, ref, is_default_ref, language, bytes, repo_did from repo_languages; 1314 + drop table repo_languages; 1315 + alter table repo_languages_new rename to repo_languages; 1316 + 1317 + create table repo_labels_new ( 1318 + id integer primary key autoincrement, 1319 + repo_at text not null, 1320 + label_at text not null, 1321 + repo_did text, 1322 + unique (repo_at, label_at), 1323 + foreign key (repo_did) references repos(repo_did) on delete cascade 1324 + ); 1325 + insert into repo_labels_new select id, repo_at, label_at, repo_did from repo_labels; 1326 + drop table repo_labels; 1327 + alter table repo_labels_new rename to repo_labels; 1328 + `) 1329 + return err 1330 + }) 1331 + conn.ExecContext(ctx, "pragma foreign_keys = on;") 1332 + 1333 + orm.RunMigration(conn, logger, "add-repo-did-indexes", func(tx *sql.Tx) error { 1334 + _, err := tx.Exec(` 1335 + create index if not exists idx_issues_repo_did on issues(repo_did); 1336 + create index if not exists idx_pulls_repo_did on pulls(repo_did); 1337 + create index if not exists idx_artifacts_repo_did on artifacts(repo_did); 1338 + create index if not exists idx_collaborators_repo_did on collaborators(repo_did); 1339 + create index if not exists idx_pull_comments_repo_did on pull_comments(repo_did); 1340 + create index if not exists idx_stars_subject_did on stars(subject_did); 1341 + `) 1342 + return err 1343 + }) 1344 + 1345 + orm.RunMigration(conn, logger, "add-repo-did-indexes-2", func(tx *sql.Tx) error { 1346 + _, err := tx.Exec(` 1347 + create index if not exists idx_repo_issue_seqs_repo_did on repo_issue_seqs(repo_did); 1348 + create index if not exists idx_repo_pull_seqs_repo_did on repo_pull_seqs(repo_did); 1349 + create index if not exists idx_repo_languages_repo_did on repo_languages(repo_did); 1350 + create index if not exists idx_repo_labels_repo_did on repo_labels(repo_did); 1351 + create index if not exists idx_webhooks_repo_did on webhooks(repo_did); 1352 + `) 1353 + return err 1354 + }) 1355 + 1356 + orm.RunMigration(conn, logger, "add-pds-rewrite-status", func(tx *sql.Tx) error { 1357 + _, err := tx.Exec(` 1358 + create table if not exists pds_rewrite_status ( 1359 + id integer primary key autoincrement, 1360 + user_did text not null, 1361 + repo_did text not null, 1362 + record_nsid text not null, 1363 + record_rkey text not null, 1364 + old_repo_at text not null, 1365 + status text not null default 'pending', 1366 + updated_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1367 + unique(user_did, record_nsid, record_rkey) 1368 + ); 1369 + create index if not exists idx_pds_rewrite_user on pds_rewrite_status(user_did, status); 1370 + `) 1371 + return err 1372 + }) 1373 + 1374 + orm.RunMigration(conn, logger, "add-pipelines-repo-did", func(tx *sql.Tx) error { 1375 + _, err := tx.Exec(` 1376 + alter table pipelines add column repo_did text; 1377 + create index if not exists idx_pipelines_repo_did on pipelines(repo_did); 1378 + `) 1379 + return err 1380 + }) 1381 + 1382 + conn.ExecContext(ctx, "pragma foreign_keys = off;") 1383 + orm.RunMigration(conn, logger, "add-repo-did-fk-content-tables", func(tx *sql.Tx) error { 1384 + _, err := tx.Exec(` 1385 + -- issues: add FK on repo_did 1386 + create table issues_fk ( 1387 + id integer primary key autoincrement, 1388 + did text not null, 1389 + rkey text not null, 1390 + at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue' || '/' || rkey) stored, 1391 + repo_at text not null, 1392 + issue_id integer not null, 1393 + title text not null, 1394 + body text not null, 1395 + open integer not null default 1, 1396 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1397 + edited text, 1398 + deleted text, 1399 + repo_did text, 1400 + unique(did, rkey), 1401 + unique(repo_at, issue_id), 1402 + unique(at_uri), 1403 + foreign key (repo_at) references repos(at_uri) on delete cascade, 1404 + foreign key (repo_did) references repos(repo_did) on delete set null 1405 + ); 1406 + insert into issues_fk (id, did, rkey, repo_at, issue_id, title, body, open, created, edited, deleted, repo_did) 1407 + select id, did, rkey, repo_at, issue_id, title, body, open, created, edited, deleted, repo_did from issues; 1408 + drop table issues; 1409 + alter table issues_fk rename to issues; 1410 + create index if not exists idx_issues_repo_did on issues(repo_did); 1411 + 1412 + -- pulls: add FK on repo_did 1413 + create table pulls_fk ( 1414 + id integer primary key autoincrement, 1415 + pull_id integer not null, 1416 + at_uri text generated always as ('at://' || owner_did || '/' || 'sh.tangled.repo.pull' || '/' || rkey) stored, 1417 + repo_at text not null, 1418 + owner_did text not null, 1419 + rkey text not null, 1420 + title text not null, 1421 + body text not null, 1422 + target_branch text not null, 1423 + state integer not null default 0 check (state in (0, 1, 2, 3)), 1424 + source_branch text, 1425 + source_repo_at text, 1426 + stack_id text, 1427 + change_id text, 1428 + parent_change_id text, 1429 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1430 + repo_did text, 1431 + unique(repo_at, pull_id), 1432 + unique(at_uri), 1433 + foreign key (repo_at) references repos(at_uri) on delete cascade, 1434 + foreign key (repo_did) references repos(repo_did) on delete set null 1435 + ); 1436 + insert into pulls_fk (id, pull_id, repo_at, owner_did, rkey, title, body, target_branch, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id, created, repo_did) 1437 + select id, pull_id, repo_at, owner_did, rkey, title, body, target_branch, state, source_branch, source_repo_at, stack_id, change_id, parent_change_id, created, repo_did from pulls; 1438 + drop table pulls; 1439 + alter table pulls_fk rename to pulls; 1440 + create index if not exists idx_pulls_repo_did on pulls(repo_did); 1441 + 1442 + -- artifacts: add FK on repo_did 1443 + create table artifacts_fk ( 1444 + id integer primary key autoincrement, 1445 + did text not null, 1446 + rkey text not null, 1447 + repo_at text not null, 1448 + tag binary(20) not null, 1449 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1450 + blob_cid text not null, 1451 + name text not null, 1452 + size integer not null default 0, 1453 + mimetype string not null default '*/*', 1454 + repo_did text, 1455 + unique(did, rkey), 1456 + unique(repo_at, tag, name), 1457 + foreign key (repo_at) references repos(at_uri) on delete cascade, 1458 + foreign key (repo_did) references repos(repo_did) on delete set null 1459 + ); 1460 + insert into artifacts_fk (id, did, rkey, repo_at, tag, created, blob_cid, name, size, mimetype, repo_did) 1461 + select id, did, rkey, repo_at, tag, created, blob_cid, name, size, mimetype, repo_did from artifacts; 1462 + drop table artifacts; 1463 + alter table artifacts_fk rename to artifacts; 1464 + create index if not exists idx_artifacts_repo_did on artifacts(repo_did); 1465 + 1466 + -- webhooks: add FK on repo_did 1467 + create table webhooks_fk ( 1468 + id integer primary key autoincrement, 1469 + repo_at text not null, 1470 + url text not null, 1471 + secret text, 1472 + active integer not null default 1, 1473 + events text not null, 1474 + created_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1475 + updated_at text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1476 + repo_did text, 1477 + foreign key (repo_at) references repos(at_uri) on delete cascade, 1478 + foreign key (repo_did) references repos(repo_did) on delete set null 1479 + ); 1480 + insert into webhooks_fk (id, repo_at, url, secret, active, events, created_at, updated_at, repo_did) 1481 + select id, repo_at, url, secret, active, events, created_at, updated_at, repo_did from webhooks; 1482 + drop table webhooks; 1483 + alter table webhooks_fk rename to webhooks; 1484 + create index if not exists idx_webhooks_repo_did on webhooks(repo_did); 1485 + 1486 + -- collaborators: add FK on repo_did 1487 + create table collaborators_fk ( 1488 + id integer primary key autoincrement, 1489 + did text not null, 1490 + rkey text, 1491 + subject_did text not null, 1492 + repo_at text not null, 1493 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1494 + repo_did text, 1495 + foreign key (repo_at) references repos(at_uri) on delete cascade, 1496 + foreign key (repo_did) references repos(repo_did) on delete set null 1497 + ); 1498 + insert into collaborators_fk (id, did, rkey, subject_did, repo_at, created, repo_did) 1499 + select id, did, rkey, subject_did, repo_at, created, repo_did from collaborators; 1500 + drop table collaborators; 1501 + alter table collaborators_fk rename to collaborators; 1502 + create index if not exists idx_collaborators_repo_did on collaborators(repo_did); 1503 + 1504 + -- pull_comments: add FK on repo_did 1505 + create table pull_comments_fk ( 1506 + id integer primary key autoincrement, 1507 + pull_id integer not null, 1508 + submission_id integer not null, 1509 + repo_at text not null, 1510 + owner_did text not null, 1511 + comment_at text not null, 1512 + body text not null, 1513 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1514 + repo_did text, 1515 + foreign key (repo_at, pull_id) references pulls(repo_at, pull_id) on delete cascade, 1516 + foreign key (submission_id) references pull_submissions(id) on delete cascade, 1517 + foreign key (repo_did) references repos(repo_did) on delete set null 1518 + ); 1519 + insert into pull_comments_fk (id, pull_id, submission_id, repo_at, owner_did, comment_at, body, created, repo_did) 1520 + select id, pull_id, submission_id, repo_at, owner_did, comment_at, body, created, repo_did from pull_comments; 1521 + drop table pull_comments; 1522 + alter table pull_comments_fk rename to pull_comments; 1523 + create index if not exists idx_pull_comments_repo_did on pull_comments(repo_did); 1524 + 1525 + -- pipelines: add FK on repo_did 1526 + create table pipelines_fk ( 1527 + id integer primary key autoincrement, 1528 + knot text not null, 1529 + rkey text not null, 1530 + repo_owner text not null, 1531 + repo_name text not null, 1532 + sha text not null check (length(sha) = 40), 1533 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 1534 + trigger_id integer not null, 1535 + repo_did text, 1536 + unique(knot, rkey), 1537 + foreign key (trigger_id) references triggers(id) on delete cascade, 1538 + foreign key (repo_did) references repos(repo_did) on delete set null 1539 + ); 1540 + insert into pipelines_fk (id, knot, rkey, repo_owner, repo_name, sha, created, trigger_id, repo_did) 1541 + select id, knot, rkey, repo_owner, repo_name, sha, created, trigger_id, repo_did from pipelines; 1542 + drop table pipelines; 1543 + alter table pipelines_fk rename to pipelines; 1544 + create index if not exists idx_pipelines_repo_did on pipelines(repo_did); 1545 + 1546 + -- profile_pinned_repositories: add FK on repo_did 1547 + create table profile_pinned_repositories_fk ( 1548 + id integer primary key autoincrement, 1549 + did text not null, 1550 + at_uri text not null, 1551 + repo_did text, 1552 + unique(did, at_uri), 1553 + foreign key (did) references profile(did) on delete cascade, 1554 + foreign key (at_uri) references repos(at_uri) on delete cascade, 1555 + foreign key (repo_did) references repos(repo_did) on delete set null 1556 + ); 1557 + insert into profile_pinned_repositories_fk (id, did, at_uri, repo_did) 1558 + select id, did, at_uri, repo_did from profile_pinned_repositories; 1559 + drop table profile_pinned_repositories; 1560 + alter table profile_pinned_repositories_fk rename to profile_pinned_repositories; 1561 + `) 1562 + return err 1563 + }) 1564 + conn.ExecContext(ctx, "pragma foreign_keys = on;") 1565 + 1566 + orm.RunMigration(conn, logger, "add-source-repo-did-to-pulls", func(tx *sql.Tx) error { 1567 + _, err := tx.Exec(`alter table pulls add column source_repo_did text;`) 1568 + return err 1569 + }) 1570 + 1571 + orm.RunMigration(conn, logger, "migrate-knots-to-repo-dids", func(tx *sql.Tx) error { 1572 + _, err := tx.Exec(`update registrations set needs_upgrade = 1`) 1573 + return err 1574 + }) 1575 + 1258 1576 return &DB{ 1259 1577 db, 1260 1578 logger,
+1
appview/models/artifact.go
··· 16 16 Rkey string 17 17 18 18 RepoAt syntax.ATURI 19 + RepoDid string 19 20 Tag plumbing.Hash 20 21 CreatedAt time.Time 21 22
+1
appview/models/collaborator.go
··· 15 15 // content 16 16 SubjectDid syntax.DID 17 17 RepoAt syntax.ATURI 18 + RepoDid string 18 19 19 20 // meta 20 21 Created time.Time
+23 -2
appview/models/issue.go
··· 14 14 Did string 15 15 Rkey string 16 16 RepoAt syntax.ATURI 17 + RepoDid string 17 18 IssueId int 18 19 Created time.Time 19 20 Edited *time.Time ··· 44 45 for i, uri := range i.References { 45 46 references[i] = string(uri) 46 47 } 48 + var repo *string 49 + var repoDid *string 50 + if i.RepoDid != "" { 51 + repoDid = &i.RepoDid 52 + } else { 53 + s := i.RepoAt.String() 54 + repo = &s 55 + } 47 56 return tangled.RepoIssue{ 48 - Repo: i.RepoAt.String(), 57 + Repo: repo, 58 + RepoDid: repoDid, 49 59 Title: i.Title, 50 60 Body: &i.Body, 51 61 Mentions: mentions, ··· 161 171 body = *record.Body 162 172 } 163 173 174 + var repoAt syntax.ATURI 175 + if record.Repo != nil { 176 + repoAt = syntax.ATURI(*record.Repo) 177 + } 178 + 179 + repoDid := "" 180 + if record.RepoDid != nil { 181 + repoDid = *record.RepoDid 182 + } 183 + 164 184 return Issue{ 165 - RepoAt: syntax.ATURI(record.Repo), 185 + RepoAt: repoAt, 186 + RepoDid: repoDid, 166 187 Did: did, 167 188 Rkey: rkey, 168 189 Created: created,
+1
appview/models/language.go
··· 7 7 type RepoLanguage struct { 8 8 Id int64 9 9 RepoAt syntax.ATURI 10 + RepoDid string 10 11 Ref string 11 12 IsDefaultRef bool 12 13 Language string
+1
appview/models/pipeline.go
··· 19 19 Knot string 20 20 RepoOwner syntax.DID 21 21 RepoName string 22 + RepoDid string 22 23 TriggerId int 23 24 Sha string 24 25 Created time.Time
+19 -5
appview/models/pull.go
··· 59 59 RepoAt syntax.ATURI 60 60 OwnerDid string 61 61 Rkey string 62 + RepoDid string 62 63 63 64 // content 64 65 Title string ··· 90 91 source = &tangled.RepoPull_Source{} 91 92 source.Branch = p.PullSource.Branch 92 93 source.Sha = p.LatestSha() 93 - if p.PullSource.RepoAt != nil { 94 + if p.PullSource.RepoDid != "" { 95 + source.RepoDid = &p.PullSource.RepoDid 96 + } else if p.PullSource.RepoAt != nil { 94 97 s := p.PullSource.RepoAt.String() 95 98 source.Repo = &s 96 99 } ··· 104 107 references[i] = string(uri) 105 108 } 106 109 110 + var targetRepo *string 111 + var targetRepoDid *string 112 + if p.RepoDid != "" { 113 + targetRepoDid = &p.RepoDid 114 + } else { 115 + s := p.RepoAt.String() 116 + targetRepo = &s 117 + } 107 118 record := tangled.RepoPull{ 108 119 Title: p.Title, 109 120 Body: &p.Body, ··· 111 122 References: references, 112 123 CreatedAt: p.Created.Format(time.RFC3339), 113 124 Target: &tangled.RepoPull_Target{ 114 - Repo: p.RepoAt.String(), 115 - Branch: p.TargetBranch, 125 + Repo: targetRepo, 126 + RepoDid: targetRepoDid, 127 + Branch: p.TargetBranch, 116 128 }, 117 129 Source: source, 118 130 } ··· 120 132 } 121 133 122 134 type PullSource struct { 123 - Branch string 124 - RepoAt *syntax.ATURI 135 + Branch string 136 + RepoAt *syntax.ATURI 137 + RepoDid string 125 138 126 139 // optionally populate this for reverse mappings 127 140 Repo *Repo ··· 155 168 RepoAt string 156 169 OwnerDid string 157 170 CommentAt string 171 + RepoDid string 158 172 159 173 // content 160 174 Body string
+12 -1
appview/models/repo.go
··· 22 22 Topics []string 23 23 Spindle string 24 24 Labels []string 25 + RepoDid string 25 26 26 27 // optionally, populate this when querying for reverse mappings 27 28 RepoStats *RepoStats ··· 49 50 website = &r.Website 50 51 } 51 52 53 + var repoDid *string 54 + if r.RepoDid != "" { 55 + repoDid = &r.RepoDid 56 + } 57 + 52 58 return tangled.Repo{ 53 59 Knot: r.Knot, 54 60 Name: r.Name, ··· 59 65 Source: source, 60 66 Spindle: spindle, 61 67 Labels: r.Labels, 68 + RepoDid: repoDid, 62 69 } 63 70 } 64 71 ··· 66 73 return syntax.ATURI(fmt.Sprintf("at://%s/%s/%s", r.Did, tangled.RepoNSID, r.Rkey)) 67 74 } 68 75 69 - func (r Repo) DidSlashRepo() string { 76 + func (r Repo) RepoIdentifier() string { 77 + if r.RepoDid != "" { 78 + return r.RepoDid 79 + } 70 80 p, _ := securejoin.SecureJoin(r.Did, r.Name) 71 81 return p 72 82 } ··· 98 108 Id int64 99 109 RepoAt syntax.ATURI 100 110 LabelAt syntax.ATURI 111 + RepoDid string 101 112 } 102 113 103 114 type RepoGroup struct {
+5 -4
appview/models/star.go
··· 7 7 ) 8 8 9 9 type Star struct { 10 - Did string 11 - RepoAt syntax.ATURI 12 - Created time.Time 13 - Rkey string 10 + Did string 11 + RepoAt syntax.ATURI 12 + SubjectDid string 13 + Created time.Time 14 + Rkey string 14 15 } 15 16 16 17 // RepoStar is used for reverse mapping to repos
+1
appview/models/webhook.go
··· 16 16 type Webhook struct { 17 17 Id int64 18 18 RepoAt syntax.ATURI 19 + RepoDid string 19 20 Url string 20 21 Secret string 21 22 Active bool