this repo has no description

appview/db: rework issues table

adds generated `at_uri` column. a lot easier for comments to refer to an
issue now. ingesting comments also does not require a lookup, we can
directly use the at-uri of the parent-issue in db queries.

Signed-off-by: oppiliappan <me@oppi.li>

oppi.li 9720c872 560c726a

verified
+134
+134
appview/db/db.go
··· 703 703 return err 704 704 }) 705 705 706 + // remove issue_at from issues and replace with generated column 707 + // 708 + // this requires a full table recreation because stored columns 709 + // cannot be added via alter 710 + // 711 + // couple other changes: 712 + // - columns renamed to be more consistent 713 + // - adds edited and deleted fields 714 + // 715 + // disable foreign-keys for the next migration 716 + conn.ExecContext(ctx, "pragma foreign_keys = off;") 717 + runMigration(conn, "remove-issue-at-from-issues", func(tx *sql.Tx) error { 718 + _, err := tx.Exec(` 719 + create table if not exists issues_new ( 720 + -- identifiers 721 + id integer primary key autoincrement, 722 + did text not null, 723 + rkey text not null, 724 + at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue' || '/' || rkey) stored, 725 + 726 + -- at identifiers 727 + repo_at text not null, 728 + 729 + -- content 730 + issue_id integer not null, 731 + title text not null, 732 + body text not null, 733 + open integer not null default 1, 734 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 735 + edited text, -- timestamp 736 + deleted text, -- timestamp 737 + 738 + unique(did, rkey), 739 + unique(repo_at, issue_id), 740 + unique(at_uri), 741 + foreign key (repo_at) references repos(at_uri) on delete cascade 742 + ); 743 + `) 744 + if err != nil { 745 + return err 746 + } 747 + 748 + // transfer data 749 + _, err = tx.Exec(` 750 + insert into issues_new (id, did, rkey, repo_at, issue_id, title, body, open, created) 751 + select 752 + i.id, 753 + i.owner_did, 754 + i.rkey, 755 + i.repo_at, 756 + i.issue_id, 757 + i.title, 758 + i.body, 759 + i.open, 760 + i.created 761 + from issues i; 762 + `) 763 + if err != nil { 764 + return err 765 + } 766 + 767 + // drop old table 768 + _, err = tx.Exec(`drop table issues`) 769 + if err != nil { 770 + return err 771 + } 772 + 773 + // rename new table 774 + _, err = tx.Exec(`alter table issues_new rename to issues`) 775 + return err 776 + }) 777 + conn.ExecContext(ctx, "pragma foreign_keys = on;") 778 + 779 + // - renames the comments table to 'issue_comments' 780 + // - rework issue comments to update constraints: 781 + // * unique(did, rkey) 782 + // * remove comment-id and just use the global ID 783 + // * foreign key (repo_at, issue_id) 784 + // - new columns 785 + // * column "reply_to" which can be any other comment 786 + // * column "at-uri" which is a generated column 787 + runMigration(conn, "rework-issue-comments", func(tx *sql.Tx) error { 788 + _, err := tx.Exec(` 789 + create table if not exists issue_comments ( 790 + -- identifiers 791 + id integer primary key autoincrement, 792 + did text not null, 793 + rkey text, 794 + at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue.comment' || '/' || rkey) stored, 795 + 796 + -- at identifiers 797 + issue_at text not null, 798 + reply_to text, -- at_uri of parent comment 799 + 800 + -- content 801 + body text not null, 802 + created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')), 803 + edited text, 804 + deleted text, 805 + 806 + -- constraints 807 + unique(did, rkey), 808 + unique(at_uri), 809 + foreign key (issue_at) references issues(at_uri) on delete cascade 810 + ); 811 + `) 812 + if err != nil { 813 + return err 814 + } 815 + 816 + // transfer data 817 + _, err = tx.Exec(` 818 + insert into issue_comments (id, did, rkey, issue_at, body, created, edited, deleted) 819 + select 820 + c.id, 821 + c.owner_did, 822 + c.rkey, 823 + i.at_uri, -- get at_uri from issues table 824 + c.body, 825 + c.created, 826 + c.edited, 827 + c.deleted 828 + from comments c 829 + join issues i on c.repo_at = i.repo_at and c.issue_id = i.issue_id; 830 + `) 831 + if err != nil { 832 + return err 833 + } 834 + 835 + // drop old table 836 + _, err = tx.Exec(`drop table comments`) 837 + return err 838 + }) 839 + 706 840 return &DB{db}, nil 707 841 } 708 842