Files
Wallos/migrations/000029.php
Miguel Ribeiro a173d2765f feat: fisrt api endpoint
feat: user has api key available on profile page
feat: api endpoint to calculate monthly cost
feat: split settings page into settings and profile page
feat: redesigned experimental mobile navigation menu
fix: small fixes and typos
2024-10-04 17:42:35 +02:00

22 lines
760 B
PHP

<?php
// This migration adds a "api_key" column to the user table
// It also generates an API key for each user
/** @noinspection PhpUndefinedVariableInspection */
$columnQuery = $db->query("SELECT * FROM pragma_table_info('user') where name='api_key'");
$columnRequired = $columnQuery->fetchArray(SQLITE3_ASSOC) === false;
if ($columnRequired) {
$db->exec('ALTER TABLE user ADD COLUMN api_key TEXT');
}
/** @noinspection PhpUndefinedVariableInspection */
$users = $db->query('SELECT * FROM user');
while ($user = $users->fetchArray(SQLITE3_ASSOC)) {
if (empty($user['api_key'])) {
$apiKey = bin2hex(random_bytes(32));
$db->exec('UPDATE user SET api_key = "' . $apiKey . '" WHERE id = ' . $user['id']);
}
}