User:Victor Grousset - tuxayo/Development/Tools and operations

From Koha Wiki
Jump to navigation Jump to search

Run automated tests

One or just a few

prove t/db_dependent/Search.t
prove t/db_dependent/Search.t t/db_dependent/selenium/administration_tasks.t

# Cypress tests
time cypress run --config video=false --spec t/cypress/integration/ERM/Dialog_spec.ts
time cypress run --config video=false --spec "t/cypress/integration/ERM/Dialog_spec.ts,t/cypress/integration/ERM/Agreements_spec.ts"

All tests or all Cypress or all Selenium tests

Continuous_Integration#Run_automated_tests

Lighter restart in KTD (koha-testing-docker)

koha-plack --restart kohadev

Interactive Perl shell - REPL (Read–Eval–Print Loop) with Devel::REPL

Install and run

sudo apt install libdevel-repl-perl
re.pl

Usage examples

Connect to the database

my $dbh = DBI->connect('dbi:mysql:;host=db;port=3306', 'koha_kohadev', 'password');

Test number formatting

use Number::Format;
my $format = new Number::Format(-decimal_point => '.');
# $Number_Format1 = Number::Format=HASH(0x39cbf00);
$format->unformat_number("12345,89");
# 12345.89

# Caveat: don't use print if you want to see the value, otherwise you will get some kind of return value of print
$ print $format->unformat_number("12345,89");
# 1

Trigger the installer

echo "DROP DATABASE koha_kohadev; CREATE DATABASE koha_kohadev;" | koha-mysql kohadev ; restart_all

Install translations

https://wiki.koha-community.org/wiki/Installation_of_additional_languages_for_OPAC_and_INTRANET_staff_client

update dababase

~/src/installer/data/mysql/updatedatabase.pl

Use yarn in KTD

# example with yarn install
sudo yarn install --modules-folder /kohadevbox/node_modules

Loop to create a lot of data in Perl

perl -MKoha::Database -e '
    my $schema = Koha::Database->schema;
    my $period = $schema->resultset("Aqbudgetperiod")->create({
        budget_period_startdate => "2000-01-01",
        budget_period_enddate => "2999-12-31"
    });
    $schema->resultset("Aqbudget")->create({
        budget_owner_id => 1,
        budget_period_id => $period->id
    }) for (1..1000)
'

Restore backup in KTD (koha-testing-docker)

It's not clean, there are errors when running koha-remove so it's an incomplete removal. That's the best I've found for now.

docker exec -it koha-koha-1 bash
service koha-common stop
service apache2 stop
koha-remove kohadev
cd koha
koha-restore kohadev-2024-03-19.sql.gz kohadev-2024-03-19.tar.gz
restart_all

Reset DB with dump

If you don't have a devbox. Using BibLibre's tools (https://git.biblibre.com/biblibre/tools)

# memcached is flushed because if you switched to another version of the code, the old version will be in cache and the updatedatabase won't run

echo "flush_all" | nc -q 2 localhost 11211 && \
~/tools/sysop/db_replace.sh --confirm --dump ~/dumps/koha-sample.sql.gz  && \
~/src/installer/data/mysql/updatedatabase.pl  && \
~/tools/zebra/rebuild_full.sh -b -a

Use Elasticsearch

Switch the SearchEngine syspref

Index data

misc/search_tools/rebuild_elasticsearch.pl -v -d

Maybe also

# as root
koha-elasticsearch -v -p 5 --commit 1000 --rebuild kohadev

Test SIP with emulator

https://wiki.koha-community.org/wiki/Koha_SIP2_server_setup#Example

Run an old Koha

# Can go at least to 19.11
sudo apt install libyaml-syck-perl libtext-unaccent-perl libemail-valid-perl  libcatmandu-marc-perl libcatmandu-store-elasticsearch-perl  libextutils-pkgconfig-perl libwww-curl-perl libcurl4-gnutls-dev
sudo cpanm Search::Elasticsearch::Client::5_0::Direct

# you might need
sudo cpanm -i JSON::Validator::OpenAPI --force
# TODO try
sudo cpanm -i JSON::Validator

# you might need
sudo apt install libdbd-mock-perl

If the container has a too old Debian

# Somehow it needs elasticsearch, here opensearch works fine
KOHA_IMAGE=22.05-stretch bin/ktd --os1 up
# It works with the master branch of KTD repo. After trying,
# older branches don't help to simplify the operation.

# and **while** it's starting go into the container and edit the repositories
vim /etc/apt/sources.list

# replace "httpredir" with "archive" and remove the 2nd line.
# In the end the content should be
```
deb http://archive.debian.org/debian stretch main
```
# otherwise it will 404 when trying to install the cpanoutdated package and crash

Apply large patches without adding a git remote

wget -q -O- http://git.biblibre.com/biblibre/kohac/commit/457524cef965faf96a9516c17e9e0a8bbdec19aa.patch | git am -3 -

Generate test data

Many patrons

base:

SELECT surname, firstname, branchcode, categorycode
  FROM
    (
      SELECT surname
        FROM borrowers
       ORDER BY rand()
    ) a,
    (
      SELECT firstname
        FROM borrowers
       ORDER BY rand() ) b,
    (
      SELECT branchcode
        FROM borrowers
       ORDER BY rand() ) c,
    (
      SELECT categorycode
        FROM borrowers
       ORDER BY rand() ) d
 LIMIT 50000

Many holds

Thanks Joubu :)

