use Cwd 'abs_path';
use YAML 'LoadFile';
use Data::Dumper;
+use DBI;
+use Term::ANSIColor;
+use Crypt::SaltedHash;
$Data::Dumper::Indent = 1;
$Data::Dumper::Sortkeys = 1;
##################
+$| = 1;
+
+my @target_tables = qw(
+ users
+);
+
+my $admin_data = {
+ 'login' => 'frank',
+ 'vorname' => 'Frank',
+ 'nachname' => 'Brehm',
+ 'email' => 'frank@brehm-online.com',
+ 'passwd' => 'uhu',
+};
+
my $debug = $ENV{'COOKBOOK_DEBUG'} ? 1 : 0;
my $home_dir = abs_path( File::Spec->canonpath( $FindBin::Bin . "/.." ) );
-print "Home-Dir: '$home_dir'\n";
+print "Home-Dir: '$home_dir'\n" if $debug;
my $global_conf_file = $home_dir . "/cookbook.yml";
my $local_conf_file = $home_dir . "/cookbook_local.yml";
}
# Konfiguration einlesen ...
-print "Lese globale Konfiguration '$global_conf_file' ein ... ";
+print green_star() . " Lese globale Konfiguration '$global_conf_file' ein ... ";
eval { ($global_conf) = LoadFile($global_conf_file); };
if ($@) {
- print "NOT OK\n";
+ print not_ok() . "\n";
warn $@ . "\n";
exit 5;
}
-print "OK\n";
+print ok() . "\n";
print "Globale Konfiguration: " . Dumper($global_conf) if $debug;
if ( -f $local_conf_file ) {
- print "Lese lokale Konfiguration '$local_conf_file' ein ... ";
+ print green_star() . " Lese lokale Konfiguration '$local_conf_file' ein ... ";
eval { ($local_conf) = LoadFile($local_conf_file); };
if ($@) {
- print "NOT OK\n";
+ print not_ok() . "\n";
warn $@ . "\n";
exit 5;
}
- print "OK\n";
+ print ok() . "\n";
} ## end if ( -f $local_conf_file )
print "Lokale Konfiguration: " . Dumper($local_conf) if $debug;
# DSN's und Nutzerangaben ermitteln:
my ( $target_dsn, $target_user, $target_passwd, $source_dsn, $source_user, $source_passwd );
+my ( $target_dbh, $target_sth, $source_dbh, $source_sth );
my $port;
printf "Ziel-Datenbank: DSN='%s', User='%s', Pwd='%s'\n", $target_dsn, $target_user, $target_passwd;
}
+END {
+ close_dbs();
+}
+
+print "\n";
+open_dbs();
print "\n";
+exit 7 unless drop_target_tables();
+exit 8 unless create_target_tables();
+
exit 0;
+
+###############################################################
+#
+
+=head1 Funktionen
+
+Alle hier verwendeten Funktionen ...
+
+=cut
+
+#-------------------------------------------------------------
+
+=head2 close_dbs( )
+
+Schließt alle Datenbank-Verbindungen.
+
+=cut
+
+sub close_dbs {
+
+ if ( $target_dbh and $target_dbh->{'Active'} ) {
+ print green_star() . " Schließe Ziel-Datenbank ... ";
+ $target_dbh->disconnect();
+ $target_dbh = undef;
+ print ok() . "\n";
+ }
+
+ if ( $source_dbh and $source_dbh->{'Active'} ) {
+ print green_star() . " Schließe Quell-Datenbank ... ";
+ $source_dbh->disconnect();
+ $source_dbh = undef;
+ print ok() . "\n";
+ }
+
+ print "\n";
+
+} ## end sub close_dbs
+
+#-------------------------------------------------------------
+
+=head2 open_dbs( )
+
+Oeffnen der beiden Datenbanken und Speicher in $source_dbh und $target_dbh
+
+Gibt zurueck, ob erfolgreich oder nicht ...
+
+=cut
+
+sub open_dbs {
+
+ my $attribs = {};
+ $attribs->{'RaiseError'} = 0;
+ $attribs->{'AutoCommit'} = 1;
+
+ if ( $source_dbh and $source_dbh->{'Active'} ) {
+ warn warning() . " Quell-Datenbank ist bereits geöffnet.\n";
+ }
+ else {
+ print green_star() . " Öffne Quell-Datenbank ... ";
+ eval {
+ unless ( $source_dbh = DBI->connect( $source_dsn, $source_user, $source_passwd, $attribs ) )
+ {
+ die $DBI::errstr;
+ }
+ };
+ if ($@) {
+ print not_ok() . "\n";
+ warn warning() . " Konnte Quell-Datenbank nicht öffnen: " . $@ . "\n";
+ exit 5;
+ }
+ print ok() . "\n";
+ } ## end else [ if ( $source_dbh and $source_dbh->{'Active'...
+
+ if ( $target_dbh and $target_dbh->{'Active'} ) {
+ warn warning() . " Ziel-Datenbank ist bereits geöffnet.\n";
+ }
+ else {
+ print green_star() . " Öffne Ziel-Datenbank ... ";
+ eval {
+ unless ( $target_dbh = DBI->connect( $target_dsn, $target_user, $target_passwd, $attribs ) )
+ {
+ die $DBI::errstr;
+ }
+ };
+ if ($@) {
+ print not_ok() . "\n";
+ warn warning() . " Konnte Ziel-Datenbank nicht öffnen: " . $@ . "\n";
+ exit 5;
+ }
+ print ok() . "\n";
+ } ## end else [ if ( $target_dbh and $target_dbh->{'Active'...
+
+ return 1;
+
+} ## end sub open_dbs
+
+#-------------------------------------------------------------
+
+=head2 red_star( ), yellow_star( ) und green_star( )
+
+Geben einen Stern in der gegebenen Farbe zurueck.
+
+=cut
+
+sub red_star {
+ return colored( "*", 'bold red' );
+}
+
+sub yellow_star {
+ return colored( "*", 'bold yellow' );
+}
+
+sub green_star {
+ return colored( "*", 'bold green' );
+}
+
+#-------------------------------------------------------------
+
+=head2 ok( )
+
+Gibt ein gruenes 'OK' zurueck.
+
+=cut
+
+sub ok {
+ return "[" . colored( "OK", 'bold green' ) . "]";
+}
+
+#-------------------------------------------------------------
+
+=head2 warning( )
+
+Gibt ein gelbes 'WARNING' zurueck.
+
+=cut
+
+sub warning {
+ return "[" . colored( "WARNING", 'bold yellow' ) . "]";
+}
+
+#-------------------------------------------------------------
+
+=head2 not_ok( )
+
+Gibt ein rotes 'NOT OK' zurueck.
+
+=cut
+
+sub not_ok {
+ return "[" . colored( "NOT OK", 'bold red' ) . "]";
+}
+
+#-------------------------------------------------------------
+
+=head2 drop_target_tables( )
+
+Loescht in der Ziel-Datenbank alle Tabellen ...
+
+=cut
+
+sub drop_target_tables {
+
+ print green_star() . " Lösche alle Zieltabellen ...\n";
+ for my $table ( reverse(@target_tables) ) {
+ my $sql = "DROP TABLE IF EXISTS `" . $table . "`";
+ print " - " . $table . " ";
+ eval { die "Nicht gelöscht.\n" unless $target_dbh->do($sql); };
+ if ($@) {
+ print not_ok() . "\n";
+ warn "$@\n";
+ return undef;
+ }
+ print ok() . "\n";
+ } ## end for my $table ( reverse(@target_tables) )
+ print "\n";
+
+ return 1;
+
+} ## end sub drop_target_tables
+
+#-------------------------------------------------------------
+
+=head2 create_target_tables( )
+
+Erstellt alle notwendigen Ziel-Tabellen ...
+
+=cut
+
+sub create_target_tables {
+
+ my %create_method = ( 'users' => \&create_user_table, );
+
+ print green_star() . " Erstelle Ziel-Tabellen ...\n";
+
+ for my $table (@target_tables) {
+ my $method = $create_method{$table};
+ unless ($method) {
+ die "Keine Create-Methode für Tabelle '$method' gefunden.\n";
+ }
+ print " - " . $table . " ";
+ eval { die "Tabelle '" . $table . "' wurde nicht erstellt.\n" unless $method->(); };
+ if ($@) {
+ print not_ok() . "\n";
+ warn $@ . "\n";
+ return undef;
+ }
+ print ok() . "\n";
+ } ## end for my $table (@target_tables)
+ print "\n";
+
+ return 1;
+} ## end sub create_target_tables
+
+#-------------------------------------------------------------
+
+=head2 create_user_table( )
+
+Erstellt die Tabelle 'users' ...
+
+=cut
+
+sub create_user_table {
+
+ my $sql = <<END_SQL;
+CREATE TABLE `users` (
+ `user_id` int(10) unsigned NOT NULL auto_increment,
+ `login` varchar(50) NOT NULL COMMENT 'Login-Name',
+ `vorname` varchar(100) NOT NULL,
+ `nachname` varchar(100) NOT NULL,
+ `password` varchar(250) NOT NULL,
+ `date_created` datetime NOT NULL,
+ `date_changed` datetime NOT NULL,
+ `email` varchar(250) NOT NULL,
+ `deleted` enum('n','y') NOT NULL default 'n',
+ `enabled` enum('y','n') NOT NULL default 'y',
+ `admin_status` enum('n','y') NOT NULL default 'n',
+ `comments` text NOT NULL,
+ PRIMARY KEY (`user_id`),
+ UNIQUE KEY `login` (`login`),
+ KEY `nachname` (`nachname`),
+ FULLTEXT KEY `comments` (`comments`),
+ FULLTEXT KEY `nachname_fulltext` (`nachname`)
+) ENGINE=MyISAM DEFAULT CHARSET=utf8 COMMENT='Alle angemeldeten Benutzer des Kochbuchs'
+END_SQL
+
+ return undef unless $target_dbh->do($sql);
+
+ my $saltlen = $local_conf->{'authentication'}{'dbic'}{'password_salt_len'}
+ || $global_conf->{'authentication'}{'dbic'}{'password_salt_len'}
+ || 4;
+ my $csh = Crypt::SaltedHash->new( 'algorithm' => 'SHA-1', 'salt_len' => $saltlen );
+ $csh->add( $admin_data->{'passwd'} );
+ my $passwd = $csh->generate();
+
+ $sql = <<END_SQL;
+INSERT INTO `users`
+ ( `user_id`, `login`, `vorname`, `nachname`, `password`, `date_created`, `date_changed`, `email`, `admin_status` )
+ VALUES
+ ( 1, %s, %s, %s, %s, now(), now(), %s, 'y' )
+END_SQL
+
+ $sql = sprintf( $sql,
+ $target_dbh->quote( $admin_data->{'login'} ),
+ $target_dbh->quote( $admin_data->{'vorname'} ),
+ $target_dbh->quote( $admin_data->{'nachname'} ),
+ $target_dbh->quote($passwd),
+ $target_dbh->quote( $admin_data->{'email'} ) );
+
+ return undef unless $target_dbh->do($sql);
+
+ return 1;
+
+} ## end sub create_user_table
+
+#-------------------------------------------------------------
+
+__END__
+