rewriteBodies(self::SINGLE_EMPHASIS, '**$1**'); } public function down(Schema $schema): void { $this->rewriteBodies(self::DOUBLE_EMPHASIS, '*$1*'); } /** * The rewrite happens in PHP, one row at a time: MySQL's REGEXP_REPLACE has no * lookbehind, and getting this wrong would quietly mangle wording that goes out to * every teamer. Only the read is done here and now - the writes go through addSql, so * they are logged and rolled back like any other migration statement. */ private function rewriteBodies(string $pattern, string $replacement): void { foreach ($this->connection->fetchAllAssociative('SELECT id, body FROM email_text') as $row) { $body = preg_replace($pattern, $replacement, (string) $row['body']); if (null === $body || $body === $row['body']) { continue; } $this->addSql('UPDATE email_text SET body = ? WHERE id = ?', [$body, $row['id']]); } } }