From e104c6566cda924094541848f6f0fd9cf9c88a10 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andrej=20Rama=C5=A1euski?= <andrej@x2.cz>
Date: Tue, 19 Jan 2021 23:59:36 +0100
Subject: [PATCH] Indexer zaznamu

---
 .gitlab-ci.yml       |  2 +-
 script/index_records | 46 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 47 insertions(+), 1 deletion(-)
 create mode 100755 script/index_records

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 2d2a7e5..e064608 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -2,7 +2,7 @@ image: docker:19.03.12
 
 variables:
   DOCKER_TLS_CERTDIR: "/certs"
-  IMAGE_VER: 1.6.0
+  IMAGE_VER: 1.7.0
 
 services:
   - docker:19.03.12-dind
diff --git a/script/index_records b/script/index_records
new file mode 100755
index 0000000..0d7aec2
--- /dev/null
+++ b/script/index_records
@@ -0,0 +1,46 @@
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+use FindBin qw($Bin);
+use lib "$Bin/../lib";
+use File::Copy;
+
+use constant YAMDI => '/usr/bin/yamdi';
+
+#use PiTube::Schema;
+use Mojo::Pg;
+
+my $pg = Mojo::Pg->new
+    ->dsn($ENV{DB_DSN})
+    ->username($ENV{DB_USERNAME})
+    ->password($ENV{DB_PASSWORD})
+;
+
+my $records = $pg->db->query(qq[
+    select id,path from records
+    where not is_indexed
+    and path <> ''
+]);
+
+RECORD:
+while (my $record = $records->hash) {
+    next RECORD if ! -f $record->{path};
+
+    print "Indexing $record->{path}\n";
+
+    system(YAMDI, '-i', $record->{path}, '-o', $record->{path} . '.idx' );
+
+    if ($?) {
+        print "ERROR $!\n";
+    }
+    else {
+        move($record->{path} . '.idx', $record->{path});
+        $pg->db->update(
+            'records',
+            {is_indexed => 't'},
+            {id => $record->{id}},
+        );
+    }
+
+}
-- 
GitLab