Skip to content

Commit

Permalink
Merge pull request #456 from nextcloud/migration-fix-2
Browse files Browse the repository at this point in the history
Migration fix 2
  • Loading branch information
dartcafe committed Dec 22, 2018
2 parents 22cb194 + 3ca3331 commit b24446c
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 13 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<name>Polls</name>
<summary>A polls app, similar to doodle/dudle with the possibility to restrict access.</summary>
<description>A polls app, similar to doodle/dudle with the possibility to restrict access (members, certain groups/users, hidden and public).</description>
<version>0.9.3</version>
<version>0.9.4</version>
<licence>agpl</licence>
<author>Vinzenz Rosenkranz</author>
<author>René Gieling</author>
Expand Down
83 changes: 83 additions & 0 deletions lib/Migration/Version0009Date20181125051900.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* @copyright Copyright (c) 2017 René Gieling <[email protected]>
*
* @author René Gieling <[email protected]>
*
* @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 <http://www.gnu.org/licenses/>.
*
*/

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;
}
}
21 changes: 9 additions & 12 deletions lib/Migration/Version0009Date20181125061900.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
}
Expand All @@ -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();
Expand All @@ -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();
}
Expand All @@ -368,13 +360,18 @@ 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();
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) {
Expand Down

0 comments on commit b24446c

Please sign in to comment.