From 9e57de043e725a729f514784b0fdb80c0f4bf3c0 Mon Sep 17 00:00:00 2001 From: Dmitry Khomutov Date: Sun, 15 Oct 2017 00:48:53 +0700 Subject: [PATCH] Removed useless 'project_id' column from 'build_meta' table. --- ...348_removed_project_id_from_build_meta.php | 28 +++++ src/PHPCensor/Model/Build.php | 2 +- src/PHPCensor/Model/BuildMeta.php | 107 ------------------ src/PHPCensor/Model/Project.php | 12 -- src/PHPCensor/Plugin/Util/Executor.php | 2 +- src/PHPCensor/Store/BuildMetaStore.php | 32 ------ src/PHPCensor/Store/BuildStore.php | 13 +-- 7 files changed, 36 insertions(+), 160 deletions(-) create mode 100644 src/PHPCensor/Migrations/20171014173348_removed_project_id_from_build_meta.php diff --git a/src/PHPCensor/Migrations/20171014173348_removed_project_id_from_build_meta.php b/src/PHPCensor/Migrations/20171014173348_removed_project_id_from_build_meta.php new file mode 100644 index 00000000..873aba78 --- /dev/null +++ b/src/PHPCensor/Migrations/20171014173348_removed_project_id_from_build_meta.php @@ -0,0 +1,28 @@ +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(); + } + } +} diff --git a/src/PHPCensor/Model/Build.php b/src/PHPCensor/Model/Build.php index 818b5000..ade8e1cc 100644 --- a/src/PHPCensor/Model/Build.php +++ b/src/PHPCensor/Model/Build.php @@ -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); } /** diff --git a/src/PHPCensor/Model/BuildMeta.php b/src/PHPCensor/Model/BuildMeta.php index 656f81b5..14e8e035 100644 --- a/src/PHPCensor/Model/BuildMeta.php +++ b/src/PHPCensor/Model/BuildMeta.php @@ -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. * diff --git a/src/PHPCensor/Model/Project.php b/src/PHPCensor/Model/Project.php index 9e20d91d..406df493 100644 --- a/src/PHPCensor/Model/Project.php +++ b/src/PHPCensor/Model/Project.php @@ -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 diff --git a/src/PHPCensor/Plugin/Util/Executor.php b/src/PHPCensor/Plugin/Util/Executor.php index 18bd2ebb..03f3ce76 100644 --- a/src/PHPCensor/Plugin/Util/Executor.php +++ b/src/PHPCensor/Plugin/Util/Executor.php @@ -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)); } } diff --git a/src/PHPCensor/Store/BuildMetaStore.php b/src/PHPCensor/Store/BuildMetaStore.php index c69782e9..2909d7f3 100644 --- a/src/PHPCensor/Store/BuildMetaStore.php +++ b/src/PHPCensor/Store/BuildMetaStore.php @@ -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 diff --git a/src/PHPCensor/Store/BuildStore.php b/src/PHPCensor/Store/BuildStore.php index 895ad872..79c28bdf 100644 --- a/src/PHPCensor/Store/BuildStore.php +++ b/src/PHPCensor/Store/BuildStore.php @@ -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);