Skip to content

Commit 2faf23e

Browse files
committed
feat(Install): migrate location / zone relation
1 parent 3c59586 commit 2faf23e

File tree

3 files changed

+284
-0
lines changed

3 files changed

+284
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
use Glpi\DBAL\QueryExpression;
34+
35+
/** @var DBmysql $DB */
36+
global $DB;
37+
38+
// Migrate relations based on a country
39+
$glpi_location_table = 'glpi_locations';
40+
$zone_table = 'glpi_plugin_carbon_zones';
41+
$source_zone_table = 'glpi_plugin_carbon_sources_zones';
42+
$iterator = $DB->request([
43+
'SELECT' => [
44+
$glpi_location_table . '.id as locations_id',
45+
$zone_table . '.plugin_carbon_sources_id_historical',
46+
$zone_table . '.id as plugin_carbon_zones_id',
47+
$source_zone_table . '.id as plugin_carbon_sources_zones_id',
48+
],
49+
'FROM' => $glpi_location_table,
50+
'INNER JOIN' => [
51+
$zone_table => [
52+
'FKEY' => [
53+
$zone_table => 'name',
54+
$glpi_location_table => 'country',
55+
]
56+
],
57+
$source_zone_table => [
58+
'FKEY' => [
59+
$source_zone_table => 'plugin_carbon_zones_id',
60+
$zone_table => 'id',
61+
[
62+
'AND' => [
63+
new QueryExpression('`' . $source_zone_table . '`.`plugin_carbon_sources_id` = `' . $zone_table . '`.`plugin_carbon_sources_id_historical`'),
64+
]
65+
]
66+
]
67+
]
68+
]
69+
]);
70+
71+
$location_table = 'glpi_plugin_carbon_locations';
72+
foreach ($iterator as $row) {
73+
$where = [
74+
'locations_id' => $row['locations_id'],
75+
];
76+
$params = [
77+
'plugin_carbon_sources_zones_id' => $row['plugin_carbon_sources_zones_id'],
78+
];
79+
$DB->updateOrInsert($location_table, $params, $where);
80+
}
81+
82+
// Migrate relations based on a state
83+
$glpi_location_table = 'glpi_locations';
84+
$zone_table = 'glpi_plugin_carbon_zones';
85+
$source_zone_table = 'glpi_plugin_carbon_sources_zones';
86+
$iterator = $DB->request([
87+
'SELECT' => [
88+
$glpi_location_table . '.id as locations_id',
89+
$zone_table . '.plugin_carbon_sources_id_historical',
90+
$zone_table . '.id as plugin_carbon_zones_id',
91+
$source_zone_table . '.id as plugin_carbon_sources_zones_id',
92+
],
93+
'FROM' => $glpi_location_table,
94+
'INNER JOIN' => [
95+
$zone_table => [
96+
'FKEY' => [
97+
$zone_table => 'name',
98+
$glpi_location_table => 'state',
99+
]
100+
],
101+
$source_zone_table => [
102+
'FKEY' => [
103+
$source_zone_table => 'plugin_carbon_zones_id',
104+
$zone_table => 'id',
105+
[
106+
'AND' => [
107+
new QueryExpression('`' . $source_zone_table . '`.`plugin_carbon_sources_id` = `' . $zone_table . '`.`plugin_carbon_sources_id_historical`'),
108+
]
109+
]
110+
]
111+
]
112+
]
113+
]);
114+
115+
$location_table = 'glpi_plugin_carbon_locations';
116+
foreach ($iterator as $row) {
117+
$where = [
118+
'locations_id' => $row['locations_id'],
119+
];
120+
$params = [
121+
'plugin_carbon_sources_zones_id' => $row['plugin_carbon_sources_zones_id'],
122+
];
123+
$DB->updateOrInsert($location_table, $params, $where);
124+
}

