|
Lines 6-53
use DBI;
Link Here
|
| 6 |
use File::Temp qw( tempfile ); |
6 |
use File::Temp qw( tempfile ); |
| 7 |
use XML::LibXML; |
7 |
use XML::LibXML; |
| 8 |
|
8 |
|
|
|
9 |
our $database; |
| 9 |
sub import { |
10 |
sub import { |
| 10 |
my ($self, %args) = @_; |
11 |
my ($self, %args) = @_; |
| 11 |
|
12 |
|
| 12 |
unless (defined $args{database}) { |
|
|
| 13 |
die "Test database is not defined"; |
| 14 |
} |
| 15 |
|
| 16 |
$args{marcflavour} //= 'MARC21'; |
| 17 |
|
| 18 |
my $xml = XML::LibXML->load_xml(location => $ENV{KOHA_CONF}); |
| 19 |
my $root = $xml->documentElement(); |
| 20 |
my ($databaseElement) = $root->findnodes('//config/database'); |
| 21 |
my $currentDatabase = $databaseElement->textContent(); |
| 22 |
|
| 23 |
if ($currentDatabase eq $args{database}) { |
| 24 |
die "Test database is the same as database in KOHA_CONF, abort!"; |
| 25 |
} |
| 26 |
|
| 27 |
$databaseElement->firstChild()->setData($args{database}); |
| 28 |
|
| 29 |
my ($fh, $filename) = tempfile('koha-conf.XXXXXX', TMPDIR => 1, UNLINK => 1); |
| 30 |
$xml->toFH($fh); |
| 31 |
close $fh; |
| 32 |
|
| 33 |
$ENV{KOHA_CONF} = $filename; |
| 34 |
|
| 35 |
require C4::Context; |
13 |
require C4::Context; |
| 36 |
C4::Context->import; |
14 |
C4::Context->import; |
| 37 |
|
15 |
|
| 38 |
require C4::Installer; |
|
|
| 39 |
C4::Installer->import; |
| 40 |
|
| 41 |
require C4::Languages; |
| 42 |
|
| 43 |
my $host = C4::Context->config('hostname'); |
16 |
my $host = C4::Context->config('hostname'); |
| 44 |
my $port = C4::Context->config('port'); |
17 |
my $port = C4::Context->config('port'); |
| 45 |
my $database = C4::Context->config('database'); |
18 |
$database = C4::Context->config("database_test") || C4::Context->config('database'); |
| 46 |
my $user = C4::Context->config('user'); |
19 |
my $user = C4::Context->config('user'); |
| 47 |
my $pass = C4::Context->config('pass'); |
20 |
my $pass = C4::Context->config('pass'); |
| 48 |
|
21 |
|
| 49 |
say "Create test database $database..."; |
|
|
| 50 |
|
| 51 |
my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, { |
22 |
my $dbh = DBI->connect("dbi:mysql:;host=$host;port=$port", $user, $pass, { |
| 52 |
RaiseError => 1, |
23 |
RaiseError => 1, |
| 53 |
PrintError => 0, |
24 |
PrintError => 0, |
|
Lines 56-77
sub import {
Link Here
|
| 56 |
$dbh->do("DROP DATABASE IF EXISTS $database"); |
27 |
$dbh->do("DROP DATABASE IF EXISTS $database"); |
| 57 |
$dbh->do("CREATE DATABASE $database"); |
28 |
$dbh->do("CREATE DATABASE $database"); |
| 58 |
|
29 |
|
| 59 |
my $installer = C4::Installer->new(); |
|
|
| 60 |
$installer->load_db_schema(); |
| 61 |
$installer->set_marcflavour_syspref($args{marcflavour}); |
| 62 |
my (undef, $fwklist) = $installer->marc_framework_sql_list('en', $args{marcflavour}); |
| 63 |
my (undef, $list) = $installer->sample_data_sql_list('en'); |
| 64 |
my @frameworks; |
| 65 |
foreach my $fwk (@$fwklist, @$list) { |
| 66 |
foreach my $framework (@{ $fwk->{frameworks} }) { |
| 67 |
push @frameworks, $framework->{fwkfile}; |
| 68 |
} |
| 69 |
} |
| 70 |
my $all_languages = C4::Languages::getAllLanguages(); |
| 71 |
$installer->load_sql_in_order($all_languages, @frameworks); |
| 72 |
require Koha::SearchEngine::Elasticsearch; |
| 73 |
Koha::SearchEngine::Elasticsearch->reset_elasticsearch_mappings; |
| 74 |
$installer->set_version_syspref(); |
| 75 |
} |
30 |
} |
| 76 |
|
31 |
|
|
|
32 |
END { |
| 33 |
my $dbh = C4::Context->dbh; |
| 34 |
$dbh->do("DROP DATABASE IF EXISTS $database") if $database; |
| 35 |
Koha::Caches->get_instance()->flush_all; |
| 36 |
Koha::Caches->get_instance('config')->flush_all; |
| 37 |
}; |
| 38 |
|
| 77 |
1; |
39 |
1; |
| 78 |
- |
|
|