Skip to content

Commit

Permalink
sync also all routes scopes from new installation
Browse files Browse the repository at this point in the history
  • Loading branch information
chriskapp committed Aug 8, 2022
1 parent 1c4a8be commit 0e00b93
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/Migrations/DataSyncronizer.php
Expand Up @@ -52,6 +52,7 @@ public static function sync(Connection $connection): void
}

$routes = $data->getData('fusio_routes');
$routeMap = [];
foreach ($routes as $row) {
$routeId = $connection->fetchOne('SELECT id FROM fusio_routes WHERE path = :path', [
'path' => $row['path']
Expand All @@ -61,6 +62,8 @@ public static function sync(Connection $connection): void
$connection->insert('fusio_routes', $row);
$routeId = $connection->lastInsertId();

$routeMap[$data->getId('fusio_routes', $row['path'])] = $routeId;

$methods = $data->getData('fusio_routes_method', 'route_id', $data->getId('fusio_routes', $row['path']));
foreach ($methods as $method) {
$method['route_id'] = $routeId;
Expand Down Expand Up @@ -119,5 +122,30 @@ public static function sync(Connection $connection): void
$connection->insert('fusio_cronjob', $cronjob);
}
}

$scopes = $data->getData('fusio_scope');
$scopeMap = [];
foreach ($scopes as $scope) {
$scopeId = $connection->fetchOne('SELECT id FROM fusio_scope WHERE name = :name', [
'name' => $scope['name']
]);

if (empty($scopeId)) {
$connection->insert('fusio_scope', $scope);
$scopeId = $connection->lastInsertId();

$scopeMap[$data->getId('fusio_scope', $scope['name'])] = $scopeId;
}
}

$scopeRoutes = $data->getData('fusio_scope_routes');
foreach ($scopeRoutes as $scopeRoute) {
if (isset($scopeMap[$scopeRoute['scope_id']]) && isset($routeMap[$scopeRoute['route_id']])) {
$scopeRoute['scope_id'] = $scopeMap[$scopeRoute['scope_id']];
$scopeRoute['route_id'] = $routeMap[$scopeRoute['route_id']];

$connection->insert('fusio_scope_routes', $scopeRoute);
}
}
}
}
13 changes: 12 additions & 1 deletion tests/Migrations/DataSyncronizerTest.php
Expand Up @@ -48,6 +48,7 @@ public function testSync()
$schema = $this->getSchema('Backend_Action');
$event = $this->getEvent('fusio.action.create');
$cronjob = $this->getCronjob('Dispatch_Event');
$scope = $this->getScope('backend.action');

DataSyncronizer::sync($this->connection);

Expand All @@ -57,6 +58,7 @@ public function testSync()
$this->assertEquals($schema, $this->getSchema('Backend_Action'));
$this->assertEquals($event, $this->getEvent('fusio.action.create'));
$this->assertEquals($cronjob, $this->getCronjob('Dispatch_Event'));
$this->assertEquals($scope, $this->getScope('backend.action'));
}

private function getConfig(string $name): array
Expand Down Expand Up @@ -146,4 +148,13 @@ private function getCronjob(string $name): array

return $cronjob;
}
}

private function getScope(string $name): array
{
$scope = $this->connection->fetchAssociative('SELECT * FROM fusio_scope WHERE name = :name', ['name' => $name]);
$this->connection->delete('fusio_scope', ['id' => $scope['id']]);
unset($scope['id']);

return $scope;
}
}

0 comments on commit 0e00b93

Please sign in to comment.