diff --git a/db/migrations/20220915100902_add_fonts_table_migration.php b/db/migrations/20220915100902_add_fonts_table_migration.php index 7f1daf3dfa..08f323acc5 100644 --- a/db/migrations/20220915100902_add_fonts_table_migration.php +++ b/db/migrations/20220915100902_add_fonts_table_migration.php @@ -70,8 +70,8 @@ public function change() foreach ($this->fetchAll('SELECT mediaId, name, type, createdDt, modifiedDt, storedAs, md5, fileSize, originalFileName FROM `media` WHERE media.type = \'font\'') as $fontMedia) {//phpcs:ignore $table ->insert([ - 'createdAt' => $fontMedia['createdDt'], - 'modifiedAt' => $fontMedia['modifiedDt'], + 'createdAt' => $fontMedia['createdDt'] ?: null, + 'modifiedAt' => $fontMedia['modifiedDt'] ?: null, 'name' => $fontMedia['name'], 'fileName' => $fontMedia['originalFileName'], 'familyName' => strtolower(preg_replace( @@ -91,11 +91,17 @@ public function change() ); // remove any potential widget links (there shouldn't be any) - $this->execute('DELETE FROM `lkwidgetmedia` WHERE `lkwidgetmedia`.`mediaId` = ' . $fontMedia['mediaId']); + $this->execute('DELETE FROM `lkwidgetmedia` WHERE `lkwidgetmedia`.`mediaId` = ' + . $fontMedia['mediaId']); // remove any potential tagLinks from font media files // otherwise we risk failing the migration on the next step when we remove records from media table. - $this->execute('DELETE FROM `lktagmedia` WHERE `lktagmedia`.`mediaId` = ' . $fontMedia['mediaId']); + $this->execute('DELETE FROM `lktagmedia` WHERE `lktagmedia`.`mediaId` = ' + . $fontMedia['mediaId']); + + // font files assigned directly to the Display. + $this->execute('DELETE FROM `lkmediadisplaygroup` WHERE `lkmediadisplaygroup`.mediaId = ' + . $fontMedia['mediaId']); } // delete font records from media table diff --git a/db/migrations/20220928091249_player_software_refactor_migration.php b/db/migrations/20220928091249_player_software_refactor_migration.php index bfdbd25d66..872623ed61 100644 --- a/db/migrations/20220928091249_player_software_refactor_migration.php +++ b/db/migrations/20220928091249_player_software_refactor_migration.php @@ -39,15 +39,19 @@ public function change() ->addColumn('modifiedAt', 'datetime', ['null' => true, 'default' => null]) ->addColumn('modifiedBy', 'string', ['null' => true, 'default' => null]) ->addColumn('fileName', 'string') - ->addColumn('size', 'integer', ['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_BIG, 'default' => null, 'null' => true]) + ->addColumn( + 'size', + 'integer', + ['limit' => \Phinx\Db\Adapter\MysqlAdapter::INT_BIG, 'default' => null, 'null' => true] + ) ->addColumn('md5', 'string', ['limit' => 32, 'default' => null, 'null' => true]) ->save(); // create playersoftware sub-folder in the library location $libraryLocation = $this->fetchRow(' - SELECT `setting`.value + SELECT `setting`.`value` FROM `setting` - WHERE `setting`.setting = \'LIBRARY_LOCATION\'')[0] ?? null; + WHERE `setting`.`setting` = \'LIBRARY_LOCATION\'')[0] ?? null; // New installs won't have a library location yet (if they are non-docker). if (!empty($libraryLocation)) { @@ -56,23 +60,58 @@ public function change() } // get all existing playersoftware records in media table and convert them - foreach ($this->fetchAll('SELECT mediaId, name, type, createdDt, modifiedDt, storedAs, md5, fileSize, originalFileName FROM `media` WHERE media.type = \'playersoftware\'') as $playersoftwareMedia) { - $this->execute('UPDATE `player_software` SET createdAt = \'' . $playersoftwareMedia['createdDt'] . '\', - modifiedAt = \'' . $playersoftwareMedia['modifiedDt'] . '\', - fileName = \'' . $playersoftwareMedia['originalFileName'] . '\', - size = ' . $playersoftwareMedia['fileSize'] . ', - md5 = \'' . $playersoftwareMedia['md5'] . '\' - WHERE `player_software`.mediaId = ' . $playersoftwareMedia['mediaId']); + $sql = ' + SELECT `mediaId`, + `name`, + `type`, + `createdDt`, + `modifiedDt`, + `storedAs`, + `md5`, + `fileSize`, + `originalFileName` + FROM `media` + WHERE `media`.`type` = \'playersoftware\' + '; + + $updateSql = ' + UPDATE `player_software` + SET `createdAt` = :createdAt, + `modifiedAt` = :modifiedAt, + `fileName` = :fileName, + `size` = :size, + `md5` = :md5 + WHERE `mediaId` = :mediaId + '; + + foreach ($this->fetchAll($sql) as $playersoftwareMedia) { + $this->execute($updateSql, [ + 'mediaId' => $playersoftwareMedia['mediaId'], + 'createdAt' => $playersoftwareMedia['createdDt'] ?: null, + 'modifiedAt' => $playersoftwareMedia['modifiedDt'] ?: null, + 'fileName' => $playersoftwareMedia['originalFileName'], + 'size' => $playersoftwareMedia['fileSize'], + 'md5' => $playersoftwareMedia['md5'] + ]); // move the stored files with new id to fonts folder - rename($libraryLocation . $playersoftwareMedia['storedAs'], $libraryLocation . 'playersoftware/' . $playersoftwareMedia['originalFileName']); + rename( + $libraryLocation . $playersoftwareMedia['storedAs'], + $libraryLocation . 'playersoftware/' . $playersoftwareMedia['originalFileName'] + ); // remove any potential widget links (there shouldn't be any) - $this->execute('DELETE FROM `lkwidgetmedia` WHERE `lkwidgetmedia`.`mediaId` = ' . $playersoftwareMedia['mediaId']); + $this->execute('DELETE FROM `lkwidgetmedia` WHERE `lkwidgetmedia`.`mediaId` = ' + . $playersoftwareMedia['mediaId']); // remove any potential tagLinks from playersoftware media files // unlikely that there will be any, but just in case. - $this->execute('DELETE FROM `lktagmedia` WHERE `lktagmedia`.mediaId = ' . $playersoftwareMedia['mediaId']); + $this->execute('DELETE FROM `lktagmedia` WHERE `lktagmedia`.mediaId = ' + . $playersoftwareMedia['mediaId']); + + // player software files assigned directly to the Display. + $this->execute('DELETE FROM `lkmediadisplaygroup` WHERE `lkmediadisplaygroup`.mediaId = ' + . $playersoftwareMedia['mediaId']); } // update versionMediaId in displayProfiles config @@ -81,10 +120,20 @@ public function change() if (!empty($displayProfile['config']) && $displayProfile['config'] !== '[]') { $config = json_decode($displayProfile['config'], true); for ($i = 0; $i < count($config); $i++) { - if ($config[$i]['name'] === 'versionMediaId') { - $row = $this->fetchRow('SELECT mediaId, versionId FROM `player_software` WHERE `player_software`.mediaId =' . $config[$i]['value']); + $configValue = $config[$i]['value'] ?? 0; + + if (!empty($configValue) && $config[$i]['name'] === 'versionMediaId') { + $row = $this->fetchRow( + 'SELECT mediaId, versionId + FROM `player_software` + WHERE `player_software`.mediaId =' . $configValue + ); $config[$i]['value'] = $row['versionId']; - $this->execute('UPDATE `displayprofile` SET config = \'' . json_encode($config) . '\' WHERE `displayprofile`.displayProfileId =' . $displayProfile['displayProfileId']); + $this->execute( + 'UPDATE `displayprofile` + SET config = \'' . json_encode($config) . '\' + WHERE `displayprofile`.displayProfileId =' . $displayProfile['displayProfileId'] + ); } } } @@ -96,10 +145,19 @@ public function change() if (!empty($display['overrideConfig']) && $display['overrideConfig'] !== '[]') { $overrideConfig = json_decode($display['overrideConfig'], true); for ($i = 0; $i < count($overrideConfig); $i++) { - if ($overrideConfig[$i]['name'] === 'versionMediaId') { - $row = $this->fetchRow('SELECT mediaId, versionId FROM `player_software` WHERE `player_software`.mediaId =' . $overrideConfig[$i]['value']); + $overrideConfigValue = $overrideConfig[$i]['value'] ?? 0; + if (!empty($overrideConfigValue) && $overrideConfig[$i]['name'] === 'versionMediaId') { + $row = $this->fetchRow( + 'SELECT mediaId, versionId + FROM `player_software` + WHERE `player_software`.mediaId =' . $overrideConfigValue + ); $overrideConfig[$i]['value'] = $row['versionId']; - $this->execute('UPDATE `display` SET overrideConfig = \'' . json_encode($overrideConfig) . '\' WHERE `display`.displayId =' . $display['displayId']); + $this->execute( + 'UPDATE `display` + SET overrideConfig = \'' . json_encode($overrideConfig) . '\' + WHERE `display`.displayId =' . $display['displayId'] + ); } } }