#!/usr/bin/env perl
our
$home
;
BEGIN {
FindBin::again();
$home
= (
$ENV
{NETDISCO_HOME} ||
$ENV
{HOME});
if
(!
exists
$ENV
{PERL_LOCAL_LIB_ROOT}) {
my
$localenv
= File::Spec->catfile(
$FindBin::RealBin
,
'localenv'
);
exec
(
$localenv
, $0,
@ARGV
)
if
-f
$localenv
;
$localenv
= File::Spec->catfile(
$home
,
'perl5'
,
'bin'
,
'localenv'
);
exec
(
$localenv
, $0,
@ARGV
)
if
-f
$localenv
;
die
"Sorry, can't find libs required for App::Netdisco.\n"
if
!
exists
$ENV
{PERLBREW_PERL};
}
}
BEGIN {
unshift
@INC
,
dir(
$FindBin::RealBin
)->parent->subdir(
'lib'
)->stringify,
dir(
$FindBin::RealBin
,
'lib'
)->stringify;
$ENV
{PATH} =
$FindBin::RealBin
.
$Config
{path_sep} .
$ENV
{PATH};
}
my
$schema
= schema(
'netdisco'
);
my
$pg_ver
=
$schema
->storage->dbh->{pg_server_version};
if
(
$pg_ver
and
$pg_ver
< 90600) {
printf
"\nFATAL: minimum PostgreSQL version for Netdisco is 9.6, you have %s\n"
,
(
join
'.'
,
reverse
map
{
scalar
reverse
}
unpack
(
"(A2)*"
,
reverse
$pg_ver
));
print
"\nUnfortunately, if you are reading this, you already upgraded Netdisco,"
;
print
"\nso your only options now are to delete and reinstall Netdisco, or upgrade"
;
print
"\nyour PostgreSQL installation.\n"
;
print
"\nPlease always read the Release Notes before upgrading:"
;
die
"\n"
;
}
if
(
scalar
@ARGV
and
$ARGV
[0] and
$ARGV
[0] eq
'--redeploy-all'
) {
$schema
->storage->dbh_do(
sub
{
my
(
$storage
,
$dbh
,
@args
) =
@_
;
$dbh
->
do
(
'DROP TABLE dbix_class_schema_versions'
);
},
);
}
if
(not
$schema
->get_db_version) {
$schema
->install(1);
$schema
->storage->disconnect;
}
try
{
$schema
->storage->dbh_do(
sub
{
my
(
$storage
,
$dbh
) =
@_
;
$dbh
->selectrow_arrayref(
"SELECT * FROM device WHERE 0 = 1"
);
});
$schema
->_set_db_version({
version
=> 2})
if
$schema
->get_db_version == 1;
$schema
->storage->disconnect;
};
my
$txn_guard
=
$ENV
{ND2_DB_ROLLBACK}
? schema(
'netdisco'
)->storage->txn_scope_guard :
undef
;
my
$db_version
= (
$schema
->get_db_version || 1);
my
$target_version
=
$schema
->schema_version;
for
(
my
$i
=
$db_version
;
$i
<
$target_version
;
$i
++) {
my
$next
=
$i
+ 1;
try
{
$schema
->upgrade_single_step(
$i
,
$next
);
}
catch
{
warn
"Error: $_"
if
$_
!~ m/(does not exist|already
exists
)/;
$schema
->_set_db_version({
version
=>
$next
})
if
$schema
->get_db_version <
$next
;
};
}
undef
$txn_guard
if
$ENV
{ND2_DB_ROLLBACK};
exit
0;