From 4927dc22c0fcb2407ac052f899f91bee0a189899 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 22 Dec 2018 11:33:19 +0100 Subject: [PATCH 1/2] Fix broken migration after orphaned votes --- appinfo/info.xml | 2 +- .../Version0009Date20181125051900.php | 83 +++++++++++++++++++ .../Version0009Date20181125061900.php | 15 ++-- 3 files changed, 90 insertions(+), 10 deletions(-) create mode 100644 lib/Migration/Version0009Date20181125051900.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 8e2c5ed79..86d763e42 100755 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -5,7 +5,7 @@ Polls A polls app, similar to doodle/dudle with the possibility to restrict access. A polls app, similar to doodle/dudle with the possibility to restrict access (members, certain groups/users, hidden and public). - 0.9.3 + 0.9.4 agpl Vinzenz Rosenkranz René Gieling diff --git a/lib/Migration/Version0009Date20181125051900.php b/lib/Migration/Version0009Date20181125051900.php new file mode 100644 index 000000000..ffac48ede --- /dev/null +++ b/lib/Migration/Version0009Date20181125051900.php @@ -0,0 +1,83 @@ + + * + * @author René Gieling + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * + */ + +namespace OCA\Polls\Migration; + +use Doctrine\DBAL\Exception\TableNotFoundException; +use Doctrine\DBAL\Platforms\PostgreSqlPlatform; +use Doctrine\DBAL\Types\Type; +use OCP\DB\ISchemaWrapper; +use OCP\DB\QueryBuilder\IQueryBuilder; +use OCP\IConfig; +use OCP\IDBConnection; +use OCP\Migration\SimpleMigrationStep; +use OCP\Migration\IOutput; + +/** + * Installation class for the polls app. + * Initial db creation + */ +class Version0009Date20181125051900 extends SimpleMigrationStep { + + /** @var IDBConnection */ + protected $connection; + + /** @var IConfig */ + protected $config; + + /** + * @param IDBConnection $connection + * @param IConfig $config + */ + public function __construct(IDBConnection $connection, IConfig $config) { + $this->connection = $connection; + $this->config = $config; + } + + /** + * @param IOutput $output + * @param \Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + * @since 13.0.0 + */ + public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if ($schema->hasTable('polls_particip') && + $schema->hasTable('polls_particip_text') && + $schema->hasTable('polls_votes')) { + + $schema->dropTable('polls_votes'); + } + + if ($schema->hasTable('polls_dts') && + $schema->hasTable('polls_txts') && + $schema->hasTable('polls_options')) { + + $schema->dropTable('polls_options'); + } + return $schema; + } +} diff --git a/lib/Migration/Version0009Date20181125061900.php b/lib/Migration/Version0009Date20181125061900.php index bcd282e73..967e71e28 100644 --- a/lib/Migration/Version0009Date20181125061900.php +++ b/lib/Migration/Version0009Date20181125061900.php @@ -301,10 +301,8 @@ protected function copyDateVotes() { ->values([ 'poll_id' => $insert->createParameter('poll_id'), 'user_id' => $insert->createParameter('user_id'), - // 'vote_date' => $insert->createParameter('vote_date'), 'vote_option_id' => $insert->createParameter('vote_option_id'), 'vote_option_text' => $insert->createParameter('vote_option_text'), - // 'vote_type' => $insert->createParameter('vote_type'), 'vote_answer' => $insert->createParameter('vote_answer'), ]); $query = $this->connection->getQueryBuilder(); @@ -315,10 +313,8 @@ protected function copyDateVotes() { $insert ->setParameter('poll_id', $row['poll_id']) ->setParameter('user_id', $row['user_id']) - // ->setParameter('vote_date', $row['dt']) ->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], $row['dt'])) ->setParameter('vote_option_text', date($row['dt'])) - // ->setParameter('vote_type', $row['type']) ->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type'])); $insert->execute(); } @@ -334,10 +330,8 @@ protected function copyTextVotes() { ->values([ 'poll_id' => $insert->createParameter('poll_id'), 'user_id' => $insert->createParameter('user_id'), - // 'vote_text' => $insert->createParameter('vote_text'), 'vote_option_id' => $insert->createParameter('vote_option_id'), 'vote_option_text' => $insert->createParameter('vote_option_text'), - // 'vote_type' => $insert->createParameter('vote_type'), 'vote_answer' => $insert->createParameter('vote_answer'), ]); $query = $this->connection->getQueryBuilder(); @@ -348,10 +342,8 @@ protected function copyTextVotes() { $insert ->setParameter('poll_id', $row['poll_id']) ->setParameter('user_id', $row['user_id']) - // ->setParameter('vote_text', $row['text']) ->setParameter('vote_option_id', $this->findOptionId($row['poll_id'], preg_replace("/_\d*$/", "$1", $row['text']))) ->setParameter('vote_option_text', preg_replace("/_\d*$/", "$1", $row['text'])) - // ->setParameter('vote_type', $row['type']) ->setParameter('vote_answer', $this->translateVoteTypeToAnswer($row['type'])); $insert->execute(); } @@ -373,8 +365,13 @@ protected function findOptionId($pollId, $text) { $resultFind = $queryFind->execute(); $row = $resultFind->fetch(); - return $row['id']; + if ($row['id'] === null) { + // seems, we have an orphaned vote, return 0 as vote_option_id + return 0; + } else { + return $row['id']; + } } protected function translateVoteTypeToAnswer($voteType) { From 3ca333159a8a2fc2064da860530bc8b9a82ea90f Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 22 Dec 2018 11:37:01 +0100 Subject: [PATCH 2/2] #454 fix PostgeSQL with workaround --- lib/Migration/Version0009Date20181125061900.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/Migration/Version0009Date20181125061900.php b/lib/Migration/Version0009Date20181125061900.php index 967e71e28..b1e3315a7 100644 --- a/lib/Migration/Version0009Date20181125061900.php +++ b/lib/Migration/Version0009Date20181125061900.php @@ -360,9 +360,9 @@ protected function findOptionId($pollId, $text) { ->from('polls_options') // ->where($queryFind->expr()->eq('poll_id', $pollId)) // ->andWhere($queryFind->expr()->eq('poll_option', $text)); - ->where('poll_id = "' . $pollId . '"') - ->andWhere('poll_option_text ="' . $text . '"'); - + ->where('poll_id = \'' . $pollId . '\'') + ->andWhere('poll_option_text =\'' . $text . '\''); + $resultFind = $queryFind->execute(); $row = $resultFind->fetch();