|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* @copyright Copyright (c) 2017 Joas Schilling <[email protected]> |
|
4
|
|
|
* |
|
5
|
|
|
* @author Joas Schilling <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* @license GNU AGPL version 3 or any later version |
|
8
|
|
|
* |
|
9
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
10
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
11
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
12
|
|
|
* License, or (at your option) any later version. |
|
13
|
|
|
* |
|
14
|
|
|
* This program is distributed in the hope that it will be useful, |
|
15
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
16
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
17
|
|
|
* GNU Affero General Public License for more details. |
|
18
|
|
|
* |
|
19
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
20
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
21
|
|
|
* |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace OCA\TwoFactorBackupCodes\Migration; |
|
25
|
|
|
|
|
26
|
|
|
use Doctrine\DBAL\Schema\Schema; |
|
27
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
28
|
|
|
use OCP\IDBConnection; |
|
29
|
|
|
use OCP\Migration\SimpleMigrationStep; |
|
30
|
|
|
use OCP\Migration\IOutput; |
|
31
|
|
|
|
|
32
|
|
|
class Version1002Date20170607113030 extends SimpleMigrationStep { |
|
33
|
|
|
|
|
34
|
|
|
/** @var IDBConnection */ |
|
35
|
|
|
protected $connection; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param IDBConnection $connection |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(IDBConnection $connection) { |
|
41
|
|
|
$this->connection = $connection; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @param IOutput $output |
|
46
|
|
|
* @param \Closure $schemaClosure The `\Closure` returns a `Schema` |
|
47
|
|
|
* @param array $options |
|
48
|
|
|
* @since 13.0.0 |
|
49
|
|
|
*/ |
|
50
|
|
|
public function preSchemaChange(IOutput $output, \Closure $schemaClosure, array $options) { |
|
51
|
|
|
/** @var Schema $schema */ |
|
52
|
|
|
$schema = $schemaClosure(); |
|
53
|
|
|
|
|
54
|
|
|
if (!$schema->hasTable('twofactor_backup_codes')) { |
|
55
|
|
|
// Legacy table does not exist |
|
56
|
|
|
return; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$insert = $this->connection->getQueryBuilder(); |
|
60
|
|
|
$insert->insert('twofactor_backupcodes') |
|
61
|
|
|
->values([ |
|
62
|
|
|
// Inserting with id might fail: 'id' => $insert->createParameter('id'), |
|
|
|
|
|
|
63
|
|
|
'user_id' => $insert->createParameter('user_id'), |
|
64
|
|
|
'code' => $insert->createParameter('code'), |
|
65
|
|
|
'used' => $insert->createParameter('used'), |
|
66
|
|
|
]); |
|
67
|
|
|
|
|
68
|
|
|
$query = $this->connection->getQueryBuilder(); |
|
69
|
|
|
$query->select('*') |
|
70
|
|
|
->from('twofactor_backup_codes') |
|
71
|
|
|
->orderBy('id', 'ASC'); |
|
72
|
|
|
$result = $query->execute(); |
|
73
|
|
|
|
|
74
|
|
|
$output->startProgress(); |
|
75
|
|
|
while ($row = $result->fetch()) { |
|
76
|
|
|
$output->advance(); |
|
77
|
|
|
|
|
78
|
|
|
$insert |
|
79
|
|
|
// Inserting with id might fail: ->setParameter('id', $row['id'], IQueryBuilder::PARAM_INT) |
|
|
|
|
|
|
80
|
|
|
->setParameter('user_id', $row['user_id'], IQueryBuilder::PARAM_STR) |
|
81
|
|
|
->setParameter('code', $row['code'], IQueryBuilder::PARAM_STR) |
|
82
|
|
|
->setParameter('used', $row['used'], IQueryBuilder::PARAM_INT) |
|
83
|
|
|
->execute(); |
|
84
|
|
|
} |
|
85
|
|
|
$output->finishProgress(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param IOutput $output |
|
90
|
|
|
* @param \Closure $schemaClosure The `\Closure` returns a `Schema` |
|
91
|
|
|
* @param array $options |
|
92
|
|
|
* @return null|Schema |
|
93
|
|
|
* @since 13.0.0 |
|
94
|
|
|
*/ |
|
95
|
|
|
public function changeSchema(IOutput $output, \Closure $schemaClosure, array $options) { |
|
96
|
|
|
/** @var Schema $schema */ |
|
97
|
|
|
$schema = $schemaClosure(); |
|
98
|
|
|
|
|
99
|
|
|
if ($schema->hasTable('twofactor_backup_codes')) { |
|
100
|
|
|
$schema->dropTable('twofactor_backup_codes'); |
|
101
|
|
|
return $schema; |
|
102
|
|
|
} |
|
103
|
|
|
return null; |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.