Browse Source

Rename pkg db tables, and fix logging typo

default_compile_flags
vector-of-bool 3 years ago
parent
commit
5a8b5eac7e
3 changed files with 29 additions and 31 deletions
  1. +19
    -21
      src/dds/pkg/db.cpp
  2. +9
    -9
      src/dds/pkg/remote.cpp
  3. +1
    -1
      src/dds/util/http/pool.cpp

+ 19
- 21
src/dds/pkg/db.cpp View File



void migrate_repodb_3(nsql::database& db) { void migrate_repodb_3(nsql::database& db) {
db.exec(R"( db.exec(R"(
CREATE TABLE dds_cat_remotes (
CREATE TABLE dds_pkg_remotes (
remote_id INTEGER PRIMARY KEY AUTOINCREMENT, remote_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE, name TEXT NOT NULL UNIQUE,
remote_url TEXT NOT NULL, remote_url TEXT NOT NULL,
db_mtime TEXT db_mtime TEXT
); );


CREATE TABLE dds_cat_pkgs_new (
CREATE TABLE dds_pkgs (
pkg_id INTEGER PRIMARY KEY AUTOINCREMENT, pkg_id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL, name TEXT NOT NULL,
version TEXT NOT NULL, version TEXT NOT NULL,
description TEXT NOT NULL, description TEXT NOT NULL,
remote_url TEXT NOT NULL, remote_url TEXT NOT NULL,
remote_id INTEGER remote_id INTEGER
REFERENCES dds_cat_remotes
REFERENCES dds_pkg_remotes
ON DELETE CASCADE, ON DELETE CASCADE,
UNIQUE (name, version, remote_id) UNIQUE (name, version, remote_id)
); );


INSERT INTO dds_cat_pkgs_new(pkg_id,
name,
version,
description,
remote_url)
INSERT INTO dds_pkgs(pkg_id,
name,
version,
description,
remote_url)
SELECT pkg_id, SELECT pkg_id,
name, name,
version, version,
) || '#' || git_ref ) || '#' || git_ref
FROM dds_cat_pkgs; FROM dds_cat_pkgs;


CREATE TABLE dds_cat_pkg_deps_new (
CREATE TABLE dds_pkg_deps (
dep_id INTEGER PRIMARY KEY AUTOINCREMENT, dep_id INTEGER PRIMARY KEY AUTOINCREMENT,
pkg_id INTEGER pkg_id INTEGER
NOT NULL NOT NULL
REFERENCES dds_cat_pkgs_new(pkg_id)
REFERENCES dds_pkgs(pkg_id)
ON DELETE CASCADE, ON DELETE CASCADE,
dep_name TEXT NOT NULL, dep_name TEXT NOT NULL,
low TEXT NOT NULL, low TEXT NOT NULL,
UNIQUE(pkg_id, dep_name) UNIQUE(pkg_id, dep_name)
); );


INSERT INTO dds_cat_pkg_deps_new SELECT * FROM dds_cat_pkg_deps;
INSERT INTO dds_pkg_deps SELECT * FROM dds_cat_pkg_deps;


DROP TABLE dds_cat_pkg_deps; DROP TABLE dds_cat_pkg_deps;
DROP TABLE dds_cat_pkgs; DROP TABLE dds_cat_pkgs;
ALTER TABLE dds_cat_pkgs_new RENAME TO dds_cat_pkgs;
ALTER TABLE dds_cat_pkg_deps_new RENAME TO dds_cat_pkg_deps;
)"); )");
} }


const http_remote_listing& http) { const http_remote_listing& http) {
nsql::exec( // nsql::exec( //
stmts(R"( stmts(R"(
INSERT OR REPLACE INTO dds_cat_pkgs (
INSERT OR REPLACE INTO dds_pkgs (
name, name,
version, version,
remote_url, remote_url,


nsql::exec( // nsql::exec( //
stmts(R"( stmts(R"(
INSERT OR REPLACE INTO dds_cat_pkgs (
INSERT OR REPLACE INTO dds_pkgs (
name, name,
version, version,
remote_url, remote_url,
std::visit([&](auto&& remote) { store_with_remote(st_cache, pkg, remote); }, pkg.remote); std::visit([&](auto&& remote) { store_with_remote(st_cache, pkg, remote); }, pkg.remote);
auto db_pkg_id = db.last_insert_rowid(); auto db_pkg_id = db.last_insert_rowid();
auto& new_dep_st = st_cache(R"( auto& new_dep_st = st_cache(R"(
INSERT INTO dds_cat_pkg_deps (
INSERT INTO dds_pkg_deps (
pkg_id, pkg_id,
dep_name, dep_name,
low, low,
version, version,
remote_url, remote_url,
description description
FROM dds_cat_pkgs
FROM dds_pkgs
WHERE name = ?1 AND version = ?2 WHERE name = ?1 AND version = ?2
ORDER BY pkg_id DESC ORDER BY pkg_id DESC
)"_sql); )"_sql);


std::vector<pkg_id> pkg_db::all() const noexcept { std::vector<pkg_id> pkg_db::all() const noexcept {
return nsql::exec_tuples<std::string, std::string>( return nsql::exec_tuples<std::string, std::string>(
_stmt_cache("SELECT name, version FROM dds_cat_pkgs"_sql))
_stmt_cache("SELECT name, version FROM dds_pkgs"_sql))
| neo::lref // | neo::lref //
| ranges::views::transform(pair_to_pkg_id) // | ranges::views::transform(pair_to_pkg_id) //
| ranges::to_vector; | ranges::to_vector;
_stmt_cache( _stmt_cache(
R"( R"(
SELECT name, version SELECT name, version
FROM dds_cat_pkgs
FROM dds_pkgs
WHERE name = ? WHERE name = ?
ORDER BY pkg_id DESC ORDER BY pkg_id DESC
)"_sql), )"_sql),
R"( R"(
WITH this_pkg_id AS ( WITH this_pkg_id AS (
SELECT pkg_id SELECT pkg_id
FROM dds_cat_pkgs
FROM dds_pkgs
WHERE name = ? AND version = ? WHERE name = ? AND version = ?
) )
SELECT dep_name, low, high SELECT dep_name, low, high
FROM dds_cat_pkg_deps
FROM dds_pkg_deps
WHERE pkg_id IN this_pkg_id WHERE pkg_id IN this_pkg_id
ORDER BY dep_name ORDER BY dep_name
)"_sql), )"_sql),

+ 9
- 9
src/dds/pkg/remote.cpp View File



void pkg_remote::store(nsql::database_ref db) { void pkg_remote::store(nsql::database_ref db) {
auto st = db.prepare(R"( auto st = db.prepare(R"(
INSERT INTO dds_cat_remotes (name, remote_url)
INSERT INTO dds_pkg_remotes (name, remote_url)
VALUES (?, ?) VALUES (?, ?)
ON CONFLICT (name) DO ON CONFLICT (name) DO
UPDATE SET remote_url = ?2 UPDATE SET remote_url = ?2


auto db_path = rdb._tempdir.path() / "repo.db"; auto db_path = rdb._tempdir.path() / "repo.db";


auto rid_st = db.prepare("SELECT remote_id FROM dds_cat_remotes WHERE name = ?");
auto rid_st = db.prepare("SELECT remote_id FROM dds_pkg_remotes WHERE name = ?");
rid_st.bindings()[1] = _name; rid_st.bindings()[1] = _name;
auto [remote_id] = nsql::unpack_single<std::int64_t>(rid_st); auto [remote_id] = nsql::unpack_single<std::int64_t>(rid_st);
rid_st.reset(); rid_st.reset();
dds_log(trace, "Clearing prior contents"); dds_log(trace, "Clearing prior contents");
nsql::exec( // nsql::exec( //
db.prepare(R"( db.prepare(R"(
DELETE FROM dds_cat_pkgs
DELETE FROM dds_pkgs
WHERE remote_id = ? WHERE remote_id = ?
)"), )"),
remote_id); remote_id);
dds_log(trace, "Importing packages"); dds_log(trace, "Importing packages");
nsql::exec( // nsql::exec( //
db.prepare(R"( db.prepare(R"(
INSERT INTO dds_cat_pkgs
INSERT INTO dds_pkgs
(name, version, description, remote_url, remote_id) (name, version, description, remote_url, remote_id)
SELECT SELECT
name, name,
base_url_str); base_url_str);
dds_log(trace, "Importing dependencies"); dds_log(trace, "Importing dependencies");
db.exec(R"( db.exec(R"(
INSERT OR REPLACE INTO dds_cat_pkg_deps (pkg_id, dep_name, low, high)
INSERT OR REPLACE INTO dds_pkg_deps (pkg_id, dep_name, low, high)
SELECT SELECT
local_pkgs.pkg_id AS pkg_id, local_pkgs.pkg_id AS pkg_id,
dep_name, dep_name,
high high
FROM remote.dds_repo_package_deps AS deps, FROM remote.dds_repo_package_deps AS deps,
remote.dds_repo_packages AS pkgs USING(package_id), remote.dds_repo_packages AS pkgs USING(package_id),
dds_cat_pkgs AS local_pkgs USING(name, version)
dds_pkgs AS local_pkgs USING(name, version)
)"); )");
// Validate our database // Validate our database
dds_log(trace, "Running integrity check"); dds_log(trace, "Running integrity check");


// Save the cache info for the remote // Save the cache info for the remote
if (auto new_etag = resp.etag()) { if (auto new_etag = resp.etag()) {
nsql::exec(db.prepare("UPDATE dds_cat_remotes SET db_etag = ? WHERE name = ?"),
nsql::exec(db.prepare("UPDATE dds_pkg_remotes SET db_etag = ? WHERE name = ?"),
*new_etag, *new_etag,
_name); _name);
} }
if (auto mtime = resp.last_modified()) { if (auto mtime = resp.last_modified()) {
nsql::exec(db.prepare("UPDATE dds_cat_remotes SET db_mtime = ? WHERE name = ?"),
nsql::exec(db.prepare("UPDATE dds_pkg_remotes SET db_mtime = ? WHERE name = ?"),
*mtime, *mtime,
_name); _name);
} }


void dds::update_all_remotes(nsql::database_ref db) { void dds::update_all_remotes(nsql::database_ref db) {
dds_log(info, "Updating catalog from all remotes"); dds_log(info, "Updating catalog from all remotes");
auto repos_st = db.prepare("SELECT name, remote_url, db_etag, db_mtime FROM dds_cat_remotes");
auto repos_st = db.prepare("SELECT name, remote_url, db_etag, db_mtime FROM dds_pkg_remotes");
auto tups = nsql::iter_tuples<std::string, auto tups = nsql::iter_tuples<std::string,
std::string, std::string,
std::optional<std::string>, std::optional<std::string>,

+ 1
- 1
src/dds/util/http/pool.cpp View File



dds_log(trace, dds_log(trace,
" --> HTTP {} {}://{}:{}{}", " --> HTTP {} {}://{}:{}{}",
origin.protocol,
params.method, params.method,
origin.protocol,
origin.hostname, origin.hostname,
origin.port, origin.port,
params.path); params.path);

Loading…
Cancel
Save