Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Version000304Date20200313092247 | |
0.00% |
0 / 31 |
|
0.00% |
0 / 4 |
56 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| preSchemaChange | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| changeSchema | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
6 | |||
| postSchemaChange | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
12 | |||
| 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\Migration\SimpleMigrationStep; |
| 10 | use OCP\Migration\IOutput; |
| 11 | use OCP\DB\QueryBuilder\IQueryBuilder; |
| 12 | use OCP\IDBConnection; |
| 13 | |
| 14 | /** |
| 15 | * Auto-generated migration step: Please modify to your needs! |
| 16 | */ |
| 17 | class Version000304Date20200313092247 extends SimpleMigrationStep { |
| 18 | |
| 19 | /** @var IDBConnection */ |
| 20 | private $connection; |
| 21 | |
| 22 | /** |
| 23 | * @param IDBConnection $connection |
| 24 | */ |
| 25 | public function __construct(IDBConnection $connection) { |
| 26 | $this->connection = $connection; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param IOutput $output |
| 31 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 32 | * @param array $options |
| 33 | */ |
| 34 | public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * @param IOutput $output |
| 39 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 40 | * @param array $options |
| 41 | * @return null|ISchemaWrapper |
| 42 | */ |
| 43 | public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) { |
| 44 | /** @var ISchemaWrapper $schema */ |
| 45 | $schema = $schemaClosure(); |
| 46 | |
| 47 | if ($schema->hasTable('cospend_bills')) { |
| 48 | $table = $schema->getTable('cospend_bills'); |
| 49 | $table->addColumn('timestamp', 'bigint', [ |
| 50 | 'notnull' => true, |
| 51 | 'length' => 10, |
| 52 | 'default' => 0 |
| 53 | ]); |
| 54 | } |
| 55 | |
| 56 | return $schema; |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * @param IOutput $output |
| 61 | * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper` |
| 62 | * @param array $options |
| 63 | */ |
| 64 | public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) { |
| 65 | $qb = $this->connection->getQueryBuilder(); |
| 66 | $timestamps = []; |
| 67 | $qb->select('id', 'date') |
| 68 | ->from('cospend_bills', 'b'); |
| 69 | $req = $qb->executeQuery(); |
| 70 | while ($row = $req->fetch()) { |
| 71 | $id = $row['id']; |
| 72 | $date = $row['date']; |
| 73 | |
| 74 | $timestamp = strtotime($date.' 12:00:00'); |
| 75 | $timestamps[$id] = $timestamp; |
| 76 | } |
| 77 | $req->closeCursor(); |
| 78 | $qb = $qb->resetQueryParts(); |
| 79 | |
| 80 | foreach ($timestamps as $bid => $ts) { |
| 81 | $qb->update('cospend_bills') |
| 82 | ->set('timestamp', $qb->createNamedParameter($ts, IQueryBuilder::PARAM_INT)) |
| 83 | ->where( |
| 84 | $qb->expr()->eq('id', $qb->createNamedParameter($bid, IQueryBuilder::PARAM_INT)) |
| 85 | ); |
| 86 | $qb->executeStatement(); |
| 87 | $qb = $qb->resetQueryParts(); |
| 88 | } |
| 89 | } |
| 90 | } |