Add sql connection pool

This commit is contained in:
jaina heartles 2022-10-12 23:19:59 -07:00
parent 23da0c6857
commit 159f1c28cc
7 changed files with 127 additions and 49 deletions

View file

@ -5,11 +5,10 @@ const sql = @import("sql");
const util = @import("util");
const test_config = .{
.db = .{
.sqlite = .{
.sqlite_file_path = ":memory:",
},
},
.db = .{ .sqlite = .{
.sqlite_file_path = "file::memory:?cache=shared",
.sqlite_is_uri = true,
} },
};
const ApiSource = api.ApiSource;
@ -18,12 +17,16 @@ const root_password = "password1234";
const admin_host = "example.com";
const admin_origin = "https://" ++ admin_host;
fn makeDb(alloc: std.mem.Allocator) !sql.Conn {
fn makeDb(alloc: std.mem.Allocator) !sql.ConnPool {
try util.seedThreadPrng();
var db = try sql.Conn.open(test_config.db);
try migrations.up(try db.acquire());
try api.setupAdmin(try db.acquire(), admin_origin, root_user, root_password, alloc);
return db;
var pool = try sql.ConnPool.init(test_config.db);
{
var db = try pool.acquire();
defer db.releaseConnection();
try migrations.up(db);
try api.setupAdmin(db, admin_origin, root_user, root_password, alloc);
}
return pool;
}
fn connectAndLogin(
@ -42,6 +45,7 @@ fn connectAndLogin(
test "login as root" {
const alloc = std.testing.allocator;
var db = try makeDb(alloc);
defer db.deinit();
var src = try ApiSource.init(&db);
const login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);
@ -59,6 +63,7 @@ test "login as root" {
test "create community" {
const alloc = std.testing.allocator;
var db = try makeDb(alloc);
defer db.deinit();
var src = try ApiSource.init(&db);
const login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);
@ -80,6 +85,7 @@ test "create community" {
test "create community and transfer to new owner" {
const alloc = std.testing.allocator;
var db = try makeDb(alloc);
defer db.deinit();
var src = try ApiSource.init(&db);
const root_login = try connectAndLogin(&src, admin_host, root_user, root_password, alloc);