···
+
// remove issue_at from issues and replace with generated column
+
// this requires a full table recreation because stored columns
+
// cannot be added via alter
+
// couple other changes:
+
// - columns renamed to be more consistent
+
// - adds edited and deleted fields
+
// disable foreign-keys for the next migration
+
conn.ExecContext(ctx, "pragma foreign_keys = off;")
+
runMigration(conn, "remove-issue-at-from-issues", func(tx *sql.Tx) error {
+
create table if not exists issues_new (
+
id integer primary key autoincrement,
+
at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue' || '/' || rkey) stored,
+
issue_id integer not null,
+
open integer not null default 1,
+
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
+
edited text, -- timestamp
+
deleted text, -- timestamp
+
unique(repo_at, issue_id),
+
foreign key (repo_at) references repos(at_uri) on delete cascade
+
insert into issues_new (id, did, rkey, repo_at, issue_id, title, body, open, created)
+
_, err = tx.Exec(`drop table issues`)
+
_, err = tx.Exec(`alter table issues_new rename to issues`)
+
conn.ExecContext(ctx, "pragma foreign_keys = on;")
+
// - renames the comments table to 'issue_comments'
+
// - rework issue comments to update constraints:
+
// * remove comment-id and just use the global ID
+
// * foreign key (repo_at, issue_id)
+
// * column "reply_to" which can be any other comment
+
// * column "at-uri" which is a generated column
+
runMigration(conn, "rework-issue-comments", func(tx *sql.Tx) error {
+
create table if not exists issue_comments (
+
id integer primary key autoincrement,
+
at_uri text generated always as ('at://' || did || '/' || 'sh.tangled.repo.issue.comment' || '/' || rkey) stored,
+
issue_at text not null,
+
reply_to text, -- at_uri of parent comment
+
created text not null default (strftime('%Y-%m-%dT%H:%M:%SZ', 'now')),
+
foreign key (issue_at) references issues(at_uri) on delete cascade
+
insert into issue_comments (id, did, rkey, issue_at, body, created, edited, deleted)
+
i.at_uri, -- get at_uri from issues table
+
join issues i on c.repo_at = i.repo_at and c.issue_id = i.issue_id;
+
_, err = tx.Exec(`drop table comments`)