Removed useless 'project_id' column from 'build_meta' table.

This commit is contained in:
Dmitry Khomutov 2017-10-15 00:48:53 +07:00
parent 145670acde
commit 9e57de043e
7 changed files with 36 additions and 160 deletions

View file

@ -0,0 +1,28 @@
<?php
use Phinx\Migration\AbstractMigration;
class RemovedProjectIdFromBuildMeta extends AbstractMigration
{
public function up()
{
$table = $this->table('build_meta');
if ($table->hasColumn('project_id')) {
$table
->removeColumn('project_id')
->save();
}
}
public function down()
{
$table = $this->table('build_meta');
if (!$table->hasColumn('project_id')) {
$table
->addColumn('project_id', 'integer', ['default' => 0])
->save();
}
}
}

View file

@ -775,7 +775,7 @@ class Build extends Model
public function storeMeta($key, $value)
{
$value = json_encode($value);
Factory::getStore('Build')->setMeta($this->getProjectId(), $this->getId(), $key, $value);
Factory::getStore('Build')->setMeta($this->getId(), $key, $value);
}
/**

View file

@ -27,7 +27,6 @@ class BuildMeta extends Model
*/
protected $data = [
'id' => null,
'project_id' => null,
'build_id' => null,
'meta_key' => null,
'meta_value' => null,
@ -39,12 +38,10 @@ class BuildMeta extends Model
protected $getters = [
// Direct property getters:
'id' => 'getId',
'project_id' => 'getProjectId',
'build_id' => 'getBuildId',
'meta_key' => 'getMetaKey',
'meta_value' => 'getMetaValue',
// Foreign key getters:
'Project' => 'getProject',
'Build' => 'getBuild',
];
@ -54,12 +51,10 @@ class BuildMeta extends Model
protected $setters = [
// Direct property setters:
'id' => 'setId',
'project_id' => 'setProjectId',
'build_id' => 'setBuildId',
'meta_key' => 'setMetaKey',
'meta_value' => 'setMetaValue',
// Foreign key setters:
'Project' => 'setProject',
'Build' => 'setBuild',
];
@ -74,11 +69,6 @@ class BuildMeta extends Model
'auto_increment' => true,
'default' => null,
],
'project_id' => [
'type' => 'int',
'length' => 11,
'default' => null,
],
'build_id' => [
'type' => 'int',
'length' => 11,
@ -101,20 +91,12 @@ class BuildMeta extends Model
public $indexes = [
'PRIMARY' => ['unique' => true, 'columns' => 'id'],
'idx_meta_id' => ['unique' => true, 'columns' => 'build_id, meta_key'],
'project_id' => ['columns' => 'project_id'],
];
/**
* @var array
*/
public $foreignKeys = [
'build_meta_ibfk_1' => [
'local_col' => 'project_id',
'update' => 'CASCADE',
'delete' => 'CASCADE',
'table' => 'project',
'col' => 'id'
],
'fk_meta_build_id' => [
'local_col' => 'build_id',
'update' => 'CASCADE',
@ -136,18 +118,6 @@ class BuildMeta extends Model
return $rtn;
}
/**
* Get the value of ProjectId / project_id.
*
* @return int
*/
public function getProjectId()
{
$rtn = $this->data['project_id'];
return $rtn;
}
/**
* Get the value of BuildId / build_id.
*
@ -204,26 +174,6 @@ class BuildMeta extends Model
$this->setModified('id');
}
/**
* Set the value of ProjectId / project_id.
*
* Must not be null.
* @param $value int
*/
public function setProjectId($value)
{
$this->validateNotNull('ProjectId', $value);
$this->validateInt('ProjectId', $value);
if ($this->data['project_id'] === $value) {
return;
}
$this->data['project_id'] = $value;
$this->setModified('project_id');
}
/**
* Set the value of BuildId / build_id.
*
@ -284,63 +234,6 @@ class BuildMeta extends Model
$this->setModified('meta_value');
}
/**
* Get the Project model for this BuildMeta by Id.
*
* @uses \PHPCensor\Store\ProjectStore::getById()
* @uses \PHPCensor\Model\Project
* @return \PHPCensor\Model\Project
*/
public function getProject()
{
$key = $this->getProjectId();
if (empty($key)) {
return null;
}
$cacheKey = 'Cache.Project.' . $key;
$rtn = $this->cache->get($cacheKey, null);
if (empty($rtn)) {
$rtn = Factory::getStore('Project', 'PHPCensor')->getById($key);
$this->cache->set($cacheKey, $rtn);
}
return $rtn;
}
/**
* Set Project - Accepts an ID, an array representing a Project or a Project model.
*
* @param $value mixed
*/
public function setProject($value)
{
// Is this an instance of Project?
if ($value instanceof Project) {
return $this->setProjectObject($value);
}
// Is this an array representing a Project item?
if (is_array($value) && !empty($value['id'])) {
return $this->setProjectId($value['id']);
}
// Is this a scalar value representing the ID of this foreign key?
return $this->setProjectId($value);
}
/**
* Set Project - Accepts a Project model.
*
* @param $value Project
*/
public function setProjectObject(Project $value)
{
return $this->setProjectId($value->getId());
}
/**
* Get the Build model for this BuildMeta by Id.
*

View file

@ -660,18 +660,6 @@ class Project extends Model
return Factory::getStore('Build', 'PHPCensor')->getByProjectId($this->getId());
}
/**
* Get BuildMeta models by ProjectId for this Project.
*
* @uses \PHPCensor\Store\BuildMetaStore::getByProjectId()
* @uses \PHPCensor\Model\BuildMeta
* @return \PHPCensor\Model\BuildMeta[]
*/
public function getProjectBuildMetas()
{
return Factory::getStore('BuildMeta', 'PHPCensor')->getByProjectId($this->getId());
}
/**
* Return the latest build from a specific branch, of a specific status, for this project.
* @param string $branch

View file

@ -283,6 +283,6 @@ class Executor
{
/** @var Build $build */
$build = $this->pluginFactory->getResourceFor('PHPCensor\Model\Build');
$this->store->setMeta($build->getProjectId(), $build->getId(), 'plugin-summary', json_encode($summary));
$this->store->setMeta($build->getId(), 'plugin-summary', json_encode($summary));
}
}

