diff options
author | Joas Schilling <coding@schilljs.com> | 2020-12-09 17:33:08 +0300 |
---|---|---|
committer | Joas Schilling <coding@schilljs.com> | 2020-12-09 19:02:21 +0300 |
commit | fbf72c1af50bfac5d1008249add629fdd5c9aca7 (patch) | |
tree | 663f4420efbbea7bb1c982e98cfcf4a15919a3b8 /lib/Migration | |
parent | b9b76ff9a7fdf880477d06bf85a5d8d102fef11f (diff) |
Create a new table for internal signaling and guest names with a PK
Signed-off-by: Joas Schilling <coding@schilljs.com>
Diffstat (limited to 'lib/Migration')
-rw-r--r-- | lib/Migration/Version11000Date20201209142525.php | 144 | ||||
-rw-r--r-- | lib/Migration/Version11000Date20201209142526.php | 58 | ||||
-rw-r--r-- | lib/Migration/Version2001Date20171026134605.php | 47 | ||||
-rw-r--r-- | lib/Migration/Version3002Date20180319104030.php | 46 |
4 files changed, 253 insertions, 42 deletions
diff --git a/lib/Migration/Version11000Date20201209142525.php b/lib/Migration/Version11000Date20201209142525.php new file mode 100644 index 000000000..1f4edc318 --- /dev/null +++ b/lib/Migration/Version11000Date20201209142525.php @@ -0,0 +1,144 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020, 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\Talk\Migration; + +use Closure; +use Doctrine\DBAL\Types\Types; +use OCP\DB\ISchemaWrapper; +use OCP\IDBConnection; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version11000Date20201209142525 extends SimpleMigrationStep { + /** @var IDBConnection */ + protected $connection; + + public function __construct(IDBConnection $connection) { + $this->connection = $connection; + } + + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $changedSchema = false; + if (!$schema->hasTable('talk_internalsignaling')) { + $table = $schema->createTable('talk_internalsignaling'); + + // Auto increment id + $table->addColumn('id', Types::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + ]); + + $table->addColumn('sender', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + $table->addColumn('recipient', Types::STRING, [ + 'notnull' => true, + 'length' => 255, + ]); + $table->addColumn('message', Types::TEXT, [ + 'notnull' => true, + ]); + $table->addColumn('timestamp', Types::INTEGER, [ + 'notnull' => true, + 'length' => 11, + ]); + + $table->setPrimaryKey(['id']); + $table->addIndex(['recipient', 'timestamp'], 'tis_recipient_time'); + + $changedSchema = true; + } + + if (!$schema->hasTable('talk_guestnames')) { + $table = $schema->createTable('talk_guestnames'); + + // Auto increment id + $table->addColumn('id', Types::BIGINT, [ + 'autoincrement' => true, + 'notnull' => true, + ]); + + $table->addColumn('session_hash', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + ]); + $table->addColumn('display_name', Types::STRING, [ + 'notnull' => false, + 'length' => 64, + 'default' => '', + ]); + + $table->setPrimaryKey(['id']); + $table->addUniqueIndex(['session_hash'], 'tg_session_hash'); + $changedSchema = true; + } + + return $changedSchema ? $schema : null; + } + + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + */ + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { + if (!$this->connection->tableExists('talk_guests')) { + return; + } + + $insert = $this->connection->getQueryBuilder(); + $insert->insert('talk_guestnames') + ->values([ + 'session_hash' => $insert->createParameter('session_hash'), + 'display_name' => $insert->createParameter('display_name'), + ]); + + $query = $this->connection->getQueryBuilder(); + $query->select('*') + ->from('talk_guests'); + + $result = $query->execute(); + while ($row = $result->fetch()) { + $insert + ->setParameter('session_hash', (string) $row['session_hash']) + ->setParameter('display_name', (string) $row['display_name']) + ; + $insert->execute(); + } + $result->closeCursor(); + } +} diff --git a/lib/Migration/Version11000Date20201209142526.php b/lib/Migration/Version11000Date20201209142526.php new file mode 100644 index 000000000..85f1a2d38 --- /dev/null +++ b/lib/Migration/Version11000Date20201209142526.php @@ -0,0 +1,58 @@ +<?php + +declare(strict_types=1); +/** + * @copyright Copyright (c) 2020, 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\Talk\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +class Version11000Date20201209142526 extends SimpleMigrationStep { + /** + * @param IOutput $output + * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` + * @param array $options + * @return null|ISchemaWrapper + */ + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + $changedSchema = false; + + if ($schema->hasTable('talk_signaling')) { + $schema->dropTable('talk_signaling'); + $changedSchema = true; + } + + if ($schema->hasTable('talk_guests')) { + $schema->dropTable('talk_guests'); + $changedSchema = true; + } + + return $changedSchema ? $schema : null; + } +} diff --git a/lib/Migration/Version2001Date20171026134605.php b/lib/Migration/Version2001Date20171026134605.php index bd40b396d..958cc16ab 100644 --- a/lib/Migration/Version2001Date20171026134605.php +++ b/lib/Migration/Version2001Date20171026134605.php @@ -60,27 +60,32 @@ class Version2001Date20171026134605 extends SimpleMigrationStep { /** @var ISchemaWrapper $schema */ $schema = $schemaClosure(); - if (!$schema->hasTable('talk_signaling')) { - $table = $schema->createTable('talk_signaling'); - - $table->addColumn('sender', Type::STRING, [ - 'notnull' => true, - 'length' => 255, - ]); - $table->addColumn('recipient', Type::STRING, [ - 'notnull' => true, - 'length' => 255, - ]); - $table->addColumn('message', Type::TEXT, [ - 'notnull' => true, - ]); - $table->addColumn('timestamp', Type::INTEGER, [ - 'notnull' => true, - 'length' => 11, - ]); - - $table->addIndex(['recipient', 'timestamp'], 'ts_recipient_time'); - } + /** + * Table had to be rebuild because it was missing a primary key + * @see Version11000Date20201209142525 + * + * if (!$schema->hasTable('talk_signaling')) { + * $table = $schema->createTable('talk_signaling'); + * + * $table->addColumn('sender', Type::STRING, [ + * 'notnull' => true, + * 'length' => 255, + * ]); + * $table->addColumn('recipient', Type::STRING, [ + * 'notnull' => true, + * 'length' => 255, + * ]); + * $table->addColumn('message', Type::TEXT, [ + * 'notnull' => true, + * ]); + * $table->addColumn('timestamp', Type::INTEGER, [ + * 'notnull' => true, + * 'length' => 11, + * ]); + * + * $table->addIndex(['recipient', 'timestamp'], 'ts_recipient_time'); + * } + */ if (!$schema->hasTable('talk_rooms')) { $table = $schema->createTable('talk_rooms'); diff --git a/lib/Migration/Version3002Date20180319104030.php b/lib/Migration/Version3002Date20180319104030.php index 35d1228c4..3186b9907 100644 --- a/lib/Migration/Version3002Date20180319104030.php +++ b/lib/Migration/Version3002Date20180319104030.php @@ -24,7 +24,6 @@ declare(strict_types=1); */ namespace OCA\Talk\Migration; -use Doctrine\DBAL\Types\Type; use OCP\DB\ISchemaWrapper; use OCP\Migration\SimpleMigrationStep; use OCP\Migration\IOutput; @@ -39,25 +38,30 @@ class Version3002Date20180319104030 extends SimpleMigrationStep { * @since 13.0.0 */ public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options): ?ISchemaWrapper { - /** @var ISchemaWrapper $schema */ - $schema = $schemaClosure(); - - if (!$schema->hasTable('talk_guests')) { - $table = $schema->createTable('talk_guests'); - - $table->addColumn('session_hash', Type::STRING, [ - 'notnull' => false, - 'length' => 64, - ]); - $table->addColumn('display_name', Type::STRING, [ - 'notnull' => false, - 'length' => 64, - 'default' => '', - ]); - - $table->addUniqueIndex(['session_hash'], 'tg_session_hash'); - } - - return $schema; + /** + * The table had to be redone so it contains a primary key + * @see Version11000Date20201209142525 + * + * $schema = $schemaClosure(); + * + * if (!$schema->hasTable('talk_guests')) { + * $table = $schema->createTable('talk_guests'); + * + * $table->addColumn('session_hash', Type::STRING, [ + * 'notnull' => false, + * 'length' => 64, + * ]); + * $table->addColumn('display_name', Type::STRING, [ + * 'notnull' => false, + * 'length' => 64, + * 'default' => '', + * ]); + * + * $table->addUniqueIndex(['session_hash'], 'tg_session_hash'); + * } + * + * return $schema; + */ + return null; } } |