use C4::Reserves;
use Koha::Patrons;
use t::lib::Mocks;
use Koha::DateUtils qw( dt_from_string );
t::lib::Mocks::mock_userenv({branchcode => 'CPL'});
my $items = Koha::Items->search;
my $patron = Koha::Patrons->find(5);
my $i;
while(my $item = $items->next){
    say $i++;
    next unless $item->barcode;
    my $reservation_date = dt_from_string->subtract(days => rand(45));
    my $expiration_date = dt_from_string->add(days => rand(45));
    C4::Reserves::AddReserve({ branchcode => $patron->branchcode, borrowernumber => $patron->borrowernumber, biblionumber => $item->biblionumber, reservation_date => $reservation_date, expiration_date => $expiration_date, itemnumber => $item->itemnumber });
    last if $i > 50;
}

Thanks Kidclamp :)

#! /usr/bin/perl
use t::lib::TestBuilder;
use Koha::Libraries;
use Koha::Holds;
use Koha::Patrons;
use Koha::DateUtils qw(dt_from_string);


my $libraries = Koha::Libraries->search();
my $builder = t::lib::TestBuilder->new();

while( my $library = $libraries->next) {
    my $several = int( rand(10) )+10;
    for( my $i = 0; $i < $several; $i++ ){
        my $holder = Koha::Patrons->search({},{'order_by'=>\"rand()"})->next;
        my $biblio = Koha::Biblios->search({},{'order_by'=>\"rand()"})->next;
        next unless $biblio;
        my $item = $biblio->items->search({},{ order_by => \["rand()"] })->next;
        # Below is to set a 50/50 chance of creating an item level versus next available hold
        my $itemnumber = $item && int( rand(2) ) ? $item->itemnumber : undef;
        my $hold = $builder->build_object({
            class => "Koha::Holds",
            value => {
                borrowernumber => $holder->borrowernumber,
                biblionumber => $biblio->biblionumber,
                reservedate => dt_from_string(),
                branchcode  => $library->branchcode,
                desk_id => undef,
                cancellationdate => undef,
                cancellation_reason => undef,
                priority => $biblio->holds->count()+1,
                found => undef,
                itemnumber => $itemnumber,
                waitingdate => undef,
                expirationdate => undef,
                suspend => 0,
                suspend_until=>undef,
                item_level_hold => $itemnumber ? 1 : 0,
                itemtype => undef,
                patron_expiration_date => undef,
            }
        });
    }
}

1;

Send emails

Setup email using your own SMTP server.

edit koha-conf.xml to add the following inside the <config> section

 <smtp_server>
    <host>yourSMTP.example.org</host>
    <port>587</port>
    <timeout>5</timeout>
    <ssl_mode>STARTTLS</ssl_mode>
    <user_name>youraddress@example.org</user_name>
    <password>yourpassword</password>
    <debug>1</debug>
 </smtp_server>

A set KohaAdminEmailAddress to your address.

Test the REST API

$.ajax({
    url: '/api/v1/biblios/55/items/117',
    method: 'PUT',
    data: JSON.stringify({
          "collection_code": "COLLECTION1",
          "external_id": "123test",
  }),
    contentType: 'application/json',
    success: function(result) {
        console.log('altered')
    },
});

Switch Starman to deployment/production environment

To see the production error pages for example.

sudo vim /usr/sbin/koha-plack

See this section

     if [ "$DEV_INSTALL" = "1" ]; then
         # Maybe we should switch off debug_mode if DEV_INSTALL is not set?
         environment="development"
     fi

Replace "development" by "deployment"

See also