<?php
/*
* Migrate $table_suffix to Declarative Tables
*/
require dirname(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!');
}


// Query all packet tables
$packet = [];
$res = pg_query($dbconn,
"
SELECT
    nmsp_parent.nspname AS parent_schema,
    parent.relname      AS parent,
    nmsp_child.nspname  AS child_schema,
    child.relname       AS child
FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname='packet';
");
while (($record = pg_fetch_assoc($res)) !== false)
{
  $packet[$record['child']] = $record;
}


// Query all path tables
$packet_path = [];
$res = pg_query($dbconn,
"
SELECT
    nmsp_parent.nspname AS parent_schema,
    parent.relname      AS parent,
    nmsp_child.nspname  AS child_schema,
    child.relname       AS child
FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname='packet_path';
");
while (($record = pg_fetch_assoc($res)) !== false)
{
  $packet_path[$record['child']] = $record;
}

// Query all weather tables
$packet_weather = [];
$res = pg_query($dbconn,
"
SELECT
    nmsp_parent.nspname AS parent_schema,
    parent.relname      AS parent,
    nmsp_child.nspname  AS child_schema,
    child.relname       AS child
FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname='packet_weather';
");
while (($record = pg_fetch_assoc($res)) !== false)
{
  $packet_weather[$record['child']] = $record;
}

// Query all telelemtry tables
$packet_telemetry = [];
$res = pg_query($dbconn,
"
SELECT
    nmsp_parent.nspname AS parent_schema,
    parent.relname      AS parent,
    nmsp_child.nspname  AS child_schema,
    child.relname       AS child
FROM pg_inherits
    JOIN pg_class parent            ON pg_inherits.inhparent = parent.oid
    JOIN pg_class child             ON pg_inherits.inhrelid   = child.oid
    JOIN pg_namespace nmsp_parent   ON nmsp_parent.oid  = parent.relnamespace
    JOIN pg_namespace nmsp_child    ON nmsp_child.oid   = child.relnamespace
WHERE parent.relname='packet_telemetry';
");
while (($record = pg_fetch_assoc($res)) !== false)
{
  $packet_telemetry[$record['child']] = $record;
}

echo "Checking table attachments...\r\n";

$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______' ORDER BY table_name ASC;");

while (($record = pg_fetch_assoc($res)) !== false)
{
  if (!isset($packet[$record['table_name']]))
  {
    echo "Error: Table {$record['table_name']} is not attached!\r\n";
  }

  if (!isset($packet_path[$record['table_name'].'_path']))
  {
    echo "Error: Table {$record['table_name']}_path is not attached!\r\n";
  }

  if (!isset($packet_weather[$record['table_name'].'_weather']))
  {
    echo "Error: Table {$record['table_name']}_weather is not attached!\r\n";
  }

  if (!isset($packet_telemetry[$record['table_name'].'_telemetry']))
  {
    echo "Error: Table {$record['table_name']}_telemetry is not attached!\r\n";
  }
}

echo "Complete!\r\n";
