Add database creation to installer.
authorThomas Hochstein <thh@inter.net>
Wed, 12 Jan 2011 15:41:21 +0000 (16:41 +0100)
committerThomas Hochstein <thh@inter.net>
Sun, 11 Aug 2013 20:16:24 +0000 (22:16 +0200)
It's not enough to create tables, one should
create the database first if it is still
missing ...

Signed-off-by: Thomas Hochstein <thh@inter.net>
install/install.pl

index 8ab7798..374d74b 100755 (executable)
@@ -47,6 +47,10 @@ my %Conf = %{ReadConfig($HomePath.'/newsstats.conf')};
 ##### Database table definitions
 ##### --------------------------------------------------------------------------
 
+my $DBCreate = <<SQLDB;
+CREATE DATABASE IF NOT EXISTS `$Conf{'DBDatabase'}` DEFAULT CHARSET=utf8;
+SQLDB
+
 my %DBCreate = ('DBTableRaw'  => <<RAW, 'DBTableGrps' => <<GRPS);
 -- 
 -- Table structure for table DBTableRaw
@@ -150,6 +154,23 @@ UPGRADE
 
 ##### --------------------------- End of definitions ---------------------------
 
+### create DB, if necessary
+if (!$OptUpdate) {
+  print "----------\nStarting database creation.\n";
+  # create database
+  # we can't use InitDB() as that will use a table name of
+  # the table that doesn't exist yet ...
+  my $DBHandle = DBI->connect(sprintf('DBI:%s:host=%s',$Conf{'DBDriver'},
+                                      $Conf{'DBHost'}), $Conf{'DBUser'},
+                                      $Conf{'DBPw'}, { PrintError => 0 });
+  my $DBQuery = $DBHandle->prepare($DBCreate);
+  $DBQuery->execute() or &Bleat(2, sprintf("Can't create database %s: %s%\n",
+                                           $Conf{'DBDatabase'}, $DBI::errstr));
+  
+  printf("Database table %s created succesfully.\n",$Conf{'DBDatabase'});
+  $DBHandle->disconnect;
+};
+
 ### DB init, read list of tables
 print "Reading database information.\n";
 my $DBHandle = InitDB(\%Conf,1);
@@ -158,7 +179,6 @@ my %TablesInDB =
 
 if (!$OptUpdate) {
   ##### installation mode
-  print "----------\nStarting database table generation.\n";
   # check for tables and create them, if they don't exist yet
   foreach my $Table (keys %DBCreate) {
     &CreateTable($Table);
This page took 0.010758 seconds and 4 git commands to generate.