From 3282db7c1921c866905e83c4ab590b5374bdee79 Mon Sep 17 00:00:00 2001 From: davegilligan Date: Fri, 8 May 2026 17:15:41 +0200 Subject: [PATCH] Support live Caveau plan enum --- scripts/setup-caveau-access.php | 41 ++++++++++++++++++++++++++++++--- 1 file changed, 38 insertions(+), 3 deletions(-) diff --git a/scripts/setup-caveau-access.php b/scripts/setup-caveau-access.php index b5f0e3d..4d0c46d 100644 --- a/scripts/setup-caveau-access.php +++ b/scripts/setup-caveau-access.php @@ -37,10 +37,45 @@ function setupJson(array $value): string return json_encode($value, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); } +function setupEnumValues(PDO $db, string $table, string $column): array +{ + $stmt = $db->prepare( + 'SELECT COLUMN_TYPE + FROM information_schema.COLUMNS + WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = ? AND COLUMN_NAME = ? + LIMIT 1' + ); + $stmt->execute([$table, $column]); + $type = (string)$stmt->fetchColumn(); + + if (!preg_match("/^enum\\((.*)\\)$/", $type, $matches)) { + return []; + } + + $values = str_getcsv($matches[1], ',', "'"); + return array_values(array_filter(array_map('strval', $values))); +} + +function setupPreferredValue(array $available, array $preferred, string $fallback): string +{ + foreach ($preferred as $value) { + if (in_array($value, $available, true)) { + return $value; + } + } + return $available[0] ?? $fallback; +} + try { $db = dbnToolsDb(); $db->beginTransaction(); + $plan = setupPreferredValue( + setupEnumValues($db, 'clients', 'plan'), + ['enterprise', 'sovereign', 'corporate', 'professional', 'beta', 'starter', 'trial', 'sandbox'], + 'professional' + ); + $client = dbnToolsFetchClient($db); if (!$client) { $apiKeyHash = hash('sha256', random_bytes(32)); @@ -52,7 +87,7 @@ try { 'contact_email' => $email, 'industry' => 'Legal services', 'country' => 'NO', - 'plan' => 'enterprise', + 'plan' => $plan, 'api_key_hash' => $apiKeyHash, 'api_key_prefix' => $apiKeyPrefix, 'monthly_query_limit' => 10000, @@ -91,7 +126,7 @@ try { 'contact_email' => $email, 'industry' => 'Legal services', 'country' => 'NO', - 'plan' => 'enterprise', + 'plan' => $plan, 'monthly_query_limit' => 10000, 'monthly_document_limit' => 1000, 'monthly_user_limit' => 25, @@ -151,7 +186,7 @@ try { 'Family Legal', 'Family law corpus package for Do Better Norge legal tools.', 'LAW', - setupJson(['enterprise']), + setupJson([$plan]), ]); $packageId = (int)$db->lastInsertId(); } else {