phpunit.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,5 +42,8 @@
4242
<testsuite name="Upgrade">
4343
<directory>./tests/upgrade/</directory>
4444
</testsuite>
45+
<testsuite name="Migration">
46+
<directory>./tests/migration/</directory>
47+
</testsuite>
4548
</testsuites>
4649
</phpunit>
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
<?php
2+
3+
/**
4+
* -------------------------------------------------------------------------
5+
* Carbon plugin for GLPI
6+
*
7+
* @copyright Copyright (C) 2024-2025 Teclib' and contributors.
8+
* @license https://www.gnu.org/licenses/gpl-3.0.txt GPLv3+
9+
* @link https://github.com/pluginsGLPI/carbon
10+
*
11+
* -------------------------------------------------------------------------
12+
*
13+
* LICENSE
14+
*
15+
* This file is part of Carbon plugin for GLPI.
16+
*
17+
* This program is free software: you can redistribute it and/or modify
18+
* it under the terms of the GNU General Public License as published by
19+
* the Free Software Foundation, either version 3 of the License, or
20+
* (at your option) any later version.
21+
*
22+
* This program is distributed in the hope that it will be useful,
23+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
24+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25+
* GNU General Public License for more details.
26+
*
27+
* You should have received a copy of the GNU General Public License
28+
* along with this program. If not, see <https://www.gnu.org/licenses/>.
29+
*
30+
* -------------------------------------------------------------------------
31+
*/
32+
33+
use DBmysql;
34+
use Location as GlpiLocation;
35+
use GlpiPlugin\Carbon\Tests\CommonTestCase;
36+
use GlpiPlugin\Carbon\Tests\DbTestCase;
37+
use GlpiPlugin\Carbon\Uninstall;
38+
39+
class migration_to_1_2_0_Test extends DbTestCase
40+
{
41+
public static function setUpBeforeClass(): void
42+
{
43+
global $DB;
44+
45+
parent::setUpBeforeClass();
46+
$plugin = new Plugin();
47+
$plugin->getFromDBbyDir('carbon');
48+
if ($plugin->fields['state'] !== Plugin::ANEW && $plugin->fields['state'] !== Plugin::NOTINSTALLED) {
49+
require_once(__DIR__ . '/../../install/Uninstall.php');
50+
$uninstall = new Uninstall();
51+
$uninstall->uninstall();
52+
}
53+
54+
require_once(__DIR__ . '/../../setup.php');
55+
$sql_file = plugin_carbon_getSchemaPath('1.2.0');
56+
$success = $DB->runFile(realpath($sql_file));
57+
}
58+
59+
public static function tearDownAfterClass(): void
60+
{
61+
parent::tearDownAfterClass();
62+
require_once(__DIR__ . '/../../install/Uninstall.php');
63+
$uninstall = new Uninstall();
64+
$uninstall->uninstall();
65+
}
66+
67+
public function testUpdateCountryLocationZoneRelation()
68+
{
69+
/** @var DBMysql $DB */
70+
global $DB;
71+
return;
72+
73+
$glpi_location = $this->createItem(GlpiLocation::class, [
74+
'country' => 'France',
75+
]);
76+
$DB->insert('glpi_plugin_carbon_zones', [
77+
'name' => 'France',
78+
'plugin_carbon_sources_id_historical' => 1,
79+
]);
80+
$zone_id = $DB->insertId();
81+
$DB->insert('glpi_plugin_carbon_sources', [
82+
'name' => 'RTE',
83+
'is_fallback' => 0,
84+
'is_carbon_intensity_source' => 1,
85+
]);
86+
$source_id = $DB->insertId();
87+
$DB->insert('glpi_plugin_carbon_sources_zones', [
88+
'plugin_carbon_sources_id' => $source_id,
89+
'plugin_carbon_zones_id' => $zone_id,
90+
]);
91+
$source_zone_id = $DB->insertId();
92+
$migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php';
93+
$migration_file = realpath($migration_file);
94+
require($migration_file);
95+
96+
$result = $DB->request([
97+
'SELECT' => '*',
98+
'FROM' => 'glpi_plugin_carbon_locations',
99+
'WHERE' => [
100+
'locations_id' => $glpi_location->getID(),
101+
],
102+
]);
103+
$this->assertEquals(1, $result->count());
104+
$expected = [
105+
'id' => $result->current()['id'],
106+
'locations_id' => $glpi_location->getID(),
107+
'boavizta_zone' => null,
108+
'plugin_carbon_sources_zones_id' => $source_zone_id,
109+
];
110+
$this->assertEquals($expected, $result->current());
111+
}
112+
113+
public function testUpdateStateLocationZoneRelation()
114+
{
115+
/** @var DBMysql $DB */
116+
global $DB;
117+
118+
$glpi_location = $this->createItem(GlpiLocation::class, [
119+
'state' => 'foo state',
120+
]);
121+
$DB->insert('glpi_plugin_carbon_zones', [
122+
'name' => 'foo state',
123+
'plugin_carbon_sources_id_historical' => 1,
124+
]);
125+
$zone_id = $DB->insertId();
126+
$DB->insert('glpi_plugin_carbon_sources', [
127+
'name' => 'foo electricity distributor',
128+
'is_fallback' => 1,
129+
'is_carbon_intensity_source' => 1,
130+
]);
131+
$source_id = $DB->insertId();
132+
$DB->insert('glpi_plugin_carbon_sources_zones', [
133+
'plugin_carbon_sources_id' => $source_id,
134+
'plugin_carbon_zones_id' => $zone_id,
135+
]);
136+
$source_zone_id = $DB->insertId();
137+
$migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php';
138+
$migration_file = realpath($migration_file);
139+
require($migration_file);
140+
141+
$result = $DB->request([
142+
'SELECT' => '*',
143+
'FROM' => 'glpi_plugin_carbon_locations',
144+
'WHERE' => [
145+
'locations_id' => $glpi_location->getID(),
146+
],
147+
]);
148+
$this->assertEquals(1, $result->count());
149+
$expected = [
150+
'id' => $result->current()['id'],
151+
'locations_id' => $glpi_location->getID(),
152+
'boavizta_zone' => null,
153+
'plugin_carbon_sources_zones_id' => $source_zone_id,
154+
];
155+
$this->assertEquals($expected, $result->current());
156+
}
157+
}

0 commit comments

Comments
 (0)