Skip to content

Don't increment id's when editing reports #41 - #44

Open
brendanheywood wants to merge 1 commit into
MOODLE_405_STABLEfrom
renames
Open

brendanheywood wants to merge 1 commit into
MOODLE_405_STABLEfrom
renames

Conversation

@brendanheywood

Copy link
Copy Markdown
Contributor

No description provided.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

SQL edits can bypass approval, remove row-level restrictions, and leave report audiences or rollback state inconsistent, creating critical data-exposure risks.

6 open issues
What changed in this PR

This PR changes published SQL-query editing to preserve existing Moodle Report Builder report IDs, keeping report links and embeds stable.

Changes:

  • Rebuilds the query view while retaining bound reports and removing references to deleted columns.
  • Adds recovery logic and tests for successful edits and invalid SQL.
  • Increments the plugin release and version.
File Description
version.php Bumps release and version to 2026091013.
tests/​query_test.php Tests report preservation, column cleanup and invalid-SQL recovery.
classes/​local/​query.php Adds automatic republishing and recovery while preserving report IDs.

Review effort: Balanced

Comment thread classes/local/query.php
// SQL change while published: rebuild the view in place and keep the report (and its id, audiences,
// filters and blocks) so that existing links and embeds keep working. Columns that no longer exist
// are removed from the report by republish().
if ($existing->status === self::STATUS_PUBLISHED && $existing->querysql !== $sql) {
Comment thread classes/local/query.php
Comment on lines +686 to +687
try {
self::get((int) $record->id)->republish();
Comment thread classes/local/query.php
self::get((int) $record->id)->republish();
} catch (\Throwable $e) {
// The new SQL can't be compiled, so restore the old SQL and view.
$DB->update_record(self::TABLE, (object) [
Comment thread classes/local/query.php
'pagecoursecolumn' => $existing->pagecoursecolumn,
]);
try {
view::create_or_replace((int) $existing->id, $existing->querysql, (int) $existing->courseid);
Comment thread classes/local/query.php

// PostgreSQL can't rename or remove columns of an existing view with CREATE OR REPLACE.
view::drop($this->id());
$this->publish();
Comment thread classes/local/query.php
Comment on lines +898 to +899
$newcolumns = array_keys($this->columns_meta());
foreach (array_diff($oldcolumns, $newcolumns) as $removed) {
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants