-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Turn posts_tags into a full post value table, and avoid duplicating info
- Add extra field to posts_tags - Remove tags from post_varchar - Add Post/Tags validator - Add Post/Tags repo
- Loading branch information
Showing
9 changed files
with
268 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
|
||
/** | ||
* Ushahidi Post Relation Repository | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
class Ushahidi_Repository_Post_Tags extends Ushahidi_Repository_Post_Value | ||
{ | ||
// Ushahidi_Repository | ||
protected function getTable() | ||
{ | ||
return 'posts_tags'; | ||
} | ||
|
||
// Override selectQuery to fetch attribute 'key' too | ||
protected function selectQuery(Array $where = []) | ||
{ | ||
$query = parent::selectQuery($where); | ||
|
||
// Select 'tag_id' as value too | ||
$query->select( | ||
['posts_tags.tag_id', 'value'] | ||
); | ||
|
||
return $query; | ||
} | ||
|
||
// PostValueRepository | ||
public function getValueQuery($form_attribute_id, $match) | ||
{ | ||
return $this->selectQuery(compact('form_attribute_id')) | ||
->where('tag_id', 'LIKE', "%$match%"); | ||
} | ||
|
||
// UpdatePostValueRepository | ||
public function createValue($value, $form_attribute_id, $post_id) | ||
{ | ||
$tag_id = $value; | ||
$input = compact('tag_id', 'form_attribute_id', 'post_id'); | ||
$input['created'] = time(); | ||
|
||
return $this->executeInsert($input); | ||
} | ||
|
||
// UpdatePostValueRepository | ||
public function updateValue($id, $value) | ||
{ | ||
$update = ['tag_id' => $value]; | ||
if ($id && $update) | ||
{ | ||
$this->executeUpdate(compact('id'), $update); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
|
||
/** | ||
* Ushahidi Post Media Validator | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
use Ushahidi\Core\Entity\TagsRepository; | ||
|
||
class Ushahidi_Validator_Post_Tags extends Ushahidi_Validator_Post_ValueValidator | ||
{ | ||
protected $media_repo; | ||
|
||
public function __construct(TagsRepository $tags_repo) | ||
{ | ||
$this->repo = $tags_repo; | ||
} | ||
|
||
protected function validate($value) | ||
{ | ||
if (!Valid::digit($value)) { | ||
return 'digit'; | ||
} | ||
|
||
if (! $this->repo->exists($value)) { | ||
return 'exists'; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
migrations/20170522004400_remove_tags_from_post_varchar.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class RemoveTagsFromPostVarchar extends AbstractMigration | ||
{ | ||
public function up() | ||
{ | ||
// Remove all tag values from post_varchar | ||
$this->execute( | ||
"DELETE from post_varchar | ||
WHERE form_attribute_id IN | ||
(SELECT form_attributes.id FROM form_attributes WHERE input = 'tags' AND type = 'varchar')" | ||
); | ||
} | ||
|
||
public function down() | ||
{ | ||
// No op - not reversible | ||
} | ||
} |
86 changes: 86 additions & 0 deletions
86
migrations/20170522004409_join_posts_tags_table_to_attribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
<?php | ||
|
||
use Phinx\Migration\AbstractMigration; | ||
|
||
class JoinPostsTagsTableToAttribute extends AbstractMigration | ||
{ | ||
public function up() | ||
{ | ||
$pdo = $this->getAdapter()->getConnection(); | ||
|
||
$this->table('posts_tags') | ||
->addColumn('id', 'integer', ['null' => false]) | ||
->addColumn('form_attribute_id', 'integer') | ||
->addColumn('created', 'integer', ['default' => 0]) | ||
->update(); | ||
|
||
// Manually fix up keys | ||
$this->execute('ALTER TABLE posts_tags | ||
DROP PRIMARY KEY, | ||
ADD PRIMARY KEY (id), | ||
ADD INDEX (post_id), | ||
MODIFY COLUMN id INT AUTO_INCREMENT, | ||
ADD UNIQUE INDEX unique_post_tag_attribute_ids (post_id, tag_id, form_attribute_id)'); | ||
|
||
// Make varchar/tags attributes into tags/tags attributes | ||
$this->execute(" | ||
UPDATE form_attributes | ||
SET type = 'tags' | ||
WHERE type = 'varchar' AND input = 'tags' | ||
"); | ||
|
||
$attributes = $this->fetchAll(" | ||
SELECT form_attributes.id, form_stages.form_id | ||
FROM form_attributes | ||
JOIN form_stages ON (form_attributes.form_stage_id = form_stages.id) | ||
WHERE | ||
form_attributes.type = 'tags' AND | ||
form_stages.type = 'post' | ||
"); | ||
|
||
// Set form_attribute_id for posts_tags entries | ||
$insert = $pdo->prepare(' | ||
UPDATE posts_tags JOIN posts ON (posts_tags.post_id = posts.id) | ||
SET form_attribute_id = :attr_id WHERE posts.form_id = :form_id | ||
'); | ||
foreach ($attributes as $attribute) { | ||
$insert->execute([ | ||
':attr_id' => $attribute['id'], | ||
':form_id' => $attribute['form_id'] | ||
]); | ||
} | ||
|
||
// Add foreign key for form_attribute_id | ||
$this->table('posts_tags') | ||
->addForeignKey('form_attribute_id', 'form_attributes', 'id', [ | ||
'delete' => 'CASCADE', | ||
'update' => 'CASCADE', | ||
]) | ||
->update(); | ||
} | ||
|
||
public function down() | ||
{ | ||
// Make tags/tags attributes into varchar/tags attributes | ||
$this->execute(" | ||
UPDATE form_attributes | ||
SET type = 'varchar' | ||
WHERE type = 'tags' AND input = 'tags' | ||
"); | ||
|
||
// Restore keys/indexs | ||
$this->execute('ALTER TABLE posts_tags | ||
DROP PRIMARY KEY, | ||
ADD PRIMARY KEY (post_id, tag_id), | ||
MODIFY COLUMN id INT, | ||
DROP INDEX unique_post_tag_attribute_ids'); | ||
|
||
// Remove columns | ||
$this->table('posts_tags') | ||
->dropForeignKey('form_attribute_id') | ||
->removeColumn('id') | ||
->removeColumn('form_attribute_id') | ||
->removeColumn('created') | ||
->update(); | ||
} | ||
} |
Oops, something went wrong.