Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Check for concurrent edits of showplans #1191

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
9 changes: 9 additions & 0 deletions schema/patches/19.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
BEGIN;
ALTER TABLE schedule.show_season_timeslot
ADD COLUMN showplan_last_modified TIMESTAMPTZ DEFAULT NULL;

UPDATE myradio.schema
SET value = 14
Copy link
Member Author

Choose a reason for hiding this comment

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

Suggested change
SET value = 14
SET value = 19

wat

WHERE attr='version';

COMMIT;
42 changes: 40 additions & 2 deletions src/Classes/ServiceAPI/MyRadio_Timeslot.php
Original file line number Diff line number Diff line change
Expand Up @@ -1063,13 +1063,27 @@ public function moveTimeslot($newStart, $newEnd)
* This is the server-side implementation of the JSONON system for tracking Show Planner alterations.
*
* @param array[] $set A JSONON operation set
* @param int|null $last_modified Timestamp of the last time the client knows the show plan was modified
*/
public function updateShowPlan($set)
public function updateShowPlan($set, $last_modified = null)
{
$result = [];
//Being a Database Transaction - this all succeeds, or none of it does
self::$db->query('BEGIN');

// First, check when it was last modified - if it's been modified since
// the client last checked, reject the request as their view of the
// state of the showplan is out of date.
$last_mod_row = self::$db->fetchColumn(
'SELECT showplan_last_modified FROM schedule.show_season_timeslot WHERE show_season_timeslot_id=$1 FOR UPDATE',
[$this->getID()]
);
if (!empty($last_mod_row) && $last_modified !== null && CoreUtils::getTimestamp($last_mod_row[0]) !== $last_modified) {
self::$db->query('ROLLBACK');

return ['status' => 'OUTDATED'];
}

foreach ($set as $op) {
switch ($op['op']) {
case 'AddItem':
Expand Down Expand Up @@ -1140,11 +1154,24 @@ public function updateShowPlan($set)
}
}

$last_mod_res = self::$db->fetchColumn(
'UPDATE schedule.show_season_timeslot SET showplan_last_modified=NOW() WHERE show_season_timeslot_id=$1 RETURNING showplan_last_modified',
[$this->getID()]
);
self::$db->query('COMMIT');

//Update the legacy baps show plans database
$this->updateLegacyShowPlan();

if ($last_modified !== null) {
// We know the client will be able to understand this format
return [
'status' => $result[count($result)-1]['status'] ? 'OK' : 'ERROR',
'result' => $result,
'last_modified' => CoreUtils::getTimestamp($last_mod_res[0])
];
}

return $result;
}

Expand All @@ -1156,7 +1183,7 @@ private function updateLegacyShowPlan()
/**
* Returns the tracks etc. and their associated channels as planned for this show. Mainly used by NIPSWeb.
*/
public function getShowPlan()
public function getShowPlan(bool $include_last_modified = false)
{
// Check we can access it, if not, require permission
if (!($this->isCurrentUserAnOwner())) {
Expand All @@ -1183,6 +1210,17 @@ public function getShowPlan()
NIPSWeb_TimeslotItem::getInstance($track['timeslot_item_id'])->toDataSource();
}

if ($include_last_modified) {
$last_modified = self::$db->fetchColumn(
'SELECT showplan_last_modified FROM schedule.show_season_timeslot WHERE show_season_timeslot_id=$1',
[$this->getID()]
);
return [
'plan' => $tracks,
'last_modified' => empty($last_modified) ? null : CoreUtils::getTimestamp($last_modified[0])
];
}

return $tracks;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Controllers/root.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* This number is incremented every time a database patch is released.
* Patches are scripts in schema/patches.
*/
define('MYRADIO_CURRENT_SCHEMA_VERSION', 18);
define('MYRADIO_CURRENT_SCHEMA_VERSION', 19);

/*
* Turn on Error Reporting for the start. Once the Config object is loaded
Expand Down