From ffbf3b3b820bb6d6ebb9436e223c160c494a4587 Mon Sep 17 00:00:00 2001 From: Brendan Heywood Date: Tue, 17 Nov 2026 16:17:17 +1100 Subject: [PATCH] Don't increment id's when editing reports #41 --- classes/local/query.php | 53 +++++++++++++++++++++++++++++++---------- tests/query_test.php | 47 +++++++++++++++++++++++++++++------- version.php | 4 ++-- 3 files changed, 81 insertions(+), 23 deletions(-) diff --git a/classes/local/query.php b/classes/local/query.php index 62e152b..fcd08cc 100644 --- a/classes/local/query.php +++ b/classes/local/query.php @@ -674,24 +674,33 @@ public static function save(\stdClass $data): int { '' ); } - // SQL change while published: drop view + report so they get rebuilt on next publish. - // A transaction here would be illusory — tear_down() issues DROP VIEW via - // change_database_structure(), and DDL implicitly commits on MySQL. So demote the - // record to draft *first*, then tear down: if the teardown fails partway, the record - // already reads draft rather than claiming published over a destroyed view/report. + // 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) { - $record->status = self::STATUS_DRAFT; - $record->viewname = null; - $record->reportid = null; - $record->chartreportid = null; - $record->columnsmeta = null; - // Old column names no longer apply once the view is rebuilt. $record->useridcolumn = null; $record->coursecolumn = null; $record->pagecoursecolumn = null; $DB->update_record(self::TABLE, $record); self::save_description_files((int) $record->id, $descdraftid, $desctext, $descformat); - self::tear_down((int) $existing->id, $existing); + try { + 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) [ + 'id' => $existing->id, + 'querysql' => $existing->querysql, + 'useridcolumn' => $existing->useridcolumn, + 'coursecolumn' => $existing->coursecolumn, + 'pagecoursecolumn' => $existing->pagecoursecolumn, + ]); + try { + view::create_or_replace((int) $existing->id, $existing->querysql, (int) $existing->courseid); + } catch (\moodle_exception $restore) { + debugging('report_sql: failed to restore view: ' . $restore->getMessage()); + } + throw $e; + } \report_sql\event\query_updated::create_and_trigger($record->id, $record->name); return $record->id; } @@ -874,6 +883,26 @@ public function publish(): void { \report_sql\event\query_published::create_and_trigger($this->id(), $this->name()); } + /** + * Rebuild the view of a published query after its SQL changed, keeping every bound report. + * + * Report columns, filters and conditions which refer to view columns that no longer exist are removed. + */ + private function republish(): void { + $oldcolumns = array_keys($this->columns_meta()); + + // PostgreSQL can't rename or remove columns of an existing view with CREATE OR REPLACE. + view::drop($this->id()); + $this->publish(); + + $newcolumns = array_keys($this->columns_meta()); + foreach (array_diff($oldcolumns, $newcolumns) as $removed) { + foreach (self::bound_report_ids($this->id()) as $reportid) { + self::purge_report_column($reportid, $removed); + } + } + } + /** * Drop view + RB report, set status to draft. */ diff --git a/tests/query_test.php b/tests/query_test.php index 1684a7c..6f9041e 100644 --- a/tests/query_test.php +++ b/tests/query_test.php @@ -290,24 +290,53 @@ public function test_unpublish_reverts_to_draft_and_clears_artefacts(): void { $this->assertNull($record->columnsmeta); } - public function test_sql_edit_while_published_demotes_to_draft(): void { + public function test_sql_edit_while_published_keeps_report(): void { global $DB; $this->resetAfterTest(); $this->setAdminUser(); - $id = query::save($this->formdata()); + $id = query::save($this->formdata(['querysql' => 'SELECT id, username FROM {user}'])); query::get($id)->publish(); $reportid = (int) query::get($id)->reportid(); - // Changing the SQL on a published query tears down view + report and returns to draft. - query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, username FROM {user}'])); + $columns = \core_reportbuilder\local\models\column::get_records(['reportid' => $reportid]); + $this->assertNotEmpty($columns); + + // Changing the SQL on a published query rebuilds the view but keeps the same report. + query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, firstname FROM {user}'])); $record = $DB->get_record(query::TABLE, ['id' => $id], '*', MUST_EXIST); - $this->assertSame(query::STATUS_DRAFT, $record->status); - $this->assertNull($record->viewname); - $this->assertNull($record->reportid); - // The bound report config key is cleaned up by tear_down(). - $this->assertFalse(get_config('report_sql', 'queryid_for_report_' . $reportid)); + $this->assertSame(query::STATUS_PUBLISHED, $record->status); + $this->assertNotNull($record->viewname); + $this->assertEquals($reportid, $record->reportid); + $this->assertEquals($id, get_config('report_sql', 'queryid_for_report_' . $reportid)); + $this->assertEqualsCanonicalizing(['id', 'firstname'], array_keys(query::get($id)->columns_meta())); + + // The column that disappeared is removed from the report, the others are kept. + $identifiers = array_map( + fn($column) => $column->get('uniqueidentifier'), + \core_reportbuilder\local\models\column::get_records(['reportid' => $reportid]) + ); + $this->assertContains('adhoc:id', $identifiers); + $this->assertNotContains('adhoc:username', $identifiers); + } + + public function test_sql_edit_while_published_with_invalid_sql_keeps_old_sql(): void { + global $DB; + $this->resetAfterTest(); + $this->setAdminUser(); + + $id = query::save($this->formdata()); + query::get($id)->publish(); + + try { + query::save($this->formdata(['id' => $id, 'querysql' => 'SELECT id, nosuchcolumn FROM {user}'])); + $this->fail('Expected exception'); + } catch (\Throwable $e) { + $record = $DB->get_record(query::TABLE, ['id' => $id], '*', MUST_EXIST); + $this->assertSame('SELECT id FROM {user}', $record->querysql); + $this->assertSame(query::STATUS_PUBLISHED, $record->status); + } } public function test_duplicate_creates_draft_copy(): void { diff --git a/version.php b/version.php index 2064bb8..731f5f2 100644 --- a/version.php +++ b/version.php @@ -25,8 +25,8 @@ defined('MOODLE_INTERNAL') || die(); $plugin->component = 'report_sql'; -$plugin->release = 2026091012; -$plugin->version = 2026091012; +$plugin->release = 2026091013; +$plugin->version = 2026091013; $plugin->requires = 2024100700; // Moodle 4.5 LTS — the Reportbuilder API this plugin uses is stable from 4.5. $plugin->maturity = MATURITY_STABLE; $plugin->supported = [405, 503];