Skip to content

Commit

Permalink
Merge pull request #584 from nextcloud/bugfix/578/postgres-update-issue
Browse files Browse the repository at this point in the history
Fix postgres issue when updating
  • Loading branch information
nickvergessen authored Jan 12, 2018
2 parents 1892a6a + f5f846c commit 70ce12f
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 37 deletions.
2 changes: 1 addition & 1 deletion appinfo/info.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ And in the works for the [coming versions](https://github.com/nextcloud/spreed/m
]]></description>

<version>3.0.0</version>
<version>3.0.1</version>
<licence>agpl</licence>

<author>Daniel Calviño Sánchez</author>
Expand Down
3 changes: 2 additions & 1 deletion lib/Migration/Version2000Date20171026140256.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function __construct(IDBConnection $connection, IConfig $config, IGroupMa
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {

if (!version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '1.1.4', '<')) {
if (version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '2.0.0', '<')) {
// Migrations only work after 2.0.0
return;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Migration/Version2000Date20171026140257.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public function __construct(IDBConnection $connection, IConfig $config, ISecureR
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {

if (version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '2.0.0', '<')) {
// Migrations only work after 2.0.0
return;
}

$chars = str_replace(['l', '0', '1'], '', ISecureRandom::CHAR_LOWER . ISecureRandom::CHAR_DIGITS);
$entropy = (int) $this->config->getAppValue('spreed', 'token_entropy', 8);

Expand Down
39 changes: 32 additions & 7 deletions lib/Migration/Version2001Date20170707115443.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,13 @@
*/
namespace OCA\Spreed\Migration;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCA\Spreed\Participant;
use OCA\Spreed\Room;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
Expand All @@ -36,11 +38,16 @@ class Version2001Date20170707115443 extends SimpleMigrationStep {
/** @var IDBConnection */
protected $db;

/** @var IConfig */
protected $config;

/**
* @param IDBConnection $db
* @param IConfig $config
*/
public function __construct(IDBConnection $db) {
public function __construct(IDBConnection $db, IConfig $config) {
$this->db = $db;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -73,6 +80,12 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
* @since 13.0.0
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {

if (version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '2.0.0', '<')) {
// Migrations only work after 2.0.0
return;
}

$query = $this->db->getQueryBuilder();

$query->selectAlias($query->createFunction('COUNT(*)'), 'num_rooms')
Expand Down Expand Up @@ -115,9 +128,15 @@ public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array
protected function makeOne2OneParticipantsOwners(array $one2oneRooms) {
$update = $this->db->getQueryBuilder();

$update->update('spreedme_room_participants')
->set('participantType', $update->createNamedParameter(Participant::OWNER))
->where($update->expr()->in('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
if (!$this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
$update->update('spreedme_room_participants')
->set('participantType', $update->createNamedParameter(Participant::OWNER))
->where($update->expr()->in('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
} else {
$update->update('spreedme_room_participants')
->set('participanttype', $update->createNamedParameter(Participant::OWNER))
->where($update->expr()->in('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
}

return $update->execute();
}
Expand All @@ -129,9 +148,15 @@ protected function makeOne2OneParticipantsOwners(array $one2oneRooms) {
protected function makeGroupParticipantsModerators(array $one2oneRooms) {
$update = $this->db->getQueryBuilder();

$update->update('spreedme_room_participants')
->set('participantType', $update->createNamedParameter(Participant::MODERATOR))
->where($update->expr()->nonEmptyString('userId'));
if (!$this->db->getDatabasePlatform() instanceof PostgreSqlPlatform) {
$update->update('spreedme_room_participants')
->set('participantType', $update->createNamedParameter(Participant::MODERATOR))
->where($update->expr()->nonEmptyString('userId'));
} else {
$update->update('spreedme_room_participants')
->set('participanttype', $update->createNamedParameter(Participant::MODERATOR))
->where($update->expr()->nonEmptyString('userId'));
}

if (!empty($one2oneRooms)) {
$update->andWhere($update->expr()->notIn('roomId', $update->createNamedParameter($one2oneRooms, IQueryBuilder::PARAM_INT_ARRAY)));
Expand Down
48 changes: 38 additions & 10 deletions lib/Migration/Version2001Date20171026134605.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
namespace OCA\Spreed\Migration;

use Doctrine\DBAL\Exception\TableNotFoundException;
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
Expand All @@ -35,11 +37,16 @@ class Version2001Date20171026134605 extends SimpleMigrationStep {
/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/**
* @param IDBConnection $connection
* @param IConfig $config
*/
public function __construct(IDBConnection $connection) {
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
}

/**
Expand Down Expand Up @@ -152,6 +159,12 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
* @since 13.0.0
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {

if (version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '2.0.0', '<')) {
// Migrations only work after 2.0.0
return;
}

$roomIdMap = $this->copyRooms();
$this->copyParticipants($roomIdMap);
$this->fixNotifications($roomIdMap);
Expand Down Expand Up @@ -200,14 +213,25 @@ protected function copyRooms() {
protected function copyParticipants(array $roomIdMap) {

$insert = $this->connection->getQueryBuilder();
$insert->insert('talk_participants')
->values([
'userId' => $insert->createParameter('userId'),
'roomId' => $insert->createParameter('roomId'),
'lastPing' => $insert->createParameter('lastPing'),
'sessionId' => $insert->createParameter('sessionId'),
'participantType' => $insert->createParameter('participantType'),
]);
if (!$this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
$insert->insert('talk_participants')
->values([
'userId' => $insert->createParameter('userId'),
'roomId' => $insert->createParameter('roomId'),
'lastPing' => $insert->createParameter('lastPing'),
'sessionId' => $insert->createParameter('sessionId'),
'participantType' => $insert->createParameter('participantType'),
]);
} else {
$insert->insert('talk_participants')
->values([
'userid' => $insert->createParameter('userId'),
'roomid' => $insert->createParameter('roomId'),
'lastping' => $insert->createParameter('lastPing'),
'sessionid' => $insert->createParameter('sessionId'),
'participanttype' => $insert->createParameter('participantType'),
]);
}

$query = $this->connection->getQueryBuilder();
$query->select('*')
Expand All @@ -224,8 +248,12 @@ protected function copyParticipants(array $roomIdMap) {
->setParameter('roomId', $roomIdMap[(int) $row['roomId']], IQueryBuilder::PARAM_INT)
->setParameter('lastPing', (int) $row['lastPing'], IQueryBuilder::PARAM_INT)
->setParameter('sessionId', $row['sessionId'])
->setParameter('participantType', (int) $row['participantType'], IQueryBuilder::PARAM_INT)
;
if (!$this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
$insert->setParameter('participantType', (int) $row['participantType'], IQueryBuilder::PARAM_INT);
} else {
$insert->setParameter('participantType', (int) $row['participanttype'], IQueryBuilder::PARAM_INT);
}
$insert->execute();
}
$result->closeCursor();
Expand Down
60 changes: 42 additions & 18 deletions lib/Migration/Version2001Date20180103144447.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;
Expand All @@ -34,11 +35,16 @@ class Version2001Date20180103144447 extends SimpleMigrationStep {
/** @var IDBConnection */
protected $connection;

/** @var IConfig */
protected $config;

/**
* @param IDBConnection $connection
* @param IConfig $config
*/
public function __construct(IDBConnection $connection) {
public function __construct(IDBConnection $connection, IConfig $config) {
$this->connection = $connection;
$this->config = $config;
}


Expand Down Expand Up @@ -113,26 +119,44 @@ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $op
*/
public function postSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) {

if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
// Couldn't install prior anyway, so we can skip this update step as well
if (version_compare($this->config->getAppValue('spreed', 'installed_version', '0.0.0'), '2.0.0', '<')) {
// Migrations only work after 2.0.0
return;
}

$update = $this->connection->getQueryBuilder();
$update->update('talk_rooms')
->set('active_since', 'activeSince')
->set('active_guests', 'activeGuests');
$update->execute();

$update = $this->connection->getQueryBuilder();
$update->update('talk_participants')
->set('user_id', 'userId')
->set('room_id', 'roomId')
->set('last_ping', 'lastPing')
->set('session_id', 'sessionId')
->set('participant_type', 'participantType')
->set('in_call', 'inCall');
$update->execute();
if (!$this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
$update = $this->connection->getQueryBuilder();
$update->update('talk_rooms')
->set('active_since', 'activeSince')
->set('active_guests', 'activeGuests');
$update->execute();

$update = $this->connection->getQueryBuilder();
$update->update('talk_participants')
->set('user_id', 'userId')
->set('room_id', 'roomId')
->set('last_ping', 'lastPing')
->set('session_id', 'sessionId')
->set('participant_type', 'participantType')
->set('in_call', 'inCall');
$update->execute();
} else {
$update = $this->connection->getQueryBuilder();
$update->update('talk_rooms')
->set('active_since', 'activesince')
->set('active_guests', 'activeguests');
$update->execute();

$update = $this->connection->getQueryBuilder();
$update->update('talk_participants')
->set('user_id', 'userid')
->set('room_id', 'roomid')
->set('last_ping', 'lastping')
->set('session_id', 'sessionid')
->set('participant_type', 'participanttype')
->set('in_call', 'incall');
$update->execute();
}

}
}

0 comments on commit 70ce12f

Please sign in to comment.