Skip to content

Commit 6dda41c

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

File tree

3 files changed

+274
-0
lines changed

3 files changed

+274
-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: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
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 CommonTestCase
40+
{
41+
public static function setUpBeforeClass(): void
42+
{
43+
global $DB;
44+
45+
require_once(__DIR__ . '/../../setup.php');
46+
$sql_file = plugin_carbon_getSchemaPath('1.2.0');
47+
$success = $DB->runFile(realpath($sql_file));
48+
}
49+
50+
public static function tearDownAfterClass(): void
51+
{
52+
require_once(__DIR__ . '/../../install/Uninstall.php');
53+
$uninstall = new Uninstall();
54+
$uninstall->uninstall();
55+
}
56+
57+
public function testUpdateCountryLocationZoneRelation()
58+
{
59+
/** @var DBMysql $DB */
60+
global $DB;
61+
return;
62+
63+
$glpi_location = $this->createItem(GlpiLocation::class, [
64+
'country' => 'France',
65+
]);
66+
$DB->insert('glpi_plugin_carbon_zones', [
67+
'name' => 'France',
68+
'plugin_carbon_sources_id_historical' => 1,
69+
]);
70+
$zone_id = $DB->insertId();
71+
$DB->insert('glpi_plugin_carbon_sources', [
72+
'name' => 'RTE',
73+
'is_fallback' => 0,
74+
'is_carbon_intensity_source' => 1,
75+
]);
76+
$source_id = $DB->insertId();
77+
$DB->insert('glpi_plugin_carbon_sources_zones', [
78+
'plugin_carbon_sources_id' => $source_id,
79+
'plugin_carbon_zones_id' => $zone_id,
80+
]);
81+
$source_zone_id = $DB->insertId();
82+
$migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php';
83+
$migration_file = realpath($migration_file);
84+
require($migration_file);
85+
86+
$result = $DB->request([
87+
'SELECT' => '*',
88+
'FROM' => 'glpi_plugin_carbon_locations',
89+
'WHERE' => [
90+
'locations_id' => $glpi_location->getID(),
91+
],
92+
]);
93+
$this->assertEquals(1, $result->count());
94+
$expected = [
95+
'id' => $result->current()['id'],
96+
'locations_id' => $glpi_location->getID(),
97+
'boavizta_zone' => null,
98+
'plugin_carbon_sources_zones_id' => $source_zone_id,
99+
];
100+
$this->assertEquals($expected, $result->current());
101+
}
102+
103+
public function testUpdateStateLocationZoneRelation()
104+
{
105+
/** @var DBMysql $DB */
106+
global $DB;
107+
108+
$glpi_location = $this->createItem(GlpiLocation::class, [
109+
'state' => 'Quebec',
110+
]);
111+
$DB->insert('glpi_plugin_carbon_zones', [
112+
'name' => 'Quebec',
113+
'plugin_carbon_sources_id_historical' => 1,
114+
]);
115+
$zone_id = $DB->insertId();
116+
$DB->insert('glpi_plugin_carbon_sources', [
117+
'name' => 'Hydro Quebec',
118+
'is_fallback' => 1,
119+
'is_carbon_intensity_source' => 1,
120+
]);
121+
$source_id = $DB->insertId();
122+
$DB->insert('glpi_plugin_carbon_sources_zones', [
123+
'plugin_carbon_sources_id' => $source_id,
124+
'plugin_carbon_zones_id' => $zone_id,
125+
]);
126+
$source_zone_id = $DB->insertId();
127+
$migration_file = __DIR__ . '/../../install/migration/update_1.1.0_to_1.2.0/04_update_location_zone_relation.php';
128+
$migration_file = realpath($migration_file);
129+
require($migration_file);
130+
131+
$result = $DB->request([
132+
'SELECT' => '*',
133+
'FROM' => 'glpi_plugin_carbon_locations',
134+
'WHERE' => [
135+
'locations_id' => $glpi_location->getID(),
136+
],
137+
]);
138+
$this->assertEquals(1, $result->count());
139+
$expected = [
140+
'id' => $result->current()['id'],
141+
'locations_id' => $glpi_location->getID(),
142+
'boavizta_zone' => null,
143+
'plugin_carbon_sources_zones_id' => $source_zone_id,
144+
];
145+
$this->assertEquals($expected, $result->current());
146+
}
147+
}

0 commit comments

Comments
 (0)