<?php
declare(strict_types=1);

require __DIR__ . '/../../config/bootstrap.php';

$db = \App\Core\Database::getInstance();

$hasTable = (int)$db->query("SELECT COUNT(*) FROM information_schema.TABLES WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dictionary'")->fetchColumn();
if ($hasTable <= 0) {
  echo "dictionary table not found. Skipping.\n";
  exit;
}

$hasOrg = (int)$db->query("SELECT COUNT(*) FROM information_schema.COLUMNS WHERE TABLE_SCHEMA=DATABASE() AND TABLE_NAME='dictionary' AND COLUMN_NAME='org_id'")->fetchColumn();
if ($hasOrg <= 0) {
  $db->exec("ALTER TABLE dictionary ADD COLUMN org_id BIGINT UNSIGNED NULL AFTER id");
  $db->exec("ALTER TABLE dictionary ADD KEY idx_dictionary_org (org_id)");
  echo "Added dictionary.org_id + index.\n";
} else {
  echo "dictionary.org_id already exists.\n";
}

echo "Pack205.15 DB OK\n";
