<?php

require dirname(__FILE__) . "/bootstrap.php";

$config = parse_ini_file(ROOT . '/../config/backend.ini', true);

if (is_array($config) && isset($config['bootstrap']))
{
    $databaseconfig = $config['bootstrap'];

    if (!isset($databaseconfig['username']))
    {
        $databaseconfig['username'] = get_current_user();
    }

    $dbconn = pg_pconnect(
      sprintf(
          'dbname=%s host=%s port=%s user=%s password=%s',
          $databaseconfig['database'],
          $databaseconfig['host'],
          $databaseconfig['port'],
          $databaseconfig['username'],
          $databaseconfig['password']
      )
    );

    if ($dbconn === false) die('Could not connect DB!');
} else {
  die('Invalid DB Config!');
}


$res = pg_query($dbconn,
"SELECT table_name
  FROM information_schema.tables
   WHERE table_schema='public'
     AND table_type='BASE TABLE'
     AND table_name LIKE 'packet20______';");

while (($record = pg_fetch_assoc($res)) !== false)
{
    // Update Fields..
    echo 'Setting fillfactor ' . $record['table_name'] . "... \r\n";
    pg_query($dbconn, "ALTER TABLE {$record['table_name']} SET (fillfactor = 70);");

    echo 'Vacuum table ' . $record['table_name'] . "... \r\n";
    pg_query($dbconn, "VACUUM FULL {$record['table_name']};");
}

echo "Nothing to do...\r\nProcessing complete!\r\n";
