@@ -188,7 +188,7 @@ fn connect_db(db_path string) !sqlite.DB {
188188}
189189
190190fn init_db (db sqlite.DB) ! {
191- db.exec (' create table if not exists bug_reports (
191+ db.exec (" create table if not exists bug_reports (
192192 id text primary key,
193193 delete_token text not null,
194194 created_at text not null,
@@ -198,12 +198,29 @@ fn init_db(db sqlite.DB) ! {
198198 target_os text not null,
199199 ccompiler text not null,
200200 error_string text not null,
201- lines text not null
202- )' )!
201+ lines text not null,
202+ v_lines text not null default ''
203+ )" )!
204+ // Add columns that were introduced after the table already existed, so older
205+ // databases pick them up without losing their stored reports.
206+ ensure_column (db, 'bug_reports' , 'v_lines' , "text not null default ''" )!
203207 db.exec ('create index if not exists idx_bug_reports_created_at
204208 on bug_reports(created_at)' )!
205209}
206210
211+ // ensure_column adds `column` to `table` when it is not present yet. SQLite has no
212+ // `add column if not exists`, so the existing columns are inspected first.
213+ fn ensure_column (db sqlite.DB, table string , column string , definition string ) ! {
214+ rows := db.exec ('pragma table_info(${table })' )!
215+ for row in rows {
216+ // pragma table_info columns are: cid, name, type, notnull, dflt_value, pk
217+ if row.vals.len > 1 && row.vals[1 ] == column {
218+ return
219+ }
220+ }
221+ db.exec ('alter table ${table } add column ${column } ${definition }' )!
222+ }
223+
207224// create stores a compiler bug report and returns its deletion token.
208225@['/bug-report' ; post]
209226pub fn (mut app App) create (mut ctx Context) veb.Result {
@@ -227,8 +244,8 @@ pub fn (mut app App) create(mut ctx Context) veb.Result {
227244 delete_token := rand.uuid_v4 ()
228245 app.db.exec_param_many ('insert into bug_reports (
229246 id, delete_token, created_at, remote_ip, user_agent,
230- c_file_name, target_os, ccompiler, error_string, lines
231- ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)' , [
247+ c_file_name, target_os, ccompiler, error_string, lines, v_lines
248+ ) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )' , [
232249 id,
233250 delete_token,
234251 time.utc ().format_rfc3339 (),
@@ -239,6 +256,7 @@ pub fn (mut app App) create(mut ctx Context) veb.Result {
239256 stored_report.ccompiler,
240257 stored_report.error_string,
241258 stored_report.lines,
259+ stored_report.v_lines,
242260 ]) or { return ctx.server_error ('could not store report' ) }
243261 return ctx.json (CreateBugReportResponse{
244262 id: id
0 commit comments