App-Dochazka-REST
view release on metacpan or search on metacpan
lib/App/Dochazka/REST.pm view on Meta::CPAN
=cut
sub reset_mason_dir {
my $status;
$log->info( "Checking permissions of Mason directory (DOCHAZKA_STATE_DIR)" );
my $statedir = $site->DOCHAZKA_STATE_DIR;
die "OUCH!!! DOCHAZKA_STATE_DIR site parameter not defined!" unless $statedir;
die "OUCH!!! DOCHAZKA_STATE_DIR $statedir is not readable by me!" unless -r $statedir;
die "OUCH!!! DOCHAZKA_STATE_DIR $statedir is not writable by me!" unless -w $statedir;
die "OUCH!!! DOCHAZKA_STATE_DIR $statedir is not executable by me!" unless -x $statedir;
my $masondir = File::Spec->catfile( $statedir, 'Mason' );
$log->debug( "Mason directory is $masondir" );
rmtree( $masondir );
mkpath( $masondir, 0, 0750 );
# re-create
my $comp_root = File::Spec->catfile( $masondir, 'comp_root' );
mkpath( $comp_root, 0, 0750 );
my $data_dir = File::Spec->catfile( $masondir, 'data_dir' );
mkpath( $data_dir, 0, 0750 );
$status = App::Dochazka::REST::Mason::init_singleton(
comp_root => $comp_root,
data_dir => $data_dir
);
return $status unless $status->ok;
$status->payload( $comp_root );
return $status;
}
=head2 initialize_activities_table
Create the activities defined in the site parameter
DOCHAZKA_ACTIVITY_DEFINITIONS
=cut
sub initialize_activities_table {
my $conn = shift;
my $status = $CELL->status_ok;
try {
$conn->txn( fixup => sub {
my $sth = $_->prepare( $site->SQL_ACTIVITY_INSERT );
foreach my $actdef ( @{ $site->DOCHAZKA_ACTIVITY_DEFINITIONS } ) {
$sth->bind_param( 1, $actdef->{code} );
$sth->bind_param( 2, $actdef->{long_desc} );
$sth->bind_param( 3, 'dbinit' );
$sth->execute;
}
} );
} catch {
$status = $CELL->status_err( 'DOCHAZKA_DBI_ERR', args => [ $_ ] );
};
return $status;
}
=head2 reset_db
Drop and re-create a Dochazka database. Takes superuser credentials as
arguments.
Be very, _very_, _VERY_ careful with this function.
=cut
sub reset_db {
my $status;
my $dbname = $site->DOCHAZKA_DBNAME;
my $dbuser = $site->DOCHAZKA_DBUSER;
my $dbpass = $site->DOCHAZKA_DBPASS;
$log->debug( "Entering " . __PACKAGE__ . "::reset_db to initialize database $dbname with credentials $dbuser / $dbpass" );
# PGTZ *must* be set
$ENV{'PGTZ'} = $site->DOCHAZKA_TIMEZONE;
# create:
# - audit schema (see config/sql/audit_Config.pm)
# - public schema (all application-specific tables, functions, triggers, etc.)
# - the 'root' and 'demo' employees
# - privhistory record for root
print "Getting database connection...";
my $conn = App::Dochazka::REST::ConnBank::get_arbitrary_dbix_conn(
$dbname, $dbuser, $dbpass
);
print "done\n";
print "Initializing audit schema...";
$status = run_sql(
$conn,
@{ $site->DBINIT_AUDIT },
);
if ( $status->not_ok ) {
print Dumper( $status ), "\n";
return $status;
}
print "done\n";
print "Initializing public schema...";
$status = run_sql(
$conn,
@{ $site->DBINIT_CREATE },
);
if ( $status->not_ok ) {
print Dumper( $status ), "\n";
return $status;
}
print "done\n";
# get EID of root employee that was just created, since
# we will need it in the second round of SQL statements
my $eids = get_eid_of( $conn, "root", "demo" );
$site->set( 'DOCHAZKA_EID_OF_ROOT', $eids->{'root'} );
$site->set( 'DOCHAZKA_EID_OF_DEMO', $eids->{'demo'} );
# the second round of SQL statements to make root employee immutable
# is taken from DBINIT_MAKE_ROOT_IMMUTABLE site param
# prep DBINIT_MAKE_ROOT_IMMUTABLE
# (replace ? with EID of root employee in all the statements
# N.B.: we avoid the /r modifier here because we might be using Perl # 5.012)
my @root_immutable_statements = map {
local $_ = $_; s/\?/$eids->{'root'}/g; $_;
} @{ $site->DBINIT_MAKE_ROOT_IMMUTABLE };
# run the modified statements
$status = run_sql(
$conn,
@root_immutable_statements,
);
return $status unless $status->ok;
( run in 1.019 second using v1.01-cache-2.11-cpan-39bf76dae61 )