combile stable and unstable into the same db file

add script to pull down sqlports dbs
This commit is contained in:
Aaron Bieber 2022-09-28 22:37:15 -06:00
parent 75af8104f8
commit d051d26fd8
No known key found for this signature in database
2 changed files with 91 additions and 20 deletions

View File

@ -1,5 +1,8 @@
#!/usr/bin/env perl
# vim: set ts=4 sw=4 tw=0:
# vim: set expandtab:
use warnings;
use strict;
use Time::HiRes qw( time );
@ -10,17 +13,15 @@ use Mojolicious::Lite -signatures;
use Mojo::SQLite;
use Text::Markdown qw{ markdown };
my $currentDB = "current.db";
my $stableDB = "stable.db";
my $dbFile = "combined.db";
if ( $^O eq "openbsd" ) {
require OpenBSD::Pledge;
require OpenBSD::Unveil;
OpenBSD::Unveil::unveil( "/", "" ) or die;
OpenBSD::Unveil::unveil( "./$currentDB", "r" ) or die;
OpenBSD::Unveil::unveil( "./$stableDB", "r" ) or die;
OpenBSD::Unveil::unveil( "/usr/local", "r" ) or die;
OpenBSD::Unveil::unveil( "/", "" ) or die;
OpenBSD::Unveil::unveil( "./$dbFile", "r" ) or die;
OpenBSD::Unveil::unveil( "/usr/local", "r" ) or die;
# Needed to create the -shm and -wal db files.
OpenBSD::Unveil::unveil( ".", "rwc" ) or die;
@ -29,22 +30,31 @@ if ( $^O eq "openbsd" ) {
or die;
}
my $mtime = ( stat($currentDB) )[9];
my $mtime = ( stat($dbFile) )[9];
$mtime = scalar localtime $mtime;
helper current => sub { state $sql = Mojo::SQLite->new("sqlite:$currentDB") };
helper stable => sub { state $sql = Mojo::SQLite->new("sqlite:$stableDB") };
helper sqlite => sub {
state $sql = Mojo::SQLite->new("sqlite:$dbFile");
$sql->on(
connection => sub {
my ( $sql, $dbh ) = @_;
my $sqlite_mode = 'DELETE';
$dbh->do("pragma journal_mode=$sqlite_mode");
}
);
return $sql;
};
my $query = q{
SELECT
FULLPKGNAME,
FULLPKGPATH,
COMMENT,
DESCRIPTION,
highlight(ports_fts, 2, '**', '**') AS COMMENT_MATCH,
highlight(ports_fts, 3, '**', '**') AS DESCR_MATCH
FROM ports_fts
WHERE ports_fts MATCH ? ORDER BY rank;
FULLPKGNAME,
FULLPKGPATH,
COMMENT,
DESCRIPTION,
highlight(%s, 2, '**', '**') AS COMMENT_MATCH,
highlight(%s, 3, '**', '**') AS DESCR_MATCH
FROM %s
WHERE %s MATCH ? ORDER BY rank;
};
my $title = "OpenBSD.app";
@ -58,6 +68,14 @@ sub to_md ($results) {
}
sub set_query ($is_current) {
if ($is_current) {
return sprintf( $query, ("current_ports_fts") x 4 );
}
return sprintf( $query, ("stable_ports_fts") x 4 );
}
get '/' => sub ($c) {
my $v = $c->validation;
@ -72,11 +90,11 @@ get '/' => sub ($c) {
$c->stash( mtime => $mtime );
if ( defined $search && $search ne "" ) {
my $db = $c->stable->db;
$db = $c->current->db if defined $current;
my $db = $c->sqlite->db;
my $q = set_query( defined $current );
my $start = time();
my $results = $db->query( $query, $search )->hashes;
my $results = $db->query( $q, $search )->hashes;
my $end = time();
my $elapsed = sprintf( "%2f\n", $end - $start );

53
upgrade.sh Executable file
View File

@ -0,0 +1,53 @@
#!/usr/bin/env sh
mkdir -p /tmp/openbsd_app/{stable,current}
CURRENT_FILE=${1:-/tmp/openbsd_app/current/share/sqlports}
STABLE_FILE=${2:-/tmp/openbsd_app/stable/share/sqlports}
(
cd /tmp/openbsd_app/current
curl -L -O https://cdn.openbsd.org/pub/OpenBSD/snapshots/packages/amd64/sqlports-7.36p0.tgz
tar -C . -zxvf sqlports-7.36p0.tgz
)
(
cd /tmp/openbsd_app/stable
curl -L -O https://cdn.openbsd.org/pub/OpenBSD/7.1/packages/amd64/sqlports-7.36p0.tgz
tar -C . -zxvf sqlports-7.36p0.tgz
)
SQL=$(cat <<EOF
ATTACH DATABASE '%s' AS ports;
CREATE VIRTUAL TABLE
%s
USING fts5(
FULLPKGNAME,
FULLPKGPATH,
COMMENT,
DESCRIPTION);
INSERT INTO
%s
(FULLPKGNAME, FULLPKGPATH, COMMENT, DESCRIPTION)
SELECT
fullpkgname,
_paths.fullpkgpath,
comment,
_descr.value
FROM
ports._ports
JOIN _paths ON _paths.id=_ports.fullpkgpath
JOIN _descr ON _descr.fullpkgpath=_ports.fullpkgpath;
EOF
)
rm -f combined.db
printf "$SQL\n" ${CURRENT_FILE} \
"current_ports_fts" \
"current_ports_fts" | sqlite3 combined.db
printf "$SQL\n" ${STABLE_FILE} \
"stable_ports_fts" \
"stable_ports_fts" | sqlite3 combined.db