@@ -42,35 +42,43 @@ type PatchRequest struct {
4242 LastUpdated string `db:"last_updated"`
4343}
4444
45+ type Patchset struct {
46+ ID int64 `db:"id"`
47+ UserID int64 `db:"user_id"`
48+ PatchRequestID int64 `db:"patch_request_id"`
49+ Review bool `db:"review"`
50+ CreatedAt time.Time `db:"created_at"`
51+ }
52+
4553// Patch is a database model for a single entry in a patchset.
4654// This usually corresponds to a git commit.
4755type Patch struct {
48- ID int64 `db:"id"`
49- UserID int64 `db:"user_id"`
50- PatchRequestID int64 `db:"patch_request_id"`
51- AuthorName string `db:"author_name"`
52- AuthorEmail string `db:"author_email"`
53- AuthorDate string `db:"author_date"`
54- Title string `db:"title"`
55- Body string `db:"body"`
56- BodyAppendix string `db:"body_appendix"`
57- CommitSha string `db:"commit_sha"`
58- ContentSha string `db:"content_sha"`
59- BaseCommitSha sql.NullString `db:"base_commit_sha"`
60- Review bool `db:"review"`
61- RawText string `db:"raw_text"`
62- CreatedAt time.Time `db:"created_at"`
56+ ID int64 `db:"id"`
57+ UserID int64 `db:"user_id"`
58+ PatchsetID int64 `db:"patchset_id"`
59+ AuthorName string `db:"author_name"`
60+ AuthorEmail string `db:"author_email"`
61+ AuthorDate string `db:"author_date"`
62+ Title string `db:"title"`
63+ Body string `db:"body"`
64+ BodyAppendix string `db:"body_appendix"`
65+ CommitSha string `db:"commit_sha"`
66+ ContentSha string `db:"content_sha"`
67+ BaseCommitSha sql.NullString `db:"base_commit_sha"`
68+ RawText string `db:"raw_text"`
69+ CreatedAt time.Time `db:"created_at"`
6370}
6471
6572// EventLog is a event log for RSS or other notification systems.
6673type EventLog struct {
67- ID int64 `db:"id"`
68- UserID int64 `db:"user_id"`
69- RepoID string `db:"repo_id"`
70- PatchRequestID int64 `db:"patch_request_id"`
71- Event string `db:"event"`
72- Data string `db:"data"`
73- CreatedAt time.Time `db:"created_at"`
74+ ID int64 `db:"id"`
75+ UserID int64 `db:"user_id"`
76+ RepoID string `db:"repo_id"`
77+ PatchRequestID sql.NullInt64 `db:"patch_request_id"`
78+ PatchsetID sql.NullInt64 `db:"patchset_id"`
79+ Event string `db:"event"`
80+ Data string `db:"data"`
81+ CreatedAt time.Time `db:"created_at"`
7482}
7583
7684// DB is the interface for a pico/git database.
@@ -111,54 +119,76 @@ CREATE TABLE IF NOT EXISTS patch_requests (
111119 ON UPDATE CASCADE
112120);
113121
114- CREATE TABLE IF NOT EXISTS patches (
122+ CREATE TABLE IF NOT EXISTS patchsets (
115123 id INTEGER PRIMARY KEY AUTOINCREMENT,
116124 user_id INTEGER NOT NULL,
117125 patch_request_id INTEGER NOT NULL,
118- author_name TEXT NOT NULL,
119- author_email TEXT NOT NULL,
120- author_date DATETIME NOT NULL,
121- title TEXT NOT NULL,
122- body TEXT NOT NULL,
123- body_appendix TEXT NOT NULL,
124- commit_sha TEXT NOT NULL,
125- content_sha TEXT NOT NULL,
126126 review BOOLEAN NOT NULL DEFAULT false,
127- raw_text TEXT NOT NULL,
128- base_commit_sha TEXT,
129127 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
130- CONSTRAINT pr_id_fk
131- FOREIGN KEY(patch_request_id ) REFERENCES patch_requests (id)
128+ CONSTRAINT patchset_user_id_fk
129+ FOREIGN KEY(user_id ) REFERENCES app_users (id)
132130 ON DELETE CASCADE
133131 ON UPDATE CASCADE,
134- CONSTRAINT patches_user_id_fk
135- FOREIGN KEY(user_id ) REFERENCES app_users (id)
132+ CONSTRAINT patchset_patch_request_id_fk
133+ FOREIGN KEY(patch_request_id ) REFERENCES patch_requests (id)
136134 ON DELETE CASCADE
137135 ON UPDATE CASCADE
138136);
139137
138+ CREATE TABLE IF NOT EXISTS patches (
139+ id INTEGER PRIMARY KEY AUTOINCREMENT,
140+ user_id INTEGER NOT NULL,
141+ patchset_id INTEGER NOT NULL,
142+ author_name TEXT NOT NULL,
143+ author_email TEXT NOT NULL,
144+ author_date DATETIME NOT NULL,
145+ title TEXT NOT NULL,
146+ body TEXT NOT NULL,
147+ body_appendix TEXT NOT NULL,
148+ commit_sha TEXT NOT NULL,
149+ content_sha TEXT NOT NULL,
150+ raw_text TEXT NOT NULL,
151+ base_commit_sha TEXT,
152+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
153+ CONSTRAINT patches_user_id_fk
154+ FOREIGN KEY(user_id) REFERENCES app_users(id)
155+ ON DELETE CASCADE
156+ ON UPDATE CASCADE,
157+ CONSTRAINT patches_patchset_id_fk
158+ FOREIGN KEY(patchset_id) REFERENCES patchsets(id)
159+ ON DELETE CASCADE
160+ ON UPDATE CASCADE
161+ );
162+
140163CREATE TABLE IF NOT EXISTS event_logs (
141- id INTEGER PRIMARY KEY AUTOINCREMENT,
142- user_id INTEGER NOT NULL,
143- repo_id TEXT,
144- patch_request_id INTEGER,
145- event TEXT NOT NULL,
146- data TEXT,
147- created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
148- CONSTRAINT event_logs_pr_id_fk
149- FOREIGN KEY(patch_request_id) REFERENCES patch_requests(id)
150- ON DELETE CASCADE
151- ON UPDATE CASCADE,
152- CONSTRAINT event_logs_user_id_fk
153- FOREIGN KEY(user_id) REFERENCES app_users(id)
154- ON DELETE CASCADE
155- ON UPDATE CASCADE
164+ id INTEGER PRIMARY KEY AUTOINCREMENT,
165+ user_id INTEGER NOT NULL,
166+ repo_id TEXT,
167+ patch_request_id INTEGER,
168+ patchset_id INTEGER,
169+ event TEXT NOT NULL,
170+ data TEXT,
171+ created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
172+ CONSTRAINT event_logs_pr_id_fk
173+ FOREIGN KEY(patch_request_id) REFERENCES patch_requests(id)
174+ ON DELETE CASCADE
175+ ON UPDATE CASCADE,
176+ CONSTRAINT event_logs_patchset_id_fk
177+ FOREIGN KEY(patchset_id) REFERENCES patchsets(id)
178+ ON DELETE CASCADE
179+ ON UPDATE CASCADE,
180+ CONSTRAINT event_logs_user_id_fk
181+ FOREIGN KEY(user_id) REFERENCES app_users(id)
182+ ON DELETE CASCADE
183+ ON UPDATE CASCADE
156184);
157185`
158186
159187var sqliteMigrations = []string {
160188 "" , // migration #0 is reserved for schema initialization
161189 "ALTER TABLE patches ADD COLUMN base_commit_sha TEXT" ,
190+ `
191+ ` ,
162192}
163193
164194// Open opens a database connection.
@@ -199,7 +229,7 @@ func (db *DB) upgrade() error {
199229 return fmt .Errorf ("git-pr (version %d) older than schema (version %d)" , len (sqliteMigrations ), version )
200230 }
201231
202- tx , err := db .Begin ()
232+ tx , err := db .Beginx ()
203233 if err != nil {
204234 return err
205235 }
0 commit comments