aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files_external/lib/Migration
diff options
context:
space:
mode:
Diffstat (limited to 'apps/files_external/lib/Migration')
-rw-r--r--apps/files_external/lib/Migration/DummyUserSession.php57
-rw-r--r--apps/files_external/lib/Migration/Version1011Date20200630192246.php136
-rw-r--r--apps/files_external/lib/Migration/Version1015Date20211104103506.php90
-rw-r--r--apps/files_external/lib/Migration/Version1016Date20220324154536.php38
-rw-r--r--apps/files_external/lib/Migration/Version22000Date20210216084416.php47
5 files changed, 368 insertions, 0 deletions
diff --git a/apps/files_external/lib/Migration/DummyUserSession.php b/apps/files_external/lib/Migration/DummyUserSession.php
new file mode 100644
index 00000000000..1ebf0e1ec4f
--- /dev/null
+++ b/apps/files_external/lib/Migration/DummyUserSession.php
@@ -0,0 +1,57 @@
+<?php
+
+/**
+ * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
+ * SPDX-License-Identifier: AGPL-3.0-only
+ */
+namespace OCA\Files_External\Migration;
+
+use OCP\IUser;
+use OCP\IUserSession;
+
+class DummyUserSession implements IUserSession {
+
+ private ?IUser $user = null;
+
+ public function login($uid, $password) {
+ }
+
+ public function logout() {
+ }
+
+ public function setUser($user) {
+ $this->user = $user;
+ }
+
+ public function setVolatileActiveUser(?IUser $user): void {
+ $this->user = $user;
+ }
+
+ public function getUser() {
+ return $this->user;
+ }
+
+ public function isLoggedIn() {
+ return !is_null($this->user);
+ }
+
+ /**
+ * get getImpersonatingUserID
+ *
+ * @return string|null
+ * @since 17.0.0
+ */
+ public function getImpersonatingUserID() : ?string {
+ return null;
+ }
+
+ /**
+ * set setImpersonatingUserID
+ *
+ * @since 17.0.0
+ */
+ public function setImpersonatingUserID(bool $useCurrentUser = true): void {
+ //no OP
+ }
+}
diff --git a/apps/files_external/lib/Migration/Version1011Date20200630192246.php b/apps/files_external/lib/Migration/Version1011Date20200630192246.php
new file mode 100644
index 00000000000..c87b1cfbc8b
--- /dev/null
+++ b/apps/files_external/lib/Migration/Version1011Date20200630192246.php
@@ -0,0 +1,136 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_External\Migration;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\DB\Types;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+class Version1011Date20200630192246 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) {
+ /** @var ISchemaWrapper $schema */
+ $schema = $schemaClosure();
+
+ if (!$schema->hasTable('external_mounts')) {
+ $table = $schema->createTable('external_mounts');
+ $table->addColumn('mount_id', Types::BIGINT, [
+ 'autoincrement' => true,
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('mount_point', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 128,
+ ]);
+ $table->addColumn('storage_backend', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 64,
+ ]);
+ $table->addColumn('auth_backend', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 64,
+ ]);
+ $table->addColumn('priority', Types::INTEGER, [
+ 'notnull' => true,
+ 'length' => 4,
+ 'default' => 100,
+ ]);
+ $table->addColumn('type', Types::INTEGER, [
+ 'notnull' => true,
+ 'length' => 4,
+ ]);
+ $table->setPrimaryKey(['mount_id']);
+ }
+
+ if (!$schema->hasTable('external_applicable')) {
+ $table = $schema->createTable('external_applicable');
+ $table->addColumn('applicable_id', Types::BIGINT, [
+ 'autoincrement' => true,
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('mount_id', Types::BIGINT, [
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('type', Types::INTEGER, [
+ 'notnull' => true,
+ 'length' => 4,
+ ]);
+ $table->addColumn('value', Types::STRING, [
+ 'notnull' => false,
+ 'length' => 64,
+ ]);
+ $table->setPrimaryKey(['applicable_id']);
+ $table->addIndex(['mount_id'], 'applicable_mount');
+ $table->addUniqueIndex(['type', 'value', 'mount_id'], 'applicable_type_value_mount');
+ }
+
+ if (!$schema->hasTable('external_config')) {
+ $table = $schema->createTable('external_config');
+ $table->addColumn('config_id', Types::BIGINT, [
+ 'autoincrement' => true,
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('mount_id', Types::BIGINT, [
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('key', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 64,
+ ]);
+ $table->addColumn('value', Types::STRING, [
+ 'notnull' => false,
+ 'length' => 4000,
+ ]);
+ $table->setPrimaryKey(['config_id']);
+ $table->addUniqueIndex(['mount_id', 'key'], 'config_mount_key');
+ } else {
+ $table = $schema->getTable('external_config');
+ $table->changeColumn('value', [
+ 'notnull' => false,
+ 'length' => 4000,
+ ]);
+ }
+
+ if (!$schema->hasTable('external_options')) {
+ $table = $schema->createTable('external_options');
+ $table->addColumn('option_id', Types::BIGINT, [
+ 'autoincrement' => true,
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('mount_id', Types::BIGINT, [
+ 'notnull' => true,
+ 'length' => 6,
+ ]);
+ $table->addColumn('key', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 64,
+ ]);
+ $table->addColumn('value', Types::STRING, [
+ 'notnull' => true,
+ 'length' => 256,
+ ]);
+ $table->setPrimaryKey(['option_id']);
+ $table->addUniqueIndex(['mount_id', 'key'], 'option_mount_key');
+ }
+ return $schema;
+ }
+}
diff --git a/apps/files_external/lib/Migration/Version1015Date20211104103506.php b/apps/files_external/lib/Migration/Version1015Date20211104103506.php
new file mode 100644
index 00000000000..6027c795cdf
--- /dev/null
+++ b/apps/files_external/lib/Migration/Version1015Date20211104103506.php
@@ -0,0 +1,90 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_External\Migration;
+
+use Closure;
+use OC\Files\Cache\Storage;
+use OCP\DB\Exception;
+use OCP\DB\IResult;
+use OCP\DB\ISchemaWrapper;
+use OCP\IDBConnection;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+use Psr\Log\LoggerInterface;
+
+class Version1015Date20211104103506 extends SimpleMigrationStep {
+
+ public function __construct(
+ private IDBConnection $connection,
+ private LoggerInterface $logger,
+ ) {
+ }
+
+ public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->update('storages')
+ ->set('id', $qb->createParameter('newId'))
+ ->where($qb->expr()->eq('id', $qb->createParameter('oldId')));
+
+ $mounts = $this->getS3Mounts();
+ if (!$mounts instanceof IResult) {
+ throw new \Exception('Could not fetch existing mounts for migration');
+ }
+
+ while ($mount = $mounts->fetch()) {
+ $config = $this->getStorageConfig((int)$mount['mount_id']);
+ $hostname = $config['hostname'];
+ $bucket = $config['bucket'];
+ $key = $config['key'];
+ $oldId = Storage::adjustStorageId('amazon::' . $bucket);
+ $newId = Storage::adjustStorageId('amazon::external::' . md5($hostname . ':' . $bucket . ':' . $key));
+ try {
+ $qb->setParameter('oldId', $oldId);
+ $qb->setParameter('newId', $newId);
+ $qb->execute();
+ $this->logger->info('Migrated s3 storage id for mount with id ' . $mount['mount_id'] . ' to ' . $newId);
+ } catch (Exception $e) {
+ $this->logger->error('Failed to migrate external s3 storage id for mount with id ' . $mount['mount_id'], [
+ 'exception' => $e
+ ]);
+ }
+ }
+ return null;
+ }
+
+ /**
+ * @throws Exception
+ * @return IResult|int
+ */
+ private function getS3Mounts() {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->select('m.mount_id')
+ ->selectAlias('c.value', 'bucket')
+ ->from('external_mounts', 'm')
+ ->innerJoin('m', 'external_config', 'c', 'c.mount_id = m.mount_id')
+ ->where($qb->expr()->eq('m.storage_backend', $qb->createPositionalParameter('amazons3')))
+ ->andWhere($qb->expr()->eq('c.key', $qb->createPositionalParameter('bucket')));
+ return $qb->execute();
+ }
+
+ /**
+ * @throws Exception
+ */
+ private function getStorageConfig(int $mountId): array {
+ $qb = $this->connection->getQueryBuilder();
+ $qb->select('key', 'value')
+ ->from('external_config')
+ ->where($qb->expr()->eq('mount_id', $qb->createPositionalParameter($mountId)));
+ $config = [];
+ foreach ($qb->execute()->fetchAll() as $row) {
+ $config[$row['key']] = $row['value'];
+ }
+ return $config;
+ }
+}
diff --git a/apps/files_external/lib/Migration/Version1016Date20220324154536.php b/apps/files_external/lib/Migration/Version1016Date20220324154536.php
new file mode 100644
index 00000000000..fb2cccfdd80
--- /dev/null
+++ b/apps/files_external/lib/Migration/Version1016Date20220324154536.php
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+/**
+ * SPDX-FileCopyrightText: 2022 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+
+namespace OCA\Files_External\Migration;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+class Version1016Date20220324154536 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();
+
+ $table = $schema->getTable('external_config');
+ $column = $table->getColumn('value');
+
+ if ($column->getLength() > 4000) {
+ $column->setLength(4000);
+ return $schema;
+ }
+
+ return null;
+ }
+}
diff --git a/apps/files_external/lib/Migration/Version22000Date20210216084416.php b/apps/files_external/lib/Migration/Version22000Date20210216084416.php
new file mode 100644
index 00000000000..c4878e602c0
--- /dev/null
+++ b/apps/files_external/lib/Migration/Version22000Date20210216084416.php
@@ -0,0 +1,47 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2021 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Files_External\Migration;
+
+use Closure;
+use OCP\DB\ISchemaWrapper;
+use OCP\Migration\IOutput;
+use OCP\Migration\SimpleMigrationStep;
+
+/**
+ * Auto-generated migration step: Please modify to your needs!
+ */
+class Version22000Date20210216084416 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();
+
+ $table = $schema->getTable('external_applicable');
+ if ($table->hasIndex('applicable_type_value')) {
+ $table->dropIndex('applicable_type_value');
+ }
+
+ $table = $schema->getTable('external_config');
+ if ($table->hasIndex('config_mount')) {
+ $table->dropIndex('config_mount');
+ }
+
+ $table = $schema->getTable('external_options');
+ if ($table->hasIndex('option_mount')) {
+ $table->dropIndex('option_mount');
+ }
+
+ return $schema;
+ }
+}