Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 41 additions & 12 deletions classes/local/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Comment on lines +686 to +687
} 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;
}
Expand Down Expand Up @@ -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) {
Comment on lines +898 to +899
foreach (self::bound_report_ids($this->id()) as $reportid) {
self::purge_report_column($reportid, $removed);
}
}
}

/**
* Drop view + RB report, set status to draft.
*/
Expand Down
47 changes: 38 additions & 9 deletions tests/query_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions version.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Loading