/** * Create the changelog table in the database. The SQL script used will * vary for MySQL and Postgres, though only slightly to account for InnoDB * use, etc. * * @return bool * @throws Exception */ protected function createTable() { $tempFile = tempnam(sys_get_temp_dir(), 'dewdrop.db.dbdeploy.'); $template = __DIR__ . '/changelog-sql/' . $this->dbType . '/dbdeploy-changelog.sql'; $content = str_replace('{{table_name}}', $this->tableName, file_get_contents($template)); if (!file_exists($tempFile) || !is_writable($tempFile)) { throw new Exception('Could not write to temporary file for dbdeploy changelog.'); } file_put_contents($tempFile, $content, LOCK_EX); $result = $this->cliExec->run($tempFile); // WARNING: Notice the error suppression "@" operator! Used because // failure is also reported by unlink() return value. if (!@unlink($tempFile)) { throw new Exception('Could not delete temporary dbdeploy changelog file.'); } return $result; }
/** * Apply changes to the database. After execution, you can get a count * of the applied changes and an array of the change files applied. * * @return bool */ public function execute() { $this->changesAppliedCount = 0; $this->appliedFilesByChangeset = array(); /** @var $changeset \Dewdrop\Db\Dbdeploy\Changeset */ foreach ($this->changesets as $changeset) { $this->appliedFilesByChangeset[$changeset->getName()] = array(); if (null === $this->changeset || $changeset->getName() === $this->changeset) { foreach ($changeset->getNewFiles() as $changeNumber => $file) { $startTime = date('Y-m-d G:i:s'); $this->cliExec->run($file); $this->changesAppliedCount += 1; $this->appliedFilesByChangeset[$changeset->getName()][] = $file; $this->changelogGateway->logAppliedFile($changeset->getName(), $changeNumber, $file, isset($_SERVER['USER']) ? $_SERVER['USER'] : '******', $startTime, date('Y-m-d G:i:s')); } } } return true; }