From d4104e7627c1594b9de8e026959b196932f293c8 Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Tue, 26 May 2026 13:58:17 +0300 Subject: [PATCH] adapt to pg.DB pool-backed API `pg.connect`/`pg.connect_with_conninfo` now return `!&pg.DB` and require a `PoolConfig{}` argument; their methods take `mut db DB`. Update the postgres backend to dereference the returned pointer back into `GitlyDb`, switch `db_last_insert_id` to the pool-aware `db.last_id()` (the previous `SELECT lastval()` would run on whatever conn the pool handed out next, not the one that ran the INSERT), and thread `mut` through all callers. The sqlite backend gets the same `mut` signatures so call sites stay identical across backends. --- admin/admin_stats.v | 6 +++--- api_token.v | 2 +- commit/commit.v | 14 +++++++++----- database_d_sqlite.v | 8 ++++---- database_notd_sqlite.v | 22 ++++++++++------------ discussion.v | 2 +- feed.v | 4 ++-- gitly.v | 2 +- issue.v | 10 +++++----- milestone.v | 2 +- pr.v | 4 ++-- project.v | 4 ++-- repo/repo.v | 4 ++-- user/user.v | 4 ++-- 14 files changed, 45 insertions(+), 43 deletions(-) diff --git a/admin/admin_stats.v b/admin/admin_stats.v index bcac3e6..9ac70ce 100644 --- a/admin/admin_stats.v +++ b/admin/admin_stats.v @@ -65,7 +65,7 @@ pub fn (mut app App) get_admin_stats(days int) AdminStats { } } - repo_rows := db_exec_values(app.db, + repo_rows := db_exec_values(mut app.db, 'select created_at from ${sql_table('Repo')} where created_at >= ${range_start}') or { [][]string{} } @@ -79,7 +79,7 @@ pub fn (mut app App) get_admin_stats(days int) AdminStats { } } - commit_rows := db_exec_values(app.db, + commit_rows := db_exec_values(mut app.db, 'select created_at from ${sql_table('Commit')} where created_at >= ${range_start}') or { [][]string{} } @@ -93,7 +93,7 @@ pub fn (mut app App) get_admin_stats(days int) AdminStats { } } - issue_rows := db_exec_values(app.db, + issue_rows := db_exec_values(mut app.db, 'select created_at from ${sql_table('Issue')} where is_pr is false and created_at >= ${range_start}') or { [][]string{} } diff --git a/api_token.v b/api_token.v index e2e1e84..25383ec 100644 --- a/api_token.v +++ b/api_token.v @@ -40,7 +40,7 @@ fn (mut app App) add_api_token(user_id int, name string) !(int, string) { sql app.db { insert t into ApiToken }! - return db_last_insert_id(app.db), plain + return db_last_insert_id(mut app.db), plain } fn (mut app App) list_user_api_tokens(user_id int) []ApiToken { diff --git a/commit/commit.v b/commit/commit.v index b68be14..fff35ed 100644 --- a/commit/commit.v +++ b/commit/commit.v @@ -51,7 +51,8 @@ fn row_to_commit(row []string) Commit { const commit_select_cols = 'c.id, c.author_id, c.author, c.hash, c.created_at, c.repo_id, c.message' fn (mut app App) commit_exists(repo_id int, branch_id int, hash string) bool { - rows := db_exec_values(app.db, 'select 1 from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} and c.hash = ${sql_literal(hash)} limit 1') or { + rows := db_exec_values(mut app.db, + 'select 1 from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} and c.hash = ${sql_literal(hash)} limit 1') or { return false } return rows.len > 0 @@ -72,7 +73,7 @@ fn (mut app App) add_commit(repo_id int, branch_id int, last_hash string, author sql app.db { insert new_commit into Commit }! - commit_id = db_last_insert_id(app.db) + commit_id = db_last_insert_id(mut app.db) } link := BranchCommit{ branch_id: branch_id @@ -84,7 +85,8 @@ fn (mut app App) add_commit(repo_id int, branch_id int, last_hash string, author } fn (mut app App) find_repo_commits_as_page(repo_id int, branch_id int, offset int) []Commit { - rows := db_exec_values(app.db, 'select ${commit_select_cols} from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} order by c.created_at desc limit 35 offset ${offset}') or { + rows := db_exec_values(mut app.db, + 'select ${commit_select_cols} from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} order by c.created_at desc limit 35 offset ${offset}') or { return []Commit{} } mut commits := []Commit{cap: rows.len} @@ -95,7 +97,8 @@ fn (mut app App) find_repo_commits_as_page(repo_id int, branch_id int, offset in } fn (mut app App) get_repo_commit_count(repo_id int, branch_id int) int { - rows := db_exec_values(app.db, 'select count(*) from ${sql_table('BranchCommit')} where branch_id = ${branch_id}') or { + rows := db_exec_values(mut app.db, + 'select count(*) from ${sql_table('BranchCommit')} where branch_id = ${branch_id}') or { return 0 } if rows.len == 0 || rows[0].len == 0 { @@ -115,7 +118,8 @@ fn (mut app App) find_repo_commit_by_hash(repo_id int, hash string) Commit { } fn (mut app App) find_repo_last_commit(repo_id int, branch_id int) Commit { - rows := db_exec_values(app.db, 'select ${commit_select_cols} from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} order by c.created_at desc limit 1') or { + rows := db_exec_values(mut app.db, + 'select ${commit_select_cols} from ${sql_table('Commit')} c join ${sql_table('BranchCommit')} bc on bc.commit_id = c.id where c.repo_id = ${repo_id} and bc.branch_id = ${branch_id} order by c.created_at desc limit 1') or { return Commit{} } if rows.len == 0 { diff --git a/database_d_sqlite.v b/database_d_sqlite.v index 39e8bc1..908764e 100644 --- a/database_d_sqlite.v +++ b/database_d_sqlite.v @@ -20,7 +20,7 @@ fn db_backend_name() string { return 'sqlite' } -fn db_exec_values(db &GitlyDb, query string) ![][]string { +fn db_exec_values(mut db GitlyDb, query string) ![][]string { rows := db.exec(query)! mut values := [][]string{cap: rows.len} for row in rows { @@ -29,7 +29,7 @@ fn db_exec_values(db &GitlyDb, query string) ![][]string { return values } -fn db_last_insert_id(db &GitlyDb) int { +fn db_last_insert_id(mut db GitlyDb) int { rows := db.exec('select last_insert_rowid()') or { return 0 } if rows.len > 0 && rows[0].vals.len > 0 { return rows[0].vals[0].int() @@ -37,8 +37,8 @@ fn db_last_insert_id(db &GitlyDb) int { return 0 } -fn db_column_exists(db &GitlyDb, table_name string, column_name string) !bool { - rows := db_exec_values(db, 'pragma table_info(${sql_table(table_name)})')! +fn db_column_exists(mut db GitlyDb, table_name string, column_name string) !bool { + rows := db_exec_values(mut db, 'pragma table_info(${sql_table(table_name)})')! for row in rows { if row.len > 1 && row[1] == column_name { return true diff --git a/database_notd_sqlite.v b/database_notd_sqlite.v index a160912..b067748 100644 --- a/database_notd_sqlite.v +++ b/database_notd_sqlite.v @@ -8,22 +8,24 @@ type GitlyDb = pg.DB fn connect_db(conf config.Config) !GitlyDb { if conninfo := first_env_opt(['GITLY_DB_CONNINFO', 'DATABASE_URL'], conf.pg.conninfo) { - return GitlyDb(pg.connect_with_conninfo(conninfo)!) + db := pg.connect_with_conninfo(conninfo, pg.PoolConfig{})! + return GitlyDb(*db) } - return GitlyDb(pg.connect( + db := pg.connect( host: first_env(['GITLY_DB_HOST', 'PGHOST'], conf.pg.host) port: first_int_env(['GITLY_DB_PORT', 'PGPORT'], conf.pg.port) dbname: first_env(['GITLY_DB_NAME', 'PGDATABASE'], conf.pg.dbname) user: first_env(['GITLY_DB_USER', 'PGUSER'], conf.pg.user) password: first_env(['GITLY_DB_PASSWORD', 'PGPASSWORD'], conf.pg.password) - )!) + )! + return GitlyDb(*db) } fn db_backend_name() string { return 'postgres' } -fn db_exec_values(db &GitlyDb, query string) ![][]string { +fn db_exec_values(mut db GitlyDb, query string) ![][]string { rows := db.exec_no_null(query)! mut values := [][]string{cap: rows.len} for row in rows { @@ -32,16 +34,12 @@ fn db_exec_values(db &GitlyDb, query string) ![][]string { return values } -fn db_last_insert_id(db &GitlyDb) int { - rows := db.exec_no_null('SELECT lastval()') or { return 0 } - if rows.len > 0 && rows[0].vals.len > 0 { - return rows[0].vals[0].int() - } - return 0 +fn db_last_insert_id(mut db GitlyDb) int { + return db.last_id() } -fn db_column_exists(db &GitlyDb, table_name string, column_name string) !bool { - rows := db_exec_values(db, +fn db_column_exists(mut db GitlyDb, table_name string, column_name string) !bool { + rows := db_exec_values(mut db, 'select column_name from information_schema.columns where table_name = ${sql_literal(table_name.to_lower())} and column_name = ${sql_literal(column_name)}')! return rows.len > 0 } diff --git a/discussion.v b/discussion.v index 51087b4..f4fda3b 100644 --- a/discussion.v +++ b/discussion.v @@ -62,7 +62,7 @@ fn (mut app App) add_discussion(repo_id int, author_id int, title string, body s sql app.db { insert d into Discussion }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) find_discussion(id int) ?Discussion { diff --git a/feed.v b/feed.v index 1280fd4..ed71888 100644 --- a/feed.v +++ b/feed.v @@ -23,7 +23,7 @@ fn (mut app App) build_user_feed_as_page(user_id int, offset int) []FeedItem { } where_repo_ids := repo_ids.map(it.str()).join(', ') - commits := db_exec_values(app.db, ' + commits := db_exec_values(mut app.db, ' select author, hash, created_at, repo_id, branch_id, message from ${sql_table('Commit')} where repo_id in (${where_repo_ids}) order by created_at desc limit ${feed_items_per_page} offset ${offset}') or { @@ -70,7 +70,7 @@ fn (mut app App) get_feed_items_count(user_id int) int { } where_repo_ids := repo_ids.map(it.str()).join(', ') - count_result := db_exec_values(app.db, + count_result := db_exec_values(mut app.db, 'select count(id) from ${sql_table('Commit')} where repo_id in (${where_repo_ids})') or { return 0 } diff --git a/gitly.v b/gitly.v index d8eb1d4..bc478d2 100644 --- a/gitly.v +++ b/gitly.v @@ -353,7 +353,7 @@ fn (mut app App) migrate_tables() ! { } fn (mut app App) add_missing_column(table_name string, column_name string, column_type string) ! { - if db_column_exists(app.db, table_name, column_name)! { + if db_column_exists(mut app.db, table_name, column_name)! { return } diff --git a/issue.v b/issue.v index f4db8d8..7b091de 100644 --- a/issue.v +++ b/issue.v @@ -65,7 +65,7 @@ fn (mut app App) add_imported_issue_returning_id(repo_id int, author_id int, tit sql app.db { insert issue into Issue }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) find_or_create_label(repo_id int, name string, color string) !int { @@ -83,7 +83,7 @@ fn (mut app App) find_or_create_label(repo_id int, name string, color string) !i sql app.db { insert label into Label }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) add_issue_label(issue_id int, label_id int) ! { @@ -151,7 +151,7 @@ fn (mut app App) find_user_mentioned_issues(username string) []Issue { needle := '@' + username mut seen := map[int]bool{} mut result := []Issue{} - direct_rows := db_exec_values(app.db, + direct_rows := db_exec_values(mut app.db, 'select id from ${sql_table('Issue')} where is_pr = 0 and text like ${sql_like_pattern(needle)} order by created_at desc') or { [][]string{} } @@ -164,7 +164,7 @@ fn (mut app App) find_user_mentioned_issues(username string) []Issue { seen[id] = true result << issue } - comment_rows := db_exec_values(app.db, + comment_rows := db_exec_values(mut app.db, 'select distinct issue_id from ${sql_table('Comment')} where text like ${sql_like_pattern(needle)}') or { [][]string{} } @@ -195,7 +195,7 @@ fn (mut app App) find_user_recent_issues(user_id int) []Issue { seen[issue.id] = true result << issue } - comment_rows := db_exec_values(app.db, + comment_rows := db_exec_values(mut app.db, 'select distinct issue_id from ${sql_table('Comment')} where author_id = ${user_id}') or { [][]string{} } diff --git a/milestone.v b/milestone.v index a6dbb32..02cc762 100644 --- a/milestone.v +++ b/milestone.v @@ -38,7 +38,7 @@ fn (mut app App) add_milestone(repo_id int, title string, description string, du sql app.db { insert m into Milestone }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) list_repo_milestones(repo_id int) []Milestone { diff --git a/pr.v b/pr.v index 15d683b..e72ea0b 100644 --- a/pr.v +++ b/pr.v @@ -150,7 +150,7 @@ fn (mut app App) add_pull_request(repo_id int, author_id int, title string, desc sql app.db { insert pr into PullRequest }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) find_pull_request_by_id(pr_id int) ?PullRequest { @@ -246,7 +246,7 @@ fn (mut app App) add_pr_review(pr_id int, author_id int, state int, body string) sql app.db { insert review into PrReview }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) get_pr_reviews(pr_id int) []PrReview { diff --git a/project.v b/project.v index 1cb244e..9d04509 100644 --- a/project.v +++ b/project.v @@ -47,7 +47,7 @@ fn (mut app App) add_project(repo_id int, name string, description string) !int sql app.db { insert pr into Project }! - project_id := db_last_insert_id(app.db) + project_id := db_last_insert_id(mut app.db) if project_id != 0 { for i, col_name in ['Todo', 'In progress', 'Done'] { app.add_project_column(project_id, col_name, i) or {} @@ -103,7 +103,7 @@ fn (mut app App) add_project_column(project_id int, name string, position int) ! sql app.db { insert c into ProjectColumn }! - return db_last_insert_id(app.db) + return db_last_insert_id(mut app.db) } fn (mut app App) list_project_columns(project_id int) []ProjectColumn { diff --git a/repo/repo.v b/repo/repo.v index e3d323b..8d9be4e 100644 --- a/repo/repo.v +++ b/repo/repo.v @@ -195,8 +195,8 @@ fn (mut app App) find_user_profile_repos(user_id int, include_private bool) []Re return app.find_user_top_repos_by_stars(user_id, include_private, profile_repos_limit) } -fn (app &App) search_public_repos(query string) []Repo { - repo_rows := db_exec_values(app.db, +fn (mut app App) search_public_repos(query string) []Repo { + repo_rows := db_exec_values(mut app.db, 'select id, name, user_id, description, stars_count from ${sql_table('Repo')} where is_public is true and is_deleted is false and name like ${sql_like_pattern(query)}') or { return [] } diff --git a/user/user.v b/user/user.v index 396698e..d76c731 100644 --- a/user/user.v +++ b/user/user.v @@ -323,11 +323,11 @@ pub fn (mut app App) get_all_registered_user_count() int { } or { 0 } } -fn (app App) search_users(query string) []User { +fn (mut app App) search_users(query string) []User { q := 'select id, full_name, username, avatar from ${sql_table('User')} where is_blocked is false and ' + '(username like ${sql_like_pattern(query)} or full_name like ${sql_like_pattern(query)})' - repo_rows := db_exec_values(app.db, q) or { return [] } + repo_rows := db_exec_values(mut app.db, q) or { return [] } mut users := []User{} for row in repo_rows { users << User{ -- 2.39.5