Skip to content

Commit

Permalink
feat: added API to update the database (#2492)
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Oct 17, 2023
1 parent 04d9646 commit c297cd9
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 10 deletions.
6 changes: 3 additions & 3 deletions docs/update.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# 3. Update

First, please download the latest package of phpMyFAQ. Upgrading to phpMyFAQ 3.3.x is possible from the following
First, please download the latest package of phpMyFAQ. Upgrading to phpMyFAQ 4.0 is possible from the following
versions:

- phpMyFAQ 3.0.x
- phpMyFAQ 3.1.x
- phpMyFAQ 3.2.x
- phpMyFAQ 3.3.x
- phpMyFAQ 4.0.x

If you're running an older version of phpMyFAQ than listed above, we recommend a new and fresh installation. If you need
support for updating an old FAQ from the 1.x or 2.x series, [please email us](thorsten_AT_phpmyfaq_DOT_de).
Expand Down Expand Up @@ -76,7 +76,7 @@ URL in your browser:
Choose your installed phpMyFAQ version and click the button of the update script, your version will automatically be
updated.

## Upgrading from phpMyFAQ 3.3.x
## Upgrading from phpMyFAQ 4.0.x

Please make a full backup before you run the upgrade!
First, log in as admin into the admin section and enable the maintenance mode.
Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
site_name: phpMyFAQ v3.3
site_name: phpMyFAQ v4.0
theme:
name: readthedocs
highlightjs: true
Expand Down
1 change: 1 addition & 0 deletions nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ server {
rewrite admin/api/extract-package /admin/api/index.php last;
rewrite admin/api/create-temporary-backup /admin/api/index.php last;
rewrite admin/api/install-package /admin/api/index.php last;
rewrite admin/api/update-database /admin/api/index.php last;

# REST API v2.0
# * http://[...]/api/v2.0/<ACTION>
Expand Down
1 change: 1 addition & 0 deletions phpmyfaq/.htaccess
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ RewriteRule admin/api/download-package admin/api/index.php
RewriteRule admin/api/extract-package admin/api/index.php
RewriteRule admin/api/create-temporary-backup admin/api/index.php
RewriteRule admin/api/install-package admin/api/index.php
RewriteRule admin/api/update-database admin/api/index.php

# REST API v2.0
# * http://[...]/api/v2.0/<ACTION>
Expand Down
11 changes: 11 additions & 0 deletions phpmyfaq/src/admin-routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,4 +97,15 @@
)
);

$routes->add(
'admin.api.update-database',
new Route(
'/update-database',
[
'_controller' => [UpdateController::class, 'updateDatabase'],
'_methods' => 'POST'
]
)
);

return $routes;
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Filter;
use phpMyFAQ\Setup\Update;
use phpMyFAQ\Setup\Upgrade;
use phpMyFAQ\System;
use phpMyFAQ\Translation;
Expand Down Expand Up @@ -245,4 +246,28 @@ public function installPackage(): StreamedResponse
}
});
}

#[Route('admin/api/update-database')]
public function updateDatabase(): StreamedResponse
{
$configuration = Configuration::getConfigurationInstance();
$update = new Update(new System(), $configuration);
$update->setVersion(System::getVersion());

return new StreamedResponse(function () use ($update) {
$progressCallback = function ($progress) {
echo json_encode(['progress' => $progress]) . "\n";
ob_flush();
flush();
};

try {
if ($update->applyUpdates($progressCallback)) {
echo json_encode(['message' => '✅ Database successfully updated.']);
}
} catch (Exception $e) {
echo json_encode(['message' => 'Update database failed: ' . $e->getMessage()]);
}
});
}
}
8 changes: 6 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Database/Mysqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,8 @@ public function nextId(string $table, string $id): int
/**
* This function sends a query to the database.
*
*
* @return mysqli_result $result
* @throws Exception
*/
public function query(string $query, int $offset = 0, int $rowcount = 0): mixed
{
Expand All @@ -305,7 +305,11 @@ public function query(string $query, int $offset = 0, int $rowcount = 0): mixed
$query .= sprintf(' LIMIT %d,%d', $offset, $rowcount);
}

$result = $this->conn->query($query);
try {
$result = $this->conn->query($query);
} catch (mysqli_sql_exception $exception) {
throw new Exception($exception->getMessage());
}

if (false === $result) {
$this->sqllog .= $this->conn->errno . ': ' . $this->error();
Expand Down
15 changes: 11 additions & 4 deletions phpmyfaq/src/phpMyFAQ/Setup/Update.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
namespace phpMyFAQ\Setup;

use phpMyFAQ\Configuration;
use phpMyFAQ\Core\Exception;
use phpMyFAQ\Database;
use phpMyFAQ\Database\DatabaseDriver;
use phpMyFAQ\Enums\ReleaseType;
Expand Down Expand Up @@ -56,7 +57,7 @@ public function isConfigTableAvailable(DatabaseDriver $database): bool
return $database->numRows($result) === 0;
}

public function applyUpdates(callable $progressCallback): void
public function applyUpdates(callable $progressCallback): bool
{
// 3.1 updates
$this->applyUpdates310Alpha();
Expand All @@ -81,6 +82,8 @@ public function applyUpdates(callable $progressCallback): void

// Always the last step: Update version number
$this->updateVersion();

return true;
}

public function optimizeTables(): void
Expand Down Expand Up @@ -114,9 +117,13 @@ private function executeQueries(callable $progressCallback): void
}
} else {
foreach ($this->queries as $query) {
$this->configuration->getDb()->query($query);
if ($progressCallback !== null) {
$progressCallback($query);
try {
$this->configuration->getDb()->query($query);
if ($progressCallback !== null) {
$progressCallback($query);
}
} catch (Exception $exception) {
throw new Exception($exception->getMessage());
}
}
}
Expand Down

0 comments on commit c297cd9

Please sign in to comment.