···
737
+
// remove issue_at from issues and replace with generated column
739
+
// this requires a full table recreation because stored columns
740
+
// cannot be added via alter
742
+
// couple other changes:
743
+
// - columns renamed to be more consistent
744
+
// - adds edited and deleted fields
746
+
// disable foreign-keys for the next migration
747
+
conn.ExecContext(ctx, "pragma foreign_keys = off;")
748
+
runMigration(conn, "remove-issue-at-from-issues", func(tx *sql.Tx) error {
749
+
_, err := tx.Exec(`
750
+
create table if not exists issues_new (
752
+
id integer primary key autoincrement,
754
+
rkey text not null,
755
+
at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue' || '/' || rkey) stored,
758
+
repo_at text not null,
761
+
issue_id integer not null,
762
+
title text not null,
763
+
body text not null,
764
+
open integer not null default 1,
765
+
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
766
+
edited text, -- timestamp
767
+
deleted text, -- timestamp
770
+
unique(repo_at, issue_id),
772
+
foreign key (repo_at) references repos(at_uri) on delete cascade
781
+
insert into issues_new (id, did, rkey, repo_at, issue_id, title, body, open, created)
799
+
_, err = tx.Exec(`drop table issues`)
804
+
// rename new table
805
+
_, err = tx.Exec(`alter table issues_new rename to issues`)
808
+
conn.ExecContext(ctx, "pragma foreign_keys = on;")
810
+
// - renames the comments table to 'issue_comments'
811
+
// - rework issue comments to update constraints:
812
+
// * unique(did, rkey)
813
+
// * remove comment-id and just use the global ID
814
+
// * foreign key (repo_at, issue_id)
816
+
// * column "reply_to" which can be any other comment
817
+
// * column "at-uri" which is a generated column
818
+
runMigration(conn, "rework-issue-comments", func(tx *sql.Tx) error {
819
+
_, err := tx.Exec(`
820
+
create table if not exists issue_comments (
822
+
id integer primary key autoincrement,
825
+
at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue.comment' || '/' || rkey) stored,
828
+
issue_at text not null,
829
+
reply_to text, -- at_uri of parent comment
832
+
body text not null,
833
+
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
840
+
foreign key (issue_at) references issues(at_uri) on delete cascade
849
+
insert into issue_comments (id, did, rkey, issue_at, body, created, edited, deleted)
854
+
i.at_uri, -- get at_uri from issues table
860
+
join issues i on c.repo_at = i.repo_at and c.issue_id = i.issue_id;
867
+
_, err = tx.Exec(`drop table comments`)