View file

@ -44,38 +44,6 @@ class BuildMetaStore extends Store
return null;
}
/**
* Get multiple BuildMeta by ProjectId.
* @return array
*/
public function getByProjectId($value, $limit = 1000, $useConnection = 'read')
{
if (is_null($value)) {
throw new HttpException('Value passed to ' . __FUNCTION__ . ' cannot be null.');
}
$query = 'SELECT * FROM {{build_meta}} WHERE {{project_id}} = :project_id LIMIT :limit';
$stmt = Database::getConnection($useConnection)->prepareCommon($query);
$stmt->bindValue(':project_id', $value);
$stmt->bindValue(':limit', (int)$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);
$count = count($rtn);
return ['items' => $rtn, 'count' => $count];
} else {
return ['items' => [], 'count' => 0];
}
}
/**
* Get multiple BuildMeta by BuildId.
* @return array

View file

@ -269,8 +269,7 @@ class BuildStore extends Store
$query = 'SELECT bm.build_id, bm.meta_key, bm.meta_value
FROM {{build_meta}} AS {{bm}}
LEFT JOIN {{build}} AS {{b}} ON b.id = bm.build_id
WHERE bm.meta_key = :key
AND bm.project_id = :projectId';
WHERE bm.meta_key = :key AND b.project_id = :projectId';
// If we're getting comparative meta data, include previous builds
// otherwise just include the specified build ID:
@ -318,20 +317,20 @@ class BuildStore extends Store
/**
* Set a metadata value for a given project and build ID.
* @param $projectId
*
* @param $buildId
* @param $key
* @param $value
*
* @return bool
*/
public function setMeta($projectId, $buildId, $key, $value)
public function setMeta($buildId, $key, $value)
{
$cols = '{{project_id}}, {{build_id}}, {{meta_key}}, {{meta_value}}';
$query = 'INSERT INTO {{build_meta}} ('.$cols.') VALUES (:projectId, :buildId, :key, :value)';
$cols = '{{build_id}}, {{meta_key}}, {{meta_value}}';
$query = 'INSERT INTO {{build_meta}} ('.$cols.') VALUES (:buildId, :key, :value)';
$stmt = Database::getConnection('read')->prepareCommon($query);
$stmt->bindValue(':key', $key, \PDO::PARAM_STR);
$stmt->bindValue(':projectId', (int)$projectId, \PDO::PARAM_INT);
$stmt->bindValue(':buildId', (int)$buildId, \PDO::PARAM_INT);
$stmt->bindValue(':value', $value, \PDO::PARAM_STR);