Permalink
Browse files

Fixed migrations for MySQL. Issue #249.

  • Loading branch information...
corpsee committed Jan 8, 2019
1 parent 6d14d76 commit d2c46d41b7df649e544d72cc09ae496a1ac335fc
@@ -8,8 +8,12 @@ public function up()
{
$this
->table('user')
->removeIndex(['email'])
->save();
$this
->table('user')
->addIndex(['email'], ['unique' => true])
->addIndex(['name'], ['unique' => true])
@@ -23,8 +27,12 @@ public function down()
->removeIndex(['email'])
->removeIndex(['name'])
->addIndex(['email'])
->save();
$this
->table('user')
->addIndex(['email'])
->save();
}
}
@@ -28,6 +28,10 @@ public function down()
$this
->table('project')
->dropForeignKey('group_id')
->save();
$this
->table('project')
->removeColumn('group_id')
->save();
@@ -8,21 +8,25 @@ public function up()
{
$this
->table('user')
->removeIndex(['name'])
->addIndex(['name'])
->save();
$this
->table('user')
->addIndex(['name'])
->save();
}
public function down()
{
$this
->table('user')
->removeIndex(['name'])
->addIndex(['name'], ['unique' => true])
->save();
$this
->table('user')
->addIndex(['name'], ['unique' => true])
->save();
}
}
@@ -34,10 +34,12 @@ public function down()
{
$this
->table('build_error')
->dropForeignKey('build_id')
->drop()
->save();
$this
->table('build_error')
->drop()
->save();
}
}
@@ -1,14 +1,187 @@
<?php
use Phinx\Migration\AbstractMigration;
use PHPCensor\Model\BuildMeta;
use PHPCensor\Model\BuildError;
use PHPCensor\Store\Factory;
class ConvertErrors extends AbstractMigration
{
/**
* @var \PHPCensor\Store\BuildMetaStore
*/
protected $metaStore;
/**
* @var \PHPCensor\Store\BuildErrorStore
*/
protected $errorStore;
public function up()
{
$count = 100;
$this->metaStore = Factory::getStore('BuildMeta');
$this->errorStore = Factory::getStore('BuildError');
while ($count >= 100) {
$data = $this->metaStore->getErrorsForUpgrade(100);
$count = count($data);
/** @var \PHPCensor\Model\BuildMeta $meta */
foreach ($data as $meta) {
switch ($meta->getMetaKey()) {
case 'phpmd-data':
$this->processPhpMdMeta($meta);
break;
case 'phpcs-data':
$this->processPhpCsMeta($meta);
break;
case 'phpdoccheck-data':
$this->processPhpDocCheckMeta($meta);
break;
case 'phpcpd-data':
$this->processPhpCpdMeta($meta);
break;
case 'technical_debt-data':
$this->processTechnicalDebtMeta($meta);
break;
}
$this->metaStore->delete($meta);
}
}
}
protected function processPhpMdMeta(BuildMeta $meta)
{
$data = json_decode($meta->getMetaValue(), true);
if (is_array($data) && count($data)) {
foreach ($data as $error) {
$buildError = new BuildError();
$buildError->setBuildId($meta->getBuildId());
$buildError->setPlugin('php_mess_detector');
$buildError->setCreateDate(new \DateTime());
$buildError->setFile($error['file']);
$buildError->setLineStart($error['line_start']);
$buildError->setLineEnd($error['line_end']);
$buildError->setSeverity(BuildError::SEVERITY_HIGH);
$buildError->setMessage($error['message']);
$this->errorStore->save($buildError);
}
}
}
protected function processPhpCsMeta(BuildMeta $meta)
{
$data = json_decode($meta->getMetaValue(), true);
if (is_array($data) && count($data)) {
foreach ($data as $error) {
$buildError = new BuildError();
$buildError->setBuildId($meta->getBuildId());
$buildError->setPlugin('php_code_sniffer');
$buildError->setCreateDate(new \DateTime());
$buildError->setFile($error['file']);
$buildError->setLineStart($error['line']);
$buildError->setLineEnd($error['line']);
$buildError->setMessage($error['message']);
switch ($error['type']) {
case 'ERROR':
$buildError->setSeverity(BuildError::SEVERITY_HIGH);
break;
case 'WARNING':
$buildError->setSeverity(BuildError::SEVERITY_LOW);
break;
}
$this->errorStore->save($buildError);
}
}
}
protected function processPhpDocCheckMeta(BuildMeta $meta)
{
$data = json_decode($meta->getMetaValue(), true);
if (is_array($data) && count($data)) {
foreach ($data as $error) {
$buildError = new BuildError();
$buildError->setBuildId($meta->getBuildId());
$buildError->setPlugin('php_docblock_checker');
$buildError->setCreateDate(new \DateTime());
$buildError->setFile($error['file']);
$buildError->setLineStart($error['line']);
$buildError->setLineEnd($error['line']);
switch ($error['type']) {
case 'method':
$buildError->setMessage($error['class'] . '::' . $error['method'] . ' is missing a docblock.');
$buildError->setSeverity(BuildError::SEVERITY_NORMAL);
break;
case 'class':
$buildError->setMessage('Class ' . $error['class'] . ' is missing a docblock.');
$buildError->setSeverity(BuildError::SEVERITY_LOW);
break;
}
$this->errorStore->save($buildError);
}
}
}
protected function processPhpCpdMeta(BuildMeta $meta)
{
$data = json_decode($meta->getMetaValue(), true);
if (is_array($data) && count($data)) {
foreach ($data as $error) {
$buildError = new BuildError();
$buildError->setBuildId($meta->getBuildId());
$buildError->setPlugin('php_cpd');
$buildError->setCreateDate(new \DateTime());
$buildError->setFile($error['file']);
$buildError->setLineStart($error['line_start']);
$buildError->setLineEnd($error['line_end']);
$buildError->setSeverity(BuildError::SEVERITY_NORMAL);
$buildError->setMessage('Copy and paste detected.');
$this->errorStore->save($buildError);
}
}
}
protected function processTechnicalDebtMeta(BuildMeta $meta)
{
$data = json_decode($meta->getMetaValue(), true);
if (is_array($data) && count($data)) {
foreach ($data as $error) {
$buildError = new BuildError();
$buildError->setBuildId($meta->getBuildId());
$buildError->setPlugin('technical_debt');
$buildError->setCreateDate(new \DateTime());
$buildError->setFile($error['file']);
$buildError->setLineStart($error['line']);
$buildError->setSeverity(BuildError::SEVERITY_NORMAL);
$buildError->setMessage($error['message']);
$this->errorStore->save($buildError);
}
}
}
public function down()
{
return;
}
}
}
@@ -8,10 +8,12 @@ public function up()
{
$this
->table('build_meta')
->dropForeignKey('project_id')
->removeColumn('project_id')
->save();
$this
->table('build_meta')
->removeColumn('project_id')
->save();
}
@@ -1,14 +1,56 @@
<?php
use Phinx\Migration\AbstractMigration;
use PHPCensor\Store\Factory;
use PHPCensor\Store\BuildStore;
use PHPCensor\Model\Build;
class AddedRequestBranchToBuild extends AbstractMigration
{
public function up()
{
/** @var BuildStore $buildStore */
$buildStore = Factory::getStore('Build');
$count = 100;
$offset = 0;
while ($count >= 100) {
$builds = $buildStore->getBuilds(100, $offset);
$offset += 100;
$count = count($builds);
/** @var Build $build */
foreach ($builds as &$build) {
$extra = $build->getExtra();
if (isset($extra['build_type'])) {
unset($extra['build_type']);
$build->setSource(Build::SOURCE_WEBHOOK_PULL_REQUEST_CREATED);
}
if (!empty($extra['remote_url'])) {
preg_match(
'/[\/:]([a-zA-Z0-9_\-]+\/[a-zA-Z0-9_\-]+)/',
$extra['remote_url'],
$matches
);
$remoteReference = $matches[1];
if ($remoteReference && empty($extra['remote_reference'])) {
$extra['remote_reference'] = $remoteReference;
}
}
unset($extra['build_type']);
unset($extra['pull_request_id']);
$build->setExtra($extra);
$buildStore->save($build);
}
unset($build);
}
}
public function down()
{
}
}
}
@@ -18,7 +18,7 @@ public function up()
public function down()
{
$this
->table('project')
->table('build')
->removeColumn('errors_total')
->removeColumn('errors_total_previous')
->removeColumn('errors_new')
@@ -135,4 +135,35 @@ public function getByBuildId($buildId, $limit = 1000, $useConnection = 'read')
return ['items' => [], 'count' => 0];
}
}
/**
* Only used by an upgrade migration to move errors from build_meta to build_error
*
* @param integer $limit
*
* @return array
*/
public function getErrorsForUpgrade($limit)
{
$query = 'SELECT * FROM {{' . $this->tableName . '}}
WHERE {{meta_key}} IN (\'phpmd-data\', \'phpcs-data\', \'phpdoccheck-data\', \'technical_debt-data\')
ORDER BY {{id}} ASC LIMIT :limit';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':limit', $limit, \PDO::PARAM_INT);
if ($stmt->execute()) {
$res = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$map = function ($item) {
return new BuildMeta($item);
};
$rtn = array_map($map, $res);
return $rtn;
} else {
return [];
}
}
}

0 comments on commit d2c46d4

Please sign in to comment.