2018-03-14 20:25:19 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* SchemaTest.php
|
|
|
|
*
|
|
|
|
* -Description-
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-02-08 23:29:04 +00:00
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-03-14 20:25:19 +00:00
|
|
|
*
|
2021-02-08 23:29:04 +00:00
|
|
|
* @link https://www.librenms.org
|
2021-09-10 18:09:53 +00:00
|
|
|
*
|
2018-03-14 20:25:19 +00:00
|
|
|
* @copyright 2018 Tony Murray
|
|
|
|
* @author Tony Murray <murraytony@gmail.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace LibreNMS\Tests;
|
|
|
|
|
|
|
|
use LibreNMS\DB\Schema;
|
|
|
|
|
|
|
|
class SchemaTest extends TestCase
|
|
|
|
{
|
|
|
|
private $mock_schema = [
|
2020-09-21 13:59:34 +00:00
|
|
|
'bills' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'bill_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => ['bill_id' => ['Name' => 'bill_id', 'Columns' => ['bill_id'], 'Unique' => true, 'Type' => 'BTREE']],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'bill_ports' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'bill_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
|
|
|
|
['Field' => 'port_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'devices' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'device_id', 'Type' => 'int(11) unsigned', 'Null' => false, 'Extra' => 'auto_increment'],
|
|
|
|
['Field' => 'location_id', 'Type' => 'int(11)', 'Null' => true, 'Extra' => ''],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['device_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'locations' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 22:49:18 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 22:49:18 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'ports' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'port_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
|
|
|
['Field' => 'device_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => '', 'Default' => '0'],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['port_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'sensors' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'sensor_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
|
|
|
['Field' => 'device_id', 'Type' => 'int(11) unsigned', 'Null' => false, 'Extra' => '', 'Default' => '0'],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['sensor_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'sensors_to_state_indexes' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'sensors_to_state_translations_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
|
|
|
['Field' => 'sensor_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
|
|
|
|
['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['sensors_to_state_translations_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'state_indexes' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['state_index_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'state_translations' => [
|
|
|
|
'Columns' => [
|
|
|
|
['Field' => 'state_translation_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => 'auto_increment'],
|
|
|
|
['Field' => 'state_index_id', 'Type' => 'int(11)', 'Null' => false, 'Extra' => ''],
|
2018-03-14 20:25:19 +00:00
|
|
|
],
|
2020-09-21 13:59:34 +00:00
|
|
|
'Indexes' => [
|
|
|
|
'PRIMARY' => ['Name' => 'PRIMARY', 'Columns' => ['state_translation_id'], 'Unique' => true, 'Type' => 'BTREE'],
|
2020-09-21 12:54:51 +00:00
|
|
|
],
|
|
|
|
],
|
2018-03-14 20:25:19 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return Schema
|
|
|
|
*/
|
|
|
|
private function getSchemaMock()
|
|
|
|
{
|
|
|
|
// use a Mock so we don't have to rely on the schema being stable.
|
|
|
|
|
|
|
|
$schema = $this->getMockBuilder(Schema::class)
|
2021-11-30 08:33:14 +00:00
|
|
|
->onlyMethods(['getSchema'])
|
2018-03-14 20:25:19 +00:00
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$schema->method('getSchema')->willReturn($this->mock_schema);
|
|
|
|
|
|
|
|
/** @var $schema Schema Mock of Schema */
|
|
|
|
return $schema;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTableRelationships()
|
|
|
|
{
|
|
|
|
// mock getSchema
|
|
|
|
$schema = $this->getSchemaMock();
|
|
|
|
|
|
|
|
$expected = [
|
|
|
|
'bills' => [],
|
|
|
|
'bill_ports' => ['bills', 'ports'],
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 22:49:18 +00:00
|
|
|
'devices' => ['locations'],
|
|
|
|
'locations' => [],
|
2018-03-14 20:25:19 +00:00
|
|
|
'ports' => ['devices'],
|
|
|
|
'sensors' => ['devices'],
|
|
|
|
'sensors_to_state_indexes' => ['sensors', 'state_indexes'],
|
|
|
|
'state_indexes' => [],
|
|
|
|
'state_translations' => ['state_indexes'],
|
|
|
|
];
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $schema->getTableRelationships());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testFindRelationshipPath()
|
|
|
|
{
|
|
|
|
$schema = $this->getSchemaMock();
|
|
|
|
|
|
|
|
$this->assertEquals(['devices'], $schema->findRelationshipPath('devices'));
|
Refactored and update Location Geocoding (#9359)
- Fix location so it is a regular database relation (this allows multiple devices to be accurately linked to one location and saves api calls)
- Parse coordinates from the location more consistently
- Add settings to webui
- ~~Used [PHP Geocoder](http://geocoder-php.org/), which has lots of backends and is well tested. (also includes reverse and geoip)~~
- Google Maps, Bing, Mapquest, and OpenStreetMap supported initially.
- Default to OpenStreetMap, which doesn't require a key. They will liberally hand out bans if you exceed 1 query per second though.
- All other Geocoding APIs require an API key. (Google requires a credit card on file, but seems to be the most accurate)
- Update all (I think) sql queries to handle the new structure
- Remove final vestiges of override_sysLocation as a device attribute
- Update existing device groups and rules in DB
- Tested all APIs with good/bad location, no/bad/good key, and no connection.
- Cannot fix advanced queries that use location
This blocks #8868
DO NOT DELETE THIS TEXT
#### Please note
> Please read this information carefully. You can run `./scripts/pre-commit.php` to check your code before submitting.
- [x] Have you followed our [code guidelines?](http://docs.librenms.org/Developing/Code-Guidelines/)
#### Testers
If you would like to test this pull request then please run: `./scripts/github-apply <pr_id>`, i.e `./scripts/github-apply 5926`
After you are done testing, you can remove the changes with `./scripts/github-remove`. If there are schema changes, you can ask on discord how to revert.
2018-11-28 22:49:18 +00:00
|
|
|
$this->assertEquals(['locations', 'devices'], $schema->findRelationshipPath('locations'));
|
2018-03-14 20:25:19 +00:00
|
|
|
$this->assertEquals(['devices', 'ports'], $schema->findRelationshipPath('ports'));
|
|
|
|
$this->assertEquals(['devices', 'ports', 'bill_ports'], $schema->findRelationshipPath('bill_ports'));
|
|
|
|
$this->assertEquals(['devices', 'ports', 'bill_ports', 'bills'], $schema->findRelationshipPath('bills'));
|
|
|
|
$this->assertEquals(
|
|
|
|
['devices', 'sensors', 'sensors_to_state_indexes', 'state_indexes', 'state_translations'],
|
|
|
|
$schema->findRelationshipPath('state_translations')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|