|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * SPDX-FileCopyrightText: 2025 LibreCode coop and contributors |
| 7 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 8 | + */ |
| 9 | + |
| 10 | +namespace OCA\Libresign\Migration; |
| 11 | + |
| 12 | +use Closure; |
| 13 | +use OCP\DB\ISchemaWrapper; |
| 14 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 15 | +use OCP\DB\Types; |
| 16 | +use OCP\Files\AppData\IAppDataFactory; |
| 17 | +use OCP\Files\IAppData; |
| 18 | +use OCP\IDBConnection; |
| 19 | +use OCP\Migration\IOutput; |
| 20 | +use OCP\Migration\SimpleMigrationStep; |
| 21 | + |
| 22 | +/** |
| 23 | + * FIXME Auto-generated migration step: Please modify to your needs! |
| 24 | + */ |
| 25 | +class Version12000Date20250325155910 extends SimpleMigrationStep { |
| 26 | + protected IAppData $appData; |
| 27 | + public function __construct( |
| 28 | + protected IDBConnection $connection, |
| 29 | + private IAppDataFactory $appDataFactory, |
| 30 | + ) { |
| 31 | + $this->appData = $appDataFactory->get('libresign'); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * @param IOutput $output |
| 36 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 37 | + * @param array $options |
| 38 | + * @return null|ISchemaWrapper |
| 39 | + */ |
| 40 | + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { |
| 41 | + /** @var ISchemaWrapper $schema */ |
| 42 | + $schema = $schemaClosure(); |
| 43 | + $tableFile = $schema->getTable('libresign_file'); |
| 44 | + if (!$tableFile->hasColumn('created_at')) { |
| 45 | + $tableFile->addColumn('created_at', Types::DATETIME, [ |
| 46 | + 'notnull' => false, |
| 47 | + ]); |
| 48 | + } |
| 49 | + |
| 50 | + $tableSignRequest = $schema->getTable('libresign_sign_request'); |
| 51 | + if (!$tableSignRequest->hasColumn('created_at')) { |
| 52 | + $tableSignRequest->addColumn('created_at', Types::DATETIME, [ |
| 53 | + 'notnull' => false, |
| 54 | + ]); |
| 55 | + } |
| 56 | + if (!$tableSignRequest->hasColumn('signed')) { |
| 57 | + $tableSignRequest->addColumn('signed', Types::DATETIME, [ |
| 58 | + 'notnull' => false, |
| 59 | + ]); |
| 60 | + } |
| 61 | + return $schema; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @param IOutput $output |
| 66 | + * @param Closure(): ISchemaWrapper $schemaClosure |
| 67 | + * @param array $options |
| 68 | + */ |
| 69 | + public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options): void { |
| 70 | + $this->updateFile(); |
| 71 | + $this->updateSignRequest(); |
| 72 | + } |
| 73 | + |
| 74 | + private function updateFile(): void { |
| 75 | + $update = $this->connection->getQueryBuilder(); |
| 76 | + $update->update('libresign_file') |
| 77 | + ->set('created_at', $update->createParameter('created_at')) |
| 78 | + ->where($update->expr()->eq('id', $update->createParameter('id'))); |
| 79 | + |
| 80 | + $folder = $this->appData->getFolder('/'); |
| 81 | + $file = $folder->getFile('backup-table-libresign_file_Version12000Date20250325143340.csv'); |
| 82 | + $handle = $file->read(); |
| 83 | + fgetcsv($handle); // header |
| 84 | + while (($row = fgetcsv($handle)) !== false) { |
| 85 | + $update->setParameter('created_at', new \DateTime('@' . $row[1]), IQueryBuilder::PARAM_DATETIME_MUTABLE) |
| 86 | + ->setParameter('id', $row[0]); |
| 87 | + $update->executeStatement(); |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + private function updateSignRequest(): void { |
| 92 | + $update = $this->connection->getQueryBuilder(); |
| 93 | + $update->update('libresign_sign_request') |
| 94 | + ->set('created_at', $update->createParameter('created_at')) |
| 95 | + ->set('signed', $update->createParameter('signed')) |
| 96 | + ->where($update->expr()->eq('id', $update->createParameter('id'))); |
| 97 | + |
| 98 | + $folder = $this->appData->getFolder('/'); |
| 99 | + $file = $folder->getFile('backup-table-libresign_sign_request_Version12000Date20250325143340.csv'); |
| 100 | + $handle = $file->read(); |
| 101 | + fgetcsv($handle); // header |
| 102 | + while (($row = fgetcsv($handle)) !== false) { |
| 103 | + $update->setParameter('created_at', new \DateTime('@' . $row[1]), IQueryBuilder::PARAM_DATETIME_MUTABLE) |
| 104 | + ->setParameter('id', $row[0]); |
| 105 | + |
| 106 | + if ($row[2]) { |
| 107 | + $update->setParameter('signed', new \DateTime('@' . $row[2]), IQueryBuilder::PARAM_DATETIME_MUTABLE); |
| 108 | + } else { |
| 109 | + $update->setParameter('signed', null, IQueryBuilder::PARAM_NULL); |
| 110 | + } |
| 111 | + $update->executeStatement(); |
| 112 | + } |
| 113 | + } |
| 114 | +} |
0 commit comments