Skip to content

Commit

Permalink
Use addSql() instead of executeStatement() in executable hash migration
Browse files Browse the repository at this point in the history
executeStatement() doesn't work well in migrations; for example it
doesn't respect the --dry-run option.
  • Loading branch information
tom93 committed Jul 28, 2024
1 parent f62d0ec commit c9bb5ef
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions webapp/migrations/Version20230508163415.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,25 @@ public function getDescription(): string

public function up(Schema $schema): void
{
$executableFiles = $this->connection->fetchAllAssociative('SELECT execfileid, file_content FROM executable_file');
foreach ($executableFiles as $file) {
$this->connection->executeStatement('UPDATE executable_file SET hash = :hash WHERE execfileid = :id', [
'hash' => md5($file['file_content']),
'id' => $file['execfileid']
]);
$executableFiles = $this->connection->fetchAllAssociativeIndexed('SELECT execfileid, file_content FROM executable_file');
foreach ($executableFiles as $execfileid => $file) {
$newHash = md5($file['file_content']);
$executableFiles[$execfileid]['hash'] = $newHash;
$this->addSql('UPDATE executable_file SET hash = :hash WHERE execfileid = :id', ['hash' => $newHash, 'id' => $execfileid]);
}
$immutableExecutables = $this->connection->fetchAllAssociative('SELECT immutable_execid FROM immutable_executable');
foreach ($immutableExecutables as $immutableExecutable) {
$files = $this->connection->fetchAllAssociative('SELECT hash, filename, is_executable FROM executable_file WHERE immutable_execid = :id', ['id' => $immutableExecutable['immutable_execid']]);
$files = $this->connection->fetchAllAssociative('SELECT execfileid, filename, is_executable FROM executable_file WHERE immutable_execid = :id', ['id' => $immutableExecutable['immutable_execid']]);
uasort($files, fn(array $a, array $b) => strcmp($a['filename'], $b['filename']));
$newHash = md5(
join(
array_map(
fn(array $file) => $file['hash'] . $file['filename'] . (bool)$file['is_executable'],
fn(array $file) => $executableFiles[$file['execfileid']]['hash'] . $file['filename'] . (bool)$file['is_executable'],
$files
)
)
);
$this->connection->executeStatement('UPDATE immutable_executable SET hash = :hash WHERE immutable_execid = :id', [
$this->addSql('UPDATE immutable_executable SET hash = :hash WHERE immutable_execid = :id', [
'hash' => $newHash,
'id' => $immutableExecutable['immutable_execid'],
]);
Expand Down

0 comments on commit c9bb5ef

Please sign in to comment.