Skip to content

Commit

Permalink
Make sure column names are lowercase to not break postgres
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Jan 3, 2018
1 parent d0967b3 commit c4c424e
Show file tree
Hide file tree
Showing 4 changed files with 309 additions and 117 deletions.
74 changes: 37 additions & 37 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ public function __construct(IDBConnection $db, IConfig $config, ISecureRandom $s
*/
public function createRoomObject(array $row) {
$activeSince = null;
if (!empty($row['activeSince'])) {
$activeSince = new \DateTime($row['activeSince']);
if (!empty($row['active_since'])) {
$activeSince = new \DateTime($row['active_since']);
}

return new Room($this, $this->db, $this->secureRandom, $this->dispatcher, $this->hasher, (int) $row['id'], (int) $row['type'], $row['token'], $row['name'], $row['password'], (int) $row['activeGuests'], $activeSince);
return new Room($this, $this->db, $this->secureRandom, $this->dispatcher, $this->hasher, (int) $row['id'], (int) $row['type'], $row['token'], $row['name'], $row['password'], (int) $row['active_guests'], $activeSince);
}

/**
Expand All @@ -80,7 +80,7 @@ public function createRoomObject(array $row) {
* @return Participant
*/
public function createParticipantObject(Room $room, array $row) {
return new Participant($this->db, $room, $row['userId'], (int) $row['participantType'], (int) $row['lastPing'], $row['sessionId'], (bool) $row['inCall']);
return new Participant($this->db, $room, $row['user_id'], (int) $row['participant_type'], (int) $row['last_ping'], $row['session_id'], (bool) $row['in_call']);
}

/**
Expand All @@ -92,17 +92,17 @@ public function getRoomsForParticipant($participant) {
$query->select('*')
->from('talk_rooms', 'r')
->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
))
->where($query->expr()->isNotNull('p.userId'));
->where($query->expr()->isNotNull('p.user_id'));

$result = $query->execute();
$rooms = [];
while ($row = $result->fetch()) {
$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}
$rooms[] = $room;
}
Expand All @@ -128,10 +128,10 @@ public function getRoomForParticipant($roomId, $participant) {
if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
))
->andWhere($query->expr()->isNotNull('p.userId'));
->andWhere($query->expr()->isNotNull('p.user_id'));
}

$result = $query->execute();
Expand All @@ -143,8 +143,8 @@ public function getRoomForParticipant($roomId, $participant) {
}

$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}

if ($participant === null && $room->getType() !== Room::PUBLIC_CALL) {
Expand Down Expand Up @@ -173,8 +173,8 @@ public function getRoomForParticipantByToken($token, $participant) {
if ($participant !== null) {
// Non guest user
$query->leftJoin('r', 'talk_participants', 'p', $query->expr()->andX(
$query->expr()->eq('p.userId', $query->createNamedParameter($participant)),
$query->expr()->eq('p.roomId', 'r.id')
$query->expr()->eq('p.user_id', $query->createNamedParameter($participant)),
$query->expr()->eq('p.room_id', 'r.id')
));
}

Expand All @@ -187,15 +187,15 @@ public function getRoomForParticipantByToken($token, $participant) {
}

$room = $this->createRoomObject($row);
if ($participant !== null && isset($row['userId'])) {
$room->setParticipant($row['userId'], $this->createParticipantObject($room, $row));
if ($participant !== null && isset($row['user_id'])) {
$room->setParticipant($row['user_id'], $this->createParticipantObject($room, $row));
}

if ($room->getType() === Room::PUBLIC_CALL) {
return $room;
}

if ($participant !== null && $row['userId'] === $participant) {
if ($participant !== null && $row['user_id'] === $participant) {
return $room;
}

Expand Down Expand Up @@ -260,8 +260,8 @@ public function getRoomForSession($userId, $sessionId) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('talk_participants', 'p')
->leftJoin('p', 'talk_rooms', 'r', $query->expr()->eq('p.roomId', 'r.id'))
->where($query->expr()->eq('p.sessionId', $query->createNamedParameter($sessionId)))
->leftJoin('p', 'talk_rooms', 'r', $query->expr()->eq('p.room_id', 'r.id'))
->where($query->expr()->eq('p.session_id', $query->createNamedParameter($sessionId)))
->setMaxResults(1);

$result = $query->execute();
Expand All @@ -272,13 +272,13 @@ public function getRoomForSession($userId, $sessionId) {
throw new RoomNotFoundException();
}

if ((string) $userId !== $row['userId']) {
if ((string) $userId !== $row['user_id']) {
throw new RoomNotFoundException();
}

$room = $this->createRoomObject($row);
$participant = $this->createParticipantObject($room, $row);
$room->setParticipant($row['userId'], $participant);
$room->setParticipant($row['user_id'], $participant);

if ($room->getType() === Room::PUBLIC_CALL || !in_array($participant->getParticipantType(), [Participant::GUEST, Participant::USER_SELF_JOINED], true)) {
return $room;
Expand All @@ -298,16 +298,16 @@ public function getOne2OneRoom($participant1, $participant2) {
$query->select('*')
->from('talk_rooms', 'r1')
->leftJoin('r1', 'talk_participants', 'p1', $query->expr()->andX(
$query->expr()->eq('p1.userId', $query->createNamedParameter($participant1)),
$query->expr()->eq('p1.roomId', 'r1.id')
$query->expr()->eq('p1.user_id', $query->createNamedParameter($participant1)),
$query->expr()->eq('p1.room_id', 'r1.id')
))
->leftJoin('r1', 'talk_participants', 'p2', $query->expr()->andX(
$query->expr()->eq('p2.userId', $query->createNamedParameter($participant2)),
$query->expr()->eq('p2.roomId', 'r1.id')
$query->expr()->eq('p2.user_id', $query->createNamedParameter($participant2)),
$query->expr()->eq('p2.room_id', 'r1.id')
))
->where($query->expr()->eq('r1.type', $query->createNamedParameter(Room::ONE_TO_ONE_CALL, IQueryBuilder::PARAM_INT)))
->andWhere($query->expr()->isNotNull('p1.userId'))
->andWhere($query->expr()->isNotNull('p2.userId'));
->andWhere($query->expr()->isNotNull('p1.user_id'))
->andWhere($query->expr()->isNotNull('p2.user_id'));

$result = $query->execute();
$row = $result->fetch();
Expand Down Expand Up @@ -377,9 +377,9 @@ public function getCurrentSessionId($userId) {
$query = $this->db->getQueryBuilder();
$query->select('*')
->from('talk_participants')
->where($query->expr()->eq('userId', $query->createNamedParameter($userId)))
->andWhere($query->expr()->neq('sessionId', $query->createNamedParameter('0')))
->orderBy('lastPing', 'DESC')
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)))
->andWhere($query->expr()->neq('session_id', $query->createNamedParameter('0')))
->orderBy('last_ping', 'DESC')
->setMaxResults(1);
$result = $query->execute();
$row = $result->fetch();
Expand All @@ -389,7 +389,7 @@ public function getCurrentSessionId($userId) {
return null;
}

return $row['sessionId'];
return $row['session_id'];
}

/**
Expand All @@ -404,15 +404,15 @@ public function getSessionIdsForUser($userId) {

// Delete all messages from or to the current user
$query = $this->db->getQueryBuilder();
$query->select('sessionId')
$query->select('session_id')
->from('talk_participants')
->where($query->expr()->eq('userId', $query->createNamedParameter($userId)));
->where($query->expr()->eq('user_id', $query->createNamedParameter($userId)));
$result = $query->execute();

$sessionIds = [];
while ($row = $result->fetch()) {
if ($row['sessionId'] !== '0') {
$sessionIds[] = $row['sessionId'];
if ($row['session_id'] !== '0') {
$sessionIds[] = $row['session_id'];
}
}
$result->closeCursor();
Expand Down
132 changes: 132 additions & 0 deletions lib/Migration/Version2001Date20180103144447.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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\Spreed\Migration;

use Doctrine\DBAL\Platforms\PostgreSqlPlatform;
use Doctrine\DBAL\Schema\Schema;
use Doctrine\DBAL\Types\Type;
use OCP\IDBConnection;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

class Version2001Date20180103144447 extends SimpleMigrationStep {

/** @var IDBConnection */
protected $connection;

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


/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @return null|Schema
* @since 13.0.0
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
/** @var Schema $schema */
$schema = $schemaClosure();

$table = $schema->getTable('talk_rooms');

if (!$table->hasColumn('active_since')) {
$table->addColumn('active_since', Type::DATETIME, [
'notnull' => false,
]);
$table->addColumn('active_guests', Type::INTEGER, [
'notnull' => true,
'length' => 4,
'default' => 0,
'unsigned' => true,
]);
}

$table = $schema->getTable('talk_participants');

if (!$table->hasColumn('user_id')) {
$table->addColumn('user_id', Type::STRING, [
'notnull' => false,
'length' => 255,
]);
$table->addColumn('room_id', Type::INTEGER, [
'notnull' => true,
'length' => 11,
]);
$table->addColumn('last_ping', Type::INTEGER, [
'notnull' => true,
'length' => 11,
]);
$table->addColumn('session_id', Type::STRING, [
'notnull' => true,
'length' => 255,
]);
$table->addColumn('participant_type', Type::SMALLINT, [
'notnull' => true,
'length' => 6,
'default' => 0,
]);
$table->addColumn('in_call', Type::BOOLEAN, [
'default' => 0,
]);
}

return $schema;
}

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @since 13.0.0
*/
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
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();

}
}
60 changes: 60 additions & 0 deletions lib/Migration/Version2001Date20180103150836.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* @copyright Copyright (c) 2017 Joas Schilling <coding@schilljs.com>
*
* @author Joas Schilling <coding@schilljs.com>
*
* @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\Spreed\Migration;

use Doctrine\DBAL\Schema\Schema;
use OCP\Migration\SimpleMigrationStep;
use OCP\Migration\IOutput;

class Version2001Date20180103150836 extends SimpleMigrationStep {

/**
* @param IOutput $output
* @param \Closure $schemaClosure The `\Closure` returns a `Schema`
* @param array $options
* @return null|Schema
* @since 13.0.0
*/
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) {
/** @var Schema $schema */
$schema = $schemaClosure();

$table = $schema->getTable('talk_rooms');
if ($table->hasColumn('activeSince')) {
$table->dropColumn('activeSince');
$table->dropColumn('activeGuests');
}

$table = $schema->getTable('talk_participants');
if ($table->hasColumn('userId')) {
$table->dropColumn('userId');
$table->dropColumn('roomId');
$table->dropColumn('lastPing');
$table->dropColumn('sessionId');
$table->dropColumn('participantType');
$table->dropColumn('inCall');
}

return $schema;
}
}
Loading

0 comments on commit c4c424e

Please sign in to comment.