From b90c97b1f096008b5fa9abf8c50a120a85c47a4e Mon Sep 17 00:00:00 2001 From: Simon Rettberg Date: Thu, 30 Nov 2017 18:30:01 +0100 Subject: [roomplanner] Refactor to use runmode --- modules-available/roomplanner/install.inc.php | 43 ++++++++++++++++++--------- 1 file changed, 29 insertions(+), 14 deletions(-) (limited to 'modules-available/roomplanner/install.inc.php') diff --git a/modules-available/roomplanner/install.inc.php b/modules-available/roomplanner/install.inc.php index a6d98384..13365fe1 100644 --- a/modules-available/roomplanner/install.inc.php +++ b/modules-available/roomplanner/install.inc.php @@ -6,7 +6,6 @@ $res = array(); $res[] = tableCreate('location_roomplan', " `locationid` INT(11) NOT NULL, `managerip` varchar(45) CHARACTER SET ascii DEFAULT '', - `dedicatedmgr` tinyint(1) NOT NULL DEFAULT 0, `tutoruuid` char(36) CHARACTER SET ascii DEFAULT NULL, `roomplan` BLOB DEFAULT NULL, PRIMARY KEY (`locationid`), @@ -29,24 +28,40 @@ if (!tableHasColumn('location_roomplan', 'tutoruuid')) { } $res[] = UPDATE_DONE; } -if (!tableHasColumn('location_roomplan', 'dedicatedmgr')) { - $ret = Database::exec("ALTER TABLE `location_roomplan` ADD `dedicatedmgr` tinyint(1) NOT NULL DEFAULT 0 AFTER `managerip`") !== false; - if ($ret === false) { - finalResponse(UPDATE_FAILED, 'Adding dedicatedmgr to location_roomplan failed: ' . Database::lastError()); - } - $res[] = UPDATE_DONE; -} if (in_array(UPDATE_DONE, $res)) { Database::exec("ALTER TABLE `location_roomplan` - ADD CONSTRAINT `location_roomplan_ibfk_1` FOREIGN KEY (`locationid`) REFERENCES `location` (`locationid`) ON DELETE CASCADE"); + ADD CONSTRAINT `location_roomplan_ibfk_1` FOREIGN KEY (`locationid`) REFERENCES `location` (`locationid`) ON DELETE CASCADE"); Database::exec("ALTER TABLE `location_roomplan` - ADD CONSTRAINT `location_roomplan_ibfk_2` FOREIGN KEY (`tutoruuid`) REFERENCES `machine` (`machineuuid`) ON DELETE SET NULL ON UPDATE CASCADE"); + ADD CONSTRAINT `location_roomplan_ibfk_2` FOREIGN KEY (`tutoruuid`) REFERENCES `machine` (`machineuuid`) ON DELETE SET NULL ON UPDATE CASCADE"); } -if (in_array(UPDATE_DONE, $res)) { - finalResponse(UPDATE_DONE, 'Table created successfully'); +// 2017-11-30: Refactor to runmode +// managerip, dedicatedmgr, --> runmode +if (tableHasColumn('location_roomplan', 'dedicatedmgr')) { + if (!tableExists('runmode') || !tableExists('machine')) { + $res[] = UPDATE_RETRY; + } else { + $ret = Database::simpleQuery('SELECT lr.locationid, lr.managerip, lr.dedicatedmgr, m.machineuuid + FROM location_roomplan lr INNER JOIN machine m ON (m.clientip = lr.managerip)'); + if ($ret === false) { + $res[] = UPDATE_FAILED; + } else { + while ($row = $ret->fetch(PDO::FETCH_ASSOC)) { + $dedi = $row['dedicatedmgr'] != 0; + $data = json_encode(array('dedicatedmgr' => $dedi)); + Database::exec("INSERT IGNORE INTO runmode (machineuuid, module, modeid, modedata, isclient) + VALUES (:machineuuid, 'roomplanner', :locationid, :modedata, :isclient)", array( + 'machineuuid' => $row['machineuuid'], + 'locationid' => $row['locationid'], + 'modedata' => $data, + 'isclient' => ($dedi ? 0 : 1) + )); + } + Database::exec('ALTER TABLE location_roomplan DROP COLUMN dedicatedmgr'); + $res[] = UPDATE_DONE; + } + } } -finalResponse(UPDATE_NOOP, 'Everything already up to date'); - +responseFromArray($res); -- cgit v1.2.3-55-g7522