From 63311d73b0d5734b8fd529d5ddf2a1d74bb89966 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sun, 14 May 2017 19:21:00 +0100 Subject: Tidy some of the low level repo/sql bits --- .../service/maintenanceGitOperations.cpp | 39 +++++++++++----------- gentoobrowse-api/service/maintenanceimpl.h | 2 +- .../service/sql/maintenance/repoSetLastCommit.sql | 1 + .../service/sql/maintenance/reposToGitRefresh.sql | 4 +-- .../service/sql/maintenance/reposToUpdate.sql | 3 +- gentoobrowse-api/unittests/testMaintenance.cpp | 3 +- 6 files changed, 27 insertions(+), 25 deletions(-) create mode 100644 gentoobrowse-api/service/sql/maintenance/repoSetLastCommit.sql diff --git a/gentoobrowse-api/service/maintenanceGitOperations.cpp b/gentoobrowse-api/service/maintenanceGitOperations.cpp index bc66882..cd463db 100644 --- a/gentoobrowse-api/service/maintenanceGitOperations.cpp +++ b/gentoobrowse-api/service/maintenanceGitOperations.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "utils/git.h" #include "converters.h" @@ -90,8 +91,9 @@ namespace Gentoo { void Maintenance::updateRepositories(const Ice::Current &) { - auto dbc = db->get(); - sql::maintenance::reposToUpdate.select(dbc.get())->forEachRow(&updateRepository); + for (auto & repo : fetch(sql::maintenance::reposToUpdate)) { + updateRepository(repo.path, repo.upstream, repo.branch); + } } static inline @@ -138,23 +140,21 @@ namespace Gentoo { writeChangesToFileList(DB::Connection * db, int64_t repoId, git_repository * repo, const git_oid & last, const git_oid & head); ChangeSet - refreshRepository(DB::Connection * db, Ice::PropertiesPtr properties, int64_t repoId, const std::string & repoName, const std::string & path, const std::string & lastCommitId) + refreshRepository(DB::Connection * db, Ice::PropertiesPtr properties, Repository & repository) { - auto targetRef = properties->getProperty("GentooBrowseAPI.RefreshTarget." + repoName); - auto repo = gitSafeGet(git_repository_open, git_repository_free, path.c_str()); - auto lastCommitOid = gitSafeGet(git_oid_fromstr, lastCommitId.c_str()); + auto targetRef = properties->getProperty("GentooBrowseAPI.RefreshTarget." + repository.name); + auto repo = gitSafeGet(git_repository_open, git_repository_free, repository.path.c_str()); + auto lastCommitOid = gitSafeGet(git_oid_fromstr, repository.lastcommit.c_str()); auto headCommitOid = targetRef.empty() ? gitSafeGet(git_reference_name_to_id, repo.get(), "HEAD") : gitSafeGet(git_oid_fromstr, targetRef.c_str()); - auto updateLastCommit = db->modify("UPDATE gentoobrowse.repos SET lastCommit = ? WHERE repoId = ?"); - char buf[41]; - git_oid_tostr(buf, sizeof(buf), &headCommitOid); - updateLastCommit->bindParamS(0, buf); - updateLastCommit->bindParamI(1, repoId); + auto updateLastCommit = sql::maintenance::repoSetLastCommit.modify(db); + updateLastCommit->bindParamS(0, *headCommitOid); + updateLastCommit->bindParamI(1, repository.repoid); updateLastCommit->execute(); - writeChangeLog(db, repoId, repo.get(), lastCommitOid, headCommitOid); - return writeChangesToFileList(db, repoId, repo.get(), lastCommitOid, headCommitOid); + writeChangeLog(db, repository.repoid, repo.get(), lastCommitOid, headCommitOid); + return writeChangesToFileList(db, repository.repoid, repo.get(), lastCommitOid, headCommitOid); } AdHocFormatter(FindingChanges, "Finding changes for repository %? with range %?...%?\n"); @@ -180,18 +180,17 @@ namespace Gentoo { auto dbc = db->get(); dbc->execute("SET search_path = gentoobrowse, pg_catalog"); DB::TransactionScope tx(dbc.get()); - sql::maintenance::reposToGitRefresh.select(dbc.get())->forEachRow( - [dbc, properties, repoRoot, this](auto repoId, auto name, auto path, auto lastCommit) { - auto changes = refreshRepository(dbc.get(), properties, repoId, name, path, lastCommit); - this->applyFileChanges(dbc.get(), repoId, path, repoRoot, changes); - }); + for (auto & repo : fetch(dbc.get(), sql::maintenance::reposToGitRefresh)) { + auto changes = refreshRepository(dbc.get(), properties, repo); + this->applyFileChanges(dbc.get(), repoRoot, repo, changes); + } dbc->execute("SET search_path = public, pg_catalog"); } AdHocFormatter(UpdatingFileContent, "Updating file content\n"); AdHocFormatter(UpdatedFileContent, "Updated file content\n"); void - Maintenance::applyFileChanges(DB::Connection * dbc, int64_t repoId, const boost::filesystem::path & path, const boost::filesystem::path & repoRoot, ChangeSet & changes) const + Maintenance::applyFileChanges(DB::Connection * dbc, const boost::filesystem::path & repoRoot, const Repository & repo, ChangeSet & changes) const { UpdatingFileContent::write(std::cerr); // Map of phase, to order, to processor @@ -216,7 +215,7 @@ namespace Gentoo { // Changes for (auto change = changes.begin(); change != changes.end(); ) { if (processor->match(change->second.pathParts)) { - funcs[change->second.changeType](processor, repoId, change->second.pathParts, repoRoot / path / change->first); + funcs[change->second.changeType](processor, repo.repoid, change->second.pathParts, repoRoot / repo.path / change->first); usedProcessors.insert(processor); change = changes.erase(change); } diff --git a/gentoobrowse-api/service/maintenanceimpl.h b/gentoobrowse-api/service/maintenanceimpl.h index 622a239..3a442b4 100644 --- a/gentoobrowse-api/service/maintenanceimpl.h +++ b/gentoobrowse-api/service/maintenanceimpl.h @@ -27,7 +27,7 @@ namespace Gentoo { protected: Maintenance(IceTray::DatabasePoolPtr d); - void applyFileChanges(DB::Connection *, int64_t, const boost::filesystem::path &, const boost::filesystem::path &, ChangeSet &) const; + void applyFileChanges(DB::Connection *, const boost::filesystem::path &, const Repository &, ChangeSet &) const; private: diff --git a/gentoobrowse-api/service/sql/maintenance/repoSetLastCommit.sql b/gentoobrowse-api/service/sql/maintenance/repoSetLastCommit.sql new file mode 100644 index 0000000..a5397ea --- /dev/null +++ b/gentoobrowse-api/service/sql/maintenance/repoSetLastCommit.sql @@ -0,0 +1 @@ +UPDATE gentoobrowse.repos SET lastCommit = ? WHERE repoId = ? diff --git a/gentoobrowse-api/service/sql/maintenance/reposToGitRefresh.sql b/gentoobrowse-api/service/sql/maintenance/reposToGitRefresh.sql index 068ddb9..f7a4638 100644 --- a/gentoobrowse-api/service/sql/maintenance/reposToGitRefresh.sql +++ b/gentoobrowse-api/service/sql/maintenance/reposToGitRefresh.sql @@ -1,4 +1,4 @@ -SELECT repoid, name, path, lastcommit +-- libdbpp:no-cursor +SELECT repoid, name, path, upstream, branch, lastcommit FROM gentoobrowse.repos WHERE lastcommit IS NOT NULL - diff --git a/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql b/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql index d373418..318a796 100644 --- a/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql +++ b/gentoobrowse-api/service/sql/maintenance/reposToUpdate.sql @@ -1,4 +1,5 @@ -SELECT path, upstream, branch +-- libdbpp:no-cursor +SELECT repoid, name, path, upstream, branch, lastcommit FROM gentoobrowse.repos WHERE upstream IS NOT NULL AND branch IS NOT NULL diff --git a/gentoobrowse-api/unittests/testMaintenance.cpp b/gentoobrowse-api/unittests/testMaintenance.cpp index 0b451ca..d36c643 100644 --- a/gentoobrowse-api/unittests/testMaintenance.cpp +++ b/gentoobrowse-api/unittests/testMaintenance.cpp @@ -87,7 +87,8 @@ class M2 : public Gentoo::Service::Maintenance { changedFiles(changes, fromFiles, toFiles); boost::filesystem::remove(treeDir); boost::filesystem::create_symlink(to, treeDir); - applyFileChanges(dbc.get(), 1, "/", to, changes); + Gentoo::Repository repo = { 1, "gentoo", to.string(), "origin", "master", "" }; + applyFileChanges(dbc.get(), "/", repo, changes); boost::filesystem::remove(treeDir); dbc->execute("SET search_path = public, pg_catalog"); } -- cgit v1.2.3