Skip to content

Commit

Permalink
Add a way to view the database schema and rows
Browse files Browse the repository at this point in the history
  • Loading branch information
austinwbest committed Dec 20, 2024
1 parent a0b0496 commit c30bf23
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
90 changes: 90 additions & 0 deletions root/app/www/public/ajax/database.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
<?php

/*
----------------------------------
------ Created: 121824 ------
------ Austin Best ------
----------------------------------
*/

require 'shared.php';

if ($_POST['m'] == 'init') {
$defines = get_defined_constants();
$tables = [];

foreach ($defines as $define => $value) {
if (str_contains($define, '_TABLE')) {
$tables[] = $value;
}
}
sort($tables);

foreach ($tables as $table) {
$q = "SELECT sql
FROM sqlite_schema
WHERE name = '" . $table . "'";
$r = $database->query($q);
$schema = $database->fetchAssoc($r);

$rows = [];
$q = "SELECT *
FROM '" . $table . "'";
$r = $database->query($q);
while ($row = $database->fetchAssoc($r)) {
$rows[] = $row;
}
?>
<div class="rounded bg-secondary mb-3">
<div class="h5 p-2 text-primary"><?= $table ?></div><hr>
<table class="table table-hover">
<tr>
<td style="width:10%;">Schema</td>
<td><pre><?= str_replace(' ', ' ', str_replace(' )', ')', $schema['sql'])) ?></pre></td>
</tr>
<tr>
<td>Data</td>
<td>
<code><?= $q ?></code><br>
<?php
if ($rows) {
$fields = [];
foreach ($rows[0] as $field => $data) {
$fields[] = $field;
}

?>
<table class="table table-bordered table-hover">
<thead>
<tr>
<?php
foreach ($fields as $field) {
?>
<td><?= $field ?></td>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($rows as $row) {
?><tr><?php
foreach ($fields as $field) {
?><td><?= $row[$field] ?></td><?php
}
?></tr><?php
}
?>
</tbody>
</table>
<?php } else { ?>
Table is currently empty
<?php } ?>
</td>
</tr>
</table>
</div>
<?php
}
}
5 changes: 5 additions & 0 deletions root/app/www/public/ajax/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,11 @@
<td><input class="form-check-input" type="checkbox" id="globalSetting-telemetry" <?= $settingsTable['telemetry'] ? 'checked' : '' ?>></td>
<td>Allow telemetry information to be collected. There is nothing personal or identifiable and what is sent can be seen in the Tasks menu or <a href="https://github.com/Notifiarr/dockwatch/blob/develop/root/app/www/public/functions/telemetry.php" target="_blank">here on github</a></td>
</tr>
<tr>
<th scope="row">Database</th>
<td><button class="btn btn-sm btn-info" onclick="initPage('database')">Browse</button></td>
<td>Browse the <?= APP_NAME ?> database</td>
</tr>
</tbody>
</table>
</div>
Expand Down
1 change: 1 addition & 0 deletions root/app/www/public/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
<div id="content-logs" style="display: none;"></div>
<div id="content-tasks" style="display: none;"></div>
<div id="content-commands" style="display: none;"></div>
<div id="content-database" style="display: none;"></div>
<div id="content-dockerPermissions" style="display: <?= $dockerPerms ? 'none' : 'block' ?>;">
If you are seeing this, it means the user:group running this container does not have permission to run docker commands. Please fix that, restart the container and try again.<br><br>
<div class="bg-secondary rounded p-4">
Expand Down

0 comments on commit c30bf23

Please sign in to comment.