Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
Version010314Date20210815170535
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 4
72
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 preSchemaChange
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 changeSchema
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 1
20
 postSchemaChange
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3declare(strict_types=1);
4
5namespace OCA\Cospend\Migration;
6
7use Closure;
8use OCP\DB\ISchemaWrapper;
9use OCP\DB\Types;
10use OCP\Migration\SimpleMigrationStep;
11use OCP\Migration\IOutput;
12use OCP\DB\QueryBuilder\IQueryBuilder;
13use OCP\IDBConnection;
14use OCP\IL10N;
15
16/**
17 * Auto-generated migration step: Please modify to your needs!
18 */
19class Version010314Date20210815170535 extends SimpleMigrationStep {
20
21    /** @var IDBConnection */
22    private $connection;
23    private $trans;
24
25    /**
26     * @param IDBConnection $connection
27     */
28    public function __construct(IDBConnection $connection, IL10N $l10n) {
29        $this->connection = $connection;
30        $this->trans = $l10n;
31    }
32
33    /**
34     * @param IOutput $output
35     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
36     * @param array $options
37     */
38    public function preSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
39    }
40
41    /**
42     * @param IOutput $output
43     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
44     * @param array $options
45     * @return null|ISchemaWrapper
46     */
47    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
48        /** @var ISchemaWrapper $schema */
49        $schema = $schemaClosure();
50
51        if (!$schema->hasTable('cospend_paymentmodes')) {
52            $table = $schema->createTable('cospend_paymentmodes');
53            $table->addColumn('id', Types::INTEGER, [
54                'autoincrement' => true,
55                'notnull' => true,
56                'length' => 4,
57            ]);
58            $table->addColumn('projectid', Types::STRING, [
59                'notnull' => true,
60                'length' => 64,
61            ]);
62            $table->addColumn('name', Types::STRING, [
63                'notnull' => false,
64                'length' => 300,
65            ]);
66            $table->addColumn('color', Types::STRING, [
67                'notnull' => false,
68                'length' => 10,
69                'default' => null
70            ]);
71            $table->addColumn('encoded_icon', Types::STRING, [
72                'notnull' => false,
73                'length' => 64,
74                'default' => null
75            ]);
76            $table->addColumn('order', Types::INTEGER, [
77                'notnull' => true,
78                'length' => 4,
79                'default' => 0,
80            ]);
81            $table->setPrimaryKey(['id']);
82        }
83
84        $table = $schema->getTable('cospend_bills');
85        if (!$table->hasColumn('paymentmodeid')) {
86            $table->addColumn('paymentmodeid', Types::INTEGER, [
87                'notnull' => true,
88                'length' => 4,
89                'default' => 0,
90            ]);
91        }
92
93        $table = $schema->getTable('cospend_projects');
94        if (!$table->hasColumn('paymentmodesort')) {
95            $table->addColumn('paymentmodesort', Types::STRING, [
96                'notnull' => true,
97                'length' => 1,
98                'default' => 'a',
99            ]);
100        }
101
102        return $schema;
103    }
104
105    /**
106     * @param IOutput $output
107     * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
108     * @param array $options
109     */
110    public function postSchemaChange(IOutput $output, Closure $schemaClosure, array $options) {
111        $qb = $this->connection->getQueryBuilder();
112
113        $ts = (new \DateTime())->getTimestamp();
114
115        // convert pm ids in existing bills
116        $PAYMENT_MODE_ID_CONVERSION = [
117            'n' => 0,
118            'c' => -1,
119            'b' => -2,
120            'f' => -3,
121            't' => -4,
122            'o' => -5,
123        ];
124        foreach ($PAYMENT_MODE_ID_CONVERSION as $old => $new) {
125            $qb->update('cospend_bills')
126                ->set('paymentmodeid', $qb->createNamedParameter($new, IQueryBuilder::PARAM_INT))
127                ->set('lastchanged', $qb->createNamedParameter($ts, IQueryBuilder::PARAM_INT))
128                ->where(
129                    $qb->expr()->eq('paymentmode', $qb->createNamedParameter($old, IQueryBuilder::PARAM_STR))
130                );
131            $qb->executeStatement();
132            $qb = $qb->resetQueryParts();
133        }
134    }
135}