Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 134 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Version010403Date20211112141106 | |
0.00% |
0 / 134 |
|
0.00% |
0 / 4 |
210 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| preSchemaChange | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| changeSchema | |
0.00% |
0 / 71 |
|
0.00% |
0 / 1 |
30 | |||
| postSchemaChange | |
0.00% |
0 / 59 |
|
0.00% |
0 / 1 |
56 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace OCA\Cospend\Migration; |
| 6 | |
| 7 | use Closure; |
| 8 | use OCP\DB\ISchemaWrapper; |
| 9 | use OCP\DB\QueryBuilder\IQueryBuilder; |
| 10 | use OCP\DB\Types; |
| 11 | use OCP\IDBConnection; |
| 12 | use OCP\Migration\SimpleMigrationStep; |
| 13 | use OCP\Migration\IOutput; |
| 14 | |
| 15 | class Version010403Date20211112141106 extends SimpleMigrationStep { |
| 16 | |
| 17 | /** |
| 18 | * @var boolean |
| 19 | */ |
| 20 | private $shouldCopyCategoryData; |
| 21 | /** |
| 22 | * @var IDBConnection |
| 23 | */ |
| 24 | private $connection; |
| 25 | /** |
| 26 | * @var boolean |
| 27 | */ |
| 28 | private $shouldCopyPaymentmodesData; |
| 29 | |
| 30 | public function __construct(IDBConnection $connection) { |
| 31 | $this->shouldCopyCategoryData = false; |
| 32 | $this->shouldCopyPaymentmodesData = false; |
| 33 | $this->connection = $connection; |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * @param IOutput $output |
| 38 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 39 | * @param array $options |
| 40 | */ |
| 41 | public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * @param IOutput $output |
| 46 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 47 | * @param array $options |
| 48 | * @return null|ISchemaWrapper |
| 49 | */ |
| 50 | public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
| 51 | /** @var ISchemaWrapper $schema */ |
| 52 | $schema = $schemaClosure(); |
| 53 | // this happens if we upgrade from 1.4.2 or lower |
| 54 | if (!$schema->hasTable('cospend_categories')) { |
| 55 | if ($schema->hasTable('cospend_project_categories')) { |
| 56 | $this->shouldCopyCategoryData = true; |
| 57 | } |
| 58 | $table = $schema->createTable('cospend_categories'); |
| 59 | $table->addColumn('id', Types::INTEGER, [ |
| 60 | 'autoincrement' => true, |
| 61 | 'notnull' => true, |
| 62 | ]); |
| 63 | $table->addColumn('projectid', Types::STRING, [ |
| 64 | 'notnull' => true, |
| 65 | 'length' => 64, |
| 66 | ]); |
| 67 | $table->addColumn('name', Types::STRING, [ |
| 68 | 'notnull' => false, |
| 69 | 'length' => 300, |
| 70 | 'default' => null, |
| 71 | ]); |
| 72 | $table->addColumn('color', Types::STRING, [ |
| 73 | 'notnull' => false, |
| 74 | 'length' => 10, |
| 75 | 'default' => null, |
| 76 | ]); |
| 77 | $table->addColumn('encoded_icon', Types::STRING, [ |
| 78 | 'notnull' => false, |
| 79 | 'length' => 64, |
| 80 | 'default' => null, |
| 81 | ]); |
| 82 | $table->addColumn('order', Types::INTEGER, [ |
| 83 | 'notnull' => true, |
| 84 | 'default' => 0, |
| 85 | ]); |
| 86 | $table->setPrimaryKey(['id']); |
| 87 | } |
| 88 | if (!$schema->hasTable('cospend_paymentmodes')) { |
| 89 | if ($schema->hasTable('cospend_project_paymentmodes')) { |
| 90 | $this->shouldCopyPaymentmodesData = true; |
| 91 | } |
| 92 | $table = $schema->createTable('cospend_paymentmodes'); |
| 93 | $table->addColumn('id', Types::INTEGER, [ |
| 94 | 'autoincrement' => true, |
| 95 | 'notnull' => true, |
| 96 | ]); |
| 97 | $table->addColumn('old_id', Types::STRING, [ |
| 98 | 'notnull' => false, |
| 99 | 'length' => 1, |
| 100 | 'default' => null, |
| 101 | ]); |
| 102 | $table->addColumn('projectid', Types::STRING, [ |
| 103 | 'notnull' => true, |
| 104 | 'length' => 64, |
| 105 | ]); |
| 106 | $table->addColumn('name', Types::STRING, [ |
| 107 | 'notnull' => false, |
| 108 | 'length' => 300, |
| 109 | 'default' => null, |
| 110 | ]); |
| 111 | $table->addColumn('color', Types::STRING, [ |
| 112 | 'notnull' => false, |
| 113 | 'length' => 10, |
| 114 | 'default' => null, |
| 115 | ]); |
| 116 | $table->addColumn('encoded_icon', Types::STRING, [ |
| 117 | 'notnull' => false, |
| 118 | 'length' => 64, |
| 119 | 'default' => null, |
| 120 | ]); |
| 121 | $table->addColumn('order', Types::INTEGER, [ |
| 122 | 'notnull' => true, |
| 123 | 'default' => 0, |
| 124 | ]); |
| 125 | $table->setPrimaryKey(['id']); |
| 126 | } |
| 127 | return $schema; |
| 128 | } |
| 129 | |
| 130 | /** |
| 131 | * @param IOutput $output |
| 132 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 133 | * @param array $options |
| 134 | */ |
| 135 | public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 136 | $qb = $this->connection->getQueryBuilder(); |
| 137 | |
| 138 | if ($this->shouldCopyCategoryData) { |
| 139 | $categories = []; |
| 140 | $qb->select('id', 'name', 'projectid', 'encoded_icon', 'color', 'order') |
| 141 | ->from('cospend_project_categories'); |
| 142 | $req = $qb->executeQuery(); |
| 143 | |
| 144 | while ($row = $req->fetch()) { |
| 145 | $categories[] = [ |
| 146 | 'id' => $row['id'], |
| 147 | 'name' => $row['name'], |
| 148 | 'projectid' => $row['projectid'], |
| 149 | 'encoded_icon' => $row['encoded_icon'], |
| 150 | 'color' => $row['color'], |
| 151 | 'order' => $row['order'], |
| 152 | ]; |
| 153 | } |
| 154 | $req->closeCursor(); |
| 155 | $qb = $qb->resetQueryParts(); |
| 156 | |
| 157 | foreach ($categories as $cat) { |
| 158 | $qb->insert('cospend_categories') |
| 159 | ->values([ |
| 160 | 'id' => $qb->createNamedParameter($cat['id'], IQueryBuilder::PARAM_INT), |
| 161 | 'projectid' => $qb->createNamedParameter($cat['projectid'], IQueryBuilder::PARAM_STR), |
| 162 | 'encoded_icon' => $qb->createNamedParameter($cat['encoded_icon'], IQueryBuilder::PARAM_STR), |
| 163 | 'color' => $qb->createNamedParameter($cat['color'], IQueryBuilder::PARAM_STR), |
| 164 | 'name' => $qb->createNamedParameter($cat['name'], IQueryBuilder::PARAM_STR), |
| 165 | 'order' => $qb->createNamedParameter($cat['order'], IQueryBuilder::PARAM_INT), |
| 166 | ]); |
| 167 | $qb->executeStatement(); |
| 168 | $qb->resetQueryParts(); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | if ($this->shouldCopyPaymentmodesData) { |
| 173 | $pms = []; |
| 174 | $qb->select('id', 'old_id', 'name', 'projectid', 'encoded_icon', 'color', 'order') |
| 175 | ->from('cospend_project_paymentmodes'); |
| 176 | $req = $qb->executeQuery(); |
| 177 | |
| 178 | while ($row = $req->fetch()) { |
| 179 | $pms[] = [ |
| 180 | 'id' => $row['id'], |
| 181 | 'old_id' => $row['old_id'], |
| 182 | 'name' => $row['name'], |
| 183 | 'projectid' => $row['projectid'], |
| 184 | 'encoded_icon' => $row['encoded_icon'], |
| 185 | 'color' => $row['color'], |
| 186 | 'order' => $row['order'], |
| 187 | ]; |
| 188 | } |
| 189 | $req->closeCursor(); |
| 190 | $qb = $qb->resetQueryParts(); |
| 191 | |
| 192 | foreach ($pms as $pm) { |
| 193 | $qb->insert('cospend_paymentmodes') |
| 194 | ->values([ |
| 195 | 'id' => $qb->createNamedParameter($pm['id'], IQueryBuilder::PARAM_INT), |
| 196 | 'old_id' => $qb->createNamedParameter($pm['old_id'], IQueryBuilder::PARAM_STR), |
| 197 | 'projectid' => $qb->createNamedParameter($pm['projectid'], IQueryBuilder::PARAM_STR), |
| 198 | 'encoded_icon' => $qb->createNamedParameter($pm['encoded_icon'], IQueryBuilder::PARAM_STR), |
| 199 | 'color' => $qb->createNamedParameter($pm['color'], IQueryBuilder::PARAM_STR), |
| 200 | 'name' => $qb->createNamedParameter($pm['name'], IQueryBuilder::PARAM_STR), |
| 201 | 'order' => $qb->createNamedParameter($pm['order'], IQueryBuilder::PARAM_INT), |
| 202 | ]); |
| 203 | $qb->executeStatement(); |
| 204 | $qb->resetQueryParts(); |
| 205 | } |
| 206 | } |
| 207 | } |
| 208 